scale.cc 64 KB

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