scale_yuv_argb.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * Copyright 2011 The LibYuv Project Authors. All rights reserved.
  3. * Copyright 2016 Mozilla Foundation
  4. *
  5. * Use of this source code is governed by a BSD-style license
  6. * that can be found in the LICENSE file in the root of the source
  7. * tree. An additional intellectual property rights grant can be found
  8. * in the file PATENTS. All contributing project authors may
  9. * be found in the AUTHORS file in the root of the source tree.
  10. */
  11. #include "scale_yuv_argb.h"
  12. #include "libyuv/scale.h"
  13. #include <assert.h>
  14. #include <string.h>
  15. #include "libyuv/cpu_id.h"
  16. #include "libyuv/row.h"
  17. #include "libyuv/scale_row.h"
  18. #include "libyuv/video_common.h"
  19. #ifdef __cplusplus
  20. namespace libyuv {
  21. extern "C" {
  22. #endif
  23. // YUV to RGB conversion and scaling functions were implemented by referencing
  24. // scale_argb.cc
  25. //
  26. // libyuv already has ScaleYUVToARGBBilinearUp(), but its implementation is not
  27. // completed yet. Implementations of the functions are based on it.
  28. // At first, ScaleYUVToARGBBilinearUp() was implemented by modidying the
  29. // libyuv's one. Then all another functions were implemented similarly.
  30. //
  31. // Function relationship between yuv_convert.cpp abd scale_argb.cc are like
  32. // the followings
  33. // - ScaleYUVToARGBDown2() <-- ScaleARGBDown2()
  34. // - ScaleYUVToARGBDownEven() <-- ScaleARGBDownEven()
  35. // - ScaleYUVToARGBBilinearDown() <-- ScaleARGBBilinearDown()
  36. // - ScaleYUVToARGBBilinearUp() <-- ScaleARGBBilinearUp() and ScaleYUVToARGBBilinearUp() in libyuv
  37. // - ScaleYUVToARGBSimple() <-- ScaleARGBSimple()
  38. // - ScaleYUVToARGB() <-- ScaleARGB() // Removed some function calls for simplicity.
  39. // - YUVToARGBScale() <-- ARGBScale()
  40. //
  41. // Callings and selections of InterpolateRow() and ScaleARGBFilterCols() were
  42. // kept as same as possible.
  43. //
  44. // The followings changes were done to each scaling functions.
  45. //
  46. // -[1] Allocate YUV conversion buffer and use it as source buffer of scaling.
  47. // Its usage is borrowed from the libyuv's ScaleYUVToARGBBilinearUp().
  48. // -[2] Conversion from YUV to RGB was abstracted as YUVBuferIter.
  49. // It is for handling multiple yuv color formats.
  50. // -[3] Modified scaling functions as to handle YUV conversion buffer and
  51. // use YUVBuferIter.
  52. // -[4] Color conversion function selections in YUVBuferIter were borrowed from
  53. // I444ToARGBMatrix(), I422ToARGBMatrix() and I420ToARGBMatrix()
  54. static __inline int Abs(int v) {
  55. return v >= 0 ? v : -v;
  56. }
  57. struct YUVBuferIter {
  58. int src_width;
  59. int src_height;
  60. int src_stride_y;
  61. int src_stride_u;
  62. int src_stride_v;
  63. const uint8* src_y;
  64. const uint8* src_u;
  65. const uint8* src_v;
  66. uint32 src_fourcc;
  67. const struct YuvConstants* yuvconstants;
  68. int y_index;
  69. const uint8* src_row_y;
  70. const uint8* src_row_u;
  71. const uint8* src_row_v;
  72. void (*YUVToARGBRow)(const uint8* y_buf,
  73. const uint8* u_buf,
  74. const uint8* v_buf,
  75. uint8* rgb_buf,
  76. const struct YuvConstants* yuvconstants,
  77. int width);
  78. void (*MoveTo)(YUVBuferIter& iter, int y_index);
  79. void (*MoveToNextRow)(YUVBuferIter& iter);
  80. };
  81. void YUVBuferIter_InitI422(YUVBuferIter& iter) {
  82. iter.YUVToARGBRow = I422ToARGBRow_C;
  83. #if defined(HAS_I422TOARGBROW_SSSE3)
  84. if (TestCpuFlag(kCpuHasSSSE3)) {
  85. iter.YUVToARGBRow = I422ToARGBRow_Any_SSSE3;
  86. if (IS_ALIGNED(iter.src_width, 8)) {
  87. iter.YUVToARGBRow = I422ToARGBRow_SSSE3;
  88. }
  89. }
  90. #endif
  91. #if defined(HAS_I422TOARGBROW_AVX2)
  92. if (TestCpuFlag(kCpuHasAVX2)) {
  93. iter.YUVToARGBRow = I422ToARGBRow_Any_AVX2;
  94. if (IS_ALIGNED(iter.src_width, 16)) {
  95. iter.YUVToARGBRow = I422ToARGBRow_AVX2;
  96. }
  97. }
  98. #endif
  99. #if defined(HAS_I422TOARGBROW_NEON)
  100. if (TestCpuFlag(kCpuHasNEON)) {
  101. iter.YUVToARGBRow = I422ToARGBRow_Any_NEON;
  102. if (IS_ALIGNED(iter.src_width, 8)) {
  103. iter.YUVToARGBRow = I422ToARGBRow_NEON;
  104. }
  105. }
  106. #endif
  107. #if defined(HAS_I422TOARGBROW_DSPR2)
  108. if (TestCpuFlag(kCpuHasDSPR2) && IS_ALIGNED(iter.src_width, 4) &&
  109. IS_ALIGNED(iter.src_y, 4) && IS_ALIGNED(iter.src_stride_y, 4) &&
  110. IS_ALIGNED(iter.src_u, 2) && IS_ALIGNED(iter.src_stride_u, 2) &&
  111. IS_ALIGNED(iter.src_v, 2) && IS_ALIGNED(iter.src_stride_v, 2) {
  112. // Always satisfy IS_ALIGNED(argb_cnv_row, 4) && IS_ALIGNED(argb_cnv_rowstride, 4)
  113. iter.YUVToARGBRow = I422ToARGBRow_DSPR2;
  114. }
  115. #endif
  116. }
  117. void YUVBuferIter_InitI444(YUVBuferIter& iter) {
  118. iter.YUVToARGBRow = I444ToARGBRow_C;
  119. #if defined(HAS_I444TOARGBROW_SSSE3)
  120. if (TestCpuFlag(kCpuHasSSSE3)) {
  121. iter.YUVToARGBRow = I444ToARGBRow_Any_SSSE3;
  122. if (IS_ALIGNED(iter.src_width, 8)) {
  123. iter.YUVToARGBRow = I444ToARGBRow_SSSE3;
  124. }
  125. }
  126. #endif
  127. #if defined(HAS_I444TOARGBROW_AVX2)
  128. if (TestCpuFlag(kCpuHasAVX2)) {
  129. iter.YUVToARGBRow = I444ToARGBRow_Any_AVX2;
  130. if (IS_ALIGNED(iter.src_width, 16)) {
  131. iter.YUVToARGBRow = I444ToARGBRow_AVX2;
  132. }
  133. }
  134. #endif
  135. #if defined(HAS_I444TOARGBROW_NEON)
  136. if (TestCpuFlag(kCpuHasNEON)) {
  137. iter.YUVToARGBRow = I444ToARGBRow_Any_NEON;
  138. if (IS_ALIGNED(iter.src_width, 8)) {
  139. iter.YUVToARGBRow = I444ToARGBRow_NEON;
  140. }
  141. }
  142. #endif
  143. }
  144. static void YUVBuferIter_MoveToForI444(YUVBuferIter& iter, int y_index) {
  145. iter.y_index = y_index;
  146. iter.src_row_y = iter.src_y + y_index * iter.src_stride_y;
  147. iter.src_row_u = iter.src_u + y_index * iter.src_stride_u;
  148. iter.src_row_v = iter.src_v + y_index * iter.src_stride_v;
  149. }
  150. static void YUVBuferIter_MoveToNextRowForI444(YUVBuferIter& iter) {
  151. iter.src_row_y += iter.src_stride_y;
  152. iter.src_row_u += iter.src_stride_u;
  153. iter.src_row_v += iter.src_stride_v;
  154. iter.y_index++;
  155. }
  156. static void YUVBuferIter_MoveToForI422(YUVBuferIter& iter, int y_index) {
  157. iter.y_index = y_index;
  158. iter.src_row_y = iter.src_y + y_index * iter.src_stride_y;
  159. iter.src_row_u = iter.src_u + y_index * iter.src_stride_u;
  160. iter.src_row_v = iter.src_v + y_index * iter.src_stride_v;
  161. }
  162. static void YUVBuferIter_MoveToNextRowForI422(YUVBuferIter& iter) {
  163. iter.src_row_y += iter.src_stride_y;
  164. iter.src_row_u += iter.src_stride_u;
  165. iter.src_row_v += iter.src_stride_v;
  166. iter.y_index++;
  167. }
  168. static void YUVBuferIter_MoveToForI420(YUVBuferIter& iter, int y_index) {
  169. const int kYShift = 1; // Shift Y by 1 to convert Y plane to UV coordinate.
  170. int uv_y_index = y_index >> kYShift;
  171. iter.y_index = y_index;
  172. iter.src_row_y = iter.src_y + y_index * iter.src_stride_y;
  173. iter.src_row_u = iter.src_u + uv_y_index * iter.src_stride_u;
  174. iter.src_row_v = iter.src_v + uv_y_index * iter.src_stride_v;
  175. }
  176. static void YUVBuferIter_MoveToNextRowForI420(YUVBuferIter& iter) {
  177. iter.src_row_y += iter.src_stride_y;
  178. if (iter.y_index & 1) {
  179. iter.src_row_u += iter.src_stride_u;
  180. iter.src_row_v += iter.src_stride_v;
  181. }
  182. iter.y_index++;
  183. }
  184. static __inline void YUVBuferIter_ConvertToARGBRow(YUVBuferIter& iter, uint8* argb_row) {
  185. iter.YUVToARGBRow(iter.src_row_y, iter.src_row_u, iter.src_row_v, argb_row, iter.yuvconstants, iter.src_width);
  186. }
  187. void YUVBuferIter_Init(YUVBuferIter& iter, uint32 src_fourcc, mozilla::YUVColorSpace yuv_color_space) {
  188. iter.src_fourcc = src_fourcc;
  189. iter.y_index = 0;
  190. iter.src_row_y = iter.src_y;
  191. iter.src_row_u = iter.src_u;
  192. iter.src_row_v = iter.src_v;
  193. if (yuv_color_space == mozilla::YUVColorSpace::BT709) {
  194. iter.yuvconstants = &kYuvH709Constants;
  195. } else {
  196. iter.yuvconstants = &kYuvI601Constants;
  197. }
  198. if (src_fourcc == FOURCC_I444) {
  199. YUVBuferIter_InitI444(iter);
  200. iter.MoveTo = YUVBuferIter_MoveToForI444;
  201. iter.MoveToNextRow = YUVBuferIter_MoveToNextRowForI444;
  202. } else if(src_fourcc == FOURCC_I422){
  203. YUVBuferIter_InitI422(iter);
  204. iter.MoveTo = YUVBuferIter_MoveToForI422;
  205. iter.MoveToNextRow = YUVBuferIter_MoveToNextRowForI422;
  206. } else {
  207. assert(src_fourcc == FOURCC_I420); // Should be FOURCC_I420
  208. YUVBuferIter_InitI422(iter);
  209. iter.MoveTo = YUVBuferIter_MoveToForI420;
  210. iter.MoveToNextRow = YUVBuferIter_MoveToNextRowForI420;
  211. }
  212. }
  213. // ScaleARGB ARGB, 1/2
  214. // This is an optimized version for scaling down a ARGB to 1/2 of
  215. // its original size.
  216. static void ScaleYUVToARGBDown2(int src_width, int src_height,
  217. int dst_width, int dst_height,
  218. int src_stride_y,
  219. int src_stride_u,
  220. int src_stride_v,
  221. int dst_stride_argb,
  222. const uint8* src_y,
  223. const uint8* src_u,
  224. const uint8* src_v,
  225. uint8* dst_argb,
  226. int x, int dx, int y, int dy,
  227. enum FilterMode filtering,
  228. uint32 src_fourcc,
  229. mozilla::YUVColorSpace yuv_color_space) {
  230. int j;
  231. // Allocate 2 rows of ARGB for source conversion.
  232. const int kRowSize = (src_width * 4 + 15) & ~15;
  233. align_buffer_64(argb_cnv_row, kRowSize * 2);
  234. uint8* argb_cnv_rowptr = argb_cnv_row;
  235. int argb_cnv_rowstride = kRowSize;
  236. YUVBuferIter iter;
  237. iter.src_width = src_width;
  238. iter.src_height = src_height;
  239. iter.src_stride_y = src_stride_y;
  240. iter.src_stride_u = src_stride_u;
  241. iter.src_stride_v = src_stride_v;
  242. iter.src_y = src_y;
  243. iter.src_u = src_u;
  244. iter.src_v = src_v;
  245. YUVBuferIter_Init(iter, src_fourcc, yuv_color_space);
  246. void (*ScaleARGBRowDown2)(const uint8* src_argb, ptrdiff_t src_stride,
  247. uint8* dst_argb, int dst_width) =
  248. filtering == kFilterNone ? ScaleARGBRowDown2_C :
  249. (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_C :
  250. ScaleARGBRowDown2Box_C);
  251. assert(dx == 65536 * 2); // Test scale factor of 2.
  252. assert((dy & 0x1ffff) == 0); // Test vertical scale is multiple of 2.
  253. // Advance to odd row, even column.
  254. int yi = y >> 16;
  255. iter.MoveTo(iter, yi);
  256. ptrdiff_t x_offset;
  257. if (filtering == kFilterBilinear) {
  258. x_offset = (x >> 16) * 4;
  259. } else {
  260. x_offset = ((x >> 16) - 1) * 4;
  261. }
  262. #if defined(HAS_SCALEARGBROWDOWN2_SSE2)
  263. if (TestCpuFlag(kCpuHasSSE2)) {
  264. ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_Any_SSE2 :
  265. (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_Any_SSE2 :
  266. ScaleARGBRowDown2Box_Any_SSE2);
  267. if (IS_ALIGNED(dst_width, 4)) {
  268. ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_SSE2 :
  269. (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_SSE2 :
  270. ScaleARGBRowDown2Box_SSE2);
  271. }
  272. }
  273. #endif
  274. #if defined(HAS_SCALEARGBROWDOWN2_NEON)
  275. if (TestCpuFlag(kCpuHasNEON)) {
  276. ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_Any_NEON :
  277. (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_Any_NEON :
  278. ScaleARGBRowDown2Box_Any_NEON);
  279. if (IS_ALIGNED(dst_width, 8)) {
  280. ScaleARGBRowDown2 = filtering == kFilterNone ? ScaleARGBRowDown2_NEON :
  281. (filtering == kFilterLinear ? ScaleARGBRowDown2Linear_NEON :
  282. ScaleARGBRowDown2Box_NEON);
  283. }
  284. }
  285. #endif
  286. const int dyi = dy >> 16;
  287. int lastyi = yi;
  288. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  289. // Prepare next row if necessary
  290. if (filtering != kFilterLinear) {
  291. if ((yi + dyi) < (src_height - 1)) {
  292. iter.MoveTo(iter, yi + dyi);
  293. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr + argb_cnv_rowstride);
  294. } else {
  295. argb_cnv_rowstride = 0;
  296. }
  297. }
  298. if (filtering == kFilterLinear) {
  299. argb_cnv_rowstride = 0;
  300. }
  301. const int max_yi = src_height - 1;
  302. const int max_yi_minus_dyi = max_yi - dyi;
  303. for (j = 0; j < dst_height; ++j) {
  304. if (yi != lastyi) {
  305. if (yi > max_yi) {
  306. yi = max_yi;
  307. }
  308. if (yi != lastyi) {
  309. if (filtering == kFilterLinear) {
  310. iter.MoveTo(iter, yi);
  311. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  312. lastyi = yi;
  313. } else {
  314. // Prepare current row
  315. if (yi == iter.y_index) {
  316. argb_cnv_rowptr = argb_cnv_rowptr + argb_cnv_rowstride;
  317. argb_cnv_rowstride = - argb_cnv_rowstride;
  318. } else {
  319. iter.MoveTo(iter, yi);
  320. argb_cnv_rowptr = argb_cnv_row;
  321. argb_cnv_rowstride = kRowSize;
  322. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  323. }
  324. // Prepare next row if necessary
  325. if (iter.y_index < max_yi) {
  326. int next_yi = yi < max_yi_minus_dyi ? yi + dyi : max_yi;
  327. iter.MoveTo(iter, next_yi);
  328. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr + argb_cnv_rowstride);
  329. } else {
  330. argb_cnv_rowstride = 0;
  331. }
  332. lastyi = yi;
  333. }
  334. }
  335. }
  336. ScaleARGBRowDown2(argb_cnv_rowptr + x_offset, argb_cnv_rowstride, dst_argb, dst_width);
  337. dst_argb += dst_stride_argb;
  338. yi += dyi;
  339. }
  340. free_aligned_buffer_64(argb_cnv_row);
  341. }
  342. // ScaleARGB ARGB Even
  343. // This is an optimized version for scaling down a ARGB to even
  344. // multiple of its original size.
  345. static void ScaleYUVToARGBDownEven(int src_width, int src_height,
  346. int dst_width, int dst_height,
  347. int src_stride_y,
  348. int src_stride_u,
  349. int src_stride_v,
  350. int dst_stride_argb,
  351. const uint8* src_y,
  352. const uint8* src_u,
  353. const uint8* src_v,
  354. uint8* dst_argb,
  355. int x, int dx, int y, int dy,
  356. enum FilterMode filtering,
  357. uint32 src_fourcc,
  358. mozilla::YUVColorSpace yuv_color_space) {
  359. int j;
  360. // Allocate 2 rows of ARGB for source conversion.
  361. const int kRowSize = (src_width * 4 + 15) & ~15;
  362. align_buffer_64(argb_cnv_row, kRowSize * 2);
  363. uint8* argb_cnv_rowptr = argb_cnv_row;
  364. int argb_cnv_rowstride = kRowSize;
  365. int col_step = dx >> 16;
  366. void (*ScaleARGBRowDownEven)(const uint8* src_argb, ptrdiff_t src_stride,
  367. int src_step, uint8* dst_argb, int dst_width) =
  368. filtering ? ScaleARGBRowDownEvenBox_C : ScaleARGBRowDownEven_C;
  369. assert(IS_ALIGNED(src_width, 2));
  370. assert(IS_ALIGNED(src_height, 2));
  371. int yi = y >> 16;
  372. const ptrdiff_t x_offset = (x >> 16) * 4;
  373. #if defined(HAS_SCALEARGBROWDOWNEVEN_SSE2)
  374. if (TestCpuFlag(kCpuHasSSE2)) {
  375. ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_Any_SSE2 :
  376. ScaleARGBRowDownEven_Any_SSE2;
  377. if (IS_ALIGNED(dst_width, 4)) {
  378. ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_SSE2 :
  379. ScaleARGBRowDownEven_SSE2;
  380. }
  381. }
  382. #endif
  383. #if defined(HAS_SCALEARGBROWDOWNEVEN_NEON)
  384. if (TestCpuFlag(kCpuHasNEON)) {
  385. ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_Any_NEON :
  386. ScaleARGBRowDownEven_Any_NEON;
  387. if (IS_ALIGNED(dst_width, 4)) {
  388. ScaleARGBRowDownEven = filtering ? ScaleARGBRowDownEvenBox_NEON :
  389. ScaleARGBRowDownEven_NEON;
  390. }
  391. }
  392. #endif
  393. YUVBuferIter iter;
  394. iter.src_width = src_width;
  395. iter.src_height = src_height;
  396. iter.src_stride_y = src_stride_y;
  397. iter.src_stride_u = src_stride_u;
  398. iter.src_stride_v = src_stride_v;
  399. iter.src_y = src_y;
  400. iter.src_u = src_u;
  401. iter.src_v = src_v;
  402. YUVBuferIter_Init(iter, src_fourcc, yuv_color_space);
  403. const int dyi = dy >> 16;
  404. int lastyi = yi;
  405. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  406. // Prepare next row if necessary
  407. if (filtering != kFilterLinear) {
  408. if ((yi + dyi) < (src_height - 1)) {
  409. iter.MoveTo(iter, yi + dyi);
  410. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr + argb_cnv_rowstride);
  411. } else {
  412. argb_cnv_rowstride = 0;
  413. }
  414. }
  415. if (filtering == kFilterLinear) {
  416. argb_cnv_rowstride = 0;
  417. }
  418. const int max_yi = src_height - 1;
  419. const int max_yi_minus_dyi = max_yi - dyi;
  420. for (j = 0; j < dst_height; ++j) {
  421. if (yi != lastyi) {
  422. if (yi > max_yi) {
  423. yi = max_yi;
  424. }
  425. if (yi != lastyi) {
  426. if (filtering == kFilterLinear) {
  427. iter.MoveTo(iter, yi);
  428. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  429. lastyi = yi;
  430. } else {
  431. // Prepare current row
  432. if (yi == iter.y_index) {
  433. argb_cnv_rowptr = argb_cnv_rowptr + argb_cnv_rowstride;
  434. argb_cnv_rowstride = - argb_cnv_rowstride;
  435. } else {
  436. iter.MoveTo(iter, yi);
  437. argb_cnv_rowptr = argb_cnv_row;
  438. argb_cnv_rowstride = kRowSize;
  439. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  440. }
  441. // Prepare next row if necessary
  442. if (iter.y_index < max_yi) {
  443. int next_yi = yi < max_yi_minus_dyi ? yi + dyi : max_yi;
  444. iter.MoveTo(iter, next_yi);
  445. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr + argb_cnv_rowstride);
  446. } else {
  447. argb_cnv_rowstride = 0;
  448. }
  449. lastyi = yi;
  450. }
  451. }
  452. }
  453. ScaleARGBRowDownEven(argb_cnv_rowptr + x_offset, argb_cnv_rowstride, col_step, dst_argb, dst_width);
  454. dst_argb += dst_stride_argb;
  455. yi += dyi;
  456. }
  457. free_aligned_buffer_64(argb_cnv_row);
  458. }
  459. // Scale YUV to ARGB down with bilinear interpolation.
  460. static void ScaleYUVToARGBBilinearDown(int src_width, int src_height,
  461. int dst_width, int dst_height,
  462. int src_stride_y,
  463. int src_stride_u,
  464. int src_stride_v,
  465. int dst_stride_argb,
  466. const uint8* src_y,
  467. const uint8* src_u,
  468. const uint8* src_v,
  469. uint8* dst_argb,
  470. int x, int dx, int y, int dy,
  471. enum FilterMode filtering,
  472. uint32 src_fourcc,
  473. mozilla::YUVColorSpace yuv_color_space) {
  474. int j;
  475. void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb,
  476. ptrdiff_t src_stride, int dst_width, int source_y_fraction) =
  477. InterpolateRow_C;
  478. void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb,
  479. int dst_width, int x, int dx) =
  480. (src_width >= 32768) ? ScaleARGBFilterCols64_C : ScaleARGBFilterCols_C;
  481. int64 xlast = x + (int64)(dst_width - 1) * dx;
  482. int64 xl = (dx >= 0) ? x : xlast;
  483. int64 xr = (dx >= 0) ? xlast : x;
  484. int clip_src_width;
  485. xl = (xl >> 16) & ~3; // Left edge aligned.
  486. xr = (xr >> 16) + 1; // Right most pixel used. Bilinear uses 2 pixels.
  487. xr = (xr + 1 + 3) & ~3; // 1 beyond 4 pixel aligned right most pixel.
  488. if (xr > src_width) {
  489. xr = src_width;
  490. }
  491. clip_src_width = (int)(xr - xl) * 4; // Width aligned to 4.
  492. const ptrdiff_t xl_offset = xl * 4;
  493. x -= (int)(xl << 16);
  494. // Allocate 2 row of ARGB for source conversion.
  495. const int kRowSize = (src_width * 4 + 15) & ~15;
  496. align_buffer_64(argb_cnv_row, kRowSize * 2);
  497. uint8* argb_cnv_rowptr = argb_cnv_row;
  498. int argb_cnv_rowstride = kRowSize;
  499. #if defined(HAS_INTERPOLATEROW_SSSE3)
  500. if (TestCpuFlag(kCpuHasSSSE3)) {
  501. InterpolateRow = InterpolateRow_Any_SSSE3;
  502. if (IS_ALIGNED(clip_src_width, 16)) {
  503. InterpolateRow = InterpolateRow_SSSE3;
  504. }
  505. }
  506. #endif
  507. #if defined(HAS_INTERPOLATEROW_AVX2)
  508. if (TestCpuFlag(kCpuHasAVX2)) {
  509. InterpolateRow = InterpolateRow_Any_AVX2;
  510. if (IS_ALIGNED(clip_src_width, 32)) {
  511. InterpolateRow = InterpolateRow_AVX2;
  512. }
  513. }
  514. #endif
  515. #if defined(HAS_INTERPOLATEROW_NEON)
  516. if (TestCpuFlag(kCpuHasNEON)) {
  517. InterpolateRow = InterpolateRow_Any_NEON;
  518. if (IS_ALIGNED(clip_src_width, 16)) {
  519. InterpolateRow = InterpolateRow_NEON;
  520. }
  521. }
  522. #endif
  523. #if defined(HAS_INTERPOLATEROW_DSPR2)
  524. if (TestCpuFlag(kCpuHasDSPR2) &&
  525. IS_ALIGNED(src_argb, 4) && IS_ALIGNED(argb_cnv_rowstride, 4)) {
  526. InterpolateRow = InterpolateRow_Any_DSPR2;
  527. if (IS_ALIGNED(clip_src_width, 4)) {
  528. InterpolateRow = InterpolateRow_DSPR2;
  529. }
  530. }
  531. #endif
  532. #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3)
  533. if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) {
  534. ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3;
  535. }
  536. #endif
  537. #if defined(HAS_SCALEARGBFILTERCOLS_NEON)
  538. if (TestCpuFlag(kCpuHasNEON)) {
  539. ScaleARGBFilterCols = ScaleARGBFilterCols_Any_NEON;
  540. if (IS_ALIGNED(dst_width, 4)) {
  541. ScaleARGBFilterCols = ScaleARGBFilterCols_NEON;
  542. }
  543. }
  544. #endif
  545. int yi = y >> 16;
  546. YUVBuferIter iter;
  547. iter.src_width = src_width;
  548. iter.src_height = src_height;
  549. iter.src_stride_y = src_stride_y;
  550. iter.src_stride_u = src_stride_u;
  551. iter.src_stride_v = src_stride_v;
  552. iter.src_y = src_y;
  553. iter.src_u = src_u;
  554. iter.src_v = src_v;
  555. YUVBuferIter_Init(iter, src_fourcc, yuv_color_space);
  556. iter.MoveTo(iter, yi);
  557. // TODO(fbarchard): Consider not allocating row buffer for kFilterLinear.
  558. // Allocate a row of ARGB.
  559. align_buffer_64(row, clip_src_width * 4);
  560. int lastyi = yi;
  561. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  562. // Prepare next row if necessary
  563. if (filtering != kFilterLinear) {
  564. if ((yi + 1) < src_height) {
  565. iter.MoveToNextRow(iter);
  566. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr + argb_cnv_rowstride);
  567. } else {
  568. argb_cnv_rowstride = 0;
  569. }
  570. }
  571. const int max_y = (src_height - 1) << 16;
  572. const int max_yi = src_height - 1;
  573. for (j = 0; j < dst_height; ++j) {
  574. yi = y >> 16;
  575. if (yi != lastyi) {
  576. if (y > max_y) {
  577. y = max_y;
  578. yi = y >> 16;
  579. }
  580. if (yi != lastyi) {
  581. if (filtering == kFilterLinear) {
  582. iter.MoveTo(iter, yi);
  583. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  584. lastyi = yi;
  585. } else {
  586. // Prepare current row
  587. if (yi == iter.y_index) {
  588. argb_cnv_rowptr = argb_cnv_rowptr + argb_cnv_rowstride;
  589. argb_cnv_rowstride = - argb_cnv_rowstride;
  590. } else {
  591. iter.MoveTo(iter, yi);
  592. argb_cnv_rowptr = argb_cnv_row;
  593. argb_cnv_rowstride = kRowSize;
  594. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr);
  595. }
  596. // Prepare next row if necessary
  597. if (iter.y_index < max_yi) {
  598. iter.MoveToNextRow(iter);
  599. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_rowptr + argb_cnv_rowstride);
  600. } else {
  601. argb_cnv_rowstride = 0;
  602. }
  603. lastyi = yi;
  604. }
  605. }
  606. }
  607. if (filtering == kFilterLinear) {
  608. ScaleARGBFilterCols(dst_argb, argb_cnv_rowptr + xl_offset, dst_width, x, dx);
  609. } else {
  610. int yf = (y >> 8) & 255;
  611. InterpolateRow(row, argb_cnv_rowptr + xl_offset, argb_cnv_rowstride, clip_src_width, yf);
  612. ScaleARGBFilterCols(dst_argb, row, dst_width, x, dx);
  613. }
  614. dst_argb += dst_stride_argb;
  615. y += dy;
  616. }
  617. free_aligned_buffer_64(row);
  618. free_aligned_buffer_64(argb_cnv_row);
  619. }
  620. // Scale YUV to ARGB up with bilinear interpolation.
  621. static void ScaleYUVToARGBBilinearUp(int src_width, int src_height,
  622. int dst_width, int dst_height,
  623. int src_stride_y,
  624. int src_stride_u,
  625. int src_stride_v,
  626. int dst_stride_argb,
  627. const uint8* src_y,
  628. const uint8* src_u,
  629. const uint8* src_v,
  630. uint8* dst_argb,
  631. int x, int dx, int y, int dy,
  632. enum FilterMode filtering,
  633. uint32 src_fourcc,
  634. mozilla::YUVColorSpace yuv_color_space) {
  635. int j;
  636. void (*InterpolateRow)(uint8* dst_argb, const uint8* src_argb,
  637. ptrdiff_t src_stride, int dst_width, int source_y_fraction) =
  638. InterpolateRow_C;
  639. void (*ScaleARGBFilterCols)(uint8* dst_argb, const uint8* src_argb,
  640. int dst_width, int x, int dx) =
  641. filtering ? ScaleARGBFilterCols_C : ScaleARGBCols_C;
  642. const int max_y = (src_height - 1) << 16;
  643. // Allocate 1 row of ARGB for source conversion.
  644. align_buffer_64(argb_cnv_row, src_width * 4);
  645. #if defined(HAS_INTERPOLATEROW_SSSE3)
  646. if (TestCpuFlag(kCpuHasSSSE3)) {
  647. InterpolateRow = InterpolateRow_Any_SSSE3;
  648. if (IS_ALIGNED(dst_width, 4)) {
  649. InterpolateRow = InterpolateRow_SSSE3;
  650. }
  651. }
  652. #endif
  653. #if defined(HAS_INTERPOLATEROW_AVX2)
  654. if (TestCpuFlag(kCpuHasAVX2)) {
  655. InterpolateRow = InterpolateRow_Any_AVX2;
  656. if (IS_ALIGNED(dst_width, 8)) {
  657. InterpolateRow = InterpolateRow_AVX2;
  658. }
  659. }
  660. #endif
  661. #if defined(HAS_INTERPOLATEROW_NEON)
  662. if (TestCpuFlag(kCpuHasNEON)) {
  663. InterpolateRow = InterpolateRow_Any_NEON;
  664. if (IS_ALIGNED(dst_width, 4)) {
  665. InterpolateRow = InterpolateRow_NEON;
  666. }
  667. }
  668. #endif
  669. #if defined(HAS_INTERPOLATEROW_DSPR2)
  670. if (TestCpuFlag(kCpuHasDSPR2) &&
  671. IS_ALIGNED(dst_argb, 4) && IS_ALIGNED(dst_stride_argb, 4)) {
  672. InterpolateRow = InterpolateRow_DSPR2;
  673. }
  674. #endif
  675. if (src_width >= 32768) {
  676. ScaleARGBFilterCols = filtering ?
  677. ScaleARGBFilterCols64_C : ScaleARGBCols64_C;
  678. }
  679. #if defined(HAS_SCALEARGBFILTERCOLS_SSSE3)
  680. if (filtering && TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) {
  681. ScaleARGBFilterCols = ScaleARGBFilterCols_SSSE3;
  682. }
  683. #endif
  684. #if defined(HAS_SCALEARGBFILTERCOLS_NEON)
  685. if (filtering && TestCpuFlag(kCpuHasNEON)) {
  686. ScaleARGBFilterCols = ScaleARGBFilterCols_Any_NEON;
  687. if (IS_ALIGNED(dst_width, 4)) {
  688. ScaleARGBFilterCols = ScaleARGBFilterCols_NEON;
  689. }
  690. }
  691. #endif
  692. #if defined(HAS_SCALEARGBCOLS_SSE2)
  693. if (!filtering && TestCpuFlag(kCpuHasSSE2) && src_width < 32768) {
  694. ScaleARGBFilterCols = ScaleARGBCols_SSE2;
  695. }
  696. #endif
  697. #if defined(HAS_SCALEARGBCOLS_NEON)
  698. if (!filtering && TestCpuFlag(kCpuHasNEON)) {
  699. ScaleARGBFilterCols = ScaleARGBCols_Any_NEON;
  700. if (IS_ALIGNED(dst_width, 8)) {
  701. ScaleARGBFilterCols = ScaleARGBCols_NEON;
  702. }
  703. }
  704. #endif
  705. if (!filtering && src_width * 2 == dst_width && x < 0x8000) {
  706. ScaleARGBFilterCols = ScaleARGBColsUp2_C;
  707. #if defined(HAS_SCALEARGBCOLSUP2_SSE2)
  708. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) {
  709. ScaleARGBFilterCols = ScaleARGBColsUp2_SSE2;
  710. }
  711. #endif
  712. }
  713. if (y > max_y) {
  714. y = max_y;
  715. }
  716. int yi = y >> 16;
  717. YUVBuferIter iter;
  718. iter.src_width = src_width;
  719. iter.src_height = src_height;
  720. iter.src_stride_y = src_stride_y;
  721. iter.src_stride_u = src_stride_u;
  722. iter.src_stride_v = src_stride_v;
  723. iter.src_y = src_y;
  724. iter.src_u = src_u;
  725. iter.src_v = src_v;
  726. YUVBuferIter_Init(iter, src_fourcc, yuv_color_space);
  727. iter.MoveTo(iter, yi);
  728. // Allocate 2 rows of ARGB.
  729. const int kRowSize = (dst_width * 4 + 15) & ~15;
  730. align_buffer_64(row, kRowSize * 2);
  731. uint8* rowptr = row;
  732. int rowstride = kRowSize;
  733. int lastyi = yi;
  734. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_row);
  735. ScaleARGBFilterCols(rowptr, argb_cnv_row, dst_width, x, dx);
  736. if (filtering == kFilterLinear) {
  737. rowstride = 0;
  738. }
  739. // Prepare next row if necessary
  740. if (filtering != kFilterLinear) {
  741. if ((yi + 1) < src_height) {
  742. iter.MoveToNextRow(iter);
  743. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_row);
  744. ScaleARGBFilterCols(rowptr + rowstride, argb_cnv_row, dst_width, x, dx);
  745. }else {
  746. rowstride = 0;
  747. }
  748. }
  749. const int max_yi = src_height - 1;
  750. for (j = 0; j < dst_height; ++j) {
  751. yi = y >> 16;
  752. if (yi != lastyi) {
  753. if (y > max_y) {
  754. y = max_y;
  755. yi = y >> 16;
  756. }
  757. if (yi != lastyi) {
  758. if (filtering == kFilterLinear) {
  759. iter.MoveToNextRow(iter);
  760. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_row);
  761. ScaleARGBFilterCols(rowptr, argb_cnv_row, dst_width, x, dx);
  762. } else {
  763. // Prepare next row if necessary
  764. if (yi < max_yi) {
  765. iter.MoveToNextRow(iter);
  766. rowptr += rowstride;
  767. rowstride = -rowstride;
  768. // TODO(fbarchard): Convert the clipped region of row.
  769. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_row);
  770. ScaleARGBFilterCols(rowptr + rowstride, argb_cnv_row, dst_width, x, dx);
  771. } else {
  772. rowstride = 0;
  773. }
  774. }
  775. lastyi = yi;
  776. }
  777. }
  778. if (filtering == kFilterLinear) {
  779. InterpolateRow(dst_argb, rowptr, 0, dst_width * 4, 0);
  780. } else {
  781. int yf = (y >> 8) & 255;
  782. InterpolateRow(dst_argb, rowptr, rowstride, dst_width * 4, yf);
  783. }
  784. dst_argb += dst_stride_argb;
  785. y += dy;
  786. }
  787. free_aligned_buffer_64(row);
  788. free_aligned_buffer_64(argb_cnv_row);
  789. }
  790. // Scale ARGB to/from any dimensions, without interpolation.
  791. // Fixed point math is used for performance: The upper 16 bits
  792. // of x and dx is the integer part of the source position and
  793. // the lower 16 bits are the fixed decimal part.
  794. static void ScaleYUVToARGBSimple(int src_width, int src_height,
  795. int dst_width, int dst_height,
  796. int src_stride_y,
  797. int src_stride_u,
  798. int src_stride_v,
  799. int dst_stride_argb,
  800. const uint8* src_y,
  801. const uint8* src_u,
  802. const uint8* src_v,
  803. uint8* dst_argb,
  804. int x, int dx, int y, int dy,
  805. uint32 src_fourcc,
  806. mozilla::YUVColorSpace yuv_color_space) {
  807. int j;
  808. void (*ScaleARGBCols)(uint8* dst_argb, const uint8* src_argb,
  809. int dst_width, int x, int dx) =
  810. (src_width >= 32768) ? ScaleARGBCols64_C : ScaleARGBCols_C;
  811. // Allocate 1 row of ARGB for source conversion.
  812. align_buffer_64(argb_cnv_row, src_width * 4);
  813. #if defined(HAS_SCALEARGBCOLS_SSE2)
  814. if (TestCpuFlag(kCpuHasSSE2) && src_width < 32768) {
  815. ScaleARGBCols = ScaleARGBCols_SSE2;
  816. }
  817. #endif
  818. #if defined(HAS_SCALEARGBCOLS_NEON)
  819. if (TestCpuFlag(kCpuHasNEON)) {
  820. ScaleARGBCols = ScaleARGBCols_Any_NEON;
  821. if (IS_ALIGNED(dst_width, 8)) {
  822. ScaleARGBCols = ScaleARGBCols_NEON;
  823. }
  824. }
  825. #endif
  826. if (src_width * 2 == dst_width && x < 0x8000) {
  827. ScaleARGBCols = ScaleARGBColsUp2_C;
  828. #if defined(HAS_SCALEARGBCOLSUP2_SSE2)
  829. if (TestCpuFlag(kCpuHasSSE2) && IS_ALIGNED(dst_width, 8)) {
  830. ScaleARGBCols = ScaleARGBColsUp2_SSE2;
  831. }
  832. #endif
  833. }
  834. int yi = y >> 16;
  835. YUVBuferIter iter;
  836. iter.src_width = src_width;
  837. iter.src_height = src_height;
  838. iter.src_stride_y = src_stride_y;
  839. iter.src_stride_u = src_stride_u;
  840. iter.src_stride_v = src_stride_v;
  841. iter.src_y = src_y;
  842. iter.src_u = src_u;
  843. iter.src_v = src_v;
  844. YUVBuferIter_Init(iter, src_fourcc, yuv_color_space);
  845. iter.MoveTo(iter, yi);
  846. int lasty = yi;
  847. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_row);
  848. for (j = 0; j < dst_height; ++j) {
  849. yi = y >> 16;
  850. if (yi != lasty) {
  851. iter.MoveTo(iter, yi);
  852. YUVBuferIter_ConvertToARGBRow(iter, argb_cnv_row);
  853. lasty = yi;
  854. }
  855. ScaleARGBCols(dst_argb, argb_cnv_row, dst_width, x, dx);
  856. dst_argb += dst_stride_argb;
  857. y += dy;
  858. }
  859. free_aligned_buffer_64(argb_cnv_row);
  860. }
  861. static void YUVToARGBCopy(const uint8* src_y, int src_stride_y,
  862. const uint8* src_u, int src_stride_u,
  863. const uint8* src_v, int src_stride_v,
  864. int src_width, int src_height,
  865. uint8* dst_argb, int dst_stride_argb,
  866. int dst_width, int dst_height,
  867. uint32 src_fourcc,
  868. mozilla::YUVColorSpace yuv_color_space)
  869. {
  870. YUVBuferIter iter;
  871. iter.src_width = src_width;
  872. iter.src_height = src_height;
  873. iter.src_stride_y = src_stride_y;
  874. iter.src_stride_u = src_stride_u;
  875. iter.src_stride_v = src_stride_v;
  876. iter.src_y = src_y;
  877. iter.src_u = src_u;
  878. iter.src_v = src_v;
  879. YUVBuferIter_Init(iter, src_fourcc, yuv_color_space);
  880. for (int j = 0; j < dst_height; ++j) {
  881. YUVBuferIter_ConvertToARGBRow(iter, dst_argb);
  882. iter.MoveToNextRow(iter);
  883. dst_argb += dst_stride_argb;
  884. }
  885. }
  886. static void ScaleYUVToARGB(const uint8* src_y, int src_stride_y,
  887. const uint8* src_u, int src_stride_u,
  888. const uint8* src_v, int src_stride_v,
  889. int src_width, int src_height,
  890. uint8* dst_argb, int dst_stride_argb,
  891. int dst_width, int dst_height,
  892. enum FilterMode filtering,
  893. uint32 src_fourcc,
  894. mozilla::YUVColorSpace yuv_color_space)
  895. {
  896. // Initial source x/y coordinate and step values as 16.16 fixed point.
  897. int x = 0;
  898. int y = 0;
  899. int dx = 0;
  900. int dy = 0;
  901. // ARGB does not support box filter yet, but allow the user to pass it.
  902. // Simplify filtering when possible.
  903. filtering = ScaleFilterReduce(src_width, src_height,
  904. dst_width, dst_height,
  905. filtering);
  906. ScaleSlope(src_width, src_height, dst_width, dst_height, filtering,
  907. &x, &y, &dx, &dy);
  908. // Special case for integer step values.
  909. if (((dx | dy) & 0xffff) == 0) {
  910. if (!dx || !dy) { // 1 pixel wide and/or tall.
  911. filtering = kFilterNone;
  912. } else {
  913. // Optimized even scale down. ie 2, 4, 6, 8, 10x.
  914. if (!(dx & 0x10000) && !(dy & 0x10000)) {
  915. if (dx == 0x20000) {
  916. // Optimized 1/2 downsample.
  917. ScaleYUVToARGBDown2(src_width, src_height,
  918. dst_width, dst_height,
  919. src_stride_y,
  920. src_stride_u,
  921. src_stride_v,
  922. dst_stride_argb,
  923. src_y,
  924. src_u,
  925. src_v,
  926. dst_argb,
  927. x, dx, y, dy,
  928. filtering,
  929. src_fourcc,
  930. yuv_color_space);
  931. return;
  932. }
  933. ScaleYUVToARGBDownEven(src_width, src_height,
  934. dst_width, dst_height,
  935. src_stride_y,
  936. src_stride_u,
  937. src_stride_v,
  938. dst_stride_argb,
  939. src_y,
  940. src_u,
  941. src_v,
  942. dst_argb,
  943. x, dx, y, dy,
  944. filtering,
  945. src_fourcc,
  946. yuv_color_space);
  947. return;
  948. }
  949. // Optimized odd scale down. ie 3, 5, 7, 9x.
  950. if ((dx & 0x10000) && (dy & 0x10000)) {
  951. filtering = kFilterNone;
  952. if (dx == 0x10000 && dy == 0x10000) {
  953. // Straight conversion and copy.
  954. YUVToARGBCopy(src_y, src_stride_y,
  955. src_u, src_stride_u,
  956. src_v, src_stride_v,
  957. src_width, src_height,
  958. dst_argb, dst_stride_argb,
  959. dst_width, dst_height,
  960. src_fourcc,
  961. yuv_color_space);
  962. return;
  963. }
  964. }
  965. }
  966. }
  967. if (filtering && dy < 65536) {
  968. ScaleYUVToARGBBilinearUp(src_width, src_height,
  969. dst_width, dst_height,
  970. src_stride_y,
  971. src_stride_u,
  972. src_stride_v,
  973. dst_stride_argb,
  974. src_y,
  975. src_u,
  976. src_v,
  977. dst_argb,
  978. x, dx, y, dy,
  979. filtering,
  980. src_fourcc,
  981. yuv_color_space);
  982. return;
  983. }
  984. if (filtering) {
  985. ScaleYUVToARGBBilinearDown(src_width, src_height,
  986. dst_width, dst_height,
  987. src_stride_y,
  988. src_stride_u,
  989. src_stride_v,
  990. dst_stride_argb,
  991. src_y,
  992. src_u,
  993. src_v,
  994. dst_argb,
  995. x, dx, y, dy,
  996. filtering,
  997. src_fourcc,
  998. yuv_color_space);
  999. return;
  1000. }
  1001. ScaleYUVToARGBSimple(src_width, src_height,
  1002. dst_width, dst_height,
  1003. src_stride_y,
  1004. src_stride_u,
  1005. src_stride_v,
  1006. dst_stride_argb,
  1007. src_y,
  1008. src_u,
  1009. src_v,
  1010. dst_argb,
  1011. x, dx, y, dy,
  1012. src_fourcc,
  1013. yuv_color_space);
  1014. }
  1015. bool IsConvertSupported(uint32 src_fourcc)
  1016. {
  1017. if (src_fourcc == FOURCC_I444 ||
  1018. src_fourcc == FOURCC_I422 ||
  1019. src_fourcc == FOURCC_I420) {
  1020. return true;
  1021. }
  1022. return false;
  1023. }
  1024. LIBYUV_API
  1025. int YUVToARGBScale(const uint8* src_y, int src_stride_y,
  1026. const uint8* src_u, int src_stride_u,
  1027. const uint8* src_v, int src_stride_v,
  1028. uint32 src_fourcc,
  1029. mozilla::YUVColorSpace yuv_color_space,
  1030. int src_width, int src_height,
  1031. uint8* dst_argb, int dst_stride_argb,
  1032. int dst_width, int dst_height,
  1033. enum FilterMode filtering)
  1034. {
  1035. if (!src_y || !src_u || !src_v ||
  1036. src_width == 0 || src_height == 0 ||
  1037. !dst_argb || dst_width <= 0 || dst_height <= 0) {
  1038. return -1;
  1039. }
  1040. if (!IsConvertSupported(src_fourcc)) {
  1041. return -1;
  1042. }
  1043. ScaleYUVToARGB(src_y, src_stride_y,
  1044. src_u, src_stride_u,
  1045. src_v, src_stride_v,
  1046. src_width, src_height,
  1047. dst_argb, dst_stride_argb,
  1048. dst_width, dst_height,
  1049. filtering,
  1050. src_fourcc,
  1051. yuv_color_space);
  1052. return 0;
  1053. }
  1054. #ifdef __cplusplus
  1055. } // extern "C"
  1056. } // namespace libyuv
  1057. #endif