planar_functions.cc 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074
  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/planar_functions.h"
  11. #include <string.h> // for memset()
  12. #include "libyuv/cpu_id.h"
  13. #ifdef HAVE_JPEG
  14. #include "libyuv/mjpeg_decoder.h"
  15. #endif
  16. #include "libyuv/row.h"
  17. #include "libyuv/scale_row.h" // for ScaleRowDown2
  18. #ifdef __cplusplus
  19. namespace libyuv {
  20. extern "C" {
  21. #endif
  22. // Copy a plane of data
  23. LIBYUV_API
  24. void CopyPlane(const uint8_t* src_y,
  25. int src_stride_y,
  26. uint8_t* dst_y,
  27. int dst_stride_y,
  28. int width,
  29. int height) {
  30. int y;
  31. void (*CopyRow)(const uint8_t* src, uint8_t* dst, int width) = CopyRow_C;
  32. // Negative height means invert the image.
  33. if (height < 0) {
  34. height = -height;
  35. dst_y = dst_y + (height - 1) * dst_stride_y;
  36. dst_stride_y = -dst_stride_y;
  37. }
  38. // Coalesce rows.
  39. if (src_stride_y == width && dst_stride_y == width) {
  40. width *= height;
  41. height = 1;
  42. src_stride_y = dst_stride_y = 0;
  43. }
  44. // Nothing to do.
  45. if (src_y == dst_y && src_stride_y == dst_stride_y) {
  46. return;
  47. }
  48. #if defined(HAS_COPYROW_SSE2)
  49. if (TestCpuFlag(kCpuHasSSE2)) {
  50. CopyRow = IS_ALIGNED(width, 32) ? CopyRow_SSE2 : CopyRow_Any_SSE2;
  51. }
  52. #endif
  53. #if defined(HAS_COPYROW_AVX)
  54. if (TestCpuFlag(kCpuHasAVX)) {
  55. CopyRow = IS_ALIGNED(width, 64) ? CopyRow_AVX : CopyRow_Any_AVX;
  56. }
  57. #endif
  58. #if defined(HAS_COPYROW_ERMS)
  59. if (TestCpuFlag(kCpuHasERMS)) {
  60. CopyRow = CopyRow_ERMS;
  61. }
  62. #endif
  63. #if defined(HAS_COPYROW_NEON)
  64. if (TestCpuFlag(kCpuHasNEON)) {
  65. CopyRow = IS_ALIGNED(width, 32) ? CopyRow_NEON : CopyRow_Any_NEON;
  66. }
  67. #endif
  68. // Copy plane
  69. for (y = 0; y < height; ++y) {
  70. CopyRow(src_y, dst_y, width);
  71. src_y += src_stride_y;
  72. dst_y += dst_stride_y;
  73. }
  74. }
  75. // TODO(fbarchard): Consider support for negative height.
  76. // TODO(fbarchard): Consider stride measured in bytes.
  77. LIBYUV_API
  78. void CopyPlane_16(const uint16_t* src_y,
  79. int src_stride_y,
  80. uint16_t* dst_y,
  81. int dst_stride_y,
  82. int width,
  83. int height) {
  84. int y;
  85. void (*CopyRow)(const uint16_t* src, uint16_t* dst, int width) = CopyRow_16_C;
  86. // Coalesce rows.
  87. if (src_stride_y == width && dst_stride_y == width) {
  88. width *= height;
  89. height = 1;
  90. src_stride_y = dst_stride_y = 0;
  91. }
  92. #if defined(HAS_COPYROW_16_SSE2)
  93. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 32)) {
  94. CopyRow = CopyRow_16_SSE2;
  95. }
  96. #endif
  97. #if defined(HAS_COPYROW_16_ERMS)
  98. if (TestCpuFlag(kCpuHasERMS)) {
  99. CopyRow = CopyRow_16_ERMS;
  100. }
  101. #endif
  102. #if defined(HAS_COPYROW_16_NEON)
  103. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 32)) {
  104. CopyRow = CopyRow_16_NEON;
  105. }
  106. #endif
  107. // Copy plane
  108. for (y = 0; y < height; ++y) {
  109. CopyRow(src_y, dst_y, width);
  110. src_y += src_stride_y;
  111. dst_y += dst_stride_y;
  112. }
  113. }
  114. // Convert a plane of 16 bit data to 8 bit
  115. LIBYUV_API
  116. void Convert16To8Plane(const uint16_t* src_y,
  117. int src_stride_y,
  118. uint8_t* dst_y,
  119. int dst_stride_y,
  120. int scale, // 16384 for 10 bits
  121. int width,
  122. int height) {
  123. int y;
  124. void (*Convert16To8Row)(const uint16_t* src_y, uint8_t* dst_y, int scale,
  125. int width) = Convert16To8Row_C;
  126. // Negative height means invert the image.
  127. if (height < 0) {
  128. height = -height;
  129. dst_y = dst_y + (height - 1) * dst_stride_y;
  130. dst_stride_y = -dst_stride_y;
  131. }
  132. // Coalesce rows.
  133. if (src_stride_y == width && dst_stride_y == width) {
  134. width *= height;
  135. height = 1;
  136. src_stride_y = dst_stride_y = 0;
  137. }
  138. #if defined(HAS_CONVERT16TO8ROW_SSSE3)
  139. if (TestCpuFlag(kCpuHasSSSE3)) {
  140. Convert16To8Row = Convert16To8Row_Any_SSSE3;
  141. if (IS_ALIGNED(width, 16)) {
  142. Convert16To8Row = Convert16To8Row_SSSE3;
  143. }
  144. }
  145. #endif
  146. #if defined(HAS_CONVERT16TO8ROW_AVX2)
  147. if (TestCpuFlag(kCpuHasAVX2)) {
  148. Convert16To8Row = Convert16To8Row_Any_AVX2;
  149. if (IS_ALIGNED(width, 32)) {
  150. Convert16To8Row = Convert16To8Row_AVX2;
  151. }
  152. }
  153. #endif
  154. // Convert plane
  155. for (y = 0; y < height; ++y) {
  156. Convert16To8Row(src_y, dst_y, scale, width);
  157. src_y += src_stride_y;
  158. dst_y += dst_stride_y;
  159. }
  160. }
  161. // Convert a plane of 8 bit data to 16 bit
  162. LIBYUV_API
  163. void Convert8To16Plane(const uint8_t* src_y,
  164. int src_stride_y,
  165. uint16_t* dst_y,
  166. int dst_stride_y,
  167. int scale, // 16384 for 10 bits
  168. int width,
  169. int height) {
  170. int y;
  171. void (*Convert8To16Row)(const uint8_t* src_y, uint16_t* dst_y, int scale,
  172. int width) = Convert8To16Row_C;
  173. // Negative height means invert the image.
  174. if (height < 0) {
  175. height = -height;
  176. dst_y = dst_y + (height - 1) * dst_stride_y;
  177. dst_stride_y = -dst_stride_y;
  178. }
  179. // Coalesce rows.
  180. if (src_stride_y == width && dst_stride_y == width) {
  181. width *= height;
  182. height = 1;
  183. src_stride_y = dst_stride_y = 0;
  184. }
  185. #if defined(HAS_CONVERT8TO16ROW_SSE2)
  186. if (TestCpuFlag(kCpuHasSSE2)) {
  187. Convert8To16Row = Convert8To16Row_Any_SSE2;
  188. if (IS_ALIGNED(width, 16)) {
  189. Convert8To16Row = Convert8To16Row_SSE2;
  190. }
  191. }
  192. #endif
  193. #if defined(HAS_CONVERT8TO16ROW_AVX2)
  194. if (TestCpuFlag(kCpuHasAVX2)) {
  195. Convert8To16Row = Convert8To16Row_Any_AVX2;
  196. if (IS_ALIGNED(width, 32)) {
  197. Convert8To16Row = Convert8To16Row_AVX2;
  198. }
  199. }
  200. #endif
  201. // Convert plane
  202. for (y = 0; y < height; ++y) {
  203. Convert8To16Row(src_y, dst_y, scale, width);
  204. src_y += src_stride_y;
  205. dst_y += dst_stride_y;
  206. }
  207. }
  208. // Copy I422.
  209. LIBYUV_API
  210. int I422Copy(const uint8_t* src_y,
  211. int src_stride_y,
  212. const uint8_t* src_u,
  213. int src_stride_u,
  214. const uint8_t* src_v,
  215. int src_stride_v,
  216. uint8_t* dst_y,
  217. int dst_stride_y,
  218. uint8_t* dst_u,
  219. int dst_stride_u,
  220. uint8_t* dst_v,
  221. int dst_stride_v,
  222. int width,
  223. int height) {
  224. int halfwidth = (width + 1) >> 1;
  225. if (!src_u || !src_v || !dst_u || !dst_v || width <= 0 || height == 0) {
  226. return -1;
  227. }
  228. // Negative height means invert the image.
  229. if (height < 0) {
  230. height = -height;
  231. src_y = src_y + (height - 1) * src_stride_y;
  232. src_u = src_u + (height - 1) * src_stride_u;
  233. src_v = src_v + (height - 1) * src_stride_v;
  234. src_stride_y = -src_stride_y;
  235. src_stride_u = -src_stride_u;
  236. src_stride_v = -src_stride_v;
  237. }
  238. if (dst_y) {
  239. CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  240. }
  241. CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, height);
  242. CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, height);
  243. return 0;
  244. }
  245. // Copy I444.
  246. LIBYUV_API
  247. int I444Copy(const uint8_t* src_y,
  248. int src_stride_y,
  249. const uint8_t* src_u,
  250. int src_stride_u,
  251. const uint8_t* src_v,
  252. int src_stride_v,
  253. uint8_t* dst_y,
  254. int dst_stride_y,
  255. uint8_t* dst_u,
  256. int dst_stride_u,
  257. uint8_t* dst_v,
  258. int dst_stride_v,
  259. int width,
  260. int height) {
  261. if (!src_u || !src_v || !dst_u || !dst_v || width <= 0 || height == 0) {
  262. return -1;
  263. }
  264. // Negative height means invert the image.
  265. if (height < 0) {
  266. height = -height;
  267. src_y = src_y + (height - 1) * src_stride_y;
  268. src_u = src_u + (height - 1) * src_stride_u;
  269. src_v = src_v + (height - 1) * src_stride_v;
  270. src_stride_y = -src_stride_y;
  271. src_stride_u = -src_stride_u;
  272. src_stride_v = -src_stride_v;
  273. }
  274. if (dst_y) {
  275. CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  276. }
  277. CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height);
  278. CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height);
  279. return 0;
  280. }
  281. // Copy I400.
  282. LIBYUV_API
  283. int I400ToI400(const uint8_t* src_y,
  284. int src_stride_y,
  285. uint8_t* dst_y,
  286. int dst_stride_y,
  287. int width,
  288. int height) {
  289. if (!src_y || !dst_y || width <= 0 || height == 0) {
  290. return -1;
  291. }
  292. // Negative height means invert the image.
  293. if (height < 0) {
  294. height = -height;
  295. src_y = src_y + (height - 1) * src_stride_y;
  296. src_stride_y = -src_stride_y;
  297. }
  298. CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  299. return 0;
  300. }
  301. // Convert I420 to I400.
  302. LIBYUV_API
  303. int I420ToI400(const uint8_t* src_y,
  304. int src_stride_y,
  305. const uint8_t* src_u,
  306. int src_stride_u,
  307. const uint8_t* src_v,
  308. int src_stride_v,
  309. uint8_t* dst_y,
  310. int dst_stride_y,
  311. int width,
  312. int height) {
  313. (void)src_u;
  314. (void)src_stride_u;
  315. (void)src_v;
  316. (void)src_stride_v;
  317. if (!src_y || !dst_y || width <= 0 || height == 0) {
  318. return -1;
  319. }
  320. // Negative height means invert the image.
  321. if (height < 0) {
  322. height = -height;
  323. src_y = src_y + (height - 1) * src_stride_y;
  324. src_stride_y = -src_stride_y;
  325. }
  326. CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  327. return 0;
  328. }
  329. // Support function for NV12 etc UV channels.
  330. // Width and height are plane sizes (typically half pixel width).
  331. LIBYUV_API
  332. void SplitUVPlane(const uint8_t* src_uv,
  333. int src_stride_uv,
  334. uint8_t* dst_u,
  335. int dst_stride_u,
  336. uint8_t* dst_v,
  337. int dst_stride_v,
  338. int width,
  339. int height) {
  340. int y;
  341. void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v,
  342. int width) = SplitUVRow_C;
  343. // Negative height means invert the image.
  344. if (height < 0) {
  345. height = -height;
  346. dst_u = dst_u + (height - 1) * dst_stride_u;
  347. dst_v = dst_v + (height - 1) * dst_stride_v;
  348. dst_stride_u = -dst_stride_u;
  349. dst_stride_v = -dst_stride_v;
  350. }
  351. // Coalesce rows.
  352. if (src_stride_uv == width * 2 && dst_stride_u == width &&
  353. dst_stride_v == width) {
  354. width *= height;
  355. height = 1;
  356. src_stride_uv = dst_stride_u = dst_stride_v = 0;
  357. }
  358. #if defined(HAS_SPLITUVROW_SSE2)
  359. if (TestCpuFlag(kCpuHasSSE2)) {
  360. SplitUVRow = SplitUVRow_Any_SSE2;
  361. if (IS_ALIGNED(width, 16)) {
  362. SplitUVRow = SplitUVRow_SSE2;
  363. }
  364. }
  365. #endif
  366. #if defined(HAS_SPLITUVROW_AVX2)
  367. if (TestCpuFlag(kCpuHasAVX2)) {
  368. SplitUVRow = SplitUVRow_Any_AVX2;
  369. if (IS_ALIGNED(width, 32)) {
  370. SplitUVRow = SplitUVRow_AVX2;
  371. }
  372. }
  373. #endif
  374. #if defined(HAS_SPLITUVROW_NEON)
  375. if (TestCpuFlag(kCpuHasNEON)) {
  376. SplitUVRow = SplitUVRow_Any_NEON;
  377. if (IS_ALIGNED(width, 16)) {
  378. SplitUVRow = SplitUVRow_NEON;
  379. }
  380. }
  381. #endif
  382. #if defined(HAS_SPLITUVROW_MMI)
  383. if (TestCpuFlag(kCpuHasMMI)) {
  384. SplitUVRow = SplitUVRow_Any_MMI;
  385. if (IS_ALIGNED(width, 8)) {
  386. SplitUVRow = SplitUVRow_MMI;
  387. }
  388. }
  389. #endif
  390. #if defined(HAS_SPLITUVROW_MSA)
  391. if (TestCpuFlag(kCpuHasMSA)) {
  392. SplitUVRow = SplitUVRow_Any_MSA;
  393. if (IS_ALIGNED(width, 32)) {
  394. SplitUVRow = SplitUVRow_MSA;
  395. }
  396. }
  397. #endif
  398. for (y = 0; y < height; ++y) {
  399. // Copy a row of UV.
  400. SplitUVRow(src_uv, dst_u, dst_v, width);
  401. dst_u += dst_stride_u;
  402. dst_v += dst_stride_v;
  403. src_uv += src_stride_uv;
  404. }
  405. }
  406. LIBYUV_API
  407. void MergeUVPlane(const uint8_t* src_u,
  408. int src_stride_u,
  409. const uint8_t* src_v,
  410. int src_stride_v,
  411. uint8_t* dst_uv,
  412. int dst_stride_uv,
  413. int width,
  414. int height) {
  415. int y;
  416. void (*MergeUVRow)(const uint8_t* src_u, const uint8_t* src_v,
  417. uint8_t* dst_uv, int width) = MergeUVRow_C;
  418. // Negative height means invert the image.
  419. if (height < 0) {
  420. height = -height;
  421. dst_uv = dst_uv + (height - 1) * dst_stride_uv;
  422. dst_stride_uv = -dst_stride_uv;
  423. }
  424. // Coalesce rows.
  425. if (src_stride_u == width && src_stride_v == width &&
  426. dst_stride_uv == width * 2) {
  427. width *= height;
  428. height = 1;
  429. src_stride_u = src_stride_v = dst_stride_uv = 0;
  430. }
  431. #if defined(HAS_MERGEUVROW_SSE2)
  432. if (TestCpuFlag(kCpuHasSSE2)) {
  433. MergeUVRow = MergeUVRow_Any_SSE2;
  434. if (IS_ALIGNED(width, 16)) {
  435. MergeUVRow = MergeUVRow_SSE2;
  436. }
  437. }
  438. #endif
  439. #if defined(HAS_MERGEUVROW_AVX2)
  440. if (TestCpuFlag(kCpuHasAVX2)) {
  441. MergeUVRow = MergeUVRow_Any_AVX2;
  442. if (IS_ALIGNED(width, 32)) {
  443. MergeUVRow = MergeUVRow_AVX2;
  444. }
  445. }
  446. #endif
  447. #if defined(HAS_MERGEUVROW_NEON)
  448. if (TestCpuFlag(kCpuHasNEON)) {
  449. MergeUVRow = MergeUVRow_Any_NEON;
  450. if (IS_ALIGNED(width, 16)) {
  451. MergeUVRow = MergeUVRow_NEON;
  452. }
  453. }
  454. #endif
  455. #if defined(HAS_MERGEUVROW_MMI)
  456. if (TestCpuFlag(kCpuHasMMI)) {
  457. MergeUVRow = MergeUVRow_Any_MMI;
  458. if (IS_ALIGNED(width, 8)) {
  459. MergeUVRow = MergeUVRow_MMI;
  460. }
  461. }
  462. #endif
  463. #if defined(HAS_MERGEUVROW_MSA)
  464. if (TestCpuFlag(kCpuHasMSA)) {
  465. MergeUVRow = MergeUVRow_Any_MSA;
  466. if (IS_ALIGNED(width, 16)) {
  467. MergeUVRow = MergeUVRow_MSA;
  468. }
  469. }
  470. #endif
  471. for (y = 0; y < height; ++y) {
  472. // Merge a row of U and V into a row of UV.
  473. MergeUVRow(src_u, src_v, dst_uv, width);
  474. src_u += src_stride_u;
  475. src_v += src_stride_v;
  476. dst_uv += dst_stride_uv;
  477. }
  478. }
  479. // Swap U and V channels in interleaved UV plane.
  480. LIBYUV_API
  481. void SwapUVPlane(const uint8_t* src_uv,
  482. int src_stride_uv,
  483. uint8_t* dst_vu,
  484. int dst_stride_vu,
  485. int width,
  486. int height) {
  487. int y;
  488. void (*SwapUVRow)(const uint8_t* src_uv, uint8_t* dst_vu, int width) =
  489. SwapUVRow_C;
  490. // Negative height means invert the image.
  491. if (height < 0) {
  492. height = -height;
  493. src_uv = src_uv + (height - 1) * src_stride_uv;
  494. src_stride_uv = -src_stride_uv;
  495. }
  496. // Coalesce rows.
  497. if (src_stride_uv == width * 2 && dst_stride_vu == width * 2) {
  498. width *= height;
  499. height = 1;
  500. src_stride_uv = dst_stride_vu = 0;
  501. }
  502. #if defined(HAS_SWAPUVROW_SSSE3)
  503. if (TestCpuFlag(kCpuHasSSSE3)) {
  504. SwapUVRow = SwapUVRow_Any_SSSE3;
  505. if (IS_ALIGNED(width, 16)) {
  506. SwapUVRow = SwapUVRow_SSSE3;
  507. }
  508. }
  509. #endif
  510. #if defined(HAS_SWAPUVROW_AVX2)
  511. if (TestCpuFlag(kCpuHasAVX2)) {
  512. SwapUVRow = SwapUVRow_Any_AVX2;
  513. if (IS_ALIGNED(width, 32)) {
  514. SwapUVRow = SwapUVRow_AVX2;
  515. }
  516. }
  517. #endif
  518. #if defined(HAS_SWAPUVROW_NEON)
  519. if (TestCpuFlag(kCpuHasNEON)) {
  520. SwapUVRow = SwapUVRow_Any_NEON;
  521. if (IS_ALIGNED(width, 16)) {
  522. SwapUVRow = SwapUVRow_NEON;
  523. }
  524. }
  525. #endif
  526. for (y = 0; y < height; ++y) {
  527. SwapUVRow(src_uv, dst_vu, width);
  528. src_uv += src_stride_uv;
  529. dst_vu += dst_stride_vu;
  530. }
  531. }
  532. // Convert NV21 to NV12.
  533. LIBYUV_API
  534. int NV21ToNV12(const uint8_t* src_y,
  535. int src_stride_y,
  536. const uint8_t* src_vu,
  537. int src_stride_vu,
  538. uint8_t* dst_y,
  539. int dst_stride_y,
  540. uint8_t* dst_uv,
  541. int dst_stride_uv,
  542. int width,
  543. int height) {
  544. int halfwidth = (width + 1) >> 1;
  545. int halfheight = (height + 1) >> 1;
  546. if (!src_vu || !dst_uv || width <= 0 || height == 0) {
  547. return -1;
  548. }
  549. if (dst_y) {
  550. CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  551. }
  552. // Negative height means invert the image.
  553. if (height < 0) {
  554. height = -height;
  555. halfheight = (height + 1) >> 1;
  556. src_vu = src_vu + (halfheight - 1) * src_stride_vu;
  557. src_stride_vu = -src_stride_vu;
  558. }
  559. SwapUVPlane(src_vu, src_stride_vu, dst_uv, dst_stride_uv, halfwidth,
  560. halfheight);
  561. return 0;
  562. }
  563. // Support function for NV12 etc RGB channels.
  564. // Width and height are plane sizes (typically half pixel width).
  565. LIBYUV_API
  566. void SplitRGBPlane(const uint8_t* src_rgb,
  567. int src_stride_rgb,
  568. uint8_t* dst_r,
  569. int dst_stride_r,
  570. uint8_t* dst_g,
  571. int dst_stride_g,
  572. uint8_t* dst_b,
  573. int dst_stride_b,
  574. int width,
  575. int height) {
  576. int y;
  577. void (*SplitRGBRow)(const uint8_t* src_rgb, uint8_t* dst_r, uint8_t* dst_g,
  578. uint8_t* dst_b, int width) = SplitRGBRow_C;
  579. // Negative height means invert the image.
  580. if (height < 0) {
  581. height = -height;
  582. dst_r = dst_r + (height - 1) * dst_stride_r;
  583. dst_g = dst_g + (height - 1) * dst_stride_g;
  584. dst_b = dst_b + (height - 1) * dst_stride_b;
  585. dst_stride_r = -dst_stride_r;
  586. dst_stride_g = -dst_stride_g;
  587. dst_stride_b = -dst_stride_b;
  588. }
  589. // Coalesce rows.
  590. if (src_stride_rgb == width * 3 && dst_stride_r == width &&
  591. dst_stride_g == width && dst_stride_b == width) {
  592. width *= height;
  593. height = 1;
  594. src_stride_rgb = dst_stride_r = dst_stride_g = dst_stride_b = 0;
  595. }
  596. #if defined(HAS_SPLITRGBROW_SSSE3)
  597. if (TestCpuFlag(kCpuHasSSSE3)) {
  598. SplitRGBRow = SplitRGBRow_Any_SSSE3;
  599. if (IS_ALIGNED(width, 16)) {
  600. SplitRGBRow = SplitRGBRow_SSSE3;
  601. }
  602. }
  603. #endif
  604. #if defined(HAS_SPLITRGBROW_MMI)
  605. if (TestCpuFlag(kCpuHasMMI)) {
  606. SplitRGBRow = SplitRGBRow_Any_MMI;
  607. if (IS_ALIGNED(width, 4)) {
  608. SplitRGBRow = SplitRGBRow_MMI;
  609. }
  610. }
  611. #endif
  612. #if defined(HAS_SPLITRGBROW_NEON)
  613. if (TestCpuFlag(kCpuHasNEON)) {
  614. SplitRGBRow = SplitRGBRow_Any_NEON;
  615. if (IS_ALIGNED(width, 16)) {
  616. SplitRGBRow = SplitRGBRow_NEON;
  617. }
  618. }
  619. #endif
  620. for (y = 0; y < height; ++y) {
  621. // Copy a row of RGB.
  622. SplitRGBRow(src_rgb, dst_r, dst_g, dst_b, width);
  623. dst_r += dst_stride_r;
  624. dst_g += dst_stride_g;
  625. dst_b += dst_stride_b;
  626. src_rgb += src_stride_rgb;
  627. }
  628. }
  629. LIBYUV_API
  630. void MergeRGBPlane(const uint8_t* src_r,
  631. int src_stride_r,
  632. const uint8_t* src_g,
  633. int src_stride_g,
  634. const uint8_t* src_b,
  635. int src_stride_b,
  636. uint8_t* dst_rgb,
  637. int dst_stride_rgb,
  638. int width,
  639. int height) {
  640. int y;
  641. void (*MergeRGBRow)(const uint8_t* src_r, const uint8_t* src_g,
  642. const uint8_t* src_b, uint8_t* dst_rgb, int width) =
  643. MergeRGBRow_C;
  644. // Coalesce rows.
  645. // Negative height means invert the image.
  646. if (height < 0) {
  647. height = -height;
  648. dst_rgb = dst_rgb + (height - 1) * dst_stride_rgb;
  649. dst_stride_rgb = -dst_stride_rgb;
  650. }
  651. // Coalesce rows.
  652. if (src_stride_r == width && src_stride_g == width && src_stride_b == width &&
  653. dst_stride_rgb == width * 3) {
  654. width *= height;
  655. height = 1;
  656. src_stride_r = src_stride_g = src_stride_b = dst_stride_rgb = 0;
  657. }
  658. #if defined(HAS_MERGERGBROW_SSSE3)
  659. if (TestCpuFlag(kCpuHasSSSE3)) {
  660. MergeRGBRow = MergeRGBRow_Any_SSSE3;
  661. if (IS_ALIGNED(width, 16)) {
  662. MergeRGBRow = MergeRGBRow_SSSE3;
  663. }
  664. }
  665. #endif
  666. #if defined(HAS_MERGERGBROW_NEON)
  667. if (TestCpuFlag(kCpuHasNEON)) {
  668. MergeRGBRow = MergeRGBRow_Any_NEON;
  669. if (IS_ALIGNED(width, 16)) {
  670. MergeRGBRow = MergeRGBRow_NEON;
  671. }
  672. }
  673. #endif
  674. #if defined(HAS_MERGERGBROW_MMI)
  675. if (TestCpuFlag(kCpuHasMMI)) {
  676. MergeRGBRow = MergeRGBRow_Any_MMI;
  677. if (IS_ALIGNED(width, 8)) {
  678. MergeRGBRow = MergeRGBRow_MMI;
  679. }
  680. }
  681. #endif
  682. for (y = 0; y < height; ++y) {
  683. // Merge a row of U and V into a row of RGB.
  684. MergeRGBRow(src_r, src_g, src_b, dst_rgb, width);
  685. src_r += src_stride_r;
  686. src_g += src_stride_g;
  687. src_b += src_stride_b;
  688. dst_rgb += dst_stride_rgb;
  689. }
  690. }
  691. // Convert YUY2 to I422.
  692. LIBYUV_API
  693. int YUY2ToI422(const uint8_t* src_yuy2,
  694. int src_stride_yuy2,
  695. uint8_t* dst_y,
  696. int dst_stride_y,
  697. uint8_t* dst_u,
  698. int dst_stride_u,
  699. uint8_t* dst_v,
  700. int dst_stride_v,
  701. int width,
  702. int height) {
  703. int y;
  704. void (*YUY2ToUV422Row)(const uint8_t* src_yuy2, uint8_t* dst_u,
  705. uint8_t* dst_v, int width) = YUY2ToUV422Row_C;
  706. void (*YUY2ToYRow)(const uint8_t* src_yuy2, uint8_t* dst_y, int width) =
  707. YUY2ToYRow_C;
  708. if (!src_yuy2 || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) {
  709. return -1;
  710. }
  711. // Negative height means invert the image.
  712. if (height < 0) {
  713. height = -height;
  714. src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2;
  715. src_stride_yuy2 = -src_stride_yuy2;
  716. }
  717. // Coalesce rows.
  718. if (src_stride_yuy2 == width * 2 && dst_stride_y == width &&
  719. dst_stride_u * 2 == width && dst_stride_v * 2 == width &&
  720. width * height <= 32768) {
  721. width *= height;
  722. height = 1;
  723. src_stride_yuy2 = dst_stride_y = dst_stride_u = dst_stride_v = 0;
  724. }
  725. #if defined(HAS_YUY2TOYROW_SSE2)
  726. if (TestCpuFlag(kCpuHasSSE2)) {
  727. YUY2ToUV422Row = YUY2ToUV422Row_Any_SSE2;
  728. YUY2ToYRow = YUY2ToYRow_Any_SSE2;
  729. if (IS_ALIGNED(width, 16)) {
  730. YUY2ToUV422Row = YUY2ToUV422Row_SSE2;
  731. YUY2ToYRow = YUY2ToYRow_SSE2;
  732. }
  733. }
  734. #endif
  735. #if defined(HAS_YUY2TOYROW_AVX2)
  736. if (TestCpuFlag(kCpuHasAVX2)) {
  737. YUY2ToUV422Row = YUY2ToUV422Row_Any_AVX2;
  738. YUY2ToYRow = YUY2ToYRow_Any_AVX2;
  739. if (IS_ALIGNED(width, 32)) {
  740. YUY2ToUV422Row = YUY2ToUV422Row_AVX2;
  741. YUY2ToYRow = YUY2ToYRow_AVX2;
  742. }
  743. }
  744. #endif
  745. #if defined(HAS_YUY2TOYROW_NEON)
  746. if (TestCpuFlag(kCpuHasNEON)) {
  747. YUY2ToYRow = YUY2ToYRow_Any_NEON;
  748. YUY2ToUV422Row = YUY2ToUV422Row_Any_NEON;
  749. if (IS_ALIGNED(width, 16)) {
  750. YUY2ToYRow = YUY2ToYRow_NEON;
  751. YUY2ToUV422Row = YUY2ToUV422Row_NEON;
  752. }
  753. }
  754. #endif
  755. #if defined(HAS_YUY2TOYROW_MMI) && defined(HAS_YUY2TOUV422ROW_MMI)
  756. if (TestCpuFlag(kCpuHasMMI)) {
  757. YUY2ToYRow = YUY2ToYRow_Any_MMI;
  758. YUY2ToUV422Row = YUY2ToUV422Row_Any_MMI;
  759. if (IS_ALIGNED(width, 8)) {
  760. YUY2ToYRow = YUY2ToYRow_MMI;
  761. YUY2ToUV422Row = YUY2ToUV422Row_MMI;
  762. }
  763. }
  764. #endif
  765. #if defined(HAS_YUY2TOYROW_MSA) && defined(HAS_YUY2TOUV422ROW_MSA)
  766. if (TestCpuFlag(kCpuHasMSA)) {
  767. YUY2ToYRow = YUY2ToYRow_Any_MSA;
  768. YUY2ToUV422Row = YUY2ToUV422Row_Any_MSA;
  769. if (IS_ALIGNED(width, 32)) {
  770. YUY2ToYRow = YUY2ToYRow_MSA;
  771. YUY2ToUV422Row = YUY2ToUV422Row_MSA;
  772. }
  773. }
  774. #endif
  775. for (y = 0; y < height; ++y) {
  776. YUY2ToUV422Row(src_yuy2, dst_u, dst_v, width);
  777. YUY2ToYRow(src_yuy2, dst_y, width);
  778. src_yuy2 += src_stride_yuy2;
  779. dst_y += dst_stride_y;
  780. dst_u += dst_stride_u;
  781. dst_v += dst_stride_v;
  782. }
  783. return 0;
  784. }
  785. // Convert UYVY to I422.
  786. LIBYUV_API
  787. int UYVYToI422(const uint8_t* src_uyvy,
  788. int src_stride_uyvy,
  789. uint8_t* dst_y,
  790. int dst_stride_y,
  791. uint8_t* dst_u,
  792. int dst_stride_u,
  793. uint8_t* dst_v,
  794. int dst_stride_v,
  795. int width,
  796. int height) {
  797. int y;
  798. void (*UYVYToUV422Row)(const uint8_t* src_uyvy, uint8_t* dst_u,
  799. uint8_t* dst_v, int width) = UYVYToUV422Row_C;
  800. void (*UYVYToYRow)(const uint8_t* src_uyvy, uint8_t* dst_y, int width) =
  801. UYVYToYRow_C;
  802. if (!src_uyvy || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) {
  803. return -1;
  804. }
  805. // Negative height means invert the image.
  806. if (height < 0) {
  807. height = -height;
  808. src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy;
  809. src_stride_uyvy = -src_stride_uyvy;
  810. }
  811. // Coalesce rows.
  812. if (src_stride_uyvy == width * 2 && dst_stride_y == width &&
  813. dst_stride_u * 2 == width && dst_stride_v * 2 == width &&
  814. width * height <= 32768) {
  815. width *= height;
  816. height = 1;
  817. src_stride_uyvy = dst_stride_y = dst_stride_u = dst_stride_v = 0;
  818. }
  819. #if defined(HAS_UYVYTOYROW_SSE2)
  820. if (TestCpuFlag(kCpuHasSSE2)) {
  821. UYVYToUV422Row = UYVYToUV422Row_Any_SSE2;
  822. UYVYToYRow = UYVYToYRow_Any_SSE2;
  823. if (IS_ALIGNED(width, 16)) {
  824. UYVYToUV422Row = UYVYToUV422Row_SSE2;
  825. UYVYToYRow = UYVYToYRow_SSE2;
  826. }
  827. }
  828. #endif
  829. #if defined(HAS_UYVYTOYROW_AVX2)
  830. if (TestCpuFlag(kCpuHasAVX2)) {
  831. UYVYToUV422Row = UYVYToUV422Row_Any_AVX2;
  832. UYVYToYRow = UYVYToYRow_Any_AVX2;
  833. if (IS_ALIGNED(width, 32)) {
  834. UYVYToUV422Row = UYVYToUV422Row_AVX2;
  835. UYVYToYRow = UYVYToYRow_AVX2;
  836. }
  837. }
  838. #endif
  839. #if defined(HAS_UYVYTOYROW_NEON)
  840. if (TestCpuFlag(kCpuHasNEON)) {
  841. UYVYToYRow = UYVYToYRow_Any_NEON;
  842. UYVYToUV422Row = UYVYToUV422Row_Any_NEON;
  843. if (IS_ALIGNED(width, 16)) {
  844. UYVYToYRow = UYVYToYRow_NEON;
  845. UYVYToUV422Row = UYVYToUV422Row_NEON;
  846. }
  847. }
  848. #endif
  849. #if defined(HAS_UYVYTOYROW_MMI) && defined(HAS_UYVYTOUV422ROW_MMI)
  850. if (TestCpuFlag(kCpuHasMMI)) {
  851. UYVYToYRow = UYVYToYRow_Any_MMI;
  852. UYVYToUV422Row = UYVYToUV422Row_Any_MMI;
  853. if (IS_ALIGNED(width, 16)) {
  854. UYVYToYRow = UYVYToYRow_MMI;
  855. UYVYToUV422Row = UYVYToUV422Row_MMI;
  856. }
  857. }
  858. #endif
  859. #if defined(HAS_UYVYTOYROW_MSA) && defined(HAS_UYVYTOUV422ROW_MSA)
  860. if (TestCpuFlag(kCpuHasMSA)) {
  861. UYVYToYRow = UYVYToYRow_Any_MSA;
  862. UYVYToUV422Row = UYVYToUV422Row_Any_MSA;
  863. if (IS_ALIGNED(width, 32)) {
  864. UYVYToYRow = UYVYToYRow_MSA;
  865. UYVYToUV422Row = UYVYToUV422Row_MSA;
  866. }
  867. }
  868. #endif
  869. for (y = 0; y < height; ++y) {
  870. UYVYToUV422Row(src_uyvy, dst_u, dst_v, width);
  871. UYVYToYRow(src_uyvy, dst_y, width);
  872. src_uyvy += src_stride_uyvy;
  873. dst_y += dst_stride_y;
  874. dst_u += dst_stride_u;
  875. dst_v += dst_stride_v;
  876. }
  877. return 0;
  878. }
  879. // Convert YUY2 to Y.
  880. LIBYUV_API
  881. int YUY2ToY(const uint8_t* src_yuy2,
  882. int src_stride_yuy2,
  883. uint8_t* dst_y,
  884. int dst_stride_y,
  885. int width,
  886. int height) {
  887. int y;
  888. void (*YUY2ToYRow)(const uint8_t* src_yuy2, uint8_t* dst_y, int width) =
  889. YUY2ToYRow_C;
  890. if (!src_yuy2 || !dst_y || width <= 0 || height == 0) {
  891. return -1;
  892. }
  893. // Negative height means invert the image.
  894. if (height < 0) {
  895. height = -height;
  896. src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2;
  897. src_stride_yuy2 = -src_stride_yuy2;
  898. }
  899. // Coalesce rows.
  900. if (src_stride_yuy2 == width * 2 && dst_stride_y == width) {
  901. width *= height;
  902. height = 1;
  903. src_stride_yuy2 = dst_stride_y = 0;
  904. }
  905. #if defined(HAS_YUY2TOYROW_SSE2)
  906. if (TestCpuFlag(kCpuHasSSE2)) {
  907. YUY2ToYRow = YUY2ToYRow_Any_SSE2;
  908. if (IS_ALIGNED(width, 16)) {
  909. YUY2ToYRow = YUY2ToYRow_SSE2;
  910. }
  911. }
  912. #endif
  913. #if defined(HAS_YUY2TOYROW_AVX2)
  914. if (TestCpuFlag(kCpuHasAVX2)) {
  915. YUY2ToYRow = YUY2ToYRow_Any_AVX2;
  916. if (IS_ALIGNED(width, 32)) {
  917. YUY2ToYRow = YUY2ToYRow_AVX2;
  918. }
  919. }
  920. #endif
  921. #if defined(HAS_YUY2TOYROW_NEON)
  922. if (TestCpuFlag(kCpuHasNEON)) {
  923. YUY2ToYRow = YUY2ToYRow_Any_NEON;
  924. if (IS_ALIGNED(width, 16)) {
  925. YUY2ToYRow = YUY2ToYRow_NEON;
  926. }
  927. }
  928. #endif
  929. #if defined(HAS_YUY2TOYROW_MMI)
  930. if (TestCpuFlag(kCpuHasMMI)) {
  931. YUY2ToYRow = YUY2ToYRow_Any_MMI;
  932. if (IS_ALIGNED(width, 8)) {
  933. YUY2ToYRow = YUY2ToYRow_MMI;
  934. }
  935. }
  936. #endif
  937. #if defined(HAS_YUY2TOYROW_MSA)
  938. if (TestCpuFlag(kCpuHasMSA)) {
  939. YUY2ToYRow = YUY2ToYRow_Any_MSA;
  940. if (IS_ALIGNED(width, 32)) {
  941. YUY2ToYRow = YUY2ToYRow_MSA;
  942. }
  943. }
  944. #endif
  945. for (y = 0; y < height; ++y) {
  946. YUY2ToYRow(src_yuy2, dst_y, width);
  947. src_yuy2 += src_stride_yuy2;
  948. dst_y += dst_stride_y;
  949. }
  950. return 0;
  951. }
  952. // Mirror a plane of data.
  953. // See Also I400Mirror
  954. LIBYUV_API
  955. void MirrorPlane(const uint8_t* src_y,
  956. int src_stride_y,
  957. uint8_t* dst_y,
  958. int dst_stride_y,
  959. int width,
  960. int height) {
  961. int y;
  962. void (*MirrorRow)(const uint8_t* src, uint8_t* dst, int width) = MirrorRow_C;
  963. // Negative height means invert the image.
  964. if (height < 0) {
  965. height = -height;
  966. src_y = src_y + (height - 1) * src_stride_y;
  967. src_stride_y = -src_stride_y;
  968. }
  969. #if defined(HAS_MIRRORROW_NEON)
  970. if (TestCpuFlag(kCpuHasNEON)) {
  971. MirrorRow = MirrorRow_Any_NEON;
  972. if (IS_ALIGNED(width, 32)) {
  973. MirrorRow = MirrorRow_NEON;
  974. }
  975. }
  976. #endif
  977. #if defined(HAS_MIRRORROW_SSSE3)
  978. if (TestCpuFlag(kCpuHasSSSE3)) {
  979. MirrorRow = MirrorRow_Any_SSSE3;
  980. if (IS_ALIGNED(width, 16)) {
  981. MirrorRow = MirrorRow_SSSE3;
  982. }
  983. }
  984. #endif
  985. #if defined(HAS_MIRRORROW_AVX2)
  986. if (TestCpuFlag(kCpuHasAVX2)) {
  987. MirrorRow = MirrorRow_Any_AVX2;
  988. if (IS_ALIGNED(width, 32)) {
  989. MirrorRow = MirrorRow_AVX2;
  990. }
  991. }
  992. #endif
  993. #if defined(HAS_MIRRORROW_MMI)
  994. if (TestCpuFlag(kCpuHasMMI)) {
  995. MirrorRow = MirrorRow_Any_MMI;
  996. if (IS_ALIGNED(width, 8)) {
  997. MirrorRow = MirrorRow_MMI;
  998. }
  999. }
  1000. #endif
  1001. #if defined(HAS_MIRRORROW_MSA)
  1002. if (TestCpuFlag(kCpuHasMSA)) {
  1003. MirrorRow = MirrorRow_Any_MSA;
  1004. if (IS_ALIGNED(width, 64)) {
  1005. MirrorRow = MirrorRow_MSA;
  1006. }
  1007. }
  1008. #endif
  1009. // Mirror plane
  1010. for (y = 0; y < height; ++y) {
  1011. MirrorRow(src_y, dst_y, width);
  1012. src_y += src_stride_y;
  1013. dst_y += dst_stride_y;
  1014. }
  1015. }
  1016. // Mirror a plane of UV data.
  1017. LIBYUV_API
  1018. void MirrorUVPlane(const uint8_t* src_uv,
  1019. int src_stride_uv,
  1020. uint8_t* dst_uv,
  1021. int dst_stride_uv,
  1022. int width,
  1023. int height) {
  1024. int y;
  1025. void (*MirrorUVRow)(const uint8_t* src, uint8_t* dst, int width) =
  1026. MirrorUVRow_C;
  1027. // Negative height means invert the image.
  1028. if (height < 0) {
  1029. height = -height;
  1030. src_uv = src_uv + (height - 1) * src_stride_uv;
  1031. src_stride_uv = -src_stride_uv;
  1032. }
  1033. #if defined(HAS_MIRRORUVROW_NEON)
  1034. if (TestCpuFlag(kCpuHasNEON)) {
  1035. MirrorUVRow = MirrorUVRow_Any_NEON;
  1036. if (IS_ALIGNED(width, 32)) {
  1037. MirrorUVRow = MirrorUVRow_NEON;
  1038. }
  1039. }
  1040. #endif
  1041. #if defined(HAS_MIRRORUVROW_SSSE3)
  1042. if (TestCpuFlag(kCpuHasSSSE3)) {
  1043. MirrorUVRow = MirrorUVRow_Any_SSSE3;
  1044. if (IS_ALIGNED(width, 8)) {
  1045. MirrorUVRow = MirrorUVRow_SSSE3;
  1046. }
  1047. }
  1048. #endif
  1049. #if defined(HAS_MIRRORUVROW_AVX2)
  1050. if (TestCpuFlag(kCpuHasAVX2)) {
  1051. MirrorUVRow = MirrorUVRow_Any_AVX2;
  1052. if (IS_ALIGNED(width, 16)) {
  1053. MirrorUVRow = MirrorUVRow_AVX2;
  1054. }
  1055. }
  1056. #endif
  1057. #if defined(HAS_MIRRORUVROW_MSA)
  1058. if (TestCpuFlag(kCpuHasMSA)) {
  1059. MirrorUVRow = MirrorUVRow_Any_MSA;
  1060. if (IS_ALIGNED(width, 8)) {
  1061. MirrorUVRow = MirrorUVRow_MSA;
  1062. }
  1063. }
  1064. #endif
  1065. // MirrorUV plane
  1066. for (y = 0; y < height; ++y) {
  1067. MirrorUVRow(src_uv, dst_uv, width);
  1068. src_uv += src_stride_uv;
  1069. dst_uv += dst_stride_uv;
  1070. }
  1071. }
  1072. // Mirror I400 with optional flipping
  1073. LIBYUV_API
  1074. int I400Mirror(const uint8_t* src_y,
  1075. int src_stride_y,
  1076. uint8_t* dst_y,
  1077. int dst_stride_y,
  1078. int width,
  1079. int height) {
  1080. if (!src_y || !dst_y || width <= 0 || height == 0) {
  1081. return -1;
  1082. }
  1083. // Negative height means invert the image.
  1084. if (height < 0) {
  1085. height = -height;
  1086. src_y = src_y + (height - 1) * src_stride_y;
  1087. src_stride_y = -src_stride_y;
  1088. }
  1089. MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  1090. return 0;
  1091. }
  1092. // Mirror I420 with optional flipping
  1093. LIBYUV_API
  1094. int I420Mirror(const uint8_t* src_y,
  1095. int src_stride_y,
  1096. const uint8_t* src_u,
  1097. int src_stride_u,
  1098. const uint8_t* src_v,
  1099. int src_stride_v,
  1100. uint8_t* dst_y,
  1101. int dst_stride_y,
  1102. uint8_t* dst_u,
  1103. int dst_stride_u,
  1104. uint8_t* dst_v,
  1105. int dst_stride_v,
  1106. int width,
  1107. int height) {
  1108. int halfwidth = (width + 1) >> 1;
  1109. int halfheight = (height + 1) >> 1;
  1110. if (!src_y || !src_u || !src_v || !dst_u || !dst_v || width <= 0 ||
  1111. height == 0) {
  1112. return -1;
  1113. }
  1114. // Negative height means invert the image.
  1115. if (height < 0) {
  1116. height = -height;
  1117. halfheight = (height + 1) >> 1;
  1118. src_y = src_y + (height - 1) * src_stride_y;
  1119. src_u = src_u + (halfheight - 1) * src_stride_u;
  1120. src_v = src_v + (halfheight - 1) * src_stride_v;
  1121. src_stride_y = -src_stride_y;
  1122. src_stride_u = -src_stride_u;
  1123. src_stride_v = -src_stride_v;
  1124. }
  1125. if (dst_y) {
  1126. MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  1127. }
  1128. MirrorPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, halfheight);
  1129. MirrorPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, halfheight);
  1130. return 0;
  1131. }
  1132. // NV12 mirror.
  1133. LIBYUV_API
  1134. int NV12Mirror(const uint8_t* src_y,
  1135. int src_stride_y,
  1136. const uint8_t* src_uv,
  1137. int src_stride_uv,
  1138. uint8_t* dst_y,
  1139. int dst_stride_y,
  1140. uint8_t* dst_uv,
  1141. int dst_stride_uv,
  1142. int width,
  1143. int height) {
  1144. int halfwidth = (width + 1) >> 1;
  1145. int halfheight = (height + 1) >> 1;
  1146. if (!src_y || !src_uv || !dst_uv || width <= 0 || height == 0) {
  1147. return -1;
  1148. }
  1149. // Negative height means invert the image.
  1150. if (height < 0) {
  1151. height = -height;
  1152. halfheight = (height + 1) >> 1;
  1153. src_y = src_y + (height - 1) * src_stride_y;
  1154. src_uv = src_uv + (halfheight - 1) * src_stride_uv;
  1155. src_stride_y = -src_stride_y;
  1156. src_stride_uv = -src_stride_uv;
  1157. }
  1158. if (dst_y) {
  1159. MirrorPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
  1160. }
  1161. MirrorUVPlane(src_uv, src_stride_uv, dst_uv, dst_stride_uv, halfwidth,
  1162. halfheight);
  1163. return 0;
  1164. }
  1165. // ARGB mirror.
  1166. LIBYUV_API
  1167. int ARGBMirror(const uint8_t* src_argb,
  1168. int src_stride_argb,
  1169. uint8_t* dst_argb,
  1170. int dst_stride_argb,
  1171. int width,
  1172. int height) {
  1173. int y;
  1174. void (*ARGBMirrorRow)(const uint8_t* src, uint8_t* dst, int width) =
  1175. ARGBMirrorRow_C;
  1176. if (!src_argb || !dst_argb || width <= 0 || height == 0) {
  1177. return -1;
  1178. }
  1179. // Negative height means invert the image.
  1180. if (height < 0) {
  1181. height = -height;
  1182. src_argb = src_argb + (height - 1) * src_stride_argb;
  1183. src_stride_argb = -src_stride_argb;
  1184. }
  1185. #if defined(HAS_ARGBMIRRORROW_NEON)
  1186. if (TestCpuFlag(kCpuHasNEON)) {
  1187. ARGBMirrorRow = ARGBMirrorRow_Any_NEON;
  1188. if (IS_ALIGNED(width, 8)) {
  1189. ARGBMirrorRow = ARGBMirrorRow_NEON;
  1190. }
  1191. }
  1192. #endif
  1193. #if defined(HAS_ARGBMIRRORROW_SSE2)
  1194. if (TestCpuFlag(kCpuHasSSE2)) {
  1195. ARGBMirrorRow = ARGBMirrorRow_Any_SSE2;
  1196. if (IS_ALIGNED(width, 4)) {
  1197. ARGBMirrorRow = ARGBMirrorRow_SSE2;
  1198. }
  1199. }
  1200. #endif
  1201. #if defined(HAS_ARGBMIRRORROW_AVX2)
  1202. if (TestCpuFlag(kCpuHasAVX2)) {
  1203. ARGBMirrorRow = ARGBMirrorRow_Any_AVX2;
  1204. if (IS_ALIGNED(width, 8)) {
  1205. ARGBMirrorRow = ARGBMirrorRow_AVX2;
  1206. }
  1207. }
  1208. #endif
  1209. #if defined(HAS_ARGBMIRRORROW_MMI)
  1210. if (TestCpuFlag(kCpuHasMMI)) {
  1211. ARGBMirrorRow = ARGBMirrorRow_Any_MMI;
  1212. if (IS_ALIGNED(width, 2)) {
  1213. ARGBMirrorRow = ARGBMirrorRow_MMI;
  1214. }
  1215. }
  1216. #endif
  1217. #if defined(HAS_ARGBMIRRORROW_MSA)
  1218. if (TestCpuFlag(kCpuHasMSA)) {
  1219. ARGBMirrorRow = ARGBMirrorRow_Any_MSA;
  1220. if (IS_ALIGNED(width, 16)) {
  1221. ARGBMirrorRow = ARGBMirrorRow_MSA;
  1222. }
  1223. }
  1224. #endif
  1225. // Mirror plane
  1226. for (y = 0; y < height; ++y) {
  1227. ARGBMirrorRow(src_argb, dst_argb, width);
  1228. src_argb += src_stride_argb;
  1229. dst_argb += dst_stride_argb;
  1230. }
  1231. return 0;
  1232. }
  1233. // RGB24 mirror.
  1234. LIBYUV_API
  1235. int RGB24Mirror(const uint8_t* src_rgb24,
  1236. int src_stride_rgb24,
  1237. uint8_t* dst_rgb24,
  1238. int dst_stride_rgb24,
  1239. int width,
  1240. int height) {
  1241. int y;
  1242. void (*RGB24MirrorRow)(const uint8_t* src, uint8_t* dst, int width) =
  1243. RGB24MirrorRow_C;
  1244. if (!src_rgb24 || !dst_rgb24 || width <= 0 || height == 0) {
  1245. return -1;
  1246. }
  1247. // Negative height means invert the image.
  1248. if (height < 0) {
  1249. height = -height;
  1250. src_rgb24 = src_rgb24 + (height - 1) * src_stride_rgb24;
  1251. src_stride_rgb24 = -src_stride_rgb24;
  1252. }
  1253. #if defined(HAS_RGB24MIRRORROW_NEON)
  1254. if (TestCpuFlag(kCpuHasNEON)) {
  1255. RGB24MirrorRow = RGB24MirrorRow_Any_NEON;
  1256. if (IS_ALIGNED(width, 16)) {
  1257. RGB24MirrorRow = RGB24MirrorRow_NEON;
  1258. }
  1259. }
  1260. #endif
  1261. #if defined(HAS_RGB24MIRRORROW_SSSE3)
  1262. if (TestCpuFlag(kCpuHasSSSE3)) {
  1263. RGB24MirrorRow = RGB24MirrorRow_Any_SSSE3;
  1264. if (IS_ALIGNED(width, 16)) {
  1265. RGB24MirrorRow = RGB24MirrorRow_SSSE3;
  1266. }
  1267. }
  1268. #endif
  1269. // Mirror plane
  1270. for (y = 0; y < height; ++y) {
  1271. RGB24MirrorRow(src_rgb24, dst_rgb24, width);
  1272. src_rgb24 += src_stride_rgb24;
  1273. dst_rgb24 += dst_stride_rgb24;
  1274. }
  1275. return 0;
  1276. }
  1277. // Get a blender that optimized for the CPU and pixel count.
  1278. // As there are 6 blenders to choose from, the caller should try to use
  1279. // the same blend function for all pixels if possible.
  1280. LIBYUV_API
  1281. ARGBBlendRow GetARGBBlend() {
  1282. void (*ARGBBlendRow)(const uint8_t* src_argb, const uint8_t* src_argb1,
  1283. uint8_t* dst_argb, int width) = ARGBBlendRow_C;
  1284. #if defined(HAS_ARGBBLENDROW_SSSE3)
  1285. if (TestCpuFlag(kCpuHasSSSE3)) {
  1286. ARGBBlendRow = ARGBBlendRow_SSSE3;
  1287. return ARGBBlendRow;
  1288. }
  1289. #endif
  1290. #if defined(HAS_ARGBBLENDROW_NEON)
  1291. if (TestCpuFlag(kCpuHasNEON)) {
  1292. ARGBBlendRow = ARGBBlendRow_NEON;
  1293. }
  1294. #endif
  1295. #if defined(HAS_ARGBBLENDROW_MMI)
  1296. if (TestCpuFlag(kCpuHasMMI)) {
  1297. ARGBBlendRow = ARGBBlendRow_MMI;
  1298. }
  1299. #endif
  1300. #if defined(HAS_ARGBBLENDROW_MSA)
  1301. if (TestCpuFlag(kCpuHasMSA)) {
  1302. ARGBBlendRow = ARGBBlendRow_MSA;
  1303. }
  1304. #endif
  1305. return ARGBBlendRow;
  1306. }
  1307. // Alpha Blend 2 ARGB images and store to destination.
  1308. LIBYUV_API
  1309. int ARGBBlend(const uint8_t* src_argb0,
  1310. int src_stride_argb0,
  1311. const uint8_t* src_argb1,
  1312. int src_stride_argb1,
  1313. uint8_t* dst_argb,
  1314. int dst_stride_argb,
  1315. int width,
  1316. int height) {
  1317. int y;
  1318. void (*ARGBBlendRow)(const uint8_t* src_argb, const uint8_t* src_argb1,
  1319. uint8_t* dst_argb, int width) = GetARGBBlend();
  1320. if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
  1321. return -1;
  1322. }
  1323. // Negative height means invert the image.
  1324. if (height < 0) {
  1325. height = -height;
  1326. dst_argb = dst_argb + (height - 1) * dst_stride_argb;
  1327. dst_stride_argb = -dst_stride_argb;
  1328. }
  1329. // Coalesce rows.
  1330. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 &&
  1331. dst_stride_argb == width * 4) {
  1332. width *= height;
  1333. height = 1;
  1334. src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
  1335. }
  1336. for (y = 0; y < height; ++y) {
  1337. ARGBBlendRow(src_argb0, src_argb1, dst_argb, width);
  1338. src_argb0 += src_stride_argb0;
  1339. src_argb1 += src_stride_argb1;
  1340. dst_argb += dst_stride_argb;
  1341. }
  1342. return 0;
  1343. }
  1344. // Alpha Blend plane and store to destination.
  1345. LIBYUV_API
  1346. int BlendPlane(const uint8_t* src_y0,
  1347. int src_stride_y0,
  1348. const uint8_t* src_y1,
  1349. int src_stride_y1,
  1350. const uint8_t* alpha,
  1351. int alpha_stride,
  1352. uint8_t* dst_y,
  1353. int dst_stride_y,
  1354. int width,
  1355. int height) {
  1356. int y;
  1357. void (*BlendPlaneRow)(const uint8_t* src0, const uint8_t* src1,
  1358. const uint8_t* alpha, uint8_t* dst, int width) =
  1359. BlendPlaneRow_C;
  1360. if (!src_y0 || !src_y1 || !alpha || !dst_y || width <= 0 || height == 0) {
  1361. return -1;
  1362. }
  1363. // Negative height means invert the image.
  1364. if (height < 0) {
  1365. height = -height;
  1366. dst_y = dst_y + (height - 1) * dst_stride_y;
  1367. dst_stride_y = -dst_stride_y;
  1368. }
  1369. // Coalesce rows for Y plane.
  1370. if (src_stride_y0 == width && src_stride_y1 == width &&
  1371. alpha_stride == width && dst_stride_y == width) {
  1372. width *= height;
  1373. height = 1;
  1374. src_stride_y0 = src_stride_y1 = alpha_stride = dst_stride_y = 0;
  1375. }
  1376. #if defined(HAS_BLENDPLANEROW_SSSE3)
  1377. if (TestCpuFlag(kCpuHasSSSE3)) {
  1378. BlendPlaneRow = BlendPlaneRow_Any_SSSE3;
  1379. if (IS_ALIGNED(width, 8)) {
  1380. BlendPlaneRow = BlendPlaneRow_SSSE3;
  1381. }
  1382. }
  1383. #endif
  1384. #if defined(HAS_BLENDPLANEROW_AVX2)
  1385. if (TestCpuFlag(kCpuHasAVX2)) {
  1386. BlendPlaneRow = BlendPlaneRow_Any_AVX2;
  1387. if (IS_ALIGNED(width, 32)) {
  1388. BlendPlaneRow = BlendPlaneRow_AVX2;
  1389. }
  1390. }
  1391. #endif
  1392. #if defined(HAS_BLENDPLANEROW_MMI)
  1393. if (TestCpuFlag(kCpuHasMMI)) {
  1394. BlendPlaneRow = BlendPlaneRow_Any_MMI;
  1395. if (IS_ALIGNED(width, 8)) {
  1396. BlendPlaneRow = BlendPlaneRow_MMI;
  1397. }
  1398. }
  1399. #endif
  1400. for (y = 0; y < height; ++y) {
  1401. BlendPlaneRow(src_y0, src_y1, alpha, dst_y, width);
  1402. src_y0 += src_stride_y0;
  1403. src_y1 += src_stride_y1;
  1404. alpha += alpha_stride;
  1405. dst_y += dst_stride_y;
  1406. }
  1407. return 0;
  1408. }
  1409. #define MAXTWIDTH 2048
  1410. // Alpha Blend YUV images and store to destination.
  1411. LIBYUV_API
  1412. int I420Blend(const uint8_t* src_y0,
  1413. int src_stride_y0,
  1414. const uint8_t* src_u0,
  1415. int src_stride_u0,
  1416. const uint8_t* src_v0,
  1417. int src_stride_v0,
  1418. const uint8_t* src_y1,
  1419. int src_stride_y1,
  1420. const uint8_t* src_u1,
  1421. int src_stride_u1,
  1422. const uint8_t* src_v1,
  1423. int src_stride_v1,
  1424. const uint8_t* alpha,
  1425. int alpha_stride,
  1426. uint8_t* dst_y,
  1427. int dst_stride_y,
  1428. uint8_t* dst_u,
  1429. int dst_stride_u,
  1430. uint8_t* dst_v,
  1431. int dst_stride_v,
  1432. int width,
  1433. int height) {
  1434. int y;
  1435. // Half width/height for UV.
  1436. int halfwidth = (width + 1) >> 1;
  1437. void (*BlendPlaneRow)(const uint8_t* src0, const uint8_t* src1,
  1438. const uint8_t* alpha, uint8_t* dst, int width) =
  1439. BlendPlaneRow_C;
  1440. void (*ScaleRowDown2)(const uint8_t* src_ptr, ptrdiff_t src_stride,
  1441. uint8_t* dst_ptr, int dst_width) = ScaleRowDown2Box_C;
  1442. if (!src_y0 || !src_u0 || !src_v0 || !src_y1 || !src_u1 || !src_v1 ||
  1443. !alpha || !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) {
  1444. return -1;
  1445. }
  1446. // Negative height means invert the image.
  1447. if (height < 0) {
  1448. height = -height;
  1449. dst_y = dst_y + (height - 1) * dst_stride_y;
  1450. dst_stride_y = -dst_stride_y;
  1451. }
  1452. // Blend Y plane.
  1453. BlendPlane(src_y0, src_stride_y0, src_y1, src_stride_y1, alpha, alpha_stride,
  1454. dst_y, dst_stride_y, width, height);
  1455. #if defined(HAS_BLENDPLANEROW_SSSE3)
  1456. if (TestCpuFlag(kCpuHasSSSE3)) {
  1457. BlendPlaneRow = BlendPlaneRow_Any_SSSE3;
  1458. if (IS_ALIGNED(halfwidth, 8)) {
  1459. BlendPlaneRow = BlendPlaneRow_SSSE3;
  1460. }
  1461. }
  1462. #endif
  1463. #if defined(HAS_BLENDPLANEROW_AVX2)
  1464. if (TestCpuFlag(kCpuHasAVX2)) {
  1465. BlendPlaneRow = BlendPlaneRow_Any_AVX2;
  1466. if (IS_ALIGNED(halfwidth, 32)) {
  1467. BlendPlaneRow = BlendPlaneRow_AVX2;
  1468. }
  1469. }
  1470. #endif
  1471. #if defined(HAS_BLENDPLANEROW_MMI)
  1472. if (TestCpuFlag(kCpuHasMMI)) {
  1473. BlendPlaneRow = BlendPlaneRow_Any_MMI;
  1474. if (IS_ALIGNED(halfwidth, 8)) {
  1475. BlendPlaneRow = BlendPlaneRow_MMI;
  1476. }
  1477. }
  1478. #endif
  1479. if (!IS_ALIGNED(width, 2)) {
  1480. ScaleRowDown2 = ScaleRowDown2Box_Odd_C;
  1481. }
  1482. #if defined(HAS_SCALEROWDOWN2_NEON)
  1483. if (TestCpuFlag(kCpuHasNEON)) {
  1484. ScaleRowDown2 = ScaleRowDown2Box_Odd_NEON;
  1485. if (IS_ALIGNED(width, 2)) {
  1486. ScaleRowDown2 = ScaleRowDown2Box_Any_NEON;
  1487. if (IS_ALIGNED(halfwidth, 16)) {
  1488. ScaleRowDown2 = ScaleRowDown2Box_NEON;
  1489. }
  1490. }
  1491. }
  1492. #endif
  1493. #if defined(HAS_SCALEROWDOWN2_SSSE3)
  1494. if (TestCpuFlag(kCpuHasSSSE3)) {
  1495. ScaleRowDown2 = ScaleRowDown2Box_Odd_SSSE3;
  1496. if (IS_ALIGNED(width, 2)) {
  1497. ScaleRowDown2 = ScaleRowDown2Box_Any_SSSE3;
  1498. if (IS_ALIGNED(halfwidth, 16)) {
  1499. ScaleRowDown2 = ScaleRowDown2Box_SSSE3;
  1500. }
  1501. }
  1502. }
  1503. #endif
  1504. #if defined(HAS_SCALEROWDOWN2_AVX2)
  1505. if (TestCpuFlag(kCpuHasAVX2)) {
  1506. ScaleRowDown2 = ScaleRowDown2Box_Odd_AVX2;
  1507. if (IS_ALIGNED(width, 2)) {
  1508. ScaleRowDown2 = ScaleRowDown2Box_Any_AVX2;
  1509. if (IS_ALIGNED(halfwidth, 32)) {
  1510. ScaleRowDown2 = ScaleRowDown2Box_AVX2;
  1511. }
  1512. }
  1513. }
  1514. #endif
  1515. #if defined(HAS_SCALEROWDOWN2_MMI)
  1516. if (TestCpuFlag(kCpuHasMMI)) {
  1517. ScaleRowDown2 = ScaleRowDown2Box_Odd_MMI;
  1518. if (IS_ALIGNED(width, 2)) {
  1519. ScaleRowDown2 = ScaleRowDown2Box_Any_MMI;
  1520. if (IS_ALIGNED(halfwidth, 8)) {
  1521. ScaleRowDown2 = ScaleRowDown2Box_MMI;
  1522. }
  1523. }
  1524. }
  1525. #endif
  1526. // Row buffer for intermediate alpha pixels.
  1527. align_buffer_64(halfalpha, halfwidth);
  1528. for (y = 0; y < height; y += 2) {
  1529. // last row of odd height image use 1 row of alpha instead of 2.
  1530. if (y == (height - 1)) {
  1531. alpha_stride = 0;
  1532. }
  1533. // Subsample 2 rows of UV to half width and half height.
  1534. ScaleRowDown2(alpha, alpha_stride, halfalpha, halfwidth);
  1535. alpha += alpha_stride * 2;
  1536. BlendPlaneRow(src_u0, src_u1, halfalpha, dst_u, halfwidth);
  1537. BlendPlaneRow(src_v0, src_v1, halfalpha, dst_v, halfwidth);
  1538. src_u0 += src_stride_u0;
  1539. src_u1 += src_stride_u1;
  1540. dst_u += dst_stride_u;
  1541. src_v0 += src_stride_v0;
  1542. src_v1 += src_stride_v1;
  1543. dst_v += dst_stride_v;
  1544. }
  1545. free_aligned_buffer_64(halfalpha);
  1546. return 0;
  1547. }
  1548. // Multiply 2 ARGB images and store to destination.
  1549. LIBYUV_API
  1550. int ARGBMultiply(const uint8_t* src_argb0,
  1551. int src_stride_argb0,
  1552. const uint8_t* src_argb1,
  1553. int src_stride_argb1,
  1554. uint8_t* dst_argb,
  1555. int dst_stride_argb,
  1556. int width,
  1557. int height) {
  1558. int y;
  1559. void (*ARGBMultiplyRow)(const uint8_t* src0, const uint8_t* src1,
  1560. uint8_t* dst, int width) = ARGBMultiplyRow_C;
  1561. if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
  1562. return -1;
  1563. }
  1564. // Negative height means invert the image.
  1565. if (height < 0) {
  1566. height = -height;
  1567. dst_argb = dst_argb + (height - 1) * dst_stride_argb;
  1568. dst_stride_argb = -dst_stride_argb;
  1569. }
  1570. // Coalesce rows.
  1571. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 &&
  1572. dst_stride_argb == width * 4) {
  1573. width *= height;
  1574. height = 1;
  1575. src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
  1576. }
  1577. #if defined(HAS_ARGBMULTIPLYROW_SSE2)
  1578. if (TestCpuFlag(kCpuHasSSE2)) {
  1579. ARGBMultiplyRow = ARGBMultiplyRow_Any_SSE2;
  1580. if (IS_ALIGNED(width, 4)) {
  1581. ARGBMultiplyRow = ARGBMultiplyRow_SSE2;
  1582. }
  1583. }
  1584. #endif
  1585. #if defined(HAS_ARGBMULTIPLYROW_AVX2)
  1586. if (TestCpuFlag(kCpuHasAVX2)) {
  1587. ARGBMultiplyRow = ARGBMultiplyRow_Any_AVX2;
  1588. if (IS_ALIGNED(width, 8)) {
  1589. ARGBMultiplyRow = ARGBMultiplyRow_AVX2;
  1590. }
  1591. }
  1592. #endif
  1593. #if defined(HAS_ARGBMULTIPLYROW_NEON)
  1594. if (TestCpuFlag(kCpuHasNEON)) {
  1595. ARGBMultiplyRow = ARGBMultiplyRow_Any_NEON;
  1596. if (IS_ALIGNED(width, 8)) {
  1597. ARGBMultiplyRow = ARGBMultiplyRow_NEON;
  1598. }
  1599. }
  1600. #endif
  1601. #if defined(HAS_ARGBMULTIPLYROW_MMI)
  1602. if (TestCpuFlag(kCpuHasMMI)) {
  1603. ARGBMultiplyRow = ARGBMultiplyRow_Any_MMI;
  1604. if (IS_ALIGNED(width, 2)) {
  1605. ARGBMultiplyRow = ARGBMultiplyRow_MMI;
  1606. }
  1607. }
  1608. #endif
  1609. #if defined(HAS_ARGBMULTIPLYROW_MSA)
  1610. if (TestCpuFlag(kCpuHasMSA)) {
  1611. ARGBMultiplyRow = ARGBMultiplyRow_Any_MSA;
  1612. if (IS_ALIGNED(width, 4)) {
  1613. ARGBMultiplyRow = ARGBMultiplyRow_MSA;
  1614. }
  1615. }
  1616. #endif
  1617. // Multiply plane
  1618. for (y = 0; y < height; ++y) {
  1619. ARGBMultiplyRow(src_argb0, src_argb1, dst_argb, width);
  1620. src_argb0 += src_stride_argb0;
  1621. src_argb1 += src_stride_argb1;
  1622. dst_argb += dst_stride_argb;
  1623. }
  1624. return 0;
  1625. }
  1626. // Add 2 ARGB images and store to destination.
  1627. LIBYUV_API
  1628. int ARGBAdd(const uint8_t* src_argb0,
  1629. int src_stride_argb0,
  1630. const uint8_t* src_argb1,
  1631. int src_stride_argb1,
  1632. uint8_t* dst_argb,
  1633. int dst_stride_argb,
  1634. int width,
  1635. int height) {
  1636. int y;
  1637. void (*ARGBAddRow)(const uint8_t* src0, const uint8_t* src1, uint8_t* dst,
  1638. int width) = ARGBAddRow_C;
  1639. if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
  1640. return -1;
  1641. }
  1642. // Negative height means invert the image.
  1643. if (height < 0) {
  1644. height = -height;
  1645. dst_argb = dst_argb + (height - 1) * dst_stride_argb;
  1646. dst_stride_argb = -dst_stride_argb;
  1647. }
  1648. // Coalesce rows.
  1649. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 &&
  1650. dst_stride_argb == width * 4) {
  1651. width *= height;
  1652. height = 1;
  1653. src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
  1654. }
  1655. #if defined(HAS_ARGBADDROW_SSE2) && (defined(_MSC_VER) && !defined(__clang__))
  1656. if (TestCpuFlag(kCpuHasSSE2)) {
  1657. ARGBAddRow = ARGBAddRow_SSE2;
  1658. }
  1659. #endif
  1660. #if defined(HAS_ARGBADDROW_SSE2) && !(defined(_MSC_VER) && !defined(__clang__))
  1661. if (TestCpuFlag(kCpuHasSSE2)) {
  1662. ARGBAddRow = ARGBAddRow_Any_SSE2;
  1663. if (IS_ALIGNED(width, 4)) {
  1664. ARGBAddRow = ARGBAddRow_SSE2;
  1665. }
  1666. }
  1667. #endif
  1668. #if defined(HAS_ARGBADDROW_AVX2)
  1669. if (TestCpuFlag(kCpuHasAVX2)) {
  1670. ARGBAddRow = ARGBAddRow_Any_AVX2;
  1671. if (IS_ALIGNED(width, 8)) {
  1672. ARGBAddRow = ARGBAddRow_AVX2;
  1673. }
  1674. }
  1675. #endif
  1676. #if defined(HAS_ARGBADDROW_NEON)
  1677. if (TestCpuFlag(kCpuHasNEON)) {
  1678. ARGBAddRow = ARGBAddRow_Any_NEON;
  1679. if (IS_ALIGNED(width, 8)) {
  1680. ARGBAddRow = ARGBAddRow_NEON;
  1681. }
  1682. }
  1683. #endif
  1684. #if defined(HAS_ARGBADDROW_MMI)
  1685. if (TestCpuFlag(kCpuHasMMI)) {
  1686. ARGBAddRow = ARGBAddRow_Any_MMI;
  1687. if (IS_ALIGNED(width, 2)) {
  1688. ARGBAddRow = ARGBAddRow_MMI;
  1689. }
  1690. }
  1691. #endif
  1692. #if defined(HAS_ARGBADDROW_MSA)
  1693. if (TestCpuFlag(kCpuHasMSA)) {
  1694. ARGBAddRow = ARGBAddRow_Any_MSA;
  1695. if (IS_ALIGNED(width, 8)) {
  1696. ARGBAddRow = ARGBAddRow_MSA;
  1697. }
  1698. }
  1699. #endif
  1700. // Add plane
  1701. for (y = 0; y < height; ++y) {
  1702. ARGBAddRow(src_argb0, src_argb1, dst_argb, width);
  1703. src_argb0 += src_stride_argb0;
  1704. src_argb1 += src_stride_argb1;
  1705. dst_argb += dst_stride_argb;
  1706. }
  1707. return 0;
  1708. }
  1709. // Subtract 2 ARGB images and store to destination.
  1710. LIBYUV_API
  1711. int ARGBSubtract(const uint8_t* src_argb0,
  1712. int src_stride_argb0,
  1713. const uint8_t* src_argb1,
  1714. int src_stride_argb1,
  1715. uint8_t* dst_argb,
  1716. int dst_stride_argb,
  1717. int width,
  1718. int height) {
  1719. int y;
  1720. void (*ARGBSubtractRow)(const uint8_t* src0, const uint8_t* src1,
  1721. uint8_t* dst, int width) = ARGBSubtractRow_C;
  1722. if (!src_argb0 || !src_argb1 || !dst_argb || width <= 0 || height == 0) {
  1723. return -1;
  1724. }
  1725. // Negative height means invert the image.
  1726. if (height < 0) {
  1727. height = -height;
  1728. dst_argb = dst_argb + (height - 1) * dst_stride_argb;
  1729. dst_stride_argb = -dst_stride_argb;
  1730. }
  1731. // Coalesce rows.
  1732. if (src_stride_argb0 == width * 4 && src_stride_argb1 == width * 4 &&
  1733. dst_stride_argb == width * 4) {
  1734. width *= height;
  1735. height = 1;
  1736. src_stride_argb0 = src_stride_argb1 = dst_stride_argb = 0;
  1737. }
  1738. #if defined(HAS_ARGBSUBTRACTROW_SSE2)
  1739. if (TestCpuFlag(kCpuHasSSE2)) {
  1740. ARGBSubtractRow = ARGBSubtractRow_Any_SSE2;
  1741. if (IS_ALIGNED(width, 4)) {
  1742. ARGBSubtractRow = ARGBSubtractRow_SSE2;
  1743. }
  1744. }
  1745. #endif
  1746. #if defined(HAS_ARGBSUBTRACTROW_AVX2)
  1747. if (TestCpuFlag(kCpuHasAVX2)) {
  1748. ARGBSubtractRow = ARGBSubtractRow_Any_AVX2;
  1749. if (IS_ALIGNED(width, 8)) {
  1750. ARGBSubtractRow = ARGBSubtractRow_AVX2;
  1751. }
  1752. }
  1753. #endif
  1754. #if defined(HAS_ARGBSUBTRACTROW_NEON)
  1755. if (TestCpuFlag(kCpuHasNEON)) {
  1756. ARGBSubtractRow = ARGBSubtractRow_Any_NEON;
  1757. if (IS_ALIGNED(width, 8)) {
  1758. ARGBSubtractRow = ARGBSubtractRow_NEON;
  1759. }
  1760. }
  1761. #endif
  1762. #if defined(HAS_ARGBSUBTRACTROW_MMI)
  1763. if (TestCpuFlag(kCpuHasMMI)) {
  1764. ARGBSubtractRow = ARGBSubtractRow_Any_MMI;
  1765. if (IS_ALIGNED(width, 2)) {
  1766. ARGBSubtractRow = ARGBSubtractRow_MMI;
  1767. }
  1768. }
  1769. #endif
  1770. #if defined(HAS_ARGBSUBTRACTROW_MSA)
  1771. if (TestCpuFlag(kCpuHasMSA)) {
  1772. ARGBSubtractRow = ARGBSubtractRow_Any_MSA;
  1773. if (IS_ALIGNED(width, 8)) {
  1774. ARGBSubtractRow = ARGBSubtractRow_MSA;
  1775. }
  1776. }
  1777. #endif
  1778. // Subtract plane
  1779. for (y = 0; y < height; ++y) {
  1780. ARGBSubtractRow(src_argb0, src_argb1, dst_argb, width);
  1781. src_argb0 += src_stride_argb0;
  1782. src_argb1 += src_stride_argb1;
  1783. dst_argb += dst_stride_argb;
  1784. }
  1785. return 0;
  1786. }
  1787. // Convert RAW to RGB24.
  1788. LIBYUV_API
  1789. int RAWToRGB24(const uint8_t* src_raw,
  1790. int src_stride_raw,
  1791. uint8_t* dst_rgb24,
  1792. int dst_stride_rgb24,
  1793. int width,
  1794. int height) {
  1795. int y;
  1796. void (*RAWToRGB24Row)(const uint8_t* src_rgb, uint8_t* dst_rgb24, int width) =
  1797. RAWToRGB24Row_C;
  1798. if (!src_raw || !dst_rgb24 || width <= 0 || height == 0) {
  1799. return -1;
  1800. }
  1801. // Negative height means invert the image.
  1802. if (height < 0) {
  1803. height = -height;
  1804. src_raw = src_raw + (height - 1) * src_stride_raw;
  1805. src_stride_raw = -src_stride_raw;
  1806. }
  1807. // Coalesce rows.
  1808. if (src_stride_raw == width * 3 && dst_stride_rgb24 == width * 3) {
  1809. width *= height;
  1810. height = 1;
  1811. src_stride_raw = dst_stride_rgb24 = 0;
  1812. }
  1813. #if defined(HAS_RAWTORGB24ROW_SSSE3)
  1814. if (TestCpuFlag(kCpuHasSSSE3)) {
  1815. RAWToRGB24Row = RAWToRGB24Row_Any_SSSE3;
  1816. if (IS_ALIGNED(width, 8)) {
  1817. RAWToRGB24Row = RAWToRGB24Row_SSSE3;
  1818. }
  1819. }
  1820. #endif
  1821. #if defined(HAS_RAWTORGB24ROW_NEON)
  1822. if (TestCpuFlag(kCpuHasNEON)) {
  1823. RAWToRGB24Row = RAWToRGB24Row_Any_NEON;
  1824. if (IS_ALIGNED(width, 8)) {
  1825. RAWToRGB24Row = RAWToRGB24Row_NEON;
  1826. }
  1827. }
  1828. #endif
  1829. #if defined(HAS_RAWTORGB24ROW_MMI)
  1830. if (TestCpuFlag(kCpuHasMMI)) {
  1831. RAWToRGB24Row = RAWToRGB24Row_Any_MMI;
  1832. if (IS_ALIGNED(width, 4)) {
  1833. RAWToRGB24Row = RAWToRGB24Row_MMI;
  1834. }
  1835. }
  1836. #endif
  1837. #if defined(HAS_RAWTORGB24ROW_MSA)
  1838. if (TestCpuFlag(kCpuHasMSA)) {
  1839. RAWToRGB24Row = RAWToRGB24Row_Any_MSA;
  1840. if (IS_ALIGNED(width, 16)) {
  1841. RAWToRGB24Row = RAWToRGB24Row_MSA;
  1842. }
  1843. }
  1844. #endif
  1845. for (y = 0; y < height; ++y) {
  1846. RAWToRGB24Row(src_raw, dst_rgb24, width);
  1847. src_raw += src_stride_raw;
  1848. dst_rgb24 += dst_stride_rgb24;
  1849. }
  1850. return 0;
  1851. }
  1852. LIBYUV_API
  1853. void SetPlane(uint8_t* dst_y,
  1854. int dst_stride_y,
  1855. int width,
  1856. int height,
  1857. uint32_t value) {
  1858. int y;
  1859. void (*SetRow)(uint8_t * dst, uint8_t value, int width) = SetRow_C;
  1860. if (height < 0) {
  1861. height = -height;
  1862. dst_y = dst_y + (height - 1) * dst_stride_y;
  1863. dst_stride_y = -dst_stride_y;
  1864. }
  1865. // Coalesce rows.
  1866. if (dst_stride_y == width) {
  1867. width *= height;
  1868. height = 1;
  1869. dst_stride_y = 0;
  1870. }
  1871. #if defined(HAS_SETROW_NEON)
  1872. if (TestCpuFlag(kCpuHasNEON)) {
  1873. SetRow = SetRow_Any_NEON;
  1874. if (IS_ALIGNED(width, 16)) {
  1875. SetRow = SetRow_NEON;
  1876. }
  1877. }
  1878. #endif
  1879. #if defined(HAS_SETROW_X86)
  1880. if (TestCpuFlag(kCpuHasX86)) {
  1881. SetRow = SetRow_Any_X86;
  1882. if (IS_ALIGNED(width, 4)) {
  1883. SetRow = SetRow_X86;
  1884. }
  1885. }
  1886. #endif
  1887. #if defined(HAS_SETROW_ERMS)
  1888. if (TestCpuFlag(kCpuHasERMS)) {
  1889. SetRow = SetRow_ERMS;
  1890. }
  1891. #endif
  1892. #if defined(HAS_SETROW_MSA)
  1893. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 16)) {
  1894. SetRow = SetRow_MSA;
  1895. }
  1896. #endif
  1897. // Set plane
  1898. for (y = 0; y < height; ++y) {
  1899. SetRow(dst_y, value, width);
  1900. dst_y += dst_stride_y;
  1901. }
  1902. }
  1903. // Draw a rectangle into I420
  1904. LIBYUV_API
  1905. int I420Rect(uint8_t* dst_y,
  1906. int dst_stride_y,
  1907. uint8_t* dst_u,
  1908. int dst_stride_u,
  1909. uint8_t* dst_v,
  1910. int dst_stride_v,
  1911. int x,
  1912. int y,
  1913. int width,
  1914. int height,
  1915. int value_y,
  1916. int value_u,
  1917. int value_v) {
  1918. int halfwidth = (width + 1) >> 1;
  1919. int halfheight = (height + 1) >> 1;
  1920. uint8_t* start_y = dst_y + y * dst_stride_y + x;
  1921. uint8_t* start_u = dst_u + (y / 2) * dst_stride_u + (x / 2);
  1922. uint8_t* start_v = dst_v + (y / 2) * dst_stride_v + (x / 2);
  1923. if (!dst_y || !dst_u || !dst_v || width <= 0 || height == 0 || x < 0 ||
  1924. y < 0 || value_y < 0 || value_y > 255 || value_u < 0 || value_u > 255 ||
  1925. value_v < 0 || value_v > 255) {
  1926. return -1;
  1927. }
  1928. SetPlane(start_y, dst_stride_y, width, height, value_y);
  1929. SetPlane(start_u, dst_stride_u, halfwidth, halfheight, value_u);
  1930. SetPlane(start_v, dst_stride_v, halfwidth, halfheight, value_v);
  1931. return 0;
  1932. }
  1933. // Draw a rectangle into ARGB
  1934. LIBYUV_API
  1935. int ARGBRect(uint8_t* dst_argb,
  1936. int dst_stride_argb,
  1937. int dst_x,
  1938. int dst_y,
  1939. int width,
  1940. int height,
  1941. uint32_t value) {
  1942. int y;
  1943. void (*ARGBSetRow)(uint8_t * dst_argb, uint32_t value, int width) =
  1944. ARGBSetRow_C;
  1945. if (!dst_argb || width <= 0 || height == 0 || dst_x < 0 || dst_y < 0) {
  1946. return -1;
  1947. }
  1948. if (height < 0) {
  1949. height = -height;
  1950. dst_argb = dst_argb + (height - 1) * dst_stride_argb;
  1951. dst_stride_argb = -dst_stride_argb;
  1952. }
  1953. dst_argb += dst_y * dst_stride_argb + dst_x * 4;
  1954. // Coalesce rows.
  1955. if (dst_stride_argb == width * 4) {
  1956. width *= height;
  1957. height = 1;
  1958. dst_stride_argb = 0;
  1959. }
  1960. #if defined(HAS_ARGBSETROW_NEON)
  1961. if (TestCpuFlag(kCpuHasNEON)) {
  1962. ARGBSetRow = ARGBSetRow_Any_NEON;
  1963. if (IS_ALIGNED(width, 4)) {
  1964. ARGBSetRow = ARGBSetRow_NEON;
  1965. }
  1966. }
  1967. #endif
  1968. #if defined(HAS_ARGBSETROW_X86)
  1969. if (TestCpuFlag(kCpuHasX86)) {
  1970. ARGBSetRow = ARGBSetRow_X86;
  1971. }
  1972. #endif
  1973. #if defined(HAS_ARGBSETROW_MMI)
  1974. if (TestCpuFlag(kCpuHasMMI)) {
  1975. ARGBSetRow = ARGBSetRow_Any_MMI;
  1976. if (IS_ALIGNED(width, 4)) {
  1977. ARGBSetRow = ARGBSetRow_MMI;
  1978. }
  1979. }
  1980. #endif
  1981. #if defined(HAS_ARGBSETROW_MSA)
  1982. if (TestCpuFlag(kCpuHasMSA)) {
  1983. ARGBSetRow = ARGBSetRow_Any_MSA;
  1984. if (IS_ALIGNED(width, 4)) {
  1985. ARGBSetRow = ARGBSetRow_MSA;
  1986. }
  1987. }
  1988. #endif
  1989. // Set plane
  1990. for (y = 0; y < height; ++y) {
  1991. ARGBSetRow(dst_argb, value, width);
  1992. dst_argb += dst_stride_argb;
  1993. }
  1994. return 0;
  1995. }
  1996. // Convert unattentuated ARGB to preattenuated ARGB.
  1997. // An unattenutated ARGB alpha blend uses the formula
  1998. // p = a * f + (1 - a) * b
  1999. // where
  2000. // p is output pixel
  2001. // f is foreground pixel
  2002. // b is background pixel
  2003. // a is alpha value from foreground pixel
  2004. // An preattenutated ARGB alpha blend uses the formula
  2005. // p = f + (1 - a) * b
  2006. // where
  2007. // f is foreground pixel premultiplied by alpha
  2008. LIBYUV_API
  2009. int ARGBAttenuate(const uint8_t* src_argb,
  2010. int src_stride_argb,
  2011. uint8_t* dst_argb,
  2012. int dst_stride_argb,
  2013. int width,
  2014. int height) {
  2015. int y;
  2016. void (*ARGBAttenuateRow)(const uint8_t* src_argb, uint8_t* dst_argb,
  2017. int width) = ARGBAttenuateRow_C;
  2018. if (!src_argb || !dst_argb || width <= 0 || height == 0) {
  2019. return -1;
  2020. }
  2021. if (height < 0) {
  2022. height = -height;
  2023. src_argb = src_argb + (height - 1) * src_stride_argb;
  2024. src_stride_argb = -src_stride_argb;
  2025. }
  2026. // Coalesce rows.
  2027. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  2028. width *= height;
  2029. height = 1;
  2030. src_stride_argb = dst_stride_argb = 0;
  2031. }
  2032. #if defined(HAS_ARGBATTENUATEROW_SSSE3)
  2033. if (TestCpuFlag(kCpuHasSSSE3)) {
  2034. ARGBAttenuateRow = ARGBAttenuateRow_Any_SSSE3;
  2035. if (IS_ALIGNED(width, 4)) {
  2036. ARGBAttenuateRow = ARGBAttenuateRow_SSSE3;
  2037. }
  2038. }
  2039. #endif
  2040. #if defined(HAS_ARGBATTENUATEROW_AVX2)
  2041. if (TestCpuFlag(kCpuHasAVX2)) {
  2042. ARGBAttenuateRow = ARGBAttenuateRow_Any_AVX2;
  2043. if (IS_ALIGNED(width, 8)) {
  2044. ARGBAttenuateRow = ARGBAttenuateRow_AVX2;
  2045. }
  2046. }
  2047. #endif
  2048. #if defined(HAS_ARGBATTENUATEROW_NEON)
  2049. if (TestCpuFlag(kCpuHasNEON)) {
  2050. ARGBAttenuateRow = ARGBAttenuateRow_Any_NEON;
  2051. if (IS_ALIGNED(width, 8)) {
  2052. ARGBAttenuateRow = ARGBAttenuateRow_NEON;
  2053. }
  2054. }
  2055. #endif
  2056. #if defined(HAS_ARGBATTENUATEROW_MMI)
  2057. if (TestCpuFlag(kCpuHasMMI)) {
  2058. ARGBAttenuateRow = ARGBAttenuateRow_Any_MMI;
  2059. if (IS_ALIGNED(width, 2)) {
  2060. ARGBAttenuateRow = ARGBAttenuateRow_MMI;
  2061. }
  2062. }
  2063. #endif
  2064. #if defined(HAS_ARGBATTENUATEROW_MSA)
  2065. if (TestCpuFlag(kCpuHasMSA)) {
  2066. ARGBAttenuateRow = ARGBAttenuateRow_Any_MSA;
  2067. if (IS_ALIGNED(width, 8)) {
  2068. ARGBAttenuateRow = ARGBAttenuateRow_MSA;
  2069. }
  2070. }
  2071. #endif
  2072. for (y = 0; y < height; ++y) {
  2073. ARGBAttenuateRow(src_argb, dst_argb, width);
  2074. src_argb += src_stride_argb;
  2075. dst_argb += dst_stride_argb;
  2076. }
  2077. return 0;
  2078. }
  2079. // Convert preattentuated ARGB to unattenuated ARGB.
  2080. LIBYUV_API
  2081. int ARGBUnattenuate(const uint8_t* src_argb,
  2082. int src_stride_argb,
  2083. uint8_t* dst_argb,
  2084. int dst_stride_argb,
  2085. int width,
  2086. int height) {
  2087. int y;
  2088. void (*ARGBUnattenuateRow)(const uint8_t* src_argb, uint8_t* dst_argb,
  2089. int width) = ARGBUnattenuateRow_C;
  2090. if (!src_argb || !dst_argb || width <= 0 || height == 0) {
  2091. return -1;
  2092. }
  2093. if (height < 0) {
  2094. height = -height;
  2095. src_argb = src_argb + (height - 1) * src_stride_argb;
  2096. src_stride_argb = -src_stride_argb;
  2097. }
  2098. // Coalesce rows.
  2099. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  2100. width *= height;
  2101. height = 1;
  2102. src_stride_argb = dst_stride_argb = 0;
  2103. }
  2104. #if defined(HAS_ARGBUNATTENUATEROW_SSE2)
  2105. if (TestCpuFlag(kCpuHasSSE2)) {
  2106. ARGBUnattenuateRow = ARGBUnattenuateRow_Any_SSE2;
  2107. if (IS_ALIGNED(width, 4)) {
  2108. ARGBUnattenuateRow = ARGBUnattenuateRow_SSE2;
  2109. }
  2110. }
  2111. #endif
  2112. #if defined(HAS_ARGBUNATTENUATEROW_AVX2)
  2113. if (TestCpuFlag(kCpuHasAVX2)) {
  2114. ARGBUnattenuateRow = ARGBUnattenuateRow_Any_AVX2;
  2115. if (IS_ALIGNED(width, 8)) {
  2116. ARGBUnattenuateRow = ARGBUnattenuateRow_AVX2;
  2117. }
  2118. }
  2119. #endif
  2120. // TODO(fbarchard): Neon version.
  2121. for (y = 0; y < height; ++y) {
  2122. ARGBUnattenuateRow(src_argb, dst_argb, width);
  2123. src_argb += src_stride_argb;
  2124. dst_argb += dst_stride_argb;
  2125. }
  2126. return 0;
  2127. }
  2128. // Convert ARGB to Grayed ARGB.
  2129. LIBYUV_API
  2130. int ARGBGrayTo(const uint8_t* src_argb,
  2131. int src_stride_argb,
  2132. uint8_t* dst_argb,
  2133. int dst_stride_argb,
  2134. int width,
  2135. int height) {
  2136. int y;
  2137. void (*ARGBGrayRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) =
  2138. ARGBGrayRow_C;
  2139. if (!src_argb || !dst_argb || width <= 0 || height == 0) {
  2140. return -1;
  2141. }
  2142. if (height < 0) {
  2143. height = -height;
  2144. src_argb = src_argb + (height - 1) * src_stride_argb;
  2145. src_stride_argb = -src_stride_argb;
  2146. }
  2147. // Coalesce rows.
  2148. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  2149. width *= height;
  2150. height = 1;
  2151. src_stride_argb = dst_stride_argb = 0;
  2152. }
  2153. #if defined(HAS_ARGBGRAYROW_SSSE3)
  2154. if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
  2155. ARGBGrayRow = ARGBGrayRow_SSSE3;
  2156. }
  2157. #endif
  2158. #if defined(HAS_ARGBGRAYROW_NEON)
  2159. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2160. ARGBGrayRow = ARGBGrayRow_NEON;
  2161. }
  2162. #endif
  2163. #if defined(HAS_ARGBGRAYROW_MMI)
  2164. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) {
  2165. ARGBGrayRow = ARGBGrayRow_MMI;
  2166. }
  2167. #endif
  2168. #if defined(HAS_ARGBGRAYROW_MSA)
  2169. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) {
  2170. ARGBGrayRow = ARGBGrayRow_MSA;
  2171. }
  2172. #endif
  2173. for (y = 0; y < height; ++y) {
  2174. ARGBGrayRow(src_argb, dst_argb, width);
  2175. src_argb += src_stride_argb;
  2176. dst_argb += dst_stride_argb;
  2177. }
  2178. return 0;
  2179. }
  2180. // Make a rectangle of ARGB gray scale.
  2181. LIBYUV_API
  2182. int ARGBGray(uint8_t* dst_argb,
  2183. int dst_stride_argb,
  2184. int dst_x,
  2185. int dst_y,
  2186. int width,
  2187. int height) {
  2188. int y;
  2189. void (*ARGBGrayRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width) =
  2190. ARGBGrayRow_C;
  2191. uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
  2192. if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
  2193. return -1;
  2194. }
  2195. // Coalesce rows.
  2196. if (dst_stride_argb == width * 4) {
  2197. width *= height;
  2198. height = 1;
  2199. dst_stride_argb = 0;
  2200. }
  2201. #if defined(HAS_ARGBGRAYROW_SSSE3)
  2202. if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
  2203. ARGBGrayRow = ARGBGrayRow_SSSE3;
  2204. }
  2205. #endif
  2206. #if defined(HAS_ARGBGRAYROW_NEON)
  2207. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2208. ARGBGrayRow = ARGBGrayRow_NEON;
  2209. }
  2210. #endif
  2211. #if defined(HAS_ARGBGRAYROW_MMI)
  2212. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) {
  2213. ARGBGrayRow = ARGBGrayRow_MMI;
  2214. }
  2215. #endif
  2216. #if defined(HAS_ARGBGRAYROW_MSA)
  2217. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) {
  2218. ARGBGrayRow = ARGBGrayRow_MSA;
  2219. }
  2220. #endif
  2221. for (y = 0; y < height; ++y) {
  2222. ARGBGrayRow(dst, dst, width);
  2223. dst += dst_stride_argb;
  2224. }
  2225. return 0;
  2226. }
  2227. // Make a rectangle of ARGB Sepia tone.
  2228. LIBYUV_API
  2229. int ARGBSepia(uint8_t* dst_argb,
  2230. int dst_stride_argb,
  2231. int dst_x,
  2232. int dst_y,
  2233. int width,
  2234. int height) {
  2235. int y;
  2236. void (*ARGBSepiaRow)(uint8_t * dst_argb, int width) = ARGBSepiaRow_C;
  2237. uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
  2238. if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0) {
  2239. return -1;
  2240. }
  2241. // Coalesce rows.
  2242. if (dst_stride_argb == width * 4) {
  2243. width *= height;
  2244. height = 1;
  2245. dst_stride_argb = 0;
  2246. }
  2247. #if defined(HAS_ARGBSEPIAROW_SSSE3)
  2248. if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
  2249. ARGBSepiaRow = ARGBSepiaRow_SSSE3;
  2250. }
  2251. #endif
  2252. #if defined(HAS_ARGBSEPIAROW_NEON)
  2253. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2254. ARGBSepiaRow = ARGBSepiaRow_NEON;
  2255. }
  2256. #endif
  2257. #if defined(HAS_ARGBSEPIAROW_MMI)
  2258. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) {
  2259. ARGBSepiaRow = ARGBSepiaRow_MMI;
  2260. }
  2261. #endif
  2262. #if defined(HAS_ARGBSEPIAROW_MSA)
  2263. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) {
  2264. ARGBSepiaRow = ARGBSepiaRow_MSA;
  2265. }
  2266. #endif
  2267. for (y = 0; y < height; ++y) {
  2268. ARGBSepiaRow(dst, width);
  2269. dst += dst_stride_argb;
  2270. }
  2271. return 0;
  2272. }
  2273. // Apply a 4x4 matrix to each ARGB pixel.
  2274. // Note: Normally for shading, but can be used to swizzle or invert.
  2275. LIBYUV_API
  2276. int ARGBColorMatrix(const uint8_t* src_argb,
  2277. int src_stride_argb,
  2278. uint8_t* dst_argb,
  2279. int dst_stride_argb,
  2280. const int8_t* matrix_argb,
  2281. int width,
  2282. int height) {
  2283. int y;
  2284. void (*ARGBColorMatrixRow)(const uint8_t* src_argb, uint8_t* dst_argb,
  2285. const int8_t* matrix_argb, int width) =
  2286. ARGBColorMatrixRow_C;
  2287. if (!src_argb || !dst_argb || !matrix_argb || width <= 0 || height == 0) {
  2288. return -1;
  2289. }
  2290. if (height < 0) {
  2291. height = -height;
  2292. src_argb = src_argb + (height - 1) * src_stride_argb;
  2293. src_stride_argb = -src_stride_argb;
  2294. }
  2295. // Coalesce rows.
  2296. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  2297. width *= height;
  2298. height = 1;
  2299. src_stride_argb = dst_stride_argb = 0;
  2300. }
  2301. #if defined(HAS_ARGBCOLORMATRIXROW_SSSE3)
  2302. if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 8)) {
  2303. ARGBColorMatrixRow = ARGBColorMatrixRow_SSSE3;
  2304. }
  2305. #endif
  2306. #if defined(HAS_ARGBCOLORMATRIXROW_NEON)
  2307. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2308. ARGBColorMatrixRow = ARGBColorMatrixRow_NEON;
  2309. }
  2310. #endif
  2311. #if defined(HAS_ARGBCOLORMATRIXROW_MMI)
  2312. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) {
  2313. ARGBColorMatrixRow = ARGBColorMatrixRow_MMI;
  2314. }
  2315. #endif
  2316. #if defined(HAS_ARGBCOLORMATRIXROW_MSA)
  2317. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) {
  2318. ARGBColorMatrixRow = ARGBColorMatrixRow_MSA;
  2319. }
  2320. #endif
  2321. for (y = 0; y < height; ++y) {
  2322. ARGBColorMatrixRow(src_argb, dst_argb, matrix_argb, width);
  2323. src_argb += src_stride_argb;
  2324. dst_argb += dst_stride_argb;
  2325. }
  2326. return 0;
  2327. }
  2328. // Apply a 4x3 matrix to each ARGB pixel.
  2329. // Deprecated.
  2330. LIBYUV_API
  2331. int RGBColorMatrix(uint8_t* dst_argb,
  2332. int dst_stride_argb,
  2333. const int8_t* matrix_rgb,
  2334. int dst_x,
  2335. int dst_y,
  2336. int width,
  2337. int height) {
  2338. SIMD_ALIGNED(int8_t matrix_argb[16]);
  2339. uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
  2340. if (!dst_argb || !matrix_rgb || width <= 0 || height <= 0 || dst_x < 0 ||
  2341. dst_y < 0) {
  2342. return -1;
  2343. }
  2344. // Convert 4x3 7 bit matrix to 4x4 6 bit matrix.
  2345. matrix_argb[0] = matrix_rgb[0] / 2;
  2346. matrix_argb[1] = matrix_rgb[1] / 2;
  2347. matrix_argb[2] = matrix_rgb[2] / 2;
  2348. matrix_argb[3] = matrix_rgb[3] / 2;
  2349. matrix_argb[4] = matrix_rgb[4] / 2;
  2350. matrix_argb[5] = matrix_rgb[5] / 2;
  2351. matrix_argb[6] = matrix_rgb[6] / 2;
  2352. matrix_argb[7] = matrix_rgb[7] / 2;
  2353. matrix_argb[8] = matrix_rgb[8] / 2;
  2354. matrix_argb[9] = matrix_rgb[9] / 2;
  2355. matrix_argb[10] = matrix_rgb[10] / 2;
  2356. matrix_argb[11] = matrix_rgb[11] / 2;
  2357. matrix_argb[14] = matrix_argb[13] = matrix_argb[12] = 0;
  2358. matrix_argb[15] = 64; // 1.0
  2359. return ARGBColorMatrix((const uint8_t*)(dst), dst_stride_argb, dst,
  2360. dst_stride_argb, &matrix_argb[0], width, height);
  2361. }
  2362. // Apply a color table each ARGB pixel.
  2363. // Table contains 256 ARGB values.
  2364. LIBYUV_API
  2365. int ARGBColorTable(uint8_t* dst_argb,
  2366. int dst_stride_argb,
  2367. const uint8_t* table_argb,
  2368. int dst_x,
  2369. int dst_y,
  2370. int width,
  2371. int height) {
  2372. int y;
  2373. void (*ARGBColorTableRow)(uint8_t * dst_argb, const uint8_t* table_argb,
  2374. int width) = ARGBColorTableRow_C;
  2375. uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
  2376. if (!dst_argb || !table_argb || width <= 0 || height <= 0 || dst_x < 0 ||
  2377. dst_y < 0) {
  2378. return -1;
  2379. }
  2380. // Coalesce rows.
  2381. if (dst_stride_argb == width * 4) {
  2382. width *= height;
  2383. height = 1;
  2384. dst_stride_argb = 0;
  2385. }
  2386. #if defined(HAS_ARGBCOLORTABLEROW_X86)
  2387. if (TestCpuFlag(kCpuHasX86)) {
  2388. ARGBColorTableRow = ARGBColorTableRow_X86;
  2389. }
  2390. #endif
  2391. for (y = 0; y < height; ++y) {
  2392. ARGBColorTableRow(dst, table_argb, width);
  2393. dst += dst_stride_argb;
  2394. }
  2395. return 0;
  2396. }
  2397. // Apply a color table each ARGB pixel but preserve destination alpha.
  2398. // Table contains 256 ARGB values.
  2399. LIBYUV_API
  2400. int RGBColorTable(uint8_t* dst_argb,
  2401. int dst_stride_argb,
  2402. const uint8_t* table_argb,
  2403. int dst_x,
  2404. int dst_y,
  2405. int width,
  2406. int height) {
  2407. int y;
  2408. void (*RGBColorTableRow)(uint8_t * dst_argb, const uint8_t* table_argb,
  2409. int width) = RGBColorTableRow_C;
  2410. uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
  2411. if (!dst_argb || !table_argb || width <= 0 || height <= 0 || dst_x < 0 ||
  2412. dst_y < 0) {
  2413. return -1;
  2414. }
  2415. // Coalesce rows.
  2416. if (dst_stride_argb == width * 4) {
  2417. width *= height;
  2418. height = 1;
  2419. dst_stride_argb = 0;
  2420. }
  2421. #if defined(HAS_RGBCOLORTABLEROW_X86)
  2422. if (TestCpuFlag(kCpuHasX86)) {
  2423. RGBColorTableRow = RGBColorTableRow_X86;
  2424. }
  2425. #endif
  2426. for (y = 0; y < height; ++y) {
  2427. RGBColorTableRow(dst, table_argb, width);
  2428. dst += dst_stride_argb;
  2429. }
  2430. return 0;
  2431. }
  2432. // ARGBQuantize is used to posterize art.
  2433. // e.g. rgb / qvalue * qvalue + qvalue / 2
  2434. // But the low levels implement efficiently with 3 parameters, and could be
  2435. // used for other high level operations.
  2436. // dst_argb[0] = (b * scale >> 16) * interval_size + interval_offset;
  2437. // where scale is 1 / interval_size as a fixed point value.
  2438. // The divide is replaces with a multiply by reciprocal fixed point multiply.
  2439. // Caveat - although SSE2 saturates, the C function does not and should be used
  2440. // with care if doing anything but quantization.
  2441. LIBYUV_API
  2442. int ARGBQuantize(uint8_t* dst_argb,
  2443. int dst_stride_argb,
  2444. int scale,
  2445. int interval_size,
  2446. int interval_offset,
  2447. int dst_x,
  2448. int dst_y,
  2449. int width,
  2450. int height) {
  2451. int y;
  2452. void (*ARGBQuantizeRow)(uint8_t * dst_argb, int scale, int interval_size,
  2453. int interval_offset, int width) = ARGBQuantizeRow_C;
  2454. uint8_t* dst = dst_argb + dst_y * dst_stride_argb + dst_x * 4;
  2455. if (!dst_argb || width <= 0 || height <= 0 || dst_x < 0 || dst_y < 0 ||
  2456. interval_size < 1 || interval_size > 255) {
  2457. return -1;
  2458. }
  2459. // Coalesce rows.
  2460. if (dst_stride_argb == width * 4) {
  2461. width *= height;
  2462. height = 1;
  2463. dst_stride_argb = 0;
  2464. }
  2465. #if defined(HAS_ARGBQUANTIZEROW_SSE2)
  2466. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) {
  2467. ARGBQuantizeRow = ARGBQuantizeRow_SSE2;
  2468. }
  2469. #endif
  2470. #if defined(HAS_ARGBQUANTIZEROW_NEON)
  2471. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2472. ARGBQuantizeRow = ARGBQuantizeRow_NEON;
  2473. }
  2474. #endif
  2475. #if defined(HAS_ARGBQUANTIZEROW_MSA)
  2476. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 8)) {
  2477. ARGBQuantizeRow = ARGBQuantizeRow_MSA;
  2478. }
  2479. #endif
  2480. for (y = 0; y < height; ++y) {
  2481. ARGBQuantizeRow(dst, scale, interval_size, interval_offset, width);
  2482. dst += dst_stride_argb;
  2483. }
  2484. return 0;
  2485. }
  2486. // Computes table of cumulative sum for image where the value is the sum
  2487. // of all values above and to the left of the entry. Used by ARGBBlur.
  2488. LIBYUV_API
  2489. int ARGBComputeCumulativeSum(const uint8_t* src_argb,
  2490. int src_stride_argb,
  2491. int32_t* dst_cumsum,
  2492. int dst_stride32_cumsum,
  2493. int width,
  2494. int height) {
  2495. int y;
  2496. void (*ComputeCumulativeSumRow)(const uint8_t* row, int32_t* cumsum,
  2497. const int32_t* previous_cumsum, int width) =
  2498. ComputeCumulativeSumRow_C;
  2499. int32_t* previous_cumsum = dst_cumsum;
  2500. if (!dst_cumsum || !src_argb || width <= 0 || height <= 0) {
  2501. return -1;
  2502. }
  2503. #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
  2504. if (TestCpuFlag(kCpuHasSSE2)) {
  2505. ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
  2506. }
  2507. #endif
  2508. #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_MMI)
  2509. if (TestCpuFlag(kCpuHasMMI)) {
  2510. ComputeCumulativeSumRow = ComputeCumulativeSumRow_MMI;
  2511. }
  2512. #endif
  2513. memset(dst_cumsum, 0, width * sizeof(dst_cumsum[0]) * 4); // 4 int per pixel.
  2514. for (y = 0; y < height; ++y) {
  2515. ComputeCumulativeSumRow(src_argb, dst_cumsum, previous_cumsum, width);
  2516. previous_cumsum = dst_cumsum;
  2517. dst_cumsum += dst_stride32_cumsum;
  2518. src_argb += src_stride_argb;
  2519. }
  2520. return 0;
  2521. }
  2522. // Blur ARGB image.
  2523. // Caller should allocate CumulativeSum table of width * height * 16 bytes
  2524. // aligned to 16 byte boundary. height can be radius * 2 + 2 to save memory
  2525. // as the buffer is treated as circular.
  2526. LIBYUV_API
  2527. int ARGBBlur(const uint8_t* src_argb,
  2528. int src_stride_argb,
  2529. uint8_t* dst_argb,
  2530. int dst_stride_argb,
  2531. int32_t* dst_cumsum,
  2532. int dst_stride32_cumsum,
  2533. int width,
  2534. int height,
  2535. int radius) {
  2536. int y;
  2537. void (*ComputeCumulativeSumRow)(const uint8_t* row, int32_t* cumsum,
  2538. const int32_t* previous_cumsum, int width) =
  2539. ComputeCumulativeSumRow_C;
  2540. void (*CumulativeSumToAverageRow)(
  2541. const int32_t* topleft, const int32_t* botleft, int width, int area,
  2542. uint8_t* dst, int count) = CumulativeSumToAverageRow_C;
  2543. int32_t* cumsum_bot_row;
  2544. int32_t* max_cumsum_bot_row;
  2545. int32_t* cumsum_top_row;
  2546. if (!src_argb || !dst_argb || width <= 0 || height == 0) {
  2547. return -1;
  2548. }
  2549. if (height < 0) {
  2550. height = -height;
  2551. src_argb = src_argb + (height - 1) * src_stride_argb;
  2552. src_stride_argb = -src_stride_argb;
  2553. }
  2554. if (radius > height) {
  2555. radius = height;
  2556. }
  2557. if (radius > (width / 2 - 1)) {
  2558. radius = width / 2 - 1;
  2559. }
  2560. if (radius <= 0) {
  2561. return -1;
  2562. }
  2563. #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_SSE2)
  2564. if (TestCpuFlag(kCpuHasSSE2)) {
  2565. ComputeCumulativeSumRow = ComputeCumulativeSumRow_SSE2;
  2566. CumulativeSumToAverageRow = CumulativeSumToAverageRow_SSE2;
  2567. }
  2568. #endif
  2569. #if defined(HAS_CUMULATIVESUMTOAVERAGEROW_MMI)
  2570. if (TestCpuFlag(kCpuHasMMI)) {
  2571. ComputeCumulativeSumRow = ComputeCumulativeSumRow_MMI;
  2572. }
  2573. #endif
  2574. // Compute enough CumulativeSum for first row to be blurred. After this
  2575. // one row of CumulativeSum is updated at a time.
  2576. ARGBComputeCumulativeSum(src_argb, src_stride_argb, dst_cumsum,
  2577. dst_stride32_cumsum, width, radius);
  2578. src_argb = src_argb + radius * src_stride_argb;
  2579. cumsum_bot_row = &dst_cumsum[(radius - 1) * dst_stride32_cumsum];
  2580. max_cumsum_bot_row = &dst_cumsum[(radius * 2 + 2) * dst_stride32_cumsum];
  2581. cumsum_top_row = &dst_cumsum[0];
  2582. for (y = 0; y < height; ++y) {
  2583. int top_y = ((y - radius - 1) >= 0) ? (y - radius - 1) : 0;
  2584. int bot_y = ((y + radius) < height) ? (y + radius) : (height - 1);
  2585. int area = radius * (bot_y - top_y);
  2586. int boxwidth = radius * 4;
  2587. int x;
  2588. int n;
  2589. // Increment cumsum_top_row pointer with circular buffer wrap around.
  2590. if (top_y) {
  2591. cumsum_top_row += dst_stride32_cumsum;
  2592. if (cumsum_top_row >= max_cumsum_bot_row) {
  2593. cumsum_top_row = dst_cumsum;
  2594. }
  2595. }
  2596. // Increment cumsum_bot_row pointer with circular buffer wrap around and
  2597. // then fill in a row of CumulativeSum.
  2598. if ((y + radius) < height) {
  2599. const int32_t* prev_cumsum_bot_row = cumsum_bot_row;
  2600. cumsum_bot_row += dst_stride32_cumsum;
  2601. if (cumsum_bot_row >= max_cumsum_bot_row) {
  2602. cumsum_bot_row = dst_cumsum;
  2603. }
  2604. ComputeCumulativeSumRow(src_argb, cumsum_bot_row, prev_cumsum_bot_row,
  2605. width);
  2606. src_argb += src_stride_argb;
  2607. }
  2608. // Left clipped.
  2609. for (x = 0; x < radius + 1; ++x) {
  2610. CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, boxwidth, area,
  2611. &dst_argb[x * 4], 1);
  2612. area += (bot_y - top_y);
  2613. boxwidth += 4;
  2614. }
  2615. // Middle unclipped.
  2616. n = (width - 1) - radius - x + 1;
  2617. CumulativeSumToAverageRow(cumsum_top_row, cumsum_bot_row, boxwidth, area,
  2618. &dst_argb[x * 4], n);
  2619. // Right clipped.
  2620. for (x += n; x <= width - 1; ++x) {
  2621. area -= (bot_y - top_y);
  2622. boxwidth -= 4;
  2623. CumulativeSumToAverageRow(cumsum_top_row + (x - radius - 1) * 4,
  2624. cumsum_bot_row + (x - radius - 1) * 4, boxwidth,
  2625. area, &dst_argb[x * 4], 1);
  2626. }
  2627. dst_argb += dst_stride_argb;
  2628. }
  2629. return 0;
  2630. }
  2631. // Multiply ARGB image by a specified ARGB value.
  2632. LIBYUV_API
  2633. int ARGBShade(const uint8_t* src_argb,
  2634. int src_stride_argb,
  2635. uint8_t* dst_argb,
  2636. int dst_stride_argb,
  2637. int width,
  2638. int height,
  2639. uint32_t value) {
  2640. int y;
  2641. void (*ARGBShadeRow)(const uint8_t* src_argb, uint8_t* dst_argb, int width,
  2642. uint32_t value) = ARGBShadeRow_C;
  2643. if (!src_argb || !dst_argb || width <= 0 || height == 0 || value == 0u) {
  2644. return -1;
  2645. }
  2646. if (height < 0) {
  2647. height = -height;
  2648. src_argb = src_argb + (height - 1) * src_stride_argb;
  2649. src_stride_argb = -src_stride_argb;
  2650. }
  2651. // Coalesce rows.
  2652. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  2653. width *= height;
  2654. height = 1;
  2655. src_stride_argb = dst_stride_argb = 0;
  2656. }
  2657. #if defined(HAS_ARGBSHADEROW_SSE2)
  2658. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 4)) {
  2659. ARGBShadeRow = ARGBShadeRow_SSE2;
  2660. }
  2661. #endif
  2662. #if defined(HAS_ARGBSHADEROW_NEON)
  2663. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2664. ARGBShadeRow = ARGBShadeRow_NEON;
  2665. }
  2666. #endif
  2667. #if defined(HAS_ARGBSHADEROW_MMI)
  2668. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(width, 2)) {
  2669. ARGBShadeRow = ARGBShadeRow_MMI;
  2670. }
  2671. #endif
  2672. #if defined(HAS_ARGBSHADEROW_MSA)
  2673. if (TestCpuFlag(kCpuHasMSA) && IS_ALIGNED(width, 4)) {
  2674. ARGBShadeRow = ARGBShadeRow_MSA;
  2675. }
  2676. #endif
  2677. for (y = 0; y < height; ++y) {
  2678. ARGBShadeRow(src_argb, dst_argb, width, value);
  2679. src_argb += src_stride_argb;
  2680. dst_argb += dst_stride_argb;
  2681. }
  2682. return 0;
  2683. }
  2684. // Interpolate 2 planes by specified amount (0 to 255).
  2685. LIBYUV_API
  2686. int InterpolatePlane(const uint8_t* src0,
  2687. int src_stride0,
  2688. const uint8_t* src1,
  2689. int src_stride1,
  2690. uint8_t* dst,
  2691. int dst_stride,
  2692. int width,
  2693. int height,
  2694. int interpolation) {
  2695. int y;
  2696. void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr,
  2697. ptrdiff_t src_stride, int dst_width,
  2698. int source_y_fraction) = InterpolateRow_C;
  2699. if (!src0 || !src1 || !dst || width <= 0 || height == 0) {
  2700. return -1;
  2701. }
  2702. // Negative height means invert the image.
  2703. if (height < 0) {
  2704. height = -height;
  2705. dst = dst + (height - 1) * dst_stride;
  2706. dst_stride = -dst_stride;
  2707. }
  2708. // Coalesce rows.
  2709. if (src_stride0 == width && src_stride1 == width && dst_stride == width) {
  2710. width *= height;
  2711. height = 1;
  2712. src_stride0 = src_stride1 = dst_stride = 0;
  2713. }
  2714. #if defined(HAS_INTERPOLATEROW_SSSE3)
  2715. if (TestCpuFlag(kCpuHasSSSE3)) {
  2716. InterpolateRow = InterpolateRow_Any_SSSE3;
  2717. if (IS_ALIGNED(width, 16)) {
  2718. InterpolateRow = InterpolateRow_SSSE3;
  2719. }
  2720. }
  2721. #endif
  2722. #if defined(HAS_INTERPOLATEROW_AVX2)
  2723. if (TestCpuFlag(kCpuHasAVX2)) {
  2724. InterpolateRow = InterpolateRow_Any_AVX2;
  2725. if (IS_ALIGNED(width, 32)) {
  2726. InterpolateRow = InterpolateRow_AVX2;
  2727. }
  2728. }
  2729. #endif
  2730. #if defined(HAS_INTERPOLATEROW_NEON)
  2731. if (TestCpuFlag(kCpuHasNEON)) {
  2732. InterpolateRow = InterpolateRow_Any_NEON;
  2733. if (IS_ALIGNED(width, 16)) {
  2734. InterpolateRow = InterpolateRow_NEON;
  2735. }
  2736. }
  2737. #endif
  2738. #if defined(HAS_INTERPOLATEROW_MMI)
  2739. if (TestCpuFlag(kCpuHasMMI)) {
  2740. InterpolateRow = InterpolateRow_Any_MMI;
  2741. if (IS_ALIGNED(width, 8)) {
  2742. InterpolateRow = InterpolateRow_MMI;
  2743. }
  2744. }
  2745. #endif
  2746. #if defined(HAS_INTERPOLATEROW_MSA)
  2747. if (TestCpuFlag(kCpuHasMSA)) {
  2748. InterpolateRow = InterpolateRow_Any_MSA;
  2749. if (IS_ALIGNED(width, 32)) {
  2750. InterpolateRow = InterpolateRow_MSA;
  2751. }
  2752. }
  2753. #endif
  2754. for (y = 0; y < height; ++y) {
  2755. InterpolateRow(dst, src0, src1 - src0, width, interpolation);
  2756. src0 += src_stride0;
  2757. src1 += src_stride1;
  2758. dst += dst_stride;
  2759. }
  2760. return 0;
  2761. }
  2762. // Interpolate 2 ARGB images by specified amount (0 to 255).
  2763. LIBYUV_API
  2764. int ARGBInterpolate(const uint8_t* src_argb0,
  2765. int src_stride_argb0,
  2766. const uint8_t* src_argb1,
  2767. int src_stride_argb1,
  2768. uint8_t* dst_argb,
  2769. int dst_stride_argb,
  2770. int width,
  2771. int height,
  2772. int interpolation) {
  2773. return InterpolatePlane(src_argb0, src_stride_argb0, src_argb1,
  2774. src_stride_argb1, dst_argb, dst_stride_argb,
  2775. width * 4, height, interpolation);
  2776. }
  2777. // Interpolate 2 YUV images by specified amount (0 to 255).
  2778. LIBYUV_API
  2779. int I420Interpolate(const uint8_t* src0_y,
  2780. int src0_stride_y,
  2781. const uint8_t* src0_u,
  2782. int src0_stride_u,
  2783. const uint8_t* src0_v,
  2784. int src0_stride_v,
  2785. const uint8_t* src1_y,
  2786. int src1_stride_y,
  2787. const uint8_t* src1_u,
  2788. int src1_stride_u,
  2789. const uint8_t* src1_v,
  2790. int src1_stride_v,
  2791. uint8_t* dst_y,
  2792. int dst_stride_y,
  2793. uint8_t* dst_u,
  2794. int dst_stride_u,
  2795. uint8_t* dst_v,
  2796. int dst_stride_v,
  2797. int width,
  2798. int height,
  2799. int interpolation) {
  2800. int halfwidth = (width + 1) >> 1;
  2801. int halfheight = (height + 1) >> 1;
  2802. if (!src0_y || !src0_u || !src0_v || !src1_y || !src1_u || !src1_v ||
  2803. !dst_y || !dst_u || !dst_v || width <= 0 || height == 0) {
  2804. return -1;
  2805. }
  2806. InterpolatePlane(src0_y, src0_stride_y, src1_y, src1_stride_y, dst_y,
  2807. dst_stride_y, width, height, interpolation);
  2808. InterpolatePlane(src0_u, src0_stride_u, src1_u, src1_stride_u, dst_u,
  2809. dst_stride_u, halfwidth, halfheight, interpolation);
  2810. InterpolatePlane(src0_v, src0_stride_v, src1_v, src1_stride_v, dst_v,
  2811. dst_stride_v, halfwidth, halfheight, interpolation);
  2812. return 0;
  2813. }
  2814. // Shuffle ARGB channel order. e.g. BGRA to ARGB.
  2815. LIBYUV_API
  2816. int ARGBShuffle(const uint8_t* src_bgra,
  2817. int src_stride_bgra,
  2818. uint8_t* dst_argb,
  2819. int dst_stride_argb,
  2820. const uint8_t* shuffler,
  2821. int width,
  2822. int height) {
  2823. int y;
  2824. void (*ARGBShuffleRow)(const uint8_t* src_bgra, uint8_t* dst_argb,
  2825. const uint8_t* shuffler, int width) = ARGBShuffleRow_C;
  2826. if (!src_bgra || !dst_argb || width <= 0 || height == 0) {
  2827. return -1;
  2828. }
  2829. // Negative height means invert the image.
  2830. if (height < 0) {
  2831. height = -height;
  2832. src_bgra = src_bgra + (height - 1) * src_stride_bgra;
  2833. src_stride_bgra = -src_stride_bgra;
  2834. }
  2835. // Coalesce rows.
  2836. if (src_stride_bgra == width * 4 && dst_stride_argb == width * 4) {
  2837. width *= height;
  2838. height = 1;
  2839. src_stride_bgra = dst_stride_argb = 0;
  2840. }
  2841. #if defined(HAS_ARGBSHUFFLEROW_SSSE3)
  2842. if (TestCpuFlag(kCpuHasSSSE3)) {
  2843. ARGBShuffleRow = ARGBShuffleRow_Any_SSSE3;
  2844. if (IS_ALIGNED(width, 8)) {
  2845. ARGBShuffleRow = ARGBShuffleRow_SSSE3;
  2846. }
  2847. }
  2848. #endif
  2849. #if defined(HAS_ARGBSHUFFLEROW_AVX2)
  2850. if (TestCpuFlag(kCpuHasAVX2)) {
  2851. ARGBShuffleRow = ARGBShuffleRow_Any_AVX2;
  2852. if (IS_ALIGNED(width, 16)) {
  2853. ARGBShuffleRow = ARGBShuffleRow_AVX2;
  2854. }
  2855. }
  2856. #endif
  2857. #if defined(HAS_ARGBSHUFFLEROW_NEON)
  2858. if (TestCpuFlag(kCpuHasNEON)) {
  2859. ARGBShuffleRow = ARGBShuffleRow_Any_NEON;
  2860. if (IS_ALIGNED(width, 4)) {
  2861. ARGBShuffleRow = ARGBShuffleRow_NEON;
  2862. }
  2863. }
  2864. #endif
  2865. #if defined(HAS_ARGBSHUFFLEROW_MMI)
  2866. if (TestCpuFlag(kCpuHasMMI)) {
  2867. ARGBShuffleRow = ARGBShuffleRow_Any_MMI;
  2868. if (IS_ALIGNED(width, 2)) {
  2869. ARGBShuffleRow = ARGBShuffleRow_MMI;
  2870. }
  2871. }
  2872. #endif
  2873. #if defined(HAS_ARGBSHUFFLEROW_MSA)
  2874. if (TestCpuFlag(kCpuHasMSA)) {
  2875. ARGBShuffleRow = ARGBShuffleRow_Any_MSA;
  2876. if (IS_ALIGNED(width, 8)) {
  2877. ARGBShuffleRow = ARGBShuffleRow_MSA;
  2878. }
  2879. }
  2880. #endif
  2881. for (y = 0; y < height; ++y) {
  2882. ARGBShuffleRow(src_bgra, dst_argb, shuffler, width);
  2883. src_bgra += src_stride_bgra;
  2884. dst_argb += dst_stride_argb;
  2885. }
  2886. return 0;
  2887. }
  2888. // Gauss blur a float plane using Gaussian 5x5 filter with
  2889. // coefficients of 1, 4, 6, 4, 1.
  2890. // Each destination pixel is a blur of the 5x5
  2891. // pixels from the source.
  2892. // Source edges are clamped.
  2893. // Edge is 2 pixels on each side, and interior is multiple of 4.
  2894. LIBYUV_API
  2895. int GaussPlane_F32(const float* src,
  2896. int src_stride,
  2897. float* dst,
  2898. int dst_stride,
  2899. int width,
  2900. int height) {
  2901. int y;
  2902. void (*GaussCol_F32)(const float* src0, const float* src1, const float* src2,
  2903. const float* src3, const float* src4, float* dst,
  2904. int width) = GaussCol_F32_C;
  2905. void (*GaussRow_F32)(const float* src, float* dst, int width) =
  2906. GaussRow_F32_C;
  2907. if (!src || !dst || width <= 0 || height == 0) {
  2908. return -1;
  2909. }
  2910. // Negative height means invert the image.
  2911. if (height < 0) {
  2912. height = -height;
  2913. src = src + (height - 1) * src_stride;
  2914. src_stride = -src_stride;
  2915. }
  2916. #if defined(HAS_GAUSSCOL_F32_NEON)
  2917. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2918. GaussCol_F32 = GaussCol_F32_NEON;
  2919. }
  2920. #endif
  2921. #if defined(HAS_GAUSSROW_F32_NEON)
  2922. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 8)) {
  2923. GaussRow_F32 = GaussRow_F32_NEON;
  2924. }
  2925. #endif
  2926. {
  2927. // 2 pixels on each side, but aligned out to 16 bytes.
  2928. align_buffer_64(rowbuf, (4 + width + 4) * 4);
  2929. memset(rowbuf, 0, 16);
  2930. memset(rowbuf + (4 + width) * 4, 0, 16);
  2931. float* row = (float*)(rowbuf + 16);
  2932. const float* src0 = src;
  2933. const float* src1 = src;
  2934. const float* src2 = src;
  2935. const float* src3 = src2 + ((height > 1) ? src_stride : 0);
  2936. const float* src4 = src3 + ((height > 2) ? src_stride : 0);
  2937. for (y = 0; y < height; ++y) {
  2938. GaussCol_F32(src0, src1, src2, src3, src4, row, width);
  2939. // Extrude edge by 2 floats
  2940. row[-2] = row[-1] = row[0];
  2941. row[width + 1] = row[width] = row[width - 1];
  2942. GaussRow_F32(row - 2, dst, width);
  2943. src0 = src1;
  2944. src1 = src2;
  2945. src2 = src3;
  2946. src3 = src4;
  2947. if ((y + 2) < (height - 1)) {
  2948. src4 += src_stride;
  2949. }
  2950. dst += dst_stride;
  2951. }
  2952. free_aligned_buffer_64(rowbuf);
  2953. }
  2954. return 0;
  2955. }
  2956. // Sobel ARGB effect.
  2957. static int ARGBSobelize(const uint8_t* src_argb,
  2958. int src_stride_argb,
  2959. uint8_t* dst_argb,
  2960. int dst_stride_argb,
  2961. int width,
  2962. int height,
  2963. void (*SobelRow)(const uint8_t* src_sobelx,
  2964. const uint8_t* src_sobely,
  2965. uint8_t* dst,
  2966. int width)) {
  2967. int y;
  2968. void (*ARGBToYJRow)(const uint8_t* src_argb, uint8_t* dst_g, int width) =
  2969. ARGBToYJRow_C;
  2970. void (*SobelYRow)(const uint8_t* src_y0, const uint8_t* src_y1,
  2971. uint8_t* dst_sobely, int width) = SobelYRow_C;
  2972. void (*SobelXRow)(const uint8_t* src_y0, const uint8_t* src_y1,
  2973. const uint8_t* src_y2, uint8_t* dst_sobely, int width) =
  2974. SobelXRow_C;
  2975. const int kEdge = 16; // Extra pixels at start of row for extrude/align.
  2976. if (!src_argb || !dst_argb || width <= 0 || height == 0) {
  2977. return -1;
  2978. }
  2979. // Negative height means invert the image.
  2980. if (height < 0) {
  2981. height = -height;
  2982. src_argb = src_argb + (height - 1) * src_stride_argb;
  2983. src_stride_argb = -src_stride_argb;
  2984. }
  2985. #if defined(HAS_ARGBTOYJROW_SSSE3)
  2986. if (TestCpuFlag(kCpuHasSSSE3)) {
  2987. ARGBToYJRow = ARGBToYJRow_Any_SSSE3;
  2988. if (IS_ALIGNED(width, 16)) {
  2989. ARGBToYJRow = ARGBToYJRow_SSSE3;
  2990. }
  2991. }
  2992. #endif
  2993. #if defined(HAS_ARGBTOYJROW_AVX2)
  2994. if (TestCpuFlag(kCpuHasAVX2)) {
  2995. ARGBToYJRow = ARGBToYJRow_Any_AVX2;
  2996. if (IS_ALIGNED(width, 32)) {
  2997. ARGBToYJRow = ARGBToYJRow_AVX2;
  2998. }
  2999. }
  3000. #endif
  3001. #if defined(HAS_ARGBTOYJROW_NEON)
  3002. if (TestCpuFlag(kCpuHasNEON)) {
  3003. ARGBToYJRow = ARGBToYJRow_Any_NEON;
  3004. if (IS_ALIGNED(width, 8)) {
  3005. ARGBToYJRow = ARGBToYJRow_NEON;
  3006. }
  3007. }
  3008. #endif
  3009. #if defined(HAS_ARGBTOYJROW_MMI)
  3010. if (TestCpuFlag(kCpuHasMMI)) {
  3011. ARGBToYJRow = ARGBToYJRow_Any_MMI;
  3012. if (IS_ALIGNED(width, 8)) {
  3013. ARGBToYJRow = ARGBToYJRow_MMI;
  3014. }
  3015. }
  3016. #endif
  3017. #if defined(HAS_ARGBTOYJROW_MSA)
  3018. if (TestCpuFlag(kCpuHasMSA)) {
  3019. ARGBToYJRow = ARGBToYJRow_Any_MSA;
  3020. if (IS_ALIGNED(width, 16)) {
  3021. ARGBToYJRow = ARGBToYJRow_MSA;
  3022. }
  3023. }
  3024. #endif
  3025. #if defined(HAS_SOBELYROW_SSE2)
  3026. if (TestCpuFlag(kCpuHasSSE2)) {
  3027. SobelYRow = SobelYRow_SSE2;
  3028. }
  3029. #endif
  3030. #if defined(HAS_SOBELYROW_NEON)
  3031. if (TestCpuFlag(kCpuHasNEON)) {
  3032. SobelYRow = SobelYRow_NEON;
  3033. }
  3034. #endif
  3035. #if defined(HAS_SOBELYROW_MMI)
  3036. if (TestCpuFlag(kCpuHasMMI)) {
  3037. SobelYRow = SobelYRow_MMI;
  3038. }
  3039. #endif
  3040. #if defined(HAS_SOBELYROW_MSA)
  3041. if (TestCpuFlag(kCpuHasMSA)) {
  3042. SobelYRow = SobelYRow_MSA;
  3043. }
  3044. #endif
  3045. #if defined(HAS_SOBELXROW_SSE2)
  3046. if (TestCpuFlag(kCpuHasSSE2)) {
  3047. SobelXRow = SobelXRow_SSE2;
  3048. }
  3049. #endif
  3050. #if defined(HAS_SOBELXROW_NEON)
  3051. if (TestCpuFlag(kCpuHasNEON)) {
  3052. SobelXRow = SobelXRow_NEON;
  3053. }
  3054. #endif
  3055. #if defined(HAS_SOBELXROW_MMI)
  3056. if (TestCpuFlag(kCpuHasMMI)) {
  3057. SobelXRow = SobelXRow_MMI;
  3058. }
  3059. #endif
  3060. #if defined(HAS_SOBELXROW_MSA)
  3061. if (TestCpuFlag(kCpuHasMSA)) {
  3062. SobelXRow = SobelXRow_MSA;
  3063. }
  3064. #endif
  3065. {
  3066. // 3 rows with edges before/after.
  3067. const int kRowSize = (width + kEdge + 31) & ~31;
  3068. align_buffer_64(rows, kRowSize * 2 + (kEdge + kRowSize * 3 + kEdge));
  3069. uint8_t* row_sobelx = rows;
  3070. uint8_t* row_sobely = rows + kRowSize;
  3071. uint8_t* row_y = rows + kRowSize * 2;
  3072. // Convert first row.
  3073. uint8_t* row_y0 = row_y + kEdge;
  3074. uint8_t* row_y1 = row_y0 + kRowSize;
  3075. uint8_t* row_y2 = row_y1 + kRowSize;
  3076. ARGBToYJRow(src_argb, row_y0, width);
  3077. row_y0[-1] = row_y0[0];
  3078. memset(row_y0 + width, row_y0[width - 1], 16); // Extrude 16 for valgrind.
  3079. ARGBToYJRow(src_argb, row_y1, width);
  3080. row_y1[-1] = row_y1[0];
  3081. memset(row_y1 + width, row_y1[width - 1], 16);
  3082. memset(row_y2 + width, 0, 16);
  3083. for (y = 0; y < height; ++y) {
  3084. // Convert next row of ARGB to G.
  3085. if (y < (height - 1)) {
  3086. src_argb += src_stride_argb;
  3087. }
  3088. ARGBToYJRow(src_argb, row_y2, width);
  3089. row_y2[-1] = row_y2[0];
  3090. row_y2[width] = row_y2[width - 1];
  3091. SobelXRow(row_y0 - 1, row_y1 - 1, row_y2 - 1, row_sobelx, width);
  3092. SobelYRow(row_y0 - 1, row_y2 - 1, row_sobely, width);
  3093. SobelRow(row_sobelx, row_sobely, dst_argb, width);
  3094. // Cycle thru circular queue of 3 row_y buffers.
  3095. {
  3096. uint8_t* row_yt = row_y0;
  3097. row_y0 = row_y1;
  3098. row_y1 = row_y2;
  3099. row_y2 = row_yt;
  3100. }
  3101. dst_argb += dst_stride_argb;
  3102. }
  3103. free_aligned_buffer_64(rows);
  3104. }
  3105. return 0;
  3106. }
  3107. // Sobel ARGB effect.
  3108. LIBYUV_API
  3109. int ARGBSobel(const uint8_t* src_argb,
  3110. int src_stride_argb,
  3111. uint8_t* dst_argb,
  3112. int dst_stride_argb,
  3113. int width,
  3114. int height) {
  3115. void (*SobelRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely,
  3116. uint8_t* dst_argb, int width) = SobelRow_C;
  3117. #if defined(HAS_SOBELROW_SSE2)
  3118. if (TestCpuFlag(kCpuHasSSE2)) {
  3119. SobelRow = SobelRow_Any_SSE2;
  3120. if (IS_ALIGNED(width, 16)) {
  3121. SobelRow = SobelRow_SSE2;
  3122. }
  3123. }
  3124. #endif
  3125. #if defined(HAS_SOBELROW_NEON)
  3126. if (TestCpuFlag(kCpuHasNEON)) {
  3127. SobelRow = SobelRow_Any_NEON;
  3128. if (IS_ALIGNED(width, 8)) {
  3129. SobelRow = SobelRow_NEON;
  3130. }
  3131. }
  3132. #endif
  3133. #if defined(HAS_SOBELROW_MMI)
  3134. if (TestCpuFlag(kCpuHasMMI)) {
  3135. SobelRow = SobelRow_Any_MMI;
  3136. if (IS_ALIGNED(width, 8)) {
  3137. SobelRow = SobelRow_MMI;
  3138. }
  3139. }
  3140. #endif
  3141. #if defined(HAS_SOBELROW_MSA)
  3142. if (TestCpuFlag(kCpuHasMSA)) {
  3143. SobelRow = SobelRow_Any_MSA;
  3144. if (IS_ALIGNED(width, 16)) {
  3145. SobelRow = SobelRow_MSA;
  3146. }
  3147. }
  3148. #endif
  3149. return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
  3150. width, height, SobelRow);
  3151. }
  3152. // Sobel ARGB effect with planar output.
  3153. LIBYUV_API
  3154. int ARGBSobelToPlane(const uint8_t* src_argb,
  3155. int src_stride_argb,
  3156. uint8_t* dst_y,
  3157. int dst_stride_y,
  3158. int width,
  3159. int height) {
  3160. void (*SobelToPlaneRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely,
  3161. uint8_t* dst_, int width) = SobelToPlaneRow_C;
  3162. #if defined(HAS_SOBELTOPLANEROW_SSE2)
  3163. if (TestCpuFlag(kCpuHasSSE2)) {
  3164. SobelToPlaneRow = SobelToPlaneRow_Any_SSE2;
  3165. if (IS_ALIGNED(width, 16)) {
  3166. SobelToPlaneRow = SobelToPlaneRow_SSE2;
  3167. }
  3168. }
  3169. #endif
  3170. #if defined(HAS_SOBELTOPLANEROW_NEON)
  3171. if (TestCpuFlag(kCpuHasNEON)) {
  3172. SobelToPlaneRow = SobelToPlaneRow_Any_NEON;
  3173. if (IS_ALIGNED(width, 16)) {
  3174. SobelToPlaneRow = SobelToPlaneRow_NEON;
  3175. }
  3176. }
  3177. #endif
  3178. #if defined(HAS_SOBELTOPLANEROW_MMI)
  3179. if (TestCpuFlag(kCpuHasMMI)) {
  3180. SobelToPlaneRow = SobelToPlaneRow_Any_MMI;
  3181. if (IS_ALIGNED(width, 8)) {
  3182. SobelToPlaneRow = SobelToPlaneRow_MMI;
  3183. }
  3184. }
  3185. #endif
  3186. #if defined(HAS_SOBELTOPLANEROW_MSA)
  3187. if (TestCpuFlag(kCpuHasMSA)) {
  3188. SobelToPlaneRow = SobelToPlaneRow_Any_MSA;
  3189. if (IS_ALIGNED(width, 32)) {
  3190. SobelToPlaneRow = SobelToPlaneRow_MSA;
  3191. }
  3192. }
  3193. #endif
  3194. return ARGBSobelize(src_argb, src_stride_argb, dst_y, dst_stride_y, width,
  3195. height, SobelToPlaneRow);
  3196. }
  3197. // SobelXY ARGB effect.
  3198. // Similar to Sobel, but also stores Sobel X in R and Sobel Y in B. G = Sobel.
  3199. LIBYUV_API
  3200. int ARGBSobelXY(const uint8_t* src_argb,
  3201. int src_stride_argb,
  3202. uint8_t* dst_argb,
  3203. int dst_stride_argb,
  3204. int width,
  3205. int height) {
  3206. void (*SobelXYRow)(const uint8_t* src_sobelx, const uint8_t* src_sobely,
  3207. uint8_t* dst_argb, int width) = SobelXYRow_C;
  3208. #if defined(HAS_SOBELXYROW_SSE2)
  3209. if (TestCpuFlag(kCpuHasSSE2)) {
  3210. SobelXYRow = SobelXYRow_Any_SSE2;
  3211. if (IS_ALIGNED(width, 16)) {
  3212. SobelXYRow = SobelXYRow_SSE2;
  3213. }
  3214. }
  3215. #endif
  3216. #if defined(HAS_SOBELXYROW_NEON)
  3217. if (TestCpuFlag(kCpuHasNEON)) {
  3218. SobelXYRow = SobelXYRow_Any_NEON;
  3219. if (IS_ALIGNED(width, 8)) {
  3220. SobelXYRow = SobelXYRow_NEON;
  3221. }
  3222. }
  3223. #endif
  3224. #if defined(HAS_SOBELXYROW_MMI)
  3225. if (TestCpuFlag(kCpuHasMMI)) {
  3226. SobelXYRow = SobelXYRow_Any_MMI;
  3227. if (IS_ALIGNED(width, 8)) {
  3228. SobelXYRow = SobelXYRow_MMI;
  3229. }
  3230. }
  3231. #endif
  3232. #if defined(HAS_SOBELXYROW_MSA)
  3233. if (TestCpuFlag(kCpuHasMSA)) {
  3234. SobelXYRow = SobelXYRow_Any_MSA;
  3235. if (IS_ALIGNED(width, 16)) {
  3236. SobelXYRow = SobelXYRow_MSA;
  3237. }
  3238. }
  3239. #endif
  3240. return ARGBSobelize(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
  3241. width, height, SobelXYRow);
  3242. }
  3243. // Apply a 4x4 polynomial to each ARGB pixel.
  3244. LIBYUV_API
  3245. int ARGBPolynomial(const uint8_t* src_argb,
  3246. int src_stride_argb,
  3247. uint8_t* dst_argb,
  3248. int dst_stride_argb,
  3249. const float* poly,
  3250. int width,
  3251. int height) {
  3252. int y;
  3253. void (*ARGBPolynomialRow)(const uint8_t* src_argb, uint8_t* dst_argb,
  3254. const float* poly, int width) = ARGBPolynomialRow_C;
  3255. if (!src_argb || !dst_argb || !poly || width <= 0 || height == 0) {
  3256. return -1;
  3257. }
  3258. // Negative height means invert the image.
  3259. if (height < 0) {
  3260. height = -height;
  3261. src_argb = src_argb + (height - 1) * src_stride_argb;
  3262. src_stride_argb = -src_stride_argb;
  3263. }
  3264. // Coalesce rows.
  3265. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  3266. width *= height;
  3267. height = 1;
  3268. src_stride_argb = dst_stride_argb = 0;
  3269. }
  3270. #if defined(HAS_ARGBPOLYNOMIALROW_SSE2)
  3271. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(width, 2)) {
  3272. ARGBPolynomialRow = ARGBPolynomialRow_SSE2;
  3273. }
  3274. #endif
  3275. #if defined(HAS_ARGBPOLYNOMIALROW_AVX2)
  3276. if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasFMA3) &&
  3277. IS_ALIGNED(width, 2)) {
  3278. ARGBPolynomialRow = ARGBPolynomialRow_AVX2;
  3279. }
  3280. #endif
  3281. for (y = 0; y < height; ++y) {
  3282. ARGBPolynomialRow(src_argb, dst_argb, poly, width);
  3283. src_argb += src_stride_argb;
  3284. dst_argb += dst_stride_argb;
  3285. }
  3286. return 0;
  3287. }
  3288. // Convert plane of 16 bit shorts to half floats.
  3289. // Source values are multiplied by scale before storing as half float.
  3290. LIBYUV_API
  3291. int HalfFloatPlane(const uint16_t* src_y,
  3292. int src_stride_y,
  3293. uint16_t* dst_y,
  3294. int dst_stride_y,
  3295. float scale,
  3296. int width,
  3297. int height) {
  3298. int y;
  3299. void (*HalfFloatRow)(const uint16_t* src, uint16_t* dst, float scale,
  3300. int width) = HalfFloatRow_C;
  3301. if (!src_y || !dst_y || width <= 0 || height == 0) {
  3302. return -1;
  3303. }
  3304. src_stride_y >>= 1;
  3305. dst_stride_y >>= 1;
  3306. // Negative height means invert the image.
  3307. if (height < 0) {
  3308. height = -height;
  3309. src_y = src_y + (height - 1) * src_stride_y;
  3310. src_stride_y = -src_stride_y;
  3311. }
  3312. // Coalesce rows.
  3313. if (src_stride_y == width && dst_stride_y == width) {
  3314. width *= height;
  3315. height = 1;
  3316. src_stride_y = dst_stride_y = 0;
  3317. }
  3318. #if defined(HAS_HALFFLOATROW_SSE2)
  3319. if (TestCpuFlag(kCpuHasSSE2)) {
  3320. HalfFloatRow = HalfFloatRow_Any_SSE2;
  3321. if (IS_ALIGNED(width, 8)) {
  3322. HalfFloatRow = HalfFloatRow_SSE2;
  3323. }
  3324. }
  3325. #endif
  3326. #if defined(HAS_HALFFLOATROW_AVX2)
  3327. if (TestCpuFlag(kCpuHasAVX2)) {
  3328. HalfFloatRow = HalfFloatRow_Any_AVX2;
  3329. if (IS_ALIGNED(width, 16)) {
  3330. HalfFloatRow = HalfFloatRow_AVX2;
  3331. }
  3332. }
  3333. #endif
  3334. #if defined(HAS_HALFFLOATROW_F16C)
  3335. if (TestCpuFlag(kCpuHasAVX2) && TestCpuFlag(kCpuHasF16C)) {
  3336. HalfFloatRow =
  3337. (scale == 1.0f) ? HalfFloat1Row_Any_F16C : HalfFloatRow_Any_F16C;
  3338. if (IS_ALIGNED(width, 16)) {
  3339. HalfFloatRow = (scale == 1.0f) ? HalfFloat1Row_F16C : HalfFloatRow_F16C;
  3340. }
  3341. }
  3342. #endif
  3343. #if defined(HAS_HALFFLOATROW_NEON)
  3344. if (TestCpuFlag(kCpuHasNEON)) {
  3345. HalfFloatRow =
  3346. (scale == 1.0f) ? HalfFloat1Row_Any_NEON : HalfFloatRow_Any_NEON;
  3347. if (IS_ALIGNED(width, 8)) {
  3348. HalfFloatRow = (scale == 1.0f) ? HalfFloat1Row_NEON : HalfFloatRow_NEON;
  3349. }
  3350. }
  3351. #endif
  3352. #if defined(HAS_HALFFLOATROW_MSA)
  3353. if (TestCpuFlag(kCpuHasMSA)) {
  3354. HalfFloatRow = HalfFloatRow_Any_MSA;
  3355. if (IS_ALIGNED(width, 32)) {
  3356. HalfFloatRow = HalfFloatRow_MSA;
  3357. }
  3358. }
  3359. #endif
  3360. for (y = 0; y < height; ++y) {
  3361. HalfFloatRow(src_y, dst_y, scale, width);
  3362. src_y += src_stride_y;
  3363. dst_y += dst_stride_y;
  3364. }
  3365. return 0;
  3366. }
  3367. // Convert a buffer of bytes to floats, scale the values and store as floats.
  3368. LIBYUV_API
  3369. int ByteToFloat(const uint8_t* src_y, float* dst_y, float scale, int width) {
  3370. void (*ByteToFloatRow)(const uint8_t* src, float* dst, float scale,
  3371. int width) = ByteToFloatRow_C;
  3372. if (!src_y || !dst_y || width <= 0) {
  3373. return -1;
  3374. }
  3375. #if defined(HAS_BYTETOFLOATROW_NEON)
  3376. if (TestCpuFlag(kCpuHasNEON)) {
  3377. ByteToFloatRow = ByteToFloatRow_Any_NEON;
  3378. if (IS_ALIGNED(width, 8)) {
  3379. ByteToFloatRow = ByteToFloatRow_NEON;
  3380. }
  3381. }
  3382. #endif
  3383. ByteToFloatRow(src_y, dst_y, scale, width);
  3384. return 0;
  3385. }
  3386. // Apply a lumacolortable to each ARGB pixel.
  3387. LIBYUV_API
  3388. int ARGBLumaColorTable(const uint8_t* src_argb,
  3389. int src_stride_argb,
  3390. uint8_t* dst_argb,
  3391. int dst_stride_argb,
  3392. const uint8_t* luma,
  3393. int width,
  3394. int height) {
  3395. int y;
  3396. void (*ARGBLumaColorTableRow)(
  3397. const uint8_t* src_argb, uint8_t* dst_argb, int width,
  3398. const uint8_t* luma, const uint32_t lumacoeff) = ARGBLumaColorTableRow_C;
  3399. if (!src_argb || !dst_argb || !luma || width <= 0 || height == 0) {
  3400. return -1;
  3401. }
  3402. // Negative height means invert the image.
  3403. if (height < 0) {
  3404. height = -height;
  3405. src_argb = src_argb + (height - 1) * src_stride_argb;
  3406. src_stride_argb = -src_stride_argb;
  3407. }
  3408. // Coalesce rows.
  3409. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  3410. width *= height;
  3411. height = 1;
  3412. src_stride_argb = dst_stride_argb = 0;
  3413. }
  3414. #if defined(HAS_ARGBLUMACOLORTABLEROW_SSSE3)
  3415. if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 4)) {
  3416. ARGBLumaColorTableRow = ARGBLumaColorTableRow_SSSE3;
  3417. }
  3418. #endif
  3419. for (y = 0; y < height; ++y) {
  3420. ARGBLumaColorTableRow(src_argb, dst_argb, width, luma, 0x00264b0f);
  3421. src_argb += src_stride_argb;
  3422. dst_argb += dst_stride_argb;
  3423. }
  3424. return 0;
  3425. }
  3426. // Copy Alpha from one ARGB image to another.
  3427. LIBYUV_API
  3428. int ARGBCopyAlpha(const uint8_t* src_argb,
  3429. int src_stride_argb,
  3430. uint8_t* dst_argb,
  3431. int dst_stride_argb,
  3432. int width,
  3433. int height) {
  3434. int y;
  3435. void (*ARGBCopyAlphaRow)(const uint8_t* src_argb, uint8_t* dst_argb,
  3436. int width) = ARGBCopyAlphaRow_C;
  3437. if (!src_argb || !dst_argb || width <= 0 || height == 0) {
  3438. return -1;
  3439. }
  3440. // Negative height means invert the image.
  3441. if (height < 0) {
  3442. height = -height;
  3443. src_argb = src_argb + (height - 1) * src_stride_argb;
  3444. src_stride_argb = -src_stride_argb;
  3445. }
  3446. // Coalesce rows.
  3447. if (src_stride_argb == width * 4 && dst_stride_argb == width * 4) {
  3448. width *= height;
  3449. height = 1;
  3450. src_stride_argb = dst_stride_argb = 0;
  3451. }
  3452. #if defined(HAS_ARGBCOPYALPHAROW_SSE2)
  3453. if (TestCpuFlag(kCpuHasSSE2)) {
  3454. ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_SSE2;
  3455. if (IS_ALIGNED(width, 8)) {
  3456. ARGBCopyAlphaRow = ARGBCopyAlphaRow_SSE2;
  3457. }
  3458. }
  3459. #endif
  3460. #if defined(HAS_ARGBCOPYALPHAROW_AVX2)
  3461. if (TestCpuFlag(kCpuHasAVX2)) {
  3462. ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_AVX2;
  3463. if (IS_ALIGNED(width, 16)) {
  3464. ARGBCopyAlphaRow = ARGBCopyAlphaRow_AVX2;
  3465. }
  3466. }
  3467. #endif
  3468. #if defined(HAS_ARGBCOPYALPHAROW_MMI)
  3469. if (TestCpuFlag(kCpuHasMMI)) {
  3470. ARGBCopyAlphaRow = ARGBCopyAlphaRow_Any_MMI;
  3471. if (IS_ALIGNED(width, 2)) {
  3472. ARGBCopyAlphaRow = ARGBCopyAlphaRow_MMI;
  3473. }
  3474. }
  3475. #endif
  3476. for (y = 0; y < height; ++y) {
  3477. ARGBCopyAlphaRow(src_argb, dst_argb, width);
  3478. src_argb += src_stride_argb;
  3479. dst_argb += dst_stride_argb;
  3480. }
  3481. return 0;
  3482. }
  3483. // Extract just the alpha channel from ARGB.
  3484. LIBYUV_API
  3485. int ARGBExtractAlpha(const uint8_t* src_argb,
  3486. int src_stride_argb,
  3487. uint8_t* dst_a,
  3488. int dst_stride_a,
  3489. int width,
  3490. int height) {
  3491. if (!src_argb || !dst_a || width <= 0 || height == 0) {
  3492. return -1;
  3493. }
  3494. // Negative height means invert the image.
  3495. if (height < 0) {
  3496. height = -height;
  3497. src_argb += (height - 1) * src_stride_argb;
  3498. src_stride_argb = -src_stride_argb;
  3499. }
  3500. // Coalesce rows.
  3501. if (src_stride_argb == width * 4 && dst_stride_a == width) {
  3502. width *= height;
  3503. height = 1;
  3504. src_stride_argb = dst_stride_a = 0;
  3505. }
  3506. void (*ARGBExtractAlphaRow)(const uint8_t* src_argb, uint8_t* dst_a,
  3507. int width) = ARGBExtractAlphaRow_C;
  3508. #if defined(HAS_ARGBEXTRACTALPHAROW_SSE2)
  3509. if (TestCpuFlag(kCpuHasSSE2)) {
  3510. ARGBExtractAlphaRow = IS_ALIGNED(width, 8) ? ARGBExtractAlphaRow_SSE2
  3511. : ARGBExtractAlphaRow_Any_SSE2;
  3512. }
  3513. #endif
  3514. #if defined(HAS_ARGBEXTRACTALPHAROW_AVX2)
  3515. if (TestCpuFlag(kCpuHasAVX2)) {
  3516. ARGBExtractAlphaRow = IS_ALIGNED(width, 32) ? ARGBExtractAlphaRow_AVX2
  3517. : ARGBExtractAlphaRow_Any_AVX2;
  3518. }
  3519. #endif
  3520. #if defined(HAS_ARGBEXTRACTALPHAROW_NEON)
  3521. if (TestCpuFlag(kCpuHasNEON)) {
  3522. ARGBExtractAlphaRow = IS_ALIGNED(width, 16) ? ARGBExtractAlphaRow_NEON
  3523. : ARGBExtractAlphaRow_Any_NEON;
  3524. }
  3525. #endif
  3526. #if defined(HAS_ARGBEXTRACTALPHAROW_MMI)
  3527. if (TestCpuFlag(kCpuHasMMI)) {
  3528. ARGBExtractAlphaRow = IS_ALIGNED(width, 8) ? ARGBExtractAlphaRow_MMI
  3529. : ARGBExtractAlphaRow_Any_MMI;
  3530. }
  3531. #endif
  3532. #if defined(HAS_ARGBEXTRACTALPHAROW_MSA)
  3533. if (TestCpuFlag(kCpuHasMSA)) {
  3534. ARGBExtractAlphaRow = IS_ALIGNED(width, 16) ? ARGBExtractAlphaRow_MSA
  3535. : ARGBExtractAlphaRow_Any_MSA;
  3536. }
  3537. #endif
  3538. for (int y = 0; y < height; ++y) {
  3539. ARGBExtractAlphaRow(src_argb, dst_a, width);
  3540. src_argb += src_stride_argb;
  3541. dst_a += dst_stride_a;
  3542. }
  3543. return 0;
  3544. }
  3545. // Copy a planar Y channel to the alpha channel of a destination ARGB image.
  3546. LIBYUV_API
  3547. int ARGBCopyYToAlpha(const uint8_t* src_y,
  3548. int src_stride_y,
  3549. uint8_t* dst_argb,
  3550. int dst_stride_argb,
  3551. int width,
  3552. int height) {
  3553. int y;
  3554. void (*ARGBCopyYToAlphaRow)(const uint8_t* src_y, uint8_t* dst_argb,
  3555. int width) = ARGBCopyYToAlphaRow_C;
  3556. if (!src_y || !dst_argb || width <= 0 || height == 0) {
  3557. return -1;
  3558. }
  3559. // Negative height means invert the image.
  3560. if (height < 0) {
  3561. height = -height;
  3562. src_y = src_y + (height - 1) * src_stride_y;
  3563. src_stride_y = -src_stride_y;
  3564. }
  3565. // Coalesce rows.
  3566. if (src_stride_y == width && dst_stride_argb == width * 4) {
  3567. width *= height;
  3568. height = 1;
  3569. src_stride_y = dst_stride_argb = 0;
  3570. }
  3571. #if defined(HAS_ARGBCOPYYTOALPHAROW_SSE2)
  3572. if (TestCpuFlag(kCpuHasSSE2)) {
  3573. ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_SSE2;
  3574. if (IS_ALIGNED(width, 8)) {
  3575. ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_SSE2;
  3576. }
  3577. }
  3578. #endif
  3579. #if defined(HAS_ARGBCOPYYTOALPHAROW_AVX2)
  3580. if (TestCpuFlag(kCpuHasAVX2)) {
  3581. ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_AVX2;
  3582. if (IS_ALIGNED(width, 16)) {
  3583. ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_AVX2;
  3584. }
  3585. }
  3586. #endif
  3587. #if defined(HAS_ARGBCOPYYTOALPHAROW_MMI)
  3588. if (TestCpuFlag(kCpuHasMMI)) {
  3589. ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_Any_MMI;
  3590. if (IS_ALIGNED(width, 8)) {
  3591. ARGBCopyYToAlphaRow = ARGBCopyYToAlphaRow_MMI;
  3592. }
  3593. }
  3594. #endif
  3595. for (y = 0; y < height; ++y) {
  3596. ARGBCopyYToAlphaRow(src_y, dst_argb, width);
  3597. src_y += src_stride_y;
  3598. dst_argb += dst_stride_argb;
  3599. }
  3600. return 0;
  3601. }
  3602. // TODO(fbarchard): Consider if width is even Y channel can be split
  3603. // directly. A SplitUVRow_Odd function could copy the remaining chroma.
  3604. LIBYUV_API
  3605. int YUY2ToNV12(const uint8_t* src_yuy2,
  3606. int src_stride_yuy2,
  3607. uint8_t* dst_y,
  3608. int dst_stride_y,
  3609. uint8_t* dst_uv,
  3610. int dst_stride_uv,
  3611. int width,
  3612. int height) {
  3613. int y;
  3614. int halfwidth = (width + 1) >> 1;
  3615. void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v,
  3616. int width) = SplitUVRow_C;
  3617. void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr,
  3618. ptrdiff_t src_stride, int dst_width,
  3619. int source_y_fraction) = InterpolateRow_C;
  3620. if (!src_yuy2 || !dst_y || !dst_uv || width <= 0 || height == 0) {
  3621. return -1;
  3622. }
  3623. // Negative height means invert the image.
  3624. if (height < 0) {
  3625. height = -height;
  3626. src_yuy2 = src_yuy2 + (height - 1) * src_stride_yuy2;
  3627. src_stride_yuy2 = -src_stride_yuy2;
  3628. }
  3629. #if defined(HAS_SPLITUVROW_SSE2)
  3630. if (TestCpuFlag(kCpuHasSSE2)) {
  3631. SplitUVRow = SplitUVRow_Any_SSE2;
  3632. if (IS_ALIGNED(width, 16)) {
  3633. SplitUVRow = SplitUVRow_SSE2;
  3634. }
  3635. }
  3636. #endif
  3637. #if defined(HAS_SPLITUVROW_AVX2)
  3638. if (TestCpuFlag(kCpuHasAVX2)) {
  3639. SplitUVRow = SplitUVRow_Any_AVX2;
  3640. if (IS_ALIGNED(width, 32)) {
  3641. SplitUVRow = SplitUVRow_AVX2;
  3642. }
  3643. }
  3644. #endif
  3645. #if defined(HAS_SPLITUVROW_NEON)
  3646. if (TestCpuFlag(kCpuHasNEON)) {
  3647. SplitUVRow = SplitUVRow_Any_NEON;
  3648. if (IS_ALIGNED(width, 16)) {
  3649. SplitUVRow = SplitUVRow_NEON;
  3650. }
  3651. }
  3652. #endif
  3653. #if defined(HAS_SPLITUVROW_MMI)
  3654. if (TestCpuFlag(kCpuHasMMI)) {
  3655. SplitUVRow = SplitUVRow_Any_MMI;
  3656. if (IS_ALIGNED(width, 8)) {
  3657. SplitUVRow = SplitUVRow_MMI;
  3658. }
  3659. }
  3660. #endif
  3661. #if defined(HAS_SPLITUVROW_MSA)
  3662. if (TestCpuFlag(kCpuHasMSA)) {
  3663. SplitUVRow = SplitUVRow_Any_MSA;
  3664. if (IS_ALIGNED(width, 32)) {
  3665. SplitUVRow = SplitUVRow_MSA;
  3666. }
  3667. }
  3668. #endif
  3669. #if defined(HAS_INTERPOLATEROW_SSSE3)
  3670. if (TestCpuFlag(kCpuHasSSSE3)) {
  3671. InterpolateRow = InterpolateRow_Any_SSSE3;
  3672. if (IS_ALIGNED(width, 16)) {
  3673. InterpolateRow = InterpolateRow_SSSE3;
  3674. }
  3675. }
  3676. #endif
  3677. #if defined(HAS_INTERPOLATEROW_AVX2)
  3678. if (TestCpuFlag(kCpuHasAVX2)) {
  3679. InterpolateRow = InterpolateRow_Any_AVX2;
  3680. if (IS_ALIGNED(width, 32)) {
  3681. InterpolateRow = InterpolateRow_AVX2;
  3682. }
  3683. }
  3684. #endif
  3685. #if defined(HAS_INTERPOLATEROW_NEON)
  3686. if (TestCpuFlag(kCpuHasNEON)) {
  3687. InterpolateRow = InterpolateRow_Any_NEON;
  3688. if (IS_ALIGNED(width, 16)) {
  3689. InterpolateRow = InterpolateRow_NEON;
  3690. }
  3691. }
  3692. #endif
  3693. #if defined(HAS_INTERPOLATEROW_MMI)
  3694. if (TestCpuFlag(kCpuHasMMI)) {
  3695. InterpolateRow = InterpolateRow_Any_MMI;
  3696. if (IS_ALIGNED(width, 8)) {
  3697. InterpolateRow = InterpolateRow_MMI;
  3698. }
  3699. }
  3700. #endif
  3701. #if defined(HAS_INTERPOLATEROW_MSA)
  3702. if (TestCpuFlag(kCpuHasMSA)) {
  3703. InterpolateRow = InterpolateRow_Any_MSA;
  3704. if (IS_ALIGNED(width, 32)) {
  3705. InterpolateRow = InterpolateRow_MSA;
  3706. }
  3707. }
  3708. #endif
  3709. {
  3710. int awidth = halfwidth * 2;
  3711. // row of y and 2 rows of uv
  3712. align_buffer_64(rows, awidth * 3);
  3713. for (y = 0; y < height - 1; y += 2) {
  3714. // Split Y from UV.
  3715. SplitUVRow(src_yuy2, rows, rows + awidth, awidth);
  3716. memcpy(dst_y, rows, width);
  3717. SplitUVRow(src_yuy2 + src_stride_yuy2, rows, rows + awidth * 2, awidth);
  3718. memcpy(dst_y + dst_stride_y, rows, width);
  3719. InterpolateRow(dst_uv, rows + awidth, awidth, awidth, 128);
  3720. src_yuy2 += src_stride_yuy2 * 2;
  3721. dst_y += dst_stride_y * 2;
  3722. dst_uv += dst_stride_uv;
  3723. }
  3724. if (height & 1) {
  3725. // Split Y from UV.
  3726. SplitUVRow(src_yuy2, rows, dst_uv, awidth);
  3727. memcpy(dst_y, rows, width);
  3728. }
  3729. free_aligned_buffer_64(rows);
  3730. }
  3731. return 0;
  3732. }
  3733. LIBYUV_API
  3734. int UYVYToNV12(const uint8_t* src_uyvy,
  3735. int src_stride_uyvy,
  3736. uint8_t* dst_y,
  3737. int dst_stride_y,
  3738. uint8_t* dst_uv,
  3739. int dst_stride_uv,
  3740. int width,
  3741. int height) {
  3742. int y;
  3743. int halfwidth = (width + 1) >> 1;
  3744. void (*SplitUVRow)(const uint8_t* src_uv, uint8_t* dst_u, uint8_t* dst_v,
  3745. int width) = SplitUVRow_C;
  3746. void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr,
  3747. ptrdiff_t src_stride, int dst_width,
  3748. int source_y_fraction) = InterpolateRow_C;
  3749. if (!src_uyvy || !dst_y || !dst_uv || width <= 0 || height == 0) {
  3750. return -1;
  3751. }
  3752. // Negative height means invert the image.
  3753. if (height < 0) {
  3754. height = -height;
  3755. src_uyvy = src_uyvy + (height - 1) * src_stride_uyvy;
  3756. src_stride_uyvy = -src_stride_uyvy;
  3757. }
  3758. #if defined(HAS_SPLITUVROW_SSE2)
  3759. if (TestCpuFlag(kCpuHasSSE2)) {
  3760. SplitUVRow = SplitUVRow_Any_SSE2;
  3761. if (IS_ALIGNED(width, 16)) {
  3762. SplitUVRow = SplitUVRow_SSE2;
  3763. }
  3764. }
  3765. #endif
  3766. #if defined(HAS_SPLITUVROW_AVX2)
  3767. if (TestCpuFlag(kCpuHasAVX2)) {
  3768. SplitUVRow = SplitUVRow_Any_AVX2;
  3769. if (IS_ALIGNED(width, 32)) {
  3770. SplitUVRow = SplitUVRow_AVX2;
  3771. }
  3772. }
  3773. #endif
  3774. #if defined(HAS_SPLITUVROW_NEON)
  3775. if (TestCpuFlag(kCpuHasNEON)) {
  3776. SplitUVRow = SplitUVRow_Any_NEON;
  3777. if (IS_ALIGNED(width, 16)) {
  3778. SplitUVRow = SplitUVRow_NEON;
  3779. }
  3780. }
  3781. #endif
  3782. #if defined(HAS_SPLITUVROW_MMI)
  3783. if (TestCpuFlag(kCpuHasMMI)) {
  3784. SplitUVRow = SplitUVRow_Any_MMI;
  3785. if (IS_ALIGNED(width, 8)) {
  3786. SplitUVRow = SplitUVRow_MMI;
  3787. }
  3788. }
  3789. #endif
  3790. #if defined(HAS_SPLITUVROW_MSA)
  3791. if (TestCpuFlag(kCpuHasMSA)) {
  3792. SplitUVRow = SplitUVRow_Any_MSA;
  3793. if (IS_ALIGNED(width, 32)) {
  3794. SplitUVRow = SplitUVRow_MSA;
  3795. }
  3796. }
  3797. #endif
  3798. #if defined(HAS_INTERPOLATEROW_SSSE3)
  3799. if (TestCpuFlag(kCpuHasSSSE3)) {
  3800. InterpolateRow = InterpolateRow_Any_SSSE3;
  3801. if (IS_ALIGNED(width, 16)) {
  3802. InterpolateRow = InterpolateRow_SSSE3;
  3803. }
  3804. }
  3805. #endif
  3806. #if defined(HAS_INTERPOLATEROW_AVX2)
  3807. if (TestCpuFlag(kCpuHasAVX2)) {
  3808. InterpolateRow = InterpolateRow_Any_AVX2;
  3809. if (IS_ALIGNED(width, 32)) {
  3810. InterpolateRow = InterpolateRow_AVX2;
  3811. }
  3812. }
  3813. #endif
  3814. #if defined(HAS_INTERPOLATEROW_NEON)
  3815. if (TestCpuFlag(kCpuHasNEON)) {
  3816. InterpolateRow = InterpolateRow_Any_NEON;
  3817. if (IS_ALIGNED(width, 16)) {
  3818. InterpolateRow = InterpolateRow_NEON;
  3819. }
  3820. }
  3821. #endif
  3822. #if defined(HAS_INTERPOLATEROW_MMI)
  3823. if (TestCpuFlag(kCpuHasMMI)) {
  3824. InterpolateRow = InterpolateRow_Any_MMI;
  3825. if (IS_ALIGNED(width, 8)) {
  3826. InterpolateRow = InterpolateRow_MMI;
  3827. }
  3828. }
  3829. #endif
  3830. #if defined(HAS_INTERPOLATEROW_MSA)
  3831. if (TestCpuFlag(kCpuHasMSA)) {
  3832. InterpolateRow = InterpolateRow_Any_MSA;
  3833. if (IS_ALIGNED(width, 32)) {
  3834. InterpolateRow = InterpolateRow_MSA;
  3835. }
  3836. }
  3837. #endif
  3838. {
  3839. int awidth = halfwidth * 2;
  3840. // row of y and 2 rows of uv
  3841. align_buffer_64(rows, awidth * 3);
  3842. for (y = 0; y < height - 1; y += 2) {
  3843. // Split Y from UV.
  3844. SplitUVRow(src_uyvy, rows + awidth, rows, awidth);
  3845. memcpy(dst_y, rows, width);
  3846. SplitUVRow(src_uyvy + src_stride_uyvy, rows + awidth * 2, rows, awidth);
  3847. memcpy(dst_y + dst_stride_y, rows, width);
  3848. InterpolateRow(dst_uv, rows + awidth, awidth, awidth, 128);
  3849. src_uyvy += src_stride_uyvy * 2;
  3850. dst_y += dst_stride_y * 2;
  3851. dst_uv += dst_stride_uv;
  3852. }
  3853. if (height & 1) {
  3854. // Split Y from UV.
  3855. SplitUVRow(src_uyvy, dst_uv, rows, awidth);
  3856. memcpy(dst_y, rows, width);
  3857. }
  3858. free_aligned_buffer_64(rows);
  3859. }
  3860. return 0;
  3861. }
  3862. // width and height are src size allowing odd size handling.
  3863. LIBYUV_API
  3864. void HalfMergeUVPlane(const uint8_t* src_u,
  3865. int src_stride_u,
  3866. const uint8_t* src_v,
  3867. int src_stride_v,
  3868. uint8_t* dst_uv,
  3869. int dst_stride_uv,
  3870. int width,
  3871. int height) {
  3872. int y;
  3873. void (*HalfMergeUVRow)(const uint8_t* src_u, int src_stride_u,
  3874. const uint8_t* src_v, int src_stride_v,
  3875. uint8_t* dst_uv, int width) = HalfMergeUVRow_C;
  3876. // Negative height means invert the image.
  3877. if (height < 0) {
  3878. height = -height;
  3879. src_u = src_u + (height - 1) * src_stride_u;
  3880. src_v = src_v + (height - 1) * src_stride_v;
  3881. src_stride_u = -src_stride_u;
  3882. src_stride_v = -src_stride_v;
  3883. }
  3884. #if defined(HAS_HALFMERGEUVROW_NEON)
  3885. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(width, 16)) {
  3886. HalfMergeUVRow = HalfMergeUVRow_NEON;
  3887. }
  3888. #endif
  3889. #if defined(HAS_HALFMERGEUVROW_SSSE3)
  3890. if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(width, 16)) {
  3891. HalfMergeUVRow = HalfMergeUVRow_SSSE3;
  3892. }
  3893. #endif
  3894. #if defined(HAS_HALFMERGEUVROW_AVX2)
  3895. if (TestCpuFlag(kCpuHasAVX2) && IS_ALIGNED(width, 32)) {
  3896. HalfMergeUVRow = HalfMergeUVRow_AVX2;
  3897. }
  3898. #endif
  3899. for (y = 0; y < height - 1; y += 2) {
  3900. // Merge a row of U and V into a row of UV.
  3901. HalfMergeUVRow(src_u, src_stride_u, src_v, src_stride_v, dst_uv, width);
  3902. src_u += src_stride_u * 2;
  3903. src_v += src_stride_v * 2;
  3904. dst_uv += dst_stride_uv;
  3905. }
  3906. if (height & 1) {
  3907. HalfMergeUVRow(src_u, 0, src_v, 0, dst_uv, width);
  3908. }
  3909. }
  3910. #ifdef __cplusplus
  3911. } // extern "C"
  3912. } // namespace libyuv
  3913. #endif