2
0

scale.cc 62 KB

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