scale.cc 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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/scale.h"
  11. #include <assert.h>
  12. #include <string.h>
  13. #include "libyuv/cpu_id.h"
  14. #include "libyuv/planar_functions.h" // For CopyPlane
  15. #include "libyuv/row.h"
  16. #include "libyuv/scale_row.h"
  17. #ifdef __cplusplus
  18. namespace libyuv {
  19. extern "C" {
  20. #endif
  21. static __inline int Abs(int v) {
  22. return v >= 0 ? v : -v;
  23. }
  24. #define SUBSAMPLE(v, a, s) (v < 0) ? (-((-v + a) >> s)) : ((v + a) >> s)
  25. // Scale plane, 1/2
  26. // This is an optimized version for scaling down a plane to 1/2 of
  27. // its original size.
  28. static void ScalePlaneDown2(int src_width,
  29. int src_height,
  30. int dst_width,
  31. int dst_height,
  32. int src_stride,
  33. int dst_stride,
  34. const uint8_t* src_ptr,
  35. uint8_t* dst_ptr,
  36. enum FilterMode filtering) {
  37. int y;
  38. void (*ScaleRowDown2)(const uint8_t* src_ptr, ptrdiff_t src_stride,
  39. uint8_t* dst_ptr, int dst_width) =
  40. filtering == kFilterNone
  41. ? ScaleRowDown2_C
  42. : (filtering == kFilterLinear ? ScaleRowDown2Linear_C
  43. : ScaleRowDown2Box_C);
  44. int row_stride = src_stride << 1;
  45. (void)src_width;
  46. (void)src_height;
  47. if (!filtering) {
  48. src_ptr += src_stride; // Point to odd rows.
  49. src_stride = 0;
  50. }
  51. #if defined(HAS_SCALEROWDOWN2_NEON)
  52. if (TestCpuFlag(kCpuHasNEON)) {
  53. ScaleRowDown2 =
  54. filtering == kFilterNone
  55. ? ScaleRowDown2_Any_NEON
  56. : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_NEON
  57. : ScaleRowDown2Box_Any_NEON);
  58. if (IS_ALIGNED(dst_width, 16)) {
  59. ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_NEON
  60. : (filtering == kFilterLinear
  61. ? ScaleRowDown2Linear_NEON
  62. : ScaleRowDown2Box_NEON);
  63. }
  64. }
  65. #endif
  66. #if defined(HAS_SCALEROWDOWN2_SSSE3)
  67. if (TestCpuFlag(kCpuHasSSSE3)) {
  68. ScaleRowDown2 =
  69. filtering == kFilterNone
  70. ? ScaleRowDown2_Any_SSSE3
  71. : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_SSSE3
  72. : ScaleRowDown2Box_Any_SSSE3);
  73. if (IS_ALIGNED(dst_width, 16)) {
  74. ScaleRowDown2 =
  75. filtering == kFilterNone
  76. ? ScaleRowDown2_SSSE3
  77. : (filtering == kFilterLinear ? ScaleRowDown2Linear_SSSE3
  78. : ScaleRowDown2Box_SSSE3);
  79. }
  80. }
  81. #endif
  82. #if defined(HAS_SCALEROWDOWN2_AVX2)
  83. if (TestCpuFlag(kCpuHasAVX2)) {
  84. ScaleRowDown2 =
  85. filtering == kFilterNone
  86. ? ScaleRowDown2_Any_AVX2
  87. : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_AVX2
  88. : ScaleRowDown2Box_Any_AVX2);
  89. if (IS_ALIGNED(dst_width, 32)) {
  90. ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_AVX2
  91. : (filtering == kFilterLinear
  92. ? ScaleRowDown2Linear_AVX2
  93. : ScaleRowDown2Box_AVX2);
  94. }
  95. }
  96. #endif
  97. #if defined(HAS_SCALEROWDOWN2_MMI)
  98. if (TestCpuFlag(kCpuHasMMI)) {
  99. ScaleRowDown2 =
  100. filtering == kFilterNone
  101. ? ScaleRowDown2_Any_MMI
  102. : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_MMI
  103. : ScaleRowDown2Box_Any_MMI);
  104. if (IS_ALIGNED(dst_width, 8)) {
  105. ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_MMI
  106. : (filtering == kFilterLinear
  107. ? ScaleRowDown2Linear_MMI
  108. : ScaleRowDown2Box_MMI);
  109. }
  110. }
  111. #endif
  112. #if defined(HAS_SCALEROWDOWN2_MSA)
  113. if (TestCpuFlag(kCpuHasMSA)) {
  114. ScaleRowDown2 =
  115. filtering == kFilterNone
  116. ? ScaleRowDown2_Any_MSA
  117. : (filtering == kFilterLinear ? ScaleRowDown2Linear_Any_MSA
  118. : ScaleRowDown2Box_Any_MSA);
  119. if (IS_ALIGNED(dst_width, 32)) {
  120. ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_MSA
  121. : (filtering == kFilterLinear
  122. ? ScaleRowDown2Linear_MSA
  123. : ScaleRowDown2Box_MSA);
  124. }
  125. }
  126. #endif
  127. if (filtering == kFilterLinear) {
  128. src_stride = 0;
  129. }
  130. // TODO(fbarchard): Loop through source height to allow odd height.
  131. for (y = 0; y < dst_height; ++y) {
  132. ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width);
  133. src_ptr += row_stride;
  134. dst_ptr += dst_stride;
  135. }
  136. }
  137. static void ScalePlaneDown2_16(int src_width,
  138. int src_height,
  139. int dst_width,
  140. int dst_height,
  141. int src_stride,
  142. int dst_stride,
  143. const uint16_t* src_ptr,
  144. uint16_t* dst_ptr,
  145. enum FilterMode filtering) {
  146. int y;
  147. void (*ScaleRowDown2)(const uint16_t* src_ptr, ptrdiff_t src_stride,
  148. uint16_t* dst_ptr, int dst_width) =
  149. filtering == kFilterNone
  150. ? ScaleRowDown2_16_C
  151. : (filtering == kFilterLinear ? ScaleRowDown2Linear_16_C
  152. : ScaleRowDown2Box_16_C);
  153. int row_stride = src_stride << 1;
  154. (void)src_width;
  155. (void)src_height;
  156. if (!filtering) {
  157. src_ptr += src_stride; // Point to odd rows.
  158. src_stride = 0;
  159. }
  160. #if defined(HAS_SCALEROWDOWN2_16_NEON)
  161. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 16)) {
  162. ScaleRowDown2 =
  163. filtering ? ScaleRowDown2Box_16_NEON : ScaleRowDown2_16_NEON;
  164. }
  165. #endif
  166. #if defined(HAS_SCALEROWDOWN2_16_SSE2)
  167. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 16)) {
  168. ScaleRowDown2 =
  169. filtering == kFilterNone
  170. ? ScaleRowDown2_16_SSE2
  171. : (filtering == kFilterLinear ? ScaleRowDown2Linear_16_SSE2
  172. : ScaleRowDown2Box_16_SSE2);
  173. }
  174. #endif
  175. #if defined(HAS_SCALEROWDOWN2_16_MMI)
  176. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(dst_width, 4)) {
  177. ScaleRowDown2 = filtering == kFilterNone ? ScaleRowDown2_16_MMI
  178. : (filtering == kFilterLinear
  179. ? ScaleRowDown2Linear_16_MMI
  180. : ScaleRowDown2Box_16_MMI);
  181. }
  182. #endif
  183. if (filtering == kFilterLinear) {
  184. src_stride = 0;
  185. }
  186. // TODO(fbarchard): Loop through source height to allow odd height.
  187. for (y = 0; y < dst_height; ++y) {
  188. ScaleRowDown2(src_ptr, src_stride, dst_ptr, dst_width);
  189. src_ptr += row_stride;
  190. dst_ptr += dst_stride;
  191. }
  192. }
  193. // Scale plane, 1/4
  194. // This is an optimized version for scaling down a plane to 1/4 of
  195. // its original size.
  196. static void ScalePlaneDown4(int src_width,
  197. int src_height,
  198. int dst_width,
  199. int dst_height,
  200. int src_stride,
  201. int dst_stride,
  202. const uint8_t* src_ptr,
  203. uint8_t* dst_ptr,
  204. enum FilterMode filtering) {
  205. int y;
  206. void (*ScaleRowDown4)(const uint8_t* src_ptr, ptrdiff_t src_stride,
  207. uint8_t* dst_ptr, int dst_width) =
  208. filtering ? ScaleRowDown4Box_C : ScaleRowDown4_C;
  209. int row_stride = src_stride << 2;
  210. (void)src_width;
  211. (void)src_height;
  212. if (!filtering) {
  213. src_ptr += src_stride * 2; // Point to row 2.
  214. src_stride = 0;
  215. }
  216. #if defined(HAS_SCALEROWDOWN4_NEON)
  217. if (TestCpuFlag(kCpuHasNEON)) {
  218. ScaleRowDown4 =
  219. filtering ? ScaleRowDown4Box_Any_NEON : ScaleRowDown4_Any_NEON;
  220. if (IS_ALIGNED(dst_width, 8)) {
  221. ScaleRowDown4 = filtering ? ScaleRowDown4Box_NEON : ScaleRowDown4_NEON;
  222. }
  223. }
  224. #endif
  225. #if defined(HAS_SCALEROWDOWN4_SSSE3)
  226. if (TestCpuFlag(kCpuHasSSSE3)) {
  227. ScaleRowDown4 =
  228. filtering ? ScaleRowDown4Box_Any_SSSE3 : ScaleRowDown4_Any_SSSE3;
  229. if (IS_ALIGNED(dst_width, 8)) {
  230. ScaleRowDown4 = filtering ? ScaleRowDown4Box_SSSE3 : ScaleRowDown4_SSSE3;
  231. }
  232. }
  233. #endif
  234. #if defined(HAS_SCALEROWDOWN4_AVX2)
  235. if (TestCpuFlag(kCpuHasAVX2)) {
  236. ScaleRowDown4 =
  237. filtering ? ScaleRowDown4Box_Any_AVX2 : ScaleRowDown4_Any_AVX2;
  238. if (IS_ALIGNED(dst_width, 16)) {
  239. ScaleRowDown4 = filtering ? ScaleRowDown4Box_AVX2 : ScaleRowDown4_AVX2;
  240. }
  241. }
  242. #endif
  243. #if defined(HAS_SCALEROWDOWN4_MMI)
  244. if (TestCpuFlag(kCpuHasMMI)) {
  245. ScaleRowDown4 =
  246. filtering ? ScaleRowDown4Box_Any_MMI : ScaleRowDown4_Any_MMI;
  247. if (IS_ALIGNED(dst_width, 8)) {
  248. ScaleRowDown4 = filtering ? ScaleRowDown4Box_MMI : ScaleRowDown4_MMI;
  249. }
  250. }
  251. #endif
  252. #if defined(HAS_SCALEROWDOWN4_MSA)
  253. if (TestCpuFlag(kCpuHasMSA)) {
  254. ScaleRowDown4 =
  255. filtering ? ScaleRowDown4Box_Any_MSA : ScaleRowDown4_Any_MSA;
  256. if (IS_ALIGNED(dst_width, 16)) {
  257. ScaleRowDown4 = filtering ? ScaleRowDown4Box_MSA : ScaleRowDown4_MSA;
  258. }
  259. }
  260. #endif
  261. if (filtering == kFilterLinear) {
  262. src_stride = 0;
  263. }
  264. for (y = 0; y < dst_height; ++y) {
  265. ScaleRowDown4(src_ptr, src_stride, dst_ptr, dst_width);
  266. src_ptr += row_stride;
  267. dst_ptr += dst_stride;
  268. }
  269. }
  270. static void ScalePlaneDown4_16(int src_width,
  271. int src_height,
  272. int dst_width,
  273. int dst_height,
  274. int src_stride,
  275. int dst_stride,
  276. const uint16_t* src_ptr,
  277. uint16_t* dst_ptr,
  278. enum FilterMode filtering) {
  279. int y;
  280. void (*ScaleRowDown4)(const uint16_t* src_ptr, ptrdiff_t src_stride,
  281. uint16_t* dst_ptr, int dst_width) =
  282. filtering ? ScaleRowDown4Box_16_C : ScaleRowDown4_16_C;
  283. int row_stride = src_stride << 2;
  284. (void)src_width;
  285. (void)src_height;
  286. if (!filtering) {
  287. src_ptr += src_stride * 2; // Point to row 2.
  288. src_stride = 0;
  289. }
  290. #if defined(HAS_SCALEROWDOWN4_16_NEON)
  291. if (TestCpuFlag(kCpuHasNEON) && IS_ALIGNED(dst_width, 8)) {
  292. ScaleRowDown4 =
  293. filtering ? ScaleRowDown4Box_16_NEON : ScaleRowDown4_16_NEON;
  294. }
  295. #endif
  296. #if defined(HAS_SCALEROWDOWN4_16_SSE2)
  297. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) {
  298. ScaleRowDown4 =
  299. filtering ? ScaleRowDown4Box_16_SSE2 : ScaleRowDown4_16_SSE2;
  300. }
  301. #endif
  302. #if defined(HAS_SCALEROWDOWN4_16_MMI)
  303. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(dst_width, 8)) {
  304. ScaleRowDown4 = filtering ? ScaleRowDown4Box_16_MMI : ScaleRowDown4_16_MMI;
  305. }
  306. #endif
  307. if (filtering == kFilterLinear) {
  308. src_stride = 0;
  309. }
  310. for (y = 0; y < dst_height; ++y) {
  311. ScaleRowDown4(src_ptr, src_stride, dst_ptr, dst_width);
  312. src_ptr += row_stride;
  313. dst_ptr += dst_stride;
  314. }
  315. }
  316. // Scale plane down, 3/4
  317. static void ScalePlaneDown34(int src_width,
  318. int src_height,
  319. int dst_width,
  320. int dst_height,
  321. int src_stride,
  322. int dst_stride,
  323. const uint8_t* src_ptr,
  324. uint8_t* dst_ptr,
  325. enum FilterMode filtering) {
  326. int y;
  327. void (*ScaleRowDown34_0)(const uint8_t* src_ptr, ptrdiff_t src_stride,
  328. uint8_t* dst_ptr, int dst_width);
  329. void (*ScaleRowDown34_1)(const uint8_t* src_ptr, ptrdiff_t src_stride,
  330. uint8_t* dst_ptr, int dst_width);
  331. const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride;
  332. (void)src_width;
  333. (void)src_height;
  334. assert(dst_width % 3 == 0);
  335. if (!filtering) {
  336. ScaleRowDown34_0 = ScaleRowDown34_C;
  337. ScaleRowDown34_1 = ScaleRowDown34_C;
  338. } else {
  339. ScaleRowDown34_0 = ScaleRowDown34_0_Box_C;
  340. ScaleRowDown34_1 = ScaleRowDown34_1_Box_C;
  341. }
  342. #if defined(HAS_SCALEROWDOWN34_NEON)
  343. if (TestCpuFlag(kCpuHasNEON)) {
  344. if (!filtering) {
  345. ScaleRowDown34_0 = ScaleRowDown34_Any_NEON;
  346. ScaleRowDown34_1 = ScaleRowDown34_Any_NEON;
  347. } else {
  348. ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_NEON;
  349. ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_NEON;
  350. }
  351. if (dst_width % 24 == 0) {
  352. if (!filtering) {
  353. ScaleRowDown34_0 = ScaleRowDown34_NEON;
  354. ScaleRowDown34_1 = ScaleRowDown34_NEON;
  355. } else {
  356. ScaleRowDown34_0 = ScaleRowDown34_0_Box_NEON;
  357. ScaleRowDown34_1 = ScaleRowDown34_1_Box_NEON;
  358. }
  359. }
  360. }
  361. #endif
  362. #if defined(HAS_SCALEROWDOWN34_MMI)
  363. if (TestCpuFlag(kCpuHasMMI)) {
  364. if (!filtering) {
  365. ScaleRowDown34_0 = ScaleRowDown34_Any_MMI;
  366. ScaleRowDown34_1 = ScaleRowDown34_Any_MMI;
  367. if (dst_width % 24 == 0) {
  368. ScaleRowDown34_0 = ScaleRowDown34_MMI;
  369. ScaleRowDown34_1 = ScaleRowDown34_MMI;
  370. }
  371. }
  372. }
  373. #endif
  374. #if defined(HAS_SCALEROWDOWN34_MSA)
  375. if (TestCpuFlag(kCpuHasMSA)) {
  376. if (!filtering) {
  377. ScaleRowDown34_0 = ScaleRowDown34_Any_MSA;
  378. ScaleRowDown34_1 = ScaleRowDown34_Any_MSA;
  379. } else {
  380. ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_MSA;
  381. ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_MSA;
  382. }
  383. if (dst_width % 48 == 0) {
  384. if (!filtering) {
  385. ScaleRowDown34_0 = ScaleRowDown34_MSA;
  386. ScaleRowDown34_1 = ScaleRowDown34_MSA;
  387. } else {
  388. ScaleRowDown34_0 = ScaleRowDown34_0_Box_MSA;
  389. ScaleRowDown34_1 = ScaleRowDown34_1_Box_MSA;
  390. }
  391. }
  392. }
  393. #endif
  394. #if defined(HAS_SCALEROWDOWN34_SSSE3)
  395. if (TestCpuFlag(kCpuHasSSSE3)) {
  396. if (!filtering) {
  397. ScaleRowDown34_0 = ScaleRowDown34_Any_SSSE3;
  398. ScaleRowDown34_1 = ScaleRowDown34_Any_SSSE3;
  399. } else {
  400. ScaleRowDown34_0 = ScaleRowDown34_0_Box_Any_SSSE3;
  401. ScaleRowDown34_1 = ScaleRowDown34_1_Box_Any_SSSE3;
  402. }
  403. if (dst_width % 24 == 0) {
  404. if (!filtering) {
  405. ScaleRowDown34_0 = ScaleRowDown34_SSSE3;
  406. ScaleRowDown34_1 = ScaleRowDown34_SSSE3;
  407. } else {
  408. ScaleRowDown34_0 = ScaleRowDown34_0_Box_SSSE3;
  409. ScaleRowDown34_1 = ScaleRowDown34_1_Box_SSSE3;
  410. }
  411. }
  412. }
  413. #endif
  414. for (y = 0; y < dst_height - 2; y += 3) {
  415. ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width);
  416. src_ptr += src_stride;
  417. dst_ptr += dst_stride;
  418. ScaleRowDown34_1(src_ptr, filter_stride, dst_ptr, dst_width);
  419. src_ptr += src_stride;
  420. dst_ptr += dst_stride;
  421. ScaleRowDown34_0(src_ptr + src_stride, -filter_stride, dst_ptr, dst_width);
  422. src_ptr += src_stride * 2;
  423. dst_ptr += dst_stride;
  424. }
  425. // Remainder 1 or 2 rows with last row vertically unfiltered
  426. if ((dst_height % 3) == 2) {
  427. ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width);
  428. src_ptr += src_stride;
  429. dst_ptr += dst_stride;
  430. ScaleRowDown34_1(src_ptr, 0, dst_ptr, dst_width);
  431. } else if ((dst_height % 3) == 1) {
  432. ScaleRowDown34_0(src_ptr, 0, dst_ptr, dst_width);
  433. }
  434. }
  435. static void ScalePlaneDown34_16(int src_width,
  436. int src_height,
  437. int dst_width,
  438. int dst_height,
  439. int src_stride,
  440. int dst_stride,
  441. const uint16_t* src_ptr,
  442. uint16_t* dst_ptr,
  443. enum FilterMode filtering) {
  444. int y;
  445. void (*ScaleRowDown34_0)(const uint16_t* src_ptr, ptrdiff_t src_stride,
  446. uint16_t* dst_ptr, int dst_width);
  447. void (*ScaleRowDown34_1)(const uint16_t* src_ptr, ptrdiff_t src_stride,
  448. uint16_t* dst_ptr, int dst_width);
  449. const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride;
  450. (void)src_width;
  451. (void)src_height;
  452. assert(dst_width % 3 == 0);
  453. if (!filtering) {
  454. ScaleRowDown34_0 = ScaleRowDown34_16_C;
  455. ScaleRowDown34_1 = ScaleRowDown34_16_C;
  456. } else {
  457. ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_C;
  458. ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_C;
  459. }
  460. #if defined(HAS_SCALEROWDOWN34_16_NEON)
  461. if (TestCpuFlag(kCpuHasNEON) && (dst_width % 24 == 0)) {
  462. if (!filtering) {
  463. ScaleRowDown34_0 = ScaleRowDown34_16_NEON;
  464. ScaleRowDown34_1 = ScaleRowDown34_16_NEON;
  465. } else {
  466. ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_NEON;
  467. ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_NEON;
  468. }
  469. }
  470. #endif
  471. #if defined(HAS_SCALEROWDOWN34_16_SSSE3)
  472. if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0)) {
  473. if (!filtering) {
  474. ScaleRowDown34_0 = ScaleRowDown34_16_SSSE3;
  475. ScaleRowDown34_1 = ScaleRowDown34_16_SSSE3;
  476. } else {
  477. ScaleRowDown34_0 = ScaleRowDown34_0_Box_16_SSSE3;
  478. ScaleRowDown34_1 = ScaleRowDown34_1_Box_16_SSSE3;
  479. }
  480. }
  481. #endif
  482. for (y = 0; y < dst_height - 2; y += 3) {
  483. ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width);
  484. src_ptr += src_stride;
  485. dst_ptr += dst_stride;
  486. ScaleRowDown34_1(src_ptr, filter_stride, dst_ptr, dst_width);
  487. src_ptr += src_stride;
  488. dst_ptr += dst_stride;
  489. ScaleRowDown34_0(src_ptr + src_stride, -filter_stride, dst_ptr, dst_width);
  490. src_ptr += src_stride * 2;
  491. dst_ptr += dst_stride;
  492. }
  493. // Remainder 1 or 2 rows with last row vertically unfiltered
  494. if ((dst_height % 3) == 2) {
  495. ScaleRowDown34_0(src_ptr, filter_stride, dst_ptr, dst_width);
  496. src_ptr += src_stride;
  497. dst_ptr += dst_stride;
  498. ScaleRowDown34_1(src_ptr, 0, dst_ptr, dst_width);
  499. } else if ((dst_height % 3) == 1) {
  500. ScaleRowDown34_0(src_ptr, 0, dst_ptr, dst_width);
  501. }
  502. }
  503. // Scale plane, 3/8
  504. // This is an optimized version for scaling down a plane to 3/8
  505. // of its original size.
  506. //
  507. // Uses box filter arranges like this
  508. // aaabbbcc -> abc
  509. // aaabbbcc def
  510. // aaabbbcc ghi
  511. // dddeeeff
  512. // dddeeeff
  513. // dddeeeff
  514. // ggghhhii
  515. // ggghhhii
  516. // Boxes are 3x3, 2x3, 3x2 and 2x2
  517. static void ScalePlaneDown38(int src_width,
  518. int src_height,
  519. int dst_width,
  520. int dst_height,
  521. int src_stride,
  522. int dst_stride,
  523. const uint8_t* src_ptr,
  524. uint8_t* dst_ptr,
  525. enum FilterMode filtering) {
  526. int y;
  527. void (*ScaleRowDown38_3)(const uint8_t* src_ptr, ptrdiff_t src_stride,
  528. uint8_t* dst_ptr, int dst_width);
  529. void (*ScaleRowDown38_2)(const uint8_t* src_ptr, ptrdiff_t src_stride,
  530. uint8_t* dst_ptr, int dst_width);
  531. const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride;
  532. assert(dst_width % 3 == 0);
  533. (void)src_width;
  534. (void)src_height;
  535. if (!filtering) {
  536. ScaleRowDown38_3 = ScaleRowDown38_C;
  537. ScaleRowDown38_2 = ScaleRowDown38_C;
  538. } else {
  539. ScaleRowDown38_3 = ScaleRowDown38_3_Box_C;
  540. ScaleRowDown38_2 = ScaleRowDown38_2_Box_C;
  541. }
  542. #if defined(HAS_SCALEROWDOWN38_NEON)
  543. if (TestCpuFlag(kCpuHasNEON)) {
  544. if (!filtering) {
  545. ScaleRowDown38_3 = ScaleRowDown38_Any_NEON;
  546. ScaleRowDown38_2 = ScaleRowDown38_Any_NEON;
  547. } else {
  548. ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_NEON;
  549. ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_NEON;
  550. }
  551. if (dst_width % 12 == 0) {
  552. if (!filtering) {
  553. ScaleRowDown38_3 = ScaleRowDown38_NEON;
  554. ScaleRowDown38_2 = ScaleRowDown38_NEON;
  555. } else {
  556. ScaleRowDown38_3 = ScaleRowDown38_3_Box_NEON;
  557. ScaleRowDown38_2 = ScaleRowDown38_2_Box_NEON;
  558. }
  559. }
  560. }
  561. #endif
  562. #if defined(HAS_SCALEROWDOWN38_SSSE3)
  563. if (TestCpuFlag(kCpuHasSSSE3)) {
  564. if (!filtering) {
  565. ScaleRowDown38_3 = ScaleRowDown38_Any_SSSE3;
  566. ScaleRowDown38_2 = ScaleRowDown38_Any_SSSE3;
  567. } else {
  568. ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_SSSE3;
  569. ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_SSSE3;
  570. }
  571. if (dst_width % 12 == 0 && !filtering) {
  572. ScaleRowDown38_3 = ScaleRowDown38_SSSE3;
  573. ScaleRowDown38_2 = ScaleRowDown38_SSSE3;
  574. }
  575. if (dst_width % 6 == 0 && filtering) {
  576. ScaleRowDown38_3 = ScaleRowDown38_3_Box_SSSE3;
  577. ScaleRowDown38_2 = ScaleRowDown38_2_Box_SSSE3;
  578. }
  579. }
  580. #endif
  581. #if defined(HAS_SCALEROWDOWN38_MSA)
  582. if (TestCpuFlag(kCpuHasMSA)) {
  583. if (!filtering) {
  584. ScaleRowDown38_3 = ScaleRowDown38_Any_MSA;
  585. ScaleRowDown38_2 = ScaleRowDown38_Any_MSA;
  586. } else {
  587. ScaleRowDown38_3 = ScaleRowDown38_3_Box_Any_MSA;
  588. ScaleRowDown38_2 = ScaleRowDown38_2_Box_Any_MSA;
  589. }
  590. if (dst_width % 12 == 0) {
  591. if (!filtering) {
  592. ScaleRowDown38_3 = ScaleRowDown38_MSA;
  593. ScaleRowDown38_2 = ScaleRowDown38_MSA;
  594. } else {
  595. ScaleRowDown38_3 = ScaleRowDown38_3_Box_MSA;
  596. ScaleRowDown38_2 = ScaleRowDown38_2_Box_MSA;
  597. }
  598. }
  599. }
  600. #endif
  601. for (y = 0; y < dst_height - 2; y += 3) {
  602. ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width);
  603. src_ptr += src_stride * 3;
  604. dst_ptr += dst_stride;
  605. ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width);
  606. src_ptr += src_stride * 3;
  607. dst_ptr += dst_stride;
  608. ScaleRowDown38_2(src_ptr, filter_stride, dst_ptr, dst_width);
  609. src_ptr += src_stride * 2;
  610. dst_ptr += dst_stride;
  611. }
  612. // Remainder 1 or 2 rows with last row vertically unfiltered
  613. if ((dst_height % 3) == 2) {
  614. ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width);
  615. src_ptr += src_stride * 3;
  616. dst_ptr += dst_stride;
  617. ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width);
  618. } else if ((dst_height % 3) == 1) {
  619. ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width);
  620. }
  621. }
  622. static void ScalePlaneDown38_16(int src_width,
  623. int src_height,
  624. int dst_width,
  625. int dst_height,
  626. int src_stride,
  627. int dst_stride,
  628. const uint16_t* src_ptr,
  629. uint16_t* dst_ptr,
  630. enum FilterMode filtering) {
  631. int y;
  632. void (*ScaleRowDown38_3)(const uint16_t* src_ptr, ptrdiff_t src_stride,
  633. uint16_t* dst_ptr, int dst_width);
  634. void (*ScaleRowDown38_2)(const uint16_t* src_ptr, ptrdiff_t src_stride,
  635. uint16_t* dst_ptr, int dst_width);
  636. const int filter_stride = (filtering == kFilterLinear) ? 0 : src_stride;
  637. (void)src_width;
  638. (void)src_height;
  639. assert(dst_width % 3 == 0);
  640. if (!filtering) {
  641. ScaleRowDown38_3 = ScaleRowDown38_16_C;
  642. ScaleRowDown38_2 = ScaleRowDown38_16_C;
  643. } else {
  644. ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_C;
  645. ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_C;
  646. }
  647. #if defined(HAS_SCALEROWDOWN38_16_NEON)
  648. if (TestCpuFlag(kCpuHasNEON) && (dst_width % 12 == 0)) {
  649. if (!filtering) {
  650. ScaleRowDown38_3 = ScaleRowDown38_16_NEON;
  651. ScaleRowDown38_2 = ScaleRowDown38_16_NEON;
  652. } else {
  653. ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_NEON;
  654. ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_NEON;
  655. }
  656. }
  657. #endif
  658. #if defined(HAS_SCALEROWDOWN38_16_SSSE3)
  659. if (TestCpuFlag(kCpuHasSSSE3) && (dst_width % 24 == 0)) {
  660. if (!filtering) {
  661. ScaleRowDown38_3 = ScaleRowDown38_16_SSSE3;
  662. ScaleRowDown38_2 = ScaleRowDown38_16_SSSE3;
  663. } else {
  664. ScaleRowDown38_3 = ScaleRowDown38_3_Box_16_SSSE3;
  665. ScaleRowDown38_2 = ScaleRowDown38_2_Box_16_SSSE3;
  666. }
  667. }
  668. #endif
  669. for (y = 0; y < dst_height - 2; y += 3) {
  670. ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width);
  671. src_ptr += src_stride * 3;
  672. dst_ptr += dst_stride;
  673. ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width);
  674. src_ptr += src_stride * 3;
  675. dst_ptr += dst_stride;
  676. ScaleRowDown38_2(src_ptr, filter_stride, dst_ptr, dst_width);
  677. src_ptr += src_stride * 2;
  678. dst_ptr += dst_stride;
  679. }
  680. // Remainder 1 or 2 rows with last row vertically unfiltered
  681. if ((dst_height % 3) == 2) {
  682. ScaleRowDown38_3(src_ptr, filter_stride, dst_ptr, dst_width);
  683. src_ptr += src_stride * 3;
  684. dst_ptr += dst_stride;
  685. ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width);
  686. } else if ((dst_height % 3) == 1) {
  687. ScaleRowDown38_3(src_ptr, 0, dst_ptr, dst_width);
  688. }
  689. }
  690. #define MIN1(x) ((x) < 1 ? 1 : (x))
  691. static __inline uint32_t SumPixels(int iboxwidth, const uint16_t* src_ptr) {
  692. uint32_t sum = 0u;
  693. int x;
  694. assert(iboxwidth > 0);
  695. for (x = 0; x < iboxwidth; ++x) {
  696. sum += src_ptr[x];
  697. }
  698. return sum;
  699. }
  700. static __inline uint32_t SumPixels_16(int iboxwidth, const uint32_t* src_ptr) {
  701. uint32_t sum = 0u;
  702. int x;
  703. assert(iboxwidth > 0);
  704. for (x = 0; x < iboxwidth; ++x) {
  705. sum += src_ptr[x];
  706. }
  707. return sum;
  708. }
  709. static void ScaleAddCols2_C(int dst_width,
  710. int boxheight,
  711. int x,
  712. int dx,
  713. const uint16_t* src_ptr,
  714. uint8_t* dst_ptr) {
  715. int i;
  716. int scaletbl[2];
  717. int minboxwidth = dx >> 16;
  718. int boxwidth;
  719. scaletbl[0] = 65536 / (MIN1(minboxwidth) * boxheight);
  720. scaletbl[1] = 65536 / (MIN1(minboxwidth + 1) * boxheight);
  721. for (i = 0; i < dst_width; ++i) {
  722. int ix = x >> 16;
  723. x += dx;
  724. boxwidth = MIN1((x >> 16) - ix);
  725. *dst_ptr++ =
  726. SumPixels(boxwidth, src_ptr + ix) * scaletbl[boxwidth - minboxwidth] >>
  727. 16;
  728. }
  729. }
  730. static void ScaleAddCols2_16_C(int dst_width,
  731. int boxheight,
  732. int x,
  733. int dx,
  734. const uint32_t* src_ptr,
  735. uint16_t* dst_ptr) {
  736. int i;
  737. int scaletbl[2];
  738. int minboxwidth = dx >> 16;
  739. int boxwidth;
  740. scaletbl[0] = 65536 / (MIN1(minboxwidth) * boxheight);
  741. scaletbl[1] = 65536 / (MIN1(minboxwidth + 1) * boxheight);
  742. for (i = 0; i < dst_width; ++i) {
  743. int ix = x >> 16;
  744. x += dx;
  745. boxwidth = MIN1((x >> 16) - ix);
  746. *dst_ptr++ = SumPixels_16(boxwidth, src_ptr + ix) *
  747. scaletbl[boxwidth - minboxwidth] >>
  748. 16;
  749. }
  750. }
  751. static void ScaleAddCols0_C(int dst_width,
  752. int boxheight,
  753. int x,
  754. int dx,
  755. const uint16_t* src_ptr,
  756. uint8_t* dst_ptr) {
  757. int scaleval = 65536 / boxheight;
  758. int i;
  759. (void)dx;
  760. src_ptr += (x >> 16);
  761. for (i = 0; i < dst_width; ++i) {
  762. *dst_ptr++ = src_ptr[i] * scaleval >> 16;
  763. }
  764. }
  765. static void ScaleAddCols1_C(int dst_width,
  766. int boxheight,
  767. int x,
  768. int dx,
  769. const uint16_t* src_ptr,
  770. uint8_t* dst_ptr) {
  771. int boxwidth = MIN1(dx >> 16);
  772. int scaleval = 65536 / (boxwidth * boxheight);
  773. int i;
  774. x >>= 16;
  775. for (i = 0; i < dst_width; ++i) {
  776. *dst_ptr++ = SumPixels(boxwidth, src_ptr + x) * scaleval >> 16;
  777. x += boxwidth;
  778. }
  779. }
  780. static void ScaleAddCols1_16_C(int dst_width,
  781. int boxheight,
  782. int x,
  783. int dx,
  784. const uint32_t* src_ptr,
  785. uint16_t* dst_ptr) {
  786. int boxwidth = MIN1(dx >> 16);
  787. int scaleval = 65536 / (boxwidth * boxheight);
  788. int i;
  789. for (i = 0; i < dst_width; ++i) {
  790. *dst_ptr++ = SumPixels_16(boxwidth, src_ptr + x) * scaleval >> 16;
  791. x += boxwidth;
  792. }
  793. }
  794. // Scale plane down to any dimensions, with interpolation.
  795. // (boxfilter).
  796. //
  797. // Same method as SimpleScale, which is fixed point, outputting
  798. // one pixel of destination using fixed point (16.16) to step
  799. // through source, sampling a box of pixel with simple
  800. // averaging.
  801. static void ScalePlaneBox(int src_width,
  802. int src_height,
  803. int dst_width,
  804. int dst_height,
  805. int src_stride,
  806. int dst_stride,
  807. const uint8_t* src_ptr,
  808. uint8_t* dst_ptr) {
  809. int j, k;
  810. // Initial source x/y coordinate and step values as 16.16 fixed point.
  811. int x = 0;
  812. int y = 0;
  813. int dx = 0;
  814. int dy = 0;
  815. const int max_y = (src_height << 16);
  816. ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterBox, &x, &y,
  817. &dx, &dy);
  818. src_width = Abs(src_width);
  819. {
  820. // Allocate a row buffer of uint16_t.
  821. align_buffer_64(row16, src_width * 2);
  822. void (*ScaleAddCols)(int dst_width, int boxheight, int x, int dx,
  823. const uint16_t* src_ptr, uint8_t* dst_ptr) =
  824. (dx & 0xffff) ? ScaleAddCols2_C
  825. : ((dx != 0x10000) ? ScaleAddCols1_C : ScaleAddCols0_C);
  826. void (*ScaleAddRow)(const uint8_t* src_ptr, uint16_t* dst_ptr,
  827. int src_width) = ScaleAddRow_C;
  828. #if defined(HAS_SCALEADDROW_SSE2)
  829. if (TestCpuFlag(kCpuHasSSE2)) {
  830. ScaleAddRow = ScaleAddRow_Any_SSE2;
  831. if (IS_ALIGNED(src_width, 16)) {
  832. ScaleAddRow = ScaleAddRow_SSE2;
  833. }
  834. }
  835. #endif
  836. #if defined(HAS_SCALEADDROW_AVX2)
  837. if (TestCpuFlag(kCpuHasAVX2)) {
  838. ScaleAddRow = ScaleAddRow_Any_AVX2;
  839. if (IS_ALIGNED(src_width, 32)) {
  840. ScaleAddRow = ScaleAddRow_AVX2;
  841. }
  842. }
  843. #endif
  844. #if defined(HAS_SCALEADDROW_NEON)
  845. if (TestCpuFlag(kCpuHasNEON)) {
  846. ScaleAddRow = ScaleAddRow_Any_NEON;
  847. if (IS_ALIGNED(src_width, 16)) {
  848. ScaleAddRow = ScaleAddRow_NEON;
  849. }
  850. }
  851. #endif
  852. #if defined(HAS_SCALEADDROW_MMI)
  853. if (TestCpuFlag(kCpuHasMMI)) {
  854. ScaleAddRow = ScaleAddRow_Any_MMI;
  855. if (IS_ALIGNED(src_width, 8)) {
  856. ScaleAddRow = ScaleAddRow_MMI;
  857. }
  858. }
  859. #endif
  860. #if defined(HAS_SCALEADDROW_MSA)
  861. if (TestCpuFlag(kCpuHasMSA)) {
  862. ScaleAddRow = ScaleAddRow_Any_MSA;
  863. if (IS_ALIGNED(src_width, 16)) {
  864. ScaleAddRow = ScaleAddRow_MSA;
  865. }
  866. }
  867. #endif
  868. for (j = 0; j < dst_height; ++j) {
  869. int boxheight;
  870. int iy = y >> 16;
  871. const uint8_t* src = src_ptr + iy * src_stride;
  872. y += dy;
  873. if (y > max_y) {
  874. y = max_y;
  875. }
  876. boxheight = MIN1((y >> 16) - iy);
  877. memset(row16, 0, src_width * 2);
  878. for (k = 0; k < boxheight; ++k) {
  879. ScaleAddRow(src, (uint16_t*)(row16), src_width);
  880. src += src_stride;
  881. }
  882. ScaleAddCols(dst_width, boxheight, x, dx, (uint16_t*)(row16), dst_ptr);
  883. dst_ptr += dst_stride;
  884. }
  885. free_aligned_buffer_64(row16);
  886. }
  887. }
  888. static void ScalePlaneBox_16(int src_width,
  889. int src_height,
  890. int dst_width,
  891. int dst_height,
  892. int src_stride,
  893. int dst_stride,
  894. const uint16_t* src_ptr,
  895. uint16_t* dst_ptr) {
  896. int j, k;
  897. // Initial source x/y coordinate and step values as 16.16 fixed point.
  898. int x = 0;
  899. int y = 0;
  900. int dx = 0;
  901. int dy = 0;
  902. const int max_y = (src_height << 16);
  903. ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterBox, &x, &y,
  904. &dx, &dy);
  905. src_width = Abs(src_width);
  906. {
  907. // Allocate a row buffer of uint32_t.
  908. align_buffer_64(row32, src_width * 4);
  909. void (*ScaleAddCols)(int dst_width, int boxheight, int x, int dx,
  910. const uint32_t* src_ptr, uint16_t* dst_ptr) =
  911. (dx & 0xffff) ? ScaleAddCols2_16_C : ScaleAddCols1_16_C;
  912. void (*ScaleAddRow)(const uint16_t* src_ptr, uint32_t* dst_ptr,
  913. int src_width) = ScaleAddRow_16_C;
  914. #if defined(HAS_SCALEADDROW_16_SSE2)
  915. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(src_width, 16)) {
  916. ScaleAddRow = ScaleAddRow_16_SSE2;
  917. }
  918. #endif
  919. #if defined(HAS_SCALEADDROW_16_MMI)
  920. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(src_width, 4)) {
  921. ScaleAddRow = ScaleAddRow_16_MMI;
  922. }
  923. #endif
  924. for (j = 0; j < dst_height; ++j) {
  925. int boxheight;
  926. int iy = y >> 16;
  927. const uint16_t* src = src_ptr + iy * src_stride;
  928. y += dy;
  929. if (y > max_y) {
  930. y = max_y;
  931. }
  932. boxheight = MIN1((y >> 16) - iy);
  933. memset(row32, 0, src_width * 4);
  934. for (k = 0; k < boxheight; ++k) {
  935. ScaleAddRow(src, (uint32_t*)(row32), src_width);
  936. src += src_stride;
  937. }
  938. ScaleAddCols(dst_width, boxheight, x, dx, (uint32_t*)(row32), dst_ptr);
  939. dst_ptr += dst_stride;
  940. }
  941. free_aligned_buffer_64(row32);
  942. }
  943. }
  944. // Scale plane down with bilinear interpolation.
  945. void ScalePlaneBilinearDown(int src_width,
  946. int src_height,
  947. int dst_width,
  948. int dst_height,
  949. int src_stride,
  950. int dst_stride,
  951. const uint8_t* src_ptr,
  952. uint8_t* dst_ptr,
  953. enum FilterMode filtering) {
  954. // Initial source x/y coordinate and step values as 16.16 fixed point.
  955. int x = 0;
  956. int y = 0;
  957. int dx = 0;
  958. int dy = 0;
  959. // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear.
  960. // Allocate a row buffer.
  961. align_buffer_64(row, src_width);
  962. const int max_y = (src_height - 1) << 16;
  963. int j;
  964. void (*ScaleFilterCols)(uint8_t * dst_ptr, const uint8_t* src_ptr,
  965. int dst_width, int x, int dx) =
  966. (src_width >= 32768) ? ScaleFilterCols64_C : ScaleFilterCols_C;
  967. void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr,
  968. ptrdiff_t src_stride, int dst_width,
  969. int source_y_fraction) = InterpolateRow_C;
  970. ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y,
  971. &dx, &dy);
  972. src_width = Abs(src_width);
  973. #if defined(HAS_INTERPOLATEROW_SSSE3)
  974. if (TestCpuFlag(kCpuHasSSSE3)) {
  975. InterpolateRow = InterpolateRow_Any_SSSE3;
  976. if (IS_ALIGNED(src_width, 16)) {
  977. InterpolateRow = InterpolateRow_SSSE3;
  978. }
  979. }
  980. #endif
  981. #if defined(HAS_INTERPOLATEROW_AVX2)
  982. if (TestCpuFlag(kCpuHasAVX2)) {
  983. InterpolateRow = InterpolateRow_Any_AVX2;
  984. if (IS_ALIGNED(src_width, 32)) {
  985. InterpolateRow = InterpolateRow_AVX2;
  986. }
  987. }
  988. #endif
  989. #if defined(HAS_INTERPOLATEROW_NEON)
  990. if (TestCpuFlag(kCpuHasNEON)) {
  991. InterpolateRow = InterpolateRow_Any_NEON;
  992. if (IS_ALIGNED(src_width, 16)) {
  993. InterpolateRow = InterpolateRow_NEON;
  994. }
  995. }
  996. #endif
  997. #if defined(HAS_INTERPOLATEROW_MMI)
  998. if (TestCpuFlag(kCpuHasMMI)) {
  999. InterpolateRow = InterpolateRow_Any_MMI;
  1000. if (IS_ALIGNED(src_width, 16)) {
  1001. InterpolateRow = InterpolateRow_MMI;
  1002. }
  1003. }
  1004. #endif
  1005. #if defined(HAS_INTERPOLATEROW_MSA)
  1006. if (TestCpuFlag(kCpuHasMSA)) {
  1007. InterpolateRow = InterpolateRow_Any_MSA;
  1008. if (IS_ALIGNED(src_width, 32)) {
  1009. InterpolateRow = InterpolateRow_MSA;
  1010. }
  1011. }
  1012. #endif
  1013. #if defined(HAS_SCALEFILTERCOLS_SSSE3)
  1014. if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) {
  1015. ScaleFilterCols = ScaleFilterCols_SSSE3;
  1016. }
  1017. #endif
  1018. #if defined(HAS_SCALEFILTERCOLS_NEON)
  1019. if (TestCpuFlag(kCpuHasNEON) && src_width < 32768) {
  1020. ScaleFilterCols = ScaleFilterCols_Any_NEON;
  1021. if (IS_ALIGNED(dst_width, 8)) {
  1022. ScaleFilterCols = ScaleFilterCols_NEON;
  1023. }
  1024. }
  1025. #endif
  1026. #if defined(HAS_SCALEFILTERCOLS_MSA)
  1027. if (TestCpuFlag(kCpuHasMSA) && src_width < 32768) {
  1028. ScaleFilterCols = ScaleFilterCols_Any_MSA;
  1029. if (IS_ALIGNED(dst_width, 16)) {
  1030. ScaleFilterCols = ScaleFilterCols_MSA;
  1031. }
  1032. }
  1033. #endif
  1034. if (y > max_y) {
  1035. y = max_y;
  1036. }
  1037. for (j = 0; j < dst_height; ++j) {
  1038. int yi = y >> 16;
  1039. const uint8_t* src = src_ptr + yi * src_stride;
  1040. if (filtering == kFilterLinear) {
  1041. ScaleFilterCols(dst_ptr, src, dst_width, x, dx);
  1042. } else {
  1043. int yf = (y >> 8) & 255;
  1044. InterpolateRow(row, src, src_stride, src_width, yf);
  1045. ScaleFilterCols(dst_ptr, row, dst_width, x, dx);
  1046. }
  1047. dst_ptr += dst_stride;
  1048. y += dy;
  1049. if (y > max_y) {
  1050. y = max_y;
  1051. }
  1052. }
  1053. free_aligned_buffer_64(row);
  1054. }
  1055. void ScalePlaneBilinearDown_16(int src_width,
  1056. int src_height,
  1057. int dst_width,
  1058. int dst_height,
  1059. int src_stride,
  1060. int dst_stride,
  1061. const uint16_t* src_ptr,
  1062. uint16_t* dst_ptr,
  1063. enum FilterMode filtering) {
  1064. // Initial source x/y coordinate and step values as 16.16 fixed point.
  1065. int x = 0;
  1066. int y = 0;
  1067. int dx = 0;
  1068. int dy = 0;
  1069. // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear.
  1070. // Allocate a row buffer.
  1071. align_buffer_64(row, src_width * 2);
  1072. const int max_y = (src_height - 1) << 16;
  1073. int j;
  1074. void (*ScaleFilterCols)(uint16_t * dst_ptr, const uint16_t* src_ptr,
  1075. int dst_width, int x, int dx) =
  1076. (src_width >= 32768) ? ScaleFilterCols64_16_C : ScaleFilterCols_16_C;
  1077. void (*InterpolateRow)(uint16_t * dst_ptr, const uint16_t* src_ptr,
  1078. ptrdiff_t src_stride, int dst_width,
  1079. int source_y_fraction) = InterpolateRow_16_C;
  1080. ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y,
  1081. &dx, &dy);
  1082. src_width = Abs(src_width);
  1083. #if defined(HAS_INTERPOLATEROW_16_SSE2)
  1084. if (TestCpuFlag(kCpuHasSSE2)) {
  1085. InterpolateRow = InterpolateRow_Any_16_SSE2;
  1086. if (IS_ALIGNED(src_width, 16)) {
  1087. InterpolateRow = InterpolateRow_16_SSE2;
  1088. }
  1089. }
  1090. #endif
  1091. #if defined(HAS_INTERPOLATEROW_16_SSSE3)
  1092. if (TestCpuFlag(kCpuHasSSSE3)) {
  1093. InterpolateRow = InterpolateRow_Any_16_SSSE3;
  1094. if (IS_ALIGNED(src_width, 16)) {
  1095. InterpolateRow = InterpolateRow_16_SSSE3;
  1096. }
  1097. }
  1098. #endif
  1099. #if defined(HAS_INTERPOLATEROW_16_AVX2)
  1100. if (TestCpuFlag(kCpuHasAVX2)) {
  1101. InterpolateRow = InterpolateRow_Any_16_AVX2;
  1102. if (IS_ALIGNED(src_width, 32)) {
  1103. InterpolateRow = InterpolateRow_16_AVX2;
  1104. }
  1105. }
  1106. #endif
  1107. #if defined(HAS_INTERPOLATEROW_16_NEON)
  1108. if (TestCpuFlag(kCpuHasNEON)) {
  1109. InterpolateRow = InterpolateRow_Any_16_NEON;
  1110. if (IS_ALIGNED(src_width, 16)) {
  1111. InterpolateRow = InterpolateRow_16_NEON;
  1112. }
  1113. }
  1114. #endif
  1115. #if defined(HAS_SCALEFILTERCOLS_16_SSSE3)
  1116. if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) {
  1117. ScaleFilterCols = ScaleFilterCols_16_SSSE3;
  1118. }
  1119. #endif
  1120. if (y > max_y) {
  1121. y = max_y;
  1122. }
  1123. for (j = 0; j < dst_height; ++j) {
  1124. int yi = y >> 16;
  1125. const uint16_t* src = src_ptr + yi * src_stride;
  1126. if (filtering == kFilterLinear) {
  1127. ScaleFilterCols(dst_ptr, src, dst_width, x, dx);
  1128. } else {
  1129. int yf = (y >> 8) & 255;
  1130. InterpolateRow((uint16_t*)row, src, src_stride, src_width, yf);
  1131. ScaleFilterCols(dst_ptr, (uint16_t*)row, dst_width, x, dx);
  1132. }
  1133. dst_ptr += dst_stride;
  1134. y += dy;
  1135. if (y > max_y) {
  1136. y = max_y;
  1137. }
  1138. }
  1139. free_aligned_buffer_64(row);
  1140. }
  1141. // Scale up down with bilinear interpolation.
  1142. void ScalePlaneBilinearUp(int src_width,
  1143. int src_height,
  1144. int dst_width,
  1145. int dst_height,
  1146. int src_stride,
  1147. int dst_stride,
  1148. const uint8_t* src_ptr,
  1149. uint8_t* dst_ptr,
  1150. enum FilterMode filtering) {
  1151. int j;
  1152. // Initial source x/y coordinate and step values as 16.16 fixed point.
  1153. int x = 0;
  1154. int y = 0;
  1155. int dx = 0;
  1156. int dy = 0;
  1157. const int max_y = (src_height - 1) << 16;
  1158. void (*InterpolateRow)(uint8_t * dst_ptr, const uint8_t* src_ptr,
  1159. ptrdiff_t src_stride, int dst_width,
  1160. int source_y_fraction) = InterpolateRow_C;
  1161. void (*ScaleFilterCols)(uint8_t * dst_ptr, const uint8_t* src_ptr,
  1162. int dst_width, int x, int dx) =
  1163. filtering ? ScaleFilterCols_C : ScaleCols_C;
  1164. ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y,
  1165. &dx, &dy);
  1166. src_width = Abs(src_width);
  1167. #if defined(HAS_INTERPOLATEROW_SSSE3)
  1168. if (TestCpuFlag(kCpuHasSSSE3)) {
  1169. InterpolateRow = InterpolateRow_Any_SSSE3;
  1170. if (IS_ALIGNED(dst_width, 16)) {
  1171. InterpolateRow = InterpolateRow_SSSE3;
  1172. }
  1173. }
  1174. #endif
  1175. #if defined(HAS_INTERPOLATEROW_AVX2)
  1176. if (TestCpuFlag(kCpuHasAVX2)) {
  1177. InterpolateRow = InterpolateRow_Any_AVX2;
  1178. if (IS_ALIGNED(dst_width, 32)) {
  1179. InterpolateRow = InterpolateRow_AVX2;
  1180. }
  1181. }
  1182. #endif
  1183. #if defined(HAS_INTERPOLATEROW_NEON)
  1184. if (TestCpuFlag(kCpuHasNEON)) {
  1185. InterpolateRow = InterpolateRow_Any_NEON;
  1186. if (IS_ALIGNED(dst_width, 16)) {
  1187. InterpolateRow = InterpolateRow_NEON;
  1188. }
  1189. }
  1190. #endif
  1191. if (filtering && src_width >= 32768) {
  1192. ScaleFilterCols = ScaleFilterCols64_C;
  1193. }
  1194. #if defined(HAS_SCALEFILTERCOLS_SSSE3)
  1195. if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) {
  1196. ScaleFilterCols = ScaleFilterCols_SSSE3;
  1197. }
  1198. #endif
  1199. #if defined(HAS_SCALEFILTERCOLS_NEON)
  1200. if (filtering && TestCpuFlag(kCpuHasNEON) && src_width < 32768) {
  1201. ScaleFilterCols = ScaleFilterCols_Any_NEON;
  1202. if (IS_ALIGNED(dst_width, 8)) {
  1203. ScaleFilterCols = ScaleFilterCols_NEON;
  1204. }
  1205. }
  1206. #endif
  1207. #if defined(HAS_SCALEFILTERCOLS_MSA)
  1208. if (filtering && TestCpuFlag(kCpuHasMSA) && src_width < 32768) {
  1209. ScaleFilterCols = ScaleFilterCols_Any_MSA;
  1210. if (IS_ALIGNED(dst_width, 16)) {
  1211. ScaleFilterCols = ScaleFilterCols_MSA;
  1212. }
  1213. }
  1214. #endif
  1215. if (!filtering && src_width * 2 == dst_width && x < 0x8000) {
  1216. ScaleFilterCols = ScaleColsUp2_C;
  1217. #if defined(HAS_SCALECOLS_SSE2)
  1218. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) {
  1219. ScaleFilterCols = ScaleColsUp2_SSE2;
  1220. }
  1221. #endif
  1222. #if defined(HAS_SCALECOLS_MMI)
  1223. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(dst_width, 8)) {
  1224. ScaleFilterCols = ScaleColsUp2_MMI;
  1225. }
  1226. #endif
  1227. }
  1228. if (y > max_y) {
  1229. y = max_y;
  1230. }
  1231. {
  1232. int yi = y >> 16;
  1233. const uint8_t* src = src_ptr + yi * src_stride;
  1234. // Allocate 2 row buffers.
  1235. const int kRowSize = (dst_width + 31) & ~31;
  1236. align_buffer_64(row, kRowSize * 2);
  1237. uint8_t* rowptr = row;
  1238. int rowstride = kRowSize;
  1239. int lasty = yi;
  1240. ScaleFilterCols(rowptr, src, dst_width, x, dx);
  1241. if (src_height > 1) {
  1242. src += src_stride;
  1243. }
  1244. ScaleFilterCols(rowptr + rowstride, src, dst_width, x, dx);
  1245. src += src_stride;
  1246. for (j = 0; j < dst_height; ++j) {
  1247. yi = y >> 16;
  1248. if (yi != lasty) {
  1249. if (y > max_y) {
  1250. y = max_y;
  1251. yi = y >> 16;
  1252. src = src_ptr + yi * src_stride;
  1253. }
  1254. if (yi != lasty) {
  1255. ScaleFilterCols(rowptr, src, dst_width, x, dx);
  1256. rowptr += rowstride;
  1257. rowstride = -rowstride;
  1258. lasty = yi;
  1259. src += src_stride;
  1260. }
  1261. }
  1262. if (filtering == kFilterLinear) {
  1263. InterpolateRow(dst_ptr, rowptr, 0, dst_width, 0);
  1264. } else {
  1265. int yf = (y >> 8) & 255;
  1266. InterpolateRow(dst_ptr, rowptr, rowstride, dst_width, yf);
  1267. }
  1268. dst_ptr += dst_stride;
  1269. y += dy;
  1270. }
  1271. free_aligned_buffer_64(row);
  1272. }
  1273. }
  1274. void ScalePlaneBilinearUp_16(int src_width,
  1275. int src_height,
  1276. int dst_width,
  1277. int dst_height,
  1278. int src_stride,
  1279. int dst_stride,
  1280. const uint16_t* src_ptr,
  1281. uint16_t* dst_ptr,
  1282. enum FilterMode filtering) {
  1283. int j;
  1284. // Initial source x/y coordinate and step values as 16.16 fixed point.
  1285. int x = 0;
  1286. int y = 0;
  1287. int dx = 0;
  1288. int dy = 0;
  1289. const int max_y = (src_height - 1) << 16;
  1290. void (*InterpolateRow)(uint16_t * dst_ptr, const uint16_t* src_ptr,
  1291. ptrdiff_t src_stride, int dst_width,
  1292. int source_y_fraction) = InterpolateRow_16_C;
  1293. void (*ScaleFilterCols)(uint16_t * dst_ptr, const uint16_t* src_ptr,
  1294. int dst_width, int x, int dx) =
  1295. filtering ? ScaleFilterCols_16_C : ScaleCols_16_C;
  1296. ScaleSlope(src_width, src_height, dst_width, dst_height, filtering, &x, &y,
  1297. &dx, &dy);
  1298. src_width = Abs(src_width);
  1299. #if defined(HAS_INTERPOLATEROW_16_SSE2)
  1300. if (TestCpuFlag(kCpuHasSSE2)) {
  1301. InterpolateRow = InterpolateRow_Any_16_SSE2;
  1302. if (IS_ALIGNED(dst_width, 16)) {
  1303. InterpolateRow = InterpolateRow_16_SSE2;
  1304. }
  1305. }
  1306. #endif
  1307. #if defined(HAS_INTERPOLATEROW_16_SSSE3)
  1308. if (TestCpuFlag(kCpuHasSSSE3)) {
  1309. InterpolateRow = InterpolateRow_Any_16_SSSE3;
  1310. if (IS_ALIGNED(dst_width, 16)) {
  1311. InterpolateRow = InterpolateRow_16_SSSE3;
  1312. }
  1313. }
  1314. #endif
  1315. #if defined(HAS_INTERPOLATEROW_16_AVX2)
  1316. if (TestCpuFlag(kCpuHasAVX2)) {
  1317. InterpolateRow = InterpolateRow_Any_16_AVX2;
  1318. if (IS_ALIGNED(dst_width, 32)) {
  1319. InterpolateRow = InterpolateRow_16_AVX2;
  1320. }
  1321. }
  1322. #endif
  1323. #if defined(HAS_INTERPOLATEROW_16_NEON)
  1324. if (TestCpuFlag(kCpuHasNEON)) {
  1325. InterpolateRow = InterpolateRow_Any_16_NEON;
  1326. if (IS_ALIGNED(dst_width, 16)) {
  1327. InterpolateRow = InterpolateRow_16_NEON;
  1328. }
  1329. }
  1330. #endif
  1331. if (filtering && src_width >= 32768) {
  1332. ScaleFilterCols = ScaleFilterCols64_16_C;
  1333. }
  1334. #if defined(HAS_SCALEFILTERCOLS_16_SSSE3)
  1335. if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) {
  1336. ScaleFilterCols = ScaleFilterCols_16_SSSE3;
  1337. }
  1338. #endif
  1339. if (!filtering && src_width * 2 == dst_width && x < 0x8000) {
  1340. ScaleFilterCols = ScaleColsUp2_16_C;
  1341. #if defined(HAS_SCALECOLS_16_SSE2)
  1342. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) {
  1343. ScaleFilterCols = ScaleColsUp2_16_SSE2;
  1344. }
  1345. #endif
  1346. #if defined(HAS_SCALECOLS_16_MMI)
  1347. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(dst_width, 8)) {
  1348. ScaleFilterCols = ScaleColsUp2_16_MMI;
  1349. }
  1350. #endif
  1351. }
  1352. if (y > max_y) {
  1353. y = max_y;
  1354. }
  1355. {
  1356. int yi = y >> 16;
  1357. const uint16_t* src = src_ptr + yi * src_stride;
  1358. // Allocate 2 row buffers.
  1359. const int kRowSize = (dst_width + 31) & ~31;
  1360. align_buffer_64(row, kRowSize * 4);
  1361. uint16_t* rowptr = (uint16_t*)row;
  1362. int rowstride = kRowSize;
  1363. int lasty = yi;
  1364. ScaleFilterCols(rowptr, src, dst_width, x, dx);
  1365. if (src_height > 1) {
  1366. src += src_stride;
  1367. }
  1368. ScaleFilterCols(rowptr + rowstride, src, dst_width, x, dx);
  1369. src += src_stride;
  1370. for (j = 0; j < dst_height; ++j) {
  1371. yi = y >> 16;
  1372. if (yi != lasty) {
  1373. if (y > max_y) {
  1374. y = max_y;
  1375. yi = y >> 16;
  1376. src = src_ptr + yi * src_stride;
  1377. }
  1378. if (yi != lasty) {
  1379. ScaleFilterCols(rowptr, src, dst_width, x, dx);
  1380. rowptr += rowstride;
  1381. rowstride = -rowstride;
  1382. lasty = yi;
  1383. src += src_stride;
  1384. }
  1385. }
  1386. if (filtering == kFilterLinear) {
  1387. InterpolateRow(dst_ptr, rowptr, 0, dst_width, 0);
  1388. } else {
  1389. int yf = (y >> 8) & 255;
  1390. InterpolateRow(dst_ptr, rowptr, rowstride, dst_width, yf);
  1391. }
  1392. dst_ptr += dst_stride;
  1393. y += dy;
  1394. }
  1395. free_aligned_buffer_64(row);
  1396. }
  1397. }
  1398. // Scale Plane to/from any dimensions, without interpolation.
  1399. // Fixed point math is used for performance: The upper 16 bits
  1400. // of x and dx is the integer part of the source position and
  1401. // the lower 16 bits are the fixed decimal part.
  1402. static void ScalePlaneSimple(int src_width,
  1403. int src_height,
  1404. int dst_width,
  1405. int dst_height,
  1406. int src_stride,
  1407. int dst_stride,
  1408. const uint8_t* src_ptr,
  1409. uint8_t* dst_ptr) {
  1410. int i;
  1411. void (*ScaleCols)(uint8_t * dst_ptr, const uint8_t* src_ptr, int dst_width,
  1412. int x, int dx) = ScaleCols_C;
  1413. // Initial source x/y coordinate and step values as 16.16 fixed point.
  1414. int x = 0;
  1415. int y = 0;
  1416. int dx = 0;
  1417. int dy = 0;
  1418. ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterNone, &x, &y,
  1419. &dx, &dy);
  1420. src_width = Abs(src_width);
  1421. if (src_width * 2 == dst_width && x < 0x8000) {
  1422. ScaleCols = ScaleColsUp2_C;
  1423. #if defined(HAS_SCALECOLS_SSE2)
  1424. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) {
  1425. ScaleCols = ScaleColsUp2_SSE2;
  1426. }
  1427. #endif
  1428. #if defined(HAS_SCALECOLS_MMI)
  1429. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(dst_width, 8)) {
  1430. ScaleCols = ScaleColsUp2_MMI;
  1431. }
  1432. #endif
  1433. }
  1434. for (i = 0; i < dst_height; ++i) {
  1435. ScaleCols(dst_ptr, src_ptr + (y >> 16) * src_stride, dst_width, x, dx);
  1436. dst_ptr += dst_stride;
  1437. y += dy;
  1438. }
  1439. }
  1440. static void ScalePlaneSimple_16(int src_width,
  1441. int src_height,
  1442. int dst_width,
  1443. int dst_height,
  1444. int src_stride,
  1445. int dst_stride,
  1446. const uint16_t* src_ptr,
  1447. uint16_t* dst_ptr) {
  1448. int i;
  1449. void (*ScaleCols)(uint16_t * dst_ptr, const uint16_t* src_ptr, int dst_width,
  1450. int x, int dx) = ScaleCols_16_C;
  1451. // Initial source x/y coordinate and step values as 16.16 fixed point.
  1452. int x = 0;
  1453. int y = 0;
  1454. int dx = 0;
  1455. int dy = 0;
  1456. ScaleSlope(src_width, src_height, dst_width, dst_height, kFilterNone, &x, &y,
  1457. &dx, &dy);
  1458. src_width = Abs(src_width);
  1459. if (src_width * 2 == dst_width && x < 0x8000) {
  1460. ScaleCols = ScaleColsUp2_16_C;
  1461. #if defined(HAS_SCALECOLS_16_SSE2)
  1462. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) {
  1463. ScaleCols = ScaleColsUp2_16_SSE2;
  1464. }
  1465. #endif
  1466. #if defined(HAS_SCALECOLS_16_MMI)
  1467. if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(dst_width, 8)) {
  1468. ScaleCols = ScaleColsUp2_16_MMI;
  1469. }
  1470. #endif
  1471. }
  1472. for (i = 0; i < dst_height; ++i) {
  1473. ScaleCols(dst_ptr, src_ptr + (y >> 16) * src_stride, dst_width, x, dx);
  1474. dst_ptr += dst_stride;
  1475. y += dy;
  1476. }
  1477. }
  1478. // Scale a plane.
  1479. // This function dispatches to a specialized scaler based on scale factor.
  1480. LIBYUV_API
  1481. void ScalePlane(const uint8_t* src,
  1482. int src_stride,
  1483. int src_width,
  1484. int src_height,
  1485. uint8_t* dst,
  1486. int dst_stride,
  1487. int dst_width,
  1488. int dst_height,
  1489. enum FilterMode filtering) {
  1490. // Simplify filtering when possible.
  1491. filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height,
  1492. filtering);
  1493. // Negative height means invert the image.
  1494. if (src_height < 0) {
  1495. src_height = -src_height;
  1496. src = src + (src_height - 1) * src_stride;
  1497. src_stride = -src_stride;
  1498. }
  1499. // Use specialized scales to improve performance for common resolutions.
  1500. // For example, all the 1/2 scalings will use ScalePlaneDown2()
  1501. if (dst_width == src_width && dst_height == src_height) {
  1502. // Straight copy.
  1503. CopyPlane(src, src_stride, dst, dst_stride, dst_width, dst_height);
  1504. return;
  1505. }
  1506. if (dst_width == src_width && filtering != kFilterBox) {
  1507. int dy = FixedDiv(src_height, dst_height);
  1508. // Arbitrary scale vertically, but unscaled horizontally.
  1509. ScalePlaneVertical(src_height, dst_width, dst_height, src_stride,
  1510. dst_stride, src, dst, 0, 0, dy, 1, filtering);
  1511. return;
  1512. }
  1513. if (dst_width <= Abs(src_width) && dst_height <= src_height) {
  1514. // Scale down.
  1515. if (4 * dst_width == 3 * src_width && 4 * dst_height == 3 * src_height) {
  1516. // optimized, 3/4
  1517. ScalePlaneDown34(src_width, src_height, dst_width, dst_height, src_stride,
  1518. dst_stride, src, dst, filtering);
  1519. return;
  1520. }
  1521. if (2 * dst_width == src_width && 2 * dst_height == src_height) {
  1522. // optimized, 1/2
  1523. ScalePlaneDown2(src_width, src_height, dst_width, dst_height, src_stride,
  1524. dst_stride, src, dst, filtering);
  1525. return;
  1526. }
  1527. // 3/8 rounded up for odd sized chroma height.
  1528. if (8 * dst_width == 3 * src_width && 8 * dst_height == 3 * src_height) {
  1529. // optimized, 3/8
  1530. ScalePlaneDown38(src_width, src_height, dst_width, dst_height, src_stride,
  1531. dst_stride, src, dst, filtering);
  1532. return;
  1533. }
  1534. if (4 * dst_width == src_width && 4 * dst_height == src_height &&
  1535. (filtering == kFilterBox || filtering == kFilterNone)) {
  1536. // optimized, 1/4
  1537. ScalePlaneDown4(src_width, src_height, dst_width, dst_height, src_stride,
  1538. dst_stride, src, dst, filtering);
  1539. return;
  1540. }
  1541. }
  1542. if (filtering == kFilterBox && dst_height * 2 < src_height) {
  1543. ScalePlaneBox(src_width, src_height, dst_width, dst_height, src_stride,
  1544. dst_stride, src, dst);
  1545. return;
  1546. }
  1547. if (filtering && dst_height > src_height) {
  1548. ScalePlaneBilinearUp(src_width, src_height, dst_width, dst_height,
  1549. src_stride, dst_stride, src, dst, filtering);
  1550. return;
  1551. }
  1552. if (filtering) {
  1553. ScalePlaneBilinearDown(src_width, src_height, dst_width, dst_height,
  1554. src_stride, dst_stride, src, dst, filtering);
  1555. return;
  1556. }
  1557. ScalePlaneSimple(src_width, src_height, dst_width, dst_height, src_stride,
  1558. dst_stride, src, dst);
  1559. }
  1560. LIBYUV_API
  1561. void ScalePlane_16(const uint16_t* src,
  1562. int src_stride,
  1563. int src_width,
  1564. int src_height,
  1565. uint16_t* dst,
  1566. int dst_stride,
  1567. int dst_width,
  1568. int dst_height,
  1569. enum FilterMode filtering) {
  1570. // Simplify filtering when possible.
  1571. filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height,
  1572. filtering);
  1573. // Negative height means invert the image.
  1574. if (src_height < 0) {
  1575. src_height = -src_height;
  1576. src = src + (src_height - 1) * src_stride;
  1577. src_stride = -src_stride;
  1578. }
  1579. // Use specialized scales to improve performance for common resolutions.
  1580. // For example, all the 1/2 scalings will use ScalePlaneDown2()
  1581. if (dst_width == src_width && dst_height == src_height) {
  1582. // Straight copy.
  1583. CopyPlane_16(src, src_stride, dst, dst_stride, dst_width, dst_height);
  1584. return;
  1585. }
  1586. if (dst_width == src_width && filtering != kFilterBox) {
  1587. int dy = FixedDiv(src_height, dst_height);
  1588. // Arbitrary scale vertically, but unscaled vertically.
  1589. ScalePlaneVertical_16(src_height, dst_width, dst_height, src_stride,
  1590. dst_stride, src, dst, 0, 0, dy, 1, filtering);
  1591. return;
  1592. }
  1593. if (dst_width <= Abs(src_width) && dst_height <= src_height) {
  1594. // Scale down.
  1595. if (4 * dst_width == 3 * src_width && 4 * dst_height == 3 * src_height) {
  1596. // optimized, 3/4
  1597. ScalePlaneDown34_16(src_width, src_height, dst_width, dst_height,
  1598. src_stride, dst_stride, src, dst, filtering);
  1599. return;
  1600. }
  1601. if (2 * dst_width == src_width && 2 * dst_height == src_height) {
  1602. // optimized, 1/2
  1603. ScalePlaneDown2_16(src_width, src_height, dst_width, dst_height,
  1604. src_stride, dst_stride, src, dst, filtering);
  1605. return;
  1606. }
  1607. // 3/8 rounded up for odd sized chroma height.
  1608. if (8 * dst_width == 3 * src_width && 8 * dst_height == 3 * src_height) {
  1609. // optimized, 3/8
  1610. ScalePlaneDown38_16(src_width, src_height, dst_width, dst_height,
  1611. src_stride, dst_stride, src, dst, filtering);
  1612. return;
  1613. }
  1614. if (4 * dst_width == src_width && 4 * dst_height == src_height &&
  1615. (filtering == kFilterBox || filtering == kFilterNone)) {
  1616. // optimized, 1/4
  1617. ScalePlaneDown4_16(src_width, src_height, dst_width, dst_height,
  1618. src_stride, dst_stride, src, dst, filtering);
  1619. return;
  1620. }
  1621. }
  1622. if (filtering == kFilterBox && dst_height * 2 < src_height) {
  1623. ScalePlaneBox_16(src_width, src_height, dst_width, dst_height, src_stride,
  1624. dst_stride, src, dst);
  1625. return;
  1626. }
  1627. if (filtering && dst_height > src_height) {
  1628. ScalePlaneBilinearUp_16(src_width, src_height, dst_width, dst_height,
  1629. src_stride, dst_stride, src, dst, filtering);
  1630. return;
  1631. }
  1632. if (filtering) {
  1633. ScalePlaneBilinearDown_16(src_width, src_height, dst_width, dst_height,
  1634. src_stride, dst_stride, src, dst, filtering);
  1635. return;
  1636. }
  1637. ScalePlaneSimple_16(src_width, src_height, dst_width, dst_height, src_stride,
  1638. dst_stride, src, dst);
  1639. }
  1640. // Scale an I420 image.
  1641. // This function in turn calls a scaling function for each plane.
  1642. LIBYUV_API
  1643. int I420Scale(const uint8_t* src_y,
  1644. int src_stride_y,
  1645. const uint8_t* src_u,
  1646. int src_stride_u,
  1647. const uint8_t* src_v,
  1648. int src_stride_v,
  1649. int src_width,
  1650. int src_height,
  1651. uint8_t* dst_y,
  1652. int dst_stride_y,
  1653. uint8_t* dst_u,
  1654. int dst_stride_u,
  1655. uint8_t* dst_v,
  1656. int dst_stride_v,
  1657. int dst_width,
  1658. int dst_height,
  1659. enum FilterMode filtering) {
  1660. int src_halfwidth = SUBSAMPLE(src_width, 1, 1);
  1661. int src_halfheight = SUBSAMPLE(src_height, 1, 1);
  1662. int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1);
  1663. int dst_halfheight = SUBSAMPLE(dst_height, 1, 1);
  1664. if (!src_y || !src_u || !src_v || src_width == 0 || src_height == 0 ||
  1665. src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v ||
  1666. dst_width <= 0 || dst_height <= 0) {
  1667. return -1;
  1668. }
  1669. ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y,
  1670. dst_width, dst_height, filtering);
  1671. ScalePlane(src_u, src_stride_u, src_halfwidth, src_halfheight, dst_u,
  1672. dst_stride_u, dst_halfwidth, dst_halfheight, filtering);
  1673. ScalePlane(src_v, src_stride_v, src_halfwidth, src_halfheight, dst_v,
  1674. dst_stride_v, dst_halfwidth, dst_halfheight, filtering);
  1675. return 0;
  1676. }
  1677. LIBYUV_API
  1678. int I420Scale_16(const uint16_t* src_y,
  1679. int src_stride_y,
  1680. const uint16_t* src_u,
  1681. int src_stride_u,
  1682. const uint16_t* src_v,
  1683. int src_stride_v,
  1684. int src_width,
  1685. int src_height,
  1686. uint16_t* dst_y,
  1687. int dst_stride_y,
  1688. uint16_t* dst_u,
  1689. int dst_stride_u,
  1690. uint16_t* dst_v,
  1691. int dst_stride_v,
  1692. int dst_width,
  1693. int dst_height,
  1694. enum FilterMode filtering) {
  1695. int src_halfwidth = SUBSAMPLE(src_width, 1, 1);
  1696. int src_halfheight = SUBSAMPLE(src_height, 1, 1);
  1697. int dst_halfwidth = SUBSAMPLE(dst_width, 1, 1);
  1698. int dst_halfheight = SUBSAMPLE(dst_height, 1, 1);
  1699. if (!src_y || !src_u || !src_v || src_width == 0 || src_height == 0 ||
  1700. src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v ||
  1701. dst_width <= 0 || dst_height <= 0) {
  1702. return -1;
  1703. }
  1704. ScalePlane_16(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y,
  1705. dst_width, dst_height, filtering);
  1706. ScalePlane_16(src_u, src_stride_u, src_halfwidth, src_halfheight, dst_u,
  1707. dst_stride_u, dst_halfwidth, dst_halfheight, filtering);
  1708. ScalePlane_16(src_v, src_stride_v, src_halfwidth, src_halfheight, dst_v,
  1709. dst_stride_v, dst_halfwidth, dst_halfheight, filtering);
  1710. return 0;
  1711. }
  1712. // Scale an I444 image.
  1713. // This function in turn calls a scaling function for each plane.
  1714. LIBYUV_API
  1715. int I444Scale(const uint8_t* src_y,
  1716. int src_stride_y,
  1717. const uint8_t* src_u,
  1718. int src_stride_u,
  1719. const uint8_t* src_v,
  1720. int src_stride_v,
  1721. int src_width,
  1722. int src_height,
  1723. uint8_t* dst_y,
  1724. int dst_stride_y,
  1725. uint8_t* dst_u,
  1726. int dst_stride_u,
  1727. uint8_t* dst_v,
  1728. int dst_stride_v,
  1729. int dst_width,
  1730. int dst_height,
  1731. enum FilterMode filtering) {
  1732. if (!src_y || !src_u || !src_v || src_width == 0 || src_height == 0 ||
  1733. src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v ||
  1734. dst_width <= 0 || dst_height <= 0) {
  1735. return -1;
  1736. }
  1737. ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y,
  1738. dst_width, dst_height, filtering);
  1739. ScalePlane(src_u, src_stride_u, src_width, src_height, dst_u, dst_stride_u,
  1740. dst_width, dst_height, filtering);
  1741. ScalePlane(src_v, src_stride_v, src_width, src_height, dst_v, dst_stride_v,
  1742. dst_width, dst_height, filtering);
  1743. return 0;
  1744. }
  1745. LIBYUV_API
  1746. int I444Scale_16(const uint16_t* src_y,
  1747. int src_stride_y,
  1748. const uint16_t* src_u,
  1749. int src_stride_u,
  1750. const uint16_t* src_v,
  1751. int src_stride_v,
  1752. int src_width,
  1753. int src_height,
  1754. uint16_t* dst_y,
  1755. int dst_stride_y,
  1756. uint16_t* dst_u,
  1757. int dst_stride_u,
  1758. uint16_t* dst_v,
  1759. int dst_stride_v,
  1760. int dst_width,
  1761. int dst_height,
  1762. enum FilterMode filtering) {
  1763. if (!src_y || !src_u || !src_v || src_width == 0 || src_height == 0 ||
  1764. src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v ||
  1765. dst_width <= 0 || dst_height <= 0) {
  1766. return -1;
  1767. }
  1768. ScalePlane_16(src_y, src_stride_y, src_width, src_height, dst_y, dst_stride_y,
  1769. dst_width, dst_height, filtering);
  1770. ScalePlane_16(src_u, src_stride_u, src_width, src_height, dst_u, dst_stride_u,
  1771. dst_width, dst_height, filtering);
  1772. ScalePlane_16(src_v, src_stride_v, src_width, src_height, dst_v, dst_stride_v,
  1773. dst_width, dst_height, filtering);
  1774. return 0;
  1775. }
  1776. // Deprecated api
  1777. LIBYUV_API
  1778. int Scale(const uint8_t* src_y,
  1779. const uint8_t* src_u,
  1780. const uint8_t* src_v,
  1781. int src_stride_y,
  1782. int src_stride_u,
  1783. int src_stride_v,
  1784. int src_width,
  1785. int src_height,
  1786. uint8_t* dst_y,
  1787. uint8_t* dst_u,
  1788. uint8_t* dst_v,
  1789. int dst_stride_y,
  1790. int dst_stride_u,
  1791. int dst_stride_v,
  1792. int dst_width,
  1793. int dst_height,
  1794. LIBYUV_BOOL interpolate) {
  1795. return I420Scale(src_y, src_stride_y, src_u, src_stride_u, src_v,
  1796. src_stride_v, src_width, src_height, dst_y, dst_stride_y,
  1797. dst_u, dst_stride_u, dst_v, dst_stride_v, dst_width,
  1798. dst_height, interpolate ? kFilterBox : kFilterNone);
  1799. }
  1800. #ifdef __cplusplus
  1801. } // extern "C"
  1802. } // namespace libyuv
  1803. #endif