vp9_frame_scale_ssse3.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /*
  2. * Copyright (c) 2016 The WebM 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 <tmmintrin.h> // SSSE3
  11. #include "./vp9_rtcd.h"
  12. #include "./vpx_dsp_rtcd.h"
  13. #include "./vpx_scale_rtcd.h"
  14. #include "vpx_dsp/x86/convolve_ssse3.h"
  15. #include "vpx_dsp/x86/mem_sse2.h"
  16. #include "vpx_dsp/x86/transpose_sse2.h"
  17. #include "vpx_scale/yv12config.h"
  18. static INLINE __m128i scale_plane_2_to_1_phase_0_kernel(
  19. const uint8_t *const src, const __m128i *const mask) {
  20. const __m128i a = _mm_loadu_si128((const __m128i *)(&src[0]));
  21. const __m128i b = _mm_loadu_si128((const __m128i *)(&src[16]));
  22. const __m128i a_and = _mm_and_si128(a, *mask);
  23. const __m128i b_and = _mm_and_si128(b, *mask);
  24. return _mm_packus_epi16(a_and, b_and);
  25. }
  26. static void scale_plane_2_to_1_phase_0(const uint8_t *src,
  27. const ptrdiff_t src_stride, uint8_t *dst,
  28. const ptrdiff_t dst_stride,
  29. const int dst_w, const int dst_h) {
  30. const int max_width = (dst_w + 15) & ~15;
  31. const __m128i mask = _mm_set1_epi16(0x00FF);
  32. int y = dst_h;
  33. do {
  34. int x = max_width;
  35. do {
  36. const __m128i d = scale_plane_2_to_1_phase_0_kernel(src, &mask);
  37. _mm_storeu_si128((__m128i *)dst, d);
  38. src += 32;
  39. dst += 16;
  40. x -= 16;
  41. } while (x);
  42. src += 2 * (src_stride - max_width);
  43. dst += dst_stride - max_width;
  44. } while (--y);
  45. }
  46. static void scale_plane_4_to_1_phase_0(const uint8_t *src,
  47. const ptrdiff_t src_stride, uint8_t *dst,
  48. const ptrdiff_t dst_stride,
  49. const int dst_w, const int dst_h) {
  50. const int max_width = (dst_w + 15) & ~15;
  51. const __m128i mask = _mm_set1_epi32(0x000000FF);
  52. int y = dst_h;
  53. do {
  54. int x = max_width;
  55. do {
  56. const __m128i d0 = scale_plane_2_to_1_phase_0_kernel(&src[0], &mask);
  57. const __m128i d1 = scale_plane_2_to_1_phase_0_kernel(&src[32], &mask);
  58. const __m128i d2 = _mm_packus_epi16(d0, d1);
  59. _mm_storeu_si128((__m128i *)dst, d2);
  60. src += 64;
  61. dst += 16;
  62. x -= 16;
  63. } while (x);
  64. src += 4 * (src_stride - max_width);
  65. dst += dst_stride - max_width;
  66. } while (--y);
  67. }
  68. static INLINE __m128i scale_plane_bilinear_kernel(const __m128i *const s,
  69. const __m128i c0c1) {
  70. const __m128i k_64 = _mm_set1_epi16(1 << 6);
  71. const __m128i t0 = _mm_maddubs_epi16(s[0], c0c1);
  72. const __m128i t1 = _mm_maddubs_epi16(s[1], c0c1);
  73. // round and shift by 7 bit each 16 bit
  74. const __m128i t2 = _mm_adds_epi16(t0, k_64);
  75. const __m128i t3 = _mm_adds_epi16(t1, k_64);
  76. const __m128i t4 = _mm_srai_epi16(t2, 7);
  77. const __m128i t5 = _mm_srai_epi16(t3, 7);
  78. return _mm_packus_epi16(t4, t5);
  79. }
  80. static void scale_plane_2_to_1_bilinear(const uint8_t *src,
  81. const ptrdiff_t src_stride,
  82. uint8_t *dst,
  83. const ptrdiff_t dst_stride,
  84. const int dst_w, const int dst_h,
  85. const __m128i c0c1) {
  86. const int max_width = (dst_w + 15) & ~15;
  87. int y = dst_h;
  88. do {
  89. int x = max_width;
  90. do {
  91. __m128i s[2], d[2];
  92. // Horizontal
  93. // Even rows
  94. s[0] = _mm_loadu_si128((const __m128i *)(src + 0));
  95. s[1] = _mm_loadu_si128((const __m128i *)(src + 16));
  96. d[0] = scale_plane_bilinear_kernel(s, c0c1);
  97. // odd rows
  98. s[0] = _mm_loadu_si128((const __m128i *)(src + src_stride + 0));
  99. s[1] = _mm_loadu_si128((const __m128i *)(src + src_stride + 16));
  100. d[1] = scale_plane_bilinear_kernel(s, c0c1);
  101. // Vertical
  102. s[0] = _mm_unpacklo_epi8(d[0], d[1]);
  103. s[1] = _mm_unpackhi_epi8(d[0], d[1]);
  104. d[0] = scale_plane_bilinear_kernel(s, c0c1);
  105. _mm_storeu_si128((__m128i *)dst, d[0]);
  106. src += 32;
  107. dst += 16;
  108. x -= 16;
  109. } while (x);
  110. src += 2 * (src_stride - max_width);
  111. dst += dst_stride - max_width;
  112. } while (--y);
  113. }
  114. static void scale_plane_4_to_1_bilinear(const uint8_t *src,
  115. const ptrdiff_t src_stride,
  116. uint8_t *dst,
  117. const ptrdiff_t dst_stride,
  118. const int dst_w, const int dst_h,
  119. const __m128i c0c1) {
  120. const int max_width = (dst_w + 15) & ~15;
  121. int y = dst_h;
  122. do {
  123. int x = max_width;
  124. do {
  125. __m128i s[8], d[8];
  126. // Note: Using _mm_packus_epi32() in SSE4.1 could be faster.
  127. // Here we tried to not use shuffle instructions which would be slow
  128. // on some x86 CPUs.
  129. // Horizontal
  130. // 000 001 xx xx 004 005 xx xx 008 009 xx xx 00C 00D xx xx
  131. // 010 011 xx xx 014 015 xx xx 018 019 xx xx 01C 01D xx xx
  132. // 020 021 xx xx 024 025 xx xx 028 029 xx xx 02C 02D xx xx
  133. // 030 031 xx xx 034 035 xx xx 038 039 xx xx 03C 03D xx xx
  134. // 100 101 xx xx 104 105 xx xx 108 109 xx xx 10C 10D xx xx
  135. // 110 111 xx xx 114 115 xx xx 118 119 xx xx 11C 11D xx xx
  136. // 120 121 xx xx 124 125 xx xx 128 129 xx xx 12C 12D xx xx
  137. // 130 131 xx xx 134 135 xx xx 138 139 xx xx 13C 13D xx xx
  138. s[0] = _mm_loadu_si128((const __m128i *)(&src[0]));
  139. s[1] = _mm_loadu_si128((const __m128i *)(&src[16]));
  140. s[2] = _mm_loadu_si128((const __m128i *)(&src[32]));
  141. s[3] = _mm_loadu_si128((const __m128i *)(&src[48]));
  142. s[4] = _mm_loadu_si128((const __m128i *)(src + src_stride + 0));
  143. s[5] = _mm_loadu_si128((const __m128i *)(src + src_stride + 16));
  144. s[6] = _mm_loadu_si128((const __m128i *)(src + src_stride + 32));
  145. s[7] = _mm_loadu_si128((const __m128i *)(src + src_stride + 48));
  146. // 000 001 100 101 xx xx xx xx 004 005 104 105 xx xx xx xx
  147. // 008 009 108 109 xx xx xx xx 00C 00D 10C 10D xx xx xx xx
  148. // 010 011 110 111 xx xx xx xx 014 015 114 115 xx xx xx xx
  149. // 018 019 118 119 xx xx xx xx 01C 01D 11C 11D xx xx xx xx
  150. // 020 021 120 121 xx xx xx xx 024 025 124 125 xx xx xx xx
  151. // 028 029 128 129 xx xx xx xx 02C 02D 12C 12D xx xx xx xx
  152. // 030 031 130 131 xx xx xx xx 034 035 134 135 xx xx xx xx
  153. // 038 039 138 139 xx xx xx xx 03C 03D 13C 13D xx xx xx xx
  154. d[0] = _mm_unpacklo_epi16(s[0], s[4]);
  155. d[1] = _mm_unpackhi_epi16(s[0], s[4]);
  156. d[2] = _mm_unpacklo_epi16(s[1], s[5]);
  157. d[3] = _mm_unpackhi_epi16(s[1], s[5]);
  158. d[4] = _mm_unpacklo_epi16(s[2], s[6]);
  159. d[5] = _mm_unpackhi_epi16(s[2], s[6]);
  160. d[6] = _mm_unpacklo_epi16(s[3], s[7]);
  161. d[7] = _mm_unpackhi_epi16(s[3], s[7]);
  162. // 000 001 100 101 008 009 108 109 xx xx xx xx xx xx xx xx
  163. // 004 005 104 105 00C 00D 10C 10D xx xx xx xx xx xx xx xx
  164. // 010 011 110 111 018 019 118 119 xx xx xx xx xx xx xx xx
  165. // 014 015 114 115 01C 01D 11C 11D xx xx xx xx xx xx xx xx
  166. // 020 021 120 121 028 029 128 129 xx xx xx xx xx xx xx xx
  167. // 024 025 124 125 02C 02D 12C 12D xx xx xx xx xx xx xx xx
  168. // 030 031 130 131 038 039 138 139 xx xx xx xx xx xx xx xx
  169. // 034 035 134 135 03C 03D 13C 13D xx xx xx xx xx xx xx xx
  170. s[0] = _mm_unpacklo_epi32(d[0], d[1]);
  171. s[1] = _mm_unpackhi_epi32(d[0], d[1]);
  172. s[2] = _mm_unpacklo_epi32(d[2], d[3]);
  173. s[3] = _mm_unpackhi_epi32(d[2], d[3]);
  174. s[4] = _mm_unpacklo_epi32(d[4], d[5]);
  175. s[5] = _mm_unpackhi_epi32(d[4], d[5]);
  176. s[6] = _mm_unpacklo_epi32(d[6], d[7]);
  177. s[7] = _mm_unpackhi_epi32(d[6], d[7]);
  178. // 000 001 100 101 004 005 104 105 008 009 108 109 00C 00D 10C 10D
  179. // 010 011 110 111 014 015 114 115 018 019 118 119 01C 01D 11C 11D
  180. // 020 021 120 121 024 025 124 125 028 029 128 129 02C 02D 12C 12D
  181. // 030 031 130 131 034 035 134 135 038 039 138 139 03C 03D 13C 13D
  182. d[0] = _mm_unpacklo_epi32(s[0], s[1]);
  183. d[1] = _mm_unpacklo_epi32(s[2], s[3]);
  184. d[2] = _mm_unpacklo_epi32(s[4], s[5]);
  185. d[3] = _mm_unpacklo_epi32(s[6], s[7]);
  186. d[0] = scale_plane_bilinear_kernel(&d[0], c0c1);
  187. d[1] = scale_plane_bilinear_kernel(&d[2], c0c1);
  188. // Vertical
  189. d[0] = scale_plane_bilinear_kernel(d, c0c1);
  190. _mm_storeu_si128((__m128i *)dst, d[0]);
  191. src += 64;
  192. dst += 16;
  193. x -= 16;
  194. } while (x);
  195. src += 4 * (src_stride - max_width);
  196. dst += dst_stride - max_width;
  197. } while (--y);
  198. }
  199. static void scale_plane_2_to_1_general(const uint8_t *src, const int src_stride,
  200. uint8_t *dst, const int dst_stride,
  201. const int w, const int h,
  202. const int16_t *const coef,
  203. uint8_t *const temp_buffer) {
  204. const int width_hor = (w + 3) & ~3;
  205. const int width_ver = (w + 7) & ~7;
  206. const int height_hor = (2 * h + SUBPEL_TAPS - 2 + 7) & ~7;
  207. const int height_ver = (h + 3) & ~3;
  208. int x, y = height_hor;
  209. uint8_t *t = temp_buffer;
  210. __m128i s[11], d[4];
  211. __m128i f[4];
  212. assert(w && h);
  213. shuffle_filter_ssse3(coef, f);
  214. src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2 + 1;
  215. // horizontal 4x8
  216. do {
  217. load_8bit_8x8(src + 2, src_stride, s);
  218. // 00 01 10 11 20 21 30 31 40 41 50 51 60 61 70 71
  219. // 02 03 12 13 22 23 32 33 42 43 52 53 62 63 72 73
  220. // 04 05 14 15 24 25 34 35 44 45 54 55 64 65 74 75
  221. // 06 07 16 17 26 27 36 37 46 47 56 57 66 67 76 77 (overlapped)
  222. transpose_16bit_4x8(s, s);
  223. x = width_hor;
  224. do {
  225. src += 8;
  226. load_8bit_8x8(src, src_stride, &s[3]);
  227. // 06 07 16 17 26 27 36 37 46 47 56 57 66 67 76 77
  228. // 08 09 18 19 28 29 38 39 48 49 58 59 68 69 78 79
  229. // 0A 0B 1A 1B 2A 2B 3A 3B 4A 4B 5A 5B 6A 6B 7A 7B
  230. // 0C 0D 1C 1D 2C 2D 3C 3D 4C 4D 5C 5D 6C 6D 7C 7D
  231. transpose_16bit_4x8(&s[3], &s[3]);
  232. d[0] = convolve8_8_ssse3(&s[0], f); // 00 10 20 30 40 50 60 70
  233. d[1] = convolve8_8_ssse3(&s[1], f); // 01 11 21 31 41 51 61 71
  234. d[2] = convolve8_8_ssse3(&s[2], f); // 02 12 22 32 42 52 62 72
  235. d[3] = convolve8_8_ssse3(&s[3], f); // 03 13 23 33 43 53 63 73
  236. // 00 10 20 30 40 50 60 70 02 12 22 32 42 52 62 72
  237. // 01 11 21 31 41 51 61 71 03 13 23 33 43 53 63 73
  238. d[0] = _mm_packus_epi16(d[0], d[2]);
  239. d[1] = _mm_packus_epi16(d[1], d[3]);
  240. // 00 10 01 11 20 30 21 31 40 50 41 51 60 70 61 71
  241. // 02 12 03 13 22 32 23 33 42 52 43 53 62 72 63 73
  242. d[2] = _mm_unpacklo_epi16(d[0], d[1]);
  243. d[3] = _mm_unpackhi_epi16(d[0], d[1]);
  244. // 00 10 01 11 02 12 03 13 20 30 21 31 22 32 23 33
  245. // 40 50 41 51 42 52 43 53 60 70 61 71 62 72 63 73
  246. d[0] = _mm_unpacklo_epi32(d[2], d[3]);
  247. d[1] = _mm_unpackhi_epi32(d[2], d[3]);
  248. store_8bit_8x4_from_16x2(d, t, 2 * width_hor);
  249. s[0] = s[4];
  250. s[1] = s[5];
  251. s[2] = s[6];
  252. t += 8;
  253. x -= 4;
  254. } while (x);
  255. src += 8 * src_stride - 2 * width_hor;
  256. t += 6 * width_hor;
  257. y -= 8;
  258. } while (y);
  259. // vertical 8x4
  260. x = width_ver;
  261. t = temp_buffer;
  262. do {
  263. // 00 10 01 11 02 12 03 13 04 14 05 15 06 16 07 17
  264. // 20 30 21 31 22 32 23 33 24 34 25 35 26 36 27 37
  265. // 40 50 41 51 42 52 43 53 44 54 45 55 46 56 47 57
  266. s[0] = _mm_loadu_si128((const __m128i *)(t + 0 * width_hor));
  267. s[1] = _mm_loadu_si128((const __m128i *)(t + 2 * width_hor));
  268. s[2] = _mm_loadu_si128((const __m128i *)(t + 4 * width_hor));
  269. t += 6 * width_hor;
  270. y = height_ver;
  271. do {
  272. // 60 70 61 71 62 72 63 73 64 74 65 75 66 76 67 77
  273. // 80 90 81 91 82 92 83 93 84 94 85 95 86 96 87 77
  274. // A0 B0 A1 B1 A2 B2 A3 B3 A4 B4 A5 B5 A6 B6 A7 77
  275. // C0 D0 C1 D1 C2 D2 C3 D3 C4 D4 C5 D5 C6 D6 C7 77
  276. loadu_8bit_16x4(t, 2 * width_hor, &s[3]);
  277. t += 8 * width_hor;
  278. d[0] = convolve8_8_ssse3(&s[0], f); // 00 01 02 03 04 05 06 07
  279. d[1] = convolve8_8_ssse3(&s[1], f); // 10 11 12 13 14 15 16 17
  280. d[2] = convolve8_8_ssse3(&s[2], f); // 20 21 22 23 24 25 26 27
  281. d[3] = convolve8_8_ssse3(&s[3], f); // 30 31 32 33 34 35 36 37
  282. // 00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17
  283. // 20 21 22 23 24 25 26 27 30 31 32 33 34 35 36 37
  284. d[0] = _mm_packus_epi16(d[0], d[1]);
  285. d[1] = _mm_packus_epi16(d[2], d[3]);
  286. store_8bit_8x4_from_16x2(d, dst, dst_stride);
  287. s[0] = s[4];
  288. s[1] = s[5];
  289. s[2] = s[6];
  290. dst += 4 * dst_stride;
  291. y -= 4;
  292. } while (y);
  293. t -= width_hor * (2 * height_ver + 6);
  294. t += 16;
  295. dst -= height_ver * dst_stride;
  296. dst += 8;
  297. x -= 8;
  298. } while (x);
  299. }
  300. static void scale_plane_4_to_1_general(const uint8_t *src, const int src_stride,
  301. uint8_t *dst, const int dst_stride,
  302. const int w, const int h,
  303. const int16_t *const coef,
  304. uint8_t *const temp_buffer) {
  305. const int width_hor = (w + 1) & ~1;
  306. const int width_ver = (w + 7) & ~7;
  307. const int height_hor = (4 * h + SUBPEL_TAPS - 2 + 7) & ~7;
  308. const int height_ver = (h + 1) & ~1;
  309. int x, y = height_hor;
  310. uint8_t *t = temp_buffer;
  311. __m128i s[11], d[4];
  312. __m128i f[4];
  313. assert(w && h);
  314. shuffle_filter_ssse3(coef, f);
  315. src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2 + 3;
  316. // horizontal 2x8
  317. do {
  318. load_8bit_8x8(src + 4, src_stride, s);
  319. // 00 01 10 11 20 21 30 31 40 41 50 51 60 61 70 71
  320. // 02 03 12 13 22 23 32 33 42 43 52 53 62 63 72 73
  321. // 04 05 14 15 24 25 34 35 44 45 54 55 64 65 74 75 (overlapped)
  322. // 06 07 16 17 26 27 36 37 46 47 56 57 66 67 76 77 (overlapped)
  323. transpose_16bit_4x8(s, s);
  324. x = width_hor;
  325. do {
  326. src += 8;
  327. load_8bit_8x8(src, src_stride, &s[2]);
  328. // 04 05 14 15 24 25 34 35 44 45 54 55 64 65 74 75
  329. // 06 07 16 17 26 27 36 37 46 47 56 57 66 67 76 77
  330. // 08 09 18 19 28 29 38 39 48 49 58 59 68 69 78 79
  331. // 0A 0B 1A 1B 2A 2B 3A 3B 4A 4B 5A 5B 6A 6B 7A 7B
  332. transpose_16bit_4x8(&s[2], &s[2]);
  333. d[0] = convolve8_8_ssse3(&s[0], f); // 00 10 20 30 40 50 60 70
  334. d[1] = convolve8_8_ssse3(&s[2], f); // 01 11 21 31 41 51 61 71
  335. // 00 10 20 30 40 50 60 70 xx xx xx xx xx xx xx xx
  336. // 01 11 21 31 41 51 61 71 xx xx xx xx xx xx xx xx
  337. d[0] = _mm_packus_epi16(d[0], d[0]);
  338. d[1] = _mm_packus_epi16(d[1], d[1]);
  339. // 00 10 01 11 20 30 21 31 40 50 41 51 60 70 61 71
  340. d[0] = _mm_unpacklo_epi16(d[0], d[1]);
  341. store_8bit_4x4_sse2(d[0], t, 2 * width_hor);
  342. s[0] = s[4];
  343. s[1] = s[5];
  344. t += 4;
  345. x -= 2;
  346. } while (x);
  347. src += 8 * src_stride - 4 * width_hor;
  348. t += 6 * width_hor;
  349. y -= 8;
  350. } while (y);
  351. // vertical 8x2
  352. x = width_ver;
  353. t = temp_buffer;
  354. do {
  355. // 00 10 01 11 02 12 03 13 04 14 05 15 06 16 07 17
  356. // 20 30 21 31 22 32 23 33 24 34 25 35 26 36 27 37
  357. s[0] = _mm_loadu_si128((const __m128i *)(t + 0 * width_hor));
  358. s[1] = _mm_loadu_si128((const __m128i *)(t + 2 * width_hor));
  359. t += 4 * width_hor;
  360. y = height_ver;
  361. do {
  362. // 40 50 41 51 42 52 43 53 44 54 45 55 46 56 47 57
  363. // 60 70 61 71 62 72 63 73 64 74 65 75 66 76 67 77
  364. // 80 90 81 91 82 92 83 93 84 94 85 95 86 96 87 77
  365. // A0 B0 A1 B1 A2 B2 A3 B3 A4 B4 A5 B5 A6 B6 A7 77
  366. loadu_8bit_16x4(t, 2 * width_hor, &s[2]);
  367. t += 8 * width_hor;
  368. d[0] = convolve8_8_ssse3(&s[0], f); // 00 01 02 03 04 05 06 07
  369. d[1] = convolve8_8_ssse3(&s[2], f); // 10 11 12 13 14 15 16 17
  370. // 00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17
  371. d[0] = _mm_packus_epi16(d[0], d[1]);
  372. _mm_storel_epi64((__m128i *)(dst + 0 * dst_stride), d[0]);
  373. _mm_storeh_epi64((__m128i *)(dst + 1 * dst_stride), d[0]);
  374. s[0] = s[4];
  375. s[1] = s[5];
  376. dst += 2 * dst_stride;
  377. y -= 2;
  378. } while (y);
  379. t -= width_hor * (4 * height_ver + 4);
  380. t += 16;
  381. dst -= height_ver * dst_stride;
  382. dst += 8;
  383. x -= 8;
  384. } while (x);
  385. }
  386. typedef void (*shuffle_filter_funcs)(const int16_t *const filter,
  387. __m128i *const f);
  388. typedef __m128i (*convolve8_funcs)(const __m128i *const s,
  389. const __m128i *const f);
  390. static void scale_plane_4_to_3_general(const uint8_t *src, const int src_stride,
  391. uint8_t *dst, const int dst_stride,
  392. const int w, const int h,
  393. const InterpKernel *const coef,
  394. const int phase_scaler,
  395. uint8_t *const temp_buffer) {
  396. static const int step_q4 = 16 * 4 / 3;
  397. const int width_hor = (w + 5) - ((w + 5) % 6);
  398. const int stride_hor = 2 * width_hor + 4; // store 4 extra pixels
  399. const int width_ver = (w + 7) & ~7;
  400. // We need (SUBPEL_TAPS - 1) extra rows: (SUBPEL_TAPS / 2 - 1) extra rows
  401. // above and (SUBPEL_TAPS / 2) extra rows below.
  402. const int height_hor = (4 * h / 3 + SUBPEL_TAPS - 1 + 7) & ~7;
  403. const int height_ver = (h + 5) - ((h + 5) % 6);
  404. int x, y = height_hor;
  405. uint8_t *t = temp_buffer;
  406. __m128i s[12], d[6], dd[4];
  407. __m128i f0[4], f1[5], f2[5];
  408. // The offset of the first row is always less than 1 pixel.
  409. const int offset1_q4 = phase_scaler + 1 * step_q4;
  410. const int offset2_q4 = phase_scaler + 2 * step_q4;
  411. // offset_idxx indicates the pixel offset is even (0) or odd (1).
  412. // It's used to choose the src offset and filter coefficient offset.
  413. const int offset_idx1 = (offset1_q4 >> 4) & 1;
  414. const int offset_idx2 = (offset2_q4 >> 4) & 1;
  415. static const shuffle_filter_funcs shuffle_filter_funcs[2] = {
  416. shuffle_filter_ssse3, shuffle_filter_odd_ssse3
  417. };
  418. static const convolve8_funcs convolve8_funcs[2] = {
  419. convolve8_8_even_offset_ssse3, convolve8_8_odd_offset_ssse3
  420. };
  421. assert(w && h);
  422. shuffle_filter_ssse3(coef[(phase_scaler + 0 * step_q4) & SUBPEL_MASK], f0);
  423. shuffle_filter_funcs[offset_idx1](coef[offset1_q4 & SUBPEL_MASK], f1);
  424. shuffle_filter_funcs[offset_idx2](coef[offset2_q4 & SUBPEL_MASK], f2);
  425. // Sub 64 to avoid overflow.
  426. // Coef 128 would be treated as -128 in PMADDUBSW. Sub 64 here.
  427. // Coef 128 is in either fx[1] or fx[2] depending on the phase idx.
  428. // When filter phase idx is 1, the two biggest coefficients are shuffled
  429. // together, and the sum of them are always no less than 128. Sub 64 here.
  430. // After the subtraction, when the sum of all positive coefficients are no
  431. // larger than 128, and the sum of all negative coefficients are no
  432. // less than -128, there will be no overflow in the convolve8 functions.
  433. f0[1] = _mm_sub_epi8(f0[1], _mm_set1_epi8(64));
  434. f1[1 + offset_idx1] = _mm_sub_epi8(f1[1 + offset_idx1], _mm_set1_epi8(64));
  435. f2[1 + offset_idx2] = _mm_sub_epi8(f2[1 + offset_idx2], _mm_set1_epi8(64));
  436. src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2 - 1;
  437. // horizontal 6x8
  438. do {
  439. load_8bit_8x8(src, src_stride, s);
  440. // 00 01 10 11 20 21 30 31 40 41 50 51 60 61 70 71
  441. // 02 03 12 13 22 23 32 33 42 43 52 53 62 63 72 73
  442. // 04 05 14 15 24 25 34 35 44 45 54 55 64 65 74 75
  443. // 06 07 16 17 26 27 36 37 46 47 56 57 66 67 76 77
  444. transpose_16bit_4x8(s, s);
  445. x = width_hor;
  446. do {
  447. src += 8;
  448. load_8bit_8x8(src, src_stride, &s[4]);
  449. // 08 09 18 19 28 29 38 39 48 49 58 59 68 69 78 79
  450. // 0A 0B 1A 1B 2A 2B 3A 3B 4A 4B 5A 5B 6A 6B 7A 7B
  451. // OC 0D 1C 1D 2C 2D 3C 3D 4C 4D 5C 5D 6C 6D 7C 7D
  452. // 0E 0F 1E 1F 2E 2F 3E 3F 4E 4F 5E 5F 6E 6F 7E 7F
  453. transpose_16bit_4x8(&s[4], &s[4]);
  454. // 00 10 20 30 40 50 60 70
  455. // 01 11 21 31 41 51 61 71
  456. // 02 12 22 32 42 52 62 72
  457. // 03 13 23 33 43 53 63 73
  458. // 04 14 24 34 44 54 64 74
  459. // 05 15 25 35 45 55 65 75
  460. d[0] = convolve8_8_even_offset_ssse3(&s[0], f0);
  461. d[1] = convolve8_funcs[offset_idx1](&s[offset1_q4 >> 5], f1);
  462. d[2] = convolve8_funcs[offset_idx2](&s[offset2_q4 >> 5], f2);
  463. d[3] = convolve8_8_even_offset_ssse3(&s[2], f0);
  464. d[4] = convolve8_funcs[offset_idx1](&s[2 + (offset1_q4 >> 5)], f1);
  465. d[5] = convolve8_funcs[offset_idx2](&s[2 + (offset2_q4 >> 5)], f2);
  466. // 00 10 20 30 40 50 60 70 02 12 22 32 42 52 62 72
  467. // 01 11 21 31 41 51 61 71 03 13 23 33 43 53 63 73
  468. // 04 14 24 34 44 54 64 74 xx xx xx xx xx xx xx xx
  469. // 05 15 25 35 45 55 65 75 xx xx xx xx xx xx xx xx
  470. dd[0] = _mm_packus_epi16(d[0], d[2]);
  471. dd[1] = _mm_packus_epi16(d[1], d[3]);
  472. dd[2] = _mm_packus_epi16(d[4], d[4]);
  473. dd[3] = _mm_packus_epi16(d[5], d[5]);
  474. // 00 10 01 11 20 30 21 31 40 50 41 51 60 70 61 71
  475. // 02 12 03 13 22 32 23 33 42 52 43 53 62 72 63 73
  476. // 04 14 05 15 24 34 25 35 44 54 45 55 64 74 65 75
  477. d[0] = _mm_unpacklo_epi16(dd[0], dd[1]);
  478. d[1] = _mm_unpackhi_epi16(dd[0], dd[1]);
  479. d[2] = _mm_unpacklo_epi16(dd[2], dd[3]);
  480. // 00 10 01 11 02 12 03 13 20 30 21 31 22 32 23 33
  481. // 40 50 41 51 42 52 43 53 60 70 61 71 62 72 63 73
  482. // 04 14 05 15 xx xx xx xx 24 34 25 35 xx xx xx xx
  483. // 44 54 45 55 xx xx xx xx 64 74 65 75 xx xx xx xx
  484. dd[0] = _mm_unpacklo_epi32(d[0], d[1]);
  485. dd[1] = _mm_unpackhi_epi32(d[0], d[1]);
  486. dd[2] = _mm_unpacklo_epi32(d[2], d[2]);
  487. dd[3] = _mm_unpackhi_epi32(d[2], d[2]);
  488. // 00 10 01 11 02 12 03 13 04 14 05 15 xx xx xx xx
  489. // 20 30 21 31 22 32 23 33 24 34 25 35 xx xx xx xx
  490. // 40 50 41 51 42 52 43 53 44 54 45 55 xx xx xx xx
  491. // 60 70 61 71 62 72 63 73 64 74 65 75 xx xx xx xx
  492. d[0] = _mm_unpacklo_epi64(dd[0], dd[2]);
  493. d[1] = _mm_unpackhi_epi64(dd[0], dd[2]);
  494. d[2] = _mm_unpacklo_epi64(dd[1], dd[3]);
  495. d[3] = _mm_unpackhi_epi64(dd[1], dd[3]);
  496. // store 4 extra pixels
  497. storeu_8bit_16x4(d, t, stride_hor);
  498. s[0] = s[4];
  499. s[1] = s[5];
  500. s[2] = s[6];
  501. s[3] = s[7];
  502. t += 12;
  503. x -= 6;
  504. } while (x);
  505. src += 8 * src_stride - 4 * width_hor / 3;
  506. t += 3 * stride_hor + 4;
  507. y -= 8;
  508. } while (y);
  509. // vertical 8x6
  510. x = width_ver;
  511. t = temp_buffer;
  512. do {
  513. // 00 10 01 11 02 12 03 13 04 14 05 15 06 16 07 17
  514. // 20 30 21 31 22 32 23 33 24 34 25 35 26 36 27 37
  515. // 40 50 41 51 42 52 43 53 44 54 45 55 46 56 47 57
  516. // 60 70 61 71 62 72 63 73 64 74 65 75 66 76 67 77
  517. loadu_8bit_16x4(t, stride_hor, s);
  518. y = height_ver;
  519. do {
  520. // 80 90 81 91 82 92 83 93 84 94 85 95 86 96 87 97
  521. // A0 B0 A1 B1 A2 B2 A3 B3 A4 B4 A5 B5 A6 B6 A7 B7
  522. // C0 D0 C1 D1 C2 D2 C3 D3 C4 D4 C5 D5 C6 D6 C7 D7
  523. // E0 F0 E1 F1 E2 F2 E3 F3 E4 F4 E5 F5 E6 F6 E7 F7
  524. t += 4 * stride_hor;
  525. loadu_8bit_16x4(t, stride_hor, &s[4]);
  526. d[0] = convolve8_8_even_offset_ssse3(&s[0], f0);
  527. d[1] = convolve8_funcs[offset_idx1](&s[offset1_q4 >> 5], f1);
  528. d[2] = convolve8_funcs[offset_idx2](&s[offset2_q4 >> 5], f2);
  529. d[3] = convolve8_8_even_offset_ssse3(&s[2], f0);
  530. d[4] = convolve8_funcs[offset_idx1](&s[2 + (offset1_q4 >> 5)], f1);
  531. d[5] = convolve8_funcs[offset_idx2](&s[2 + (offset2_q4 >> 5)], f2);
  532. // 00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17
  533. // 20 21 22 23 24 25 26 27 30 31 32 33 34 35 36 37
  534. // 40 41 42 43 44 45 46 47 50 51 52 53 54 55 56 57
  535. d[0] = _mm_packus_epi16(d[0], d[1]);
  536. d[2] = _mm_packus_epi16(d[2], d[3]);
  537. d[4] = _mm_packus_epi16(d[4], d[5]);
  538. _mm_storel_epi64((__m128i *)(dst + 0 * dst_stride), d[0]);
  539. _mm_storeh_epi64((__m128i *)(dst + 1 * dst_stride), d[0]);
  540. _mm_storel_epi64((__m128i *)(dst + 2 * dst_stride), d[2]);
  541. _mm_storeh_epi64((__m128i *)(dst + 3 * dst_stride), d[2]);
  542. _mm_storel_epi64((__m128i *)(dst + 4 * dst_stride), d[4]);
  543. _mm_storeh_epi64((__m128i *)(dst + 5 * dst_stride), d[4]);
  544. s[0] = s[4];
  545. s[1] = s[5];
  546. s[2] = s[6];
  547. s[3] = s[7];
  548. dst += 6 * dst_stride;
  549. y -= 6;
  550. } while (y);
  551. t -= stride_hor * 2 * height_ver / 3;
  552. t += 16;
  553. dst -= height_ver * dst_stride;
  554. dst += 8;
  555. x -= 8;
  556. } while (x);
  557. }
  558. static INLINE __m128i scale_1_to_2_phase_0_kernel(const __m128i *const s,
  559. const __m128i *const f) {
  560. __m128i ss[4], temp;
  561. ss[0] = _mm_unpacklo_epi8(s[0], s[1]);
  562. ss[1] = _mm_unpacklo_epi8(s[2], s[3]);
  563. ss[2] = _mm_unpacklo_epi8(s[4], s[5]);
  564. ss[3] = _mm_unpacklo_epi8(s[6], s[7]);
  565. temp = convolve8_8_ssse3(ss, f);
  566. return _mm_packus_epi16(temp, temp);
  567. }
  568. // Only calculate odd columns since even columns are just src pixels' copies.
  569. static void scale_1_to_2_phase_0_row(const uint8_t *src, uint8_t *dst,
  570. const int w, const __m128i *const f) {
  571. int x = w;
  572. do {
  573. __m128i s[8], temp;
  574. s[0] = _mm_loadl_epi64((const __m128i *)(src + 0));
  575. s[1] = _mm_loadl_epi64((const __m128i *)(src + 1));
  576. s[2] = _mm_loadl_epi64((const __m128i *)(src + 2));
  577. s[3] = _mm_loadl_epi64((const __m128i *)(src + 3));
  578. s[4] = _mm_loadl_epi64((const __m128i *)(src + 4));
  579. s[5] = _mm_loadl_epi64((const __m128i *)(src + 5));
  580. s[6] = _mm_loadl_epi64((const __m128i *)(src + 6));
  581. s[7] = _mm_loadl_epi64((const __m128i *)(src + 7));
  582. temp = scale_1_to_2_phase_0_kernel(s, f);
  583. _mm_storel_epi64((__m128i *)dst, temp);
  584. src += 8;
  585. dst += 8;
  586. x -= 8;
  587. } while (x);
  588. }
  589. static void scale_plane_1_to_2_phase_0(const uint8_t *src,
  590. const ptrdiff_t src_stride, uint8_t *dst,
  591. const ptrdiff_t dst_stride,
  592. const int src_w, const int src_h,
  593. const int16_t *const coef,
  594. uint8_t *const temp_buffer) {
  595. int max_width;
  596. int y;
  597. uint8_t *tmp[9];
  598. __m128i f[4];
  599. max_width = (src_w + 7) & ~7;
  600. tmp[0] = temp_buffer + 0 * max_width;
  601. tmp[1] = temp_buffer + 1 * max_width;
  602. tmp[2] = temp_buffer + 2 * max_width;
  603. tmp[3] = temp_buffer + 3 * max_width;
  604. tmp[4] = temp_buffer + 4 * max_width;
  605. tmp[5] = temp_buffer + 5 * max_width;
  606. tmp[6] = temp_buffer + 6 * max_width;
  607. tmp[7] = temp_buffer + 7 * max_width;
  608. shuffle_filter_ssse3(coef, f);
  609. scale_1_to_2_phase_0_row(src - 3 * src_stride - 3, tmp[0], max_width, f);
  610. scale_1_to_2_phase_0_row(src - 2 * src_stride - 3, tmp[1], max_width, f);
  611. scale_1_to_2_phase_0_row(src - 1 * src_stride - 3, tmp[2], max_width, f);
  612. scale_1_to_2_phase_0_row(src + 0 * src_stride - 3, tmp[3], max_width, f);
  613. scale_1_to_2_phase_0_row(src + 1 * src_stride - 3, tmp[4], max_width, f);
  614. scale_1_to_2_phase_0_row(src + 2 * src_stride - 3, tmp[5], max_width, f);
  615. scale_1_to_2_phase_0_row(src + 3 * src_stride - 3, tmp[6], max_width, f);
  616. y = src_h;
  617. do {
  618. int x;
  619. scale_1_to_2_phase_0_row(src + 4 * src_stride - 3, tmp[7], max_width, f);
  620. for (x = 0; x < max_width; x += 8) {
  621. __m128i s[8], C, D, CD;
  622. // Even rows
  623. const __m128i a = _mm_loadl_epi64((const __m128i *)(src + x));
  624. const __m128i b = _mm_loadl_epi64((const __m128i *)(tmp[3] + x));
  625. const __m128i ab = _mm_unpacklo_epi8(a, b);
  626. _mm_storeu_si128((__m128i *)(dst + 2 * x), ab);
  627. // Odd rows
  628. // Even columns
  629. load_8bit_8x8(src + x - 3 * src_stride, src_stride, s);
  630. C = scale_1_to_2_phase_0_kernel(s, f);
  631. // Odd columns
  632. s[0] = _mm_loadl_epi64((const __m128i *)(tmp[0] + x));
  633. s[1] = _mm_loadl_epi64((const __m128i *)(tmp[1] + x));
  634. s[2] = _mm_loadl_epi64((const __m128i *)(tmp[2] + x));
  635. s[3] = _mm_loadl_epi64((const __m128i *)(tmp[3] + x));
  636. s[4] = _mm_loadl_epi64((const __m128i *)(tmp[4] + x));
  637. s[5] = _mm_loadl_epi64((const __m128i *)(tmp[5] + x));
  638. s[6] = _mm_loadl_epi64((const __m128i *)(tmp[6] + x));
  639. s[7] = _mm_loadl_epi64((const __m128i *)(tmp[7] + x));
  640. D = scale_1_to_2_phase_0_kernel(s, f);
  641. CD = _mm_unpacklo_epi8(C, D);
  642. _mm_storeu_si128((__m128i *)(dst + dst_stride + 2 * x), CD);
  643. }
  644. src += src_stride;
  645. dst += 2 * dst_stride;
  646. tmp[8] = tmp[0];
  647. tmp[0] = tmp[1];
  648. tmp[1] = tmp[2];
  649. tmp[2] = tmp[3];
  650. tmp[3] = tmp[4];
  651. tmp[4] = tmp[5];
  652. tmp[5] = tmp[6];
  653. tmp[6] = tmp[7];
  654. tmp[7] = tmp[8];
  655. } while (--y);
  656. }
  657. void vp9_scale_and_extend_frame_ssse3(const YV12_BUFFER_CONFIG *src,
  658. YV12_BUFFER_CONFIG *dst,
  659. uint8_t filter_type, int phase_scaler) {
  660. const int src_w = src->y_crop_width;
  661. const int src_h = src->y_crop_height;
  662. const int dst_w = dst->y_crop_width;
  663. const int dst_h = dst->y_crop_height;
  664. const int dst_uv_w = dst_w / 2;
  665. const int dst_uv_h = dst_h / 2;
  666. int scaled = 0;
  667. // phase_scaler is usually 0 or 8.
  668. assert(phase_scaler >= 0 && phase_scaler < 16);
  669. if (dst_w * 2 == src_w && dst_h * 2 == src_h) {
  670. // 2 to 1
  671. scaled = 1;
  672. if (phase_scaler == 0) {
  673. scale_plane_2_to_1_phase_0(src->y_buffer, src->y_stride, dst->y_buffer,
  674. dst->y_stride, dst_w, dst_h);
  675. scale_plane_2_to_1_phase_0(src->u_buffer, src->uv_stride, dst->u_buffer,
  676. dst->uv_stride, dst_uv_w, dst_uv_h);
  677. scale_plane_2_to_1_phase_0(src->v_buffer, src->uv_stride, dst->v_buffer,
  678. dst->uv_stride, dst_uv_w, dst_uv_h);
  679. } else if (filter_type == BILINEAR) {
  680. const int16_t c0 = vp9_filter_kernels[BILINEAR][phase_scaler][3];
  681. const int16_t c1 = vp9_filter_kernels[BILINEAR][phase_scaler][4];
  682. const __m128i c0c1 = _mm_set1_epi16(c0 | (c1 << 8)); // c0 and c1 >= 0
  683. scale_plane_2_to_1_bilinear(src->y_buffer, src->y_stride, dst->y_buffer,
  684. dst->y_stride, dst_w, dst_h, c0c1);
  685. scale_plane_2_to_1_bilinear(src->u_buffer, src->uv_stride, dst->u_buffer,
  686. dst->uv_stride, dst_uv_w, dst_uv_h, c0c1);
  687. scale_plane_2_to_1_bilinear(src->v_buffer, src->uv_stride, dst->v_buffer,
  688. dst->uv_stride, dst_uv_w, dst_uv_h, c0c1);
  689. } else {
  690. const int buffer_stride = (dst_w + 3) & ~3;
  691. const int buffer_height = (2 * dst_h + SUBPEL_TAPS - 2 + 7) & ~7;
  692. uint8_t *const temp_buffer =
  693. (uint8_t *)malloc(buffer_stride * buffer_height);
  694. if (temp_buffer) {
  695. scale_plane_2_to_1_general(
  696. src->y_buffer, src->y_stride, dst->y_buffer, dst->y_stride, dst_w,
  697. dst_h, vp9_filter_kernels[filter_type][phase_scaler], temp_buffer);
  698. scale_plane_2_to_1_general(
  699. src->u_buffer, src->uv_stride, dst->u_buffer, dst->uv_stride,
  700. dst_uv_w, dst_uv_h, vp9_filter_kernels[filter_type][phase_scaler],
  701. temp_buffer);
  702. scale_plane_2_to_1_general(
  703. src->v_buffer, src->uv_stride, dst->v_buffer, dst->uv_stride,
  704. dst_uv_w, dst_uv_h, vp9_filter_kernels[filter_type][phase_scaler],
  705. temp_buffer);
  706. free(temp_buffer);
  707. } else {
  708. scaled = 0;
  709. }
  710. }
  711. } else if (4 * dst_w == src_w && 4 * dst_h == src_h) {
  712. // 4 to 1
  713. scaled = 1;
  714. if (phase_scaler == 0) {
  715. scale_plane_4_to_1_phase_0(src->y_buffer, src->y_stride, dst->y_buffer,
  716. dst->y_stride, dst_w, dst_h);
  717. scale_plane_4_to_1_phase_0(src->u_buffer, src->uv_stride, dst->u_buffer,
  718. dst->uv_stride, dst_uv_w, dst_uv_h);
  719. scale_plane_4_to_1_phase_0(src->v_buffer, src->uv_stride, dst->v_buffer,
  720. dst->uv_stride, dst_uv_w, dst_uv_h);
  721. } else if (filter_type == BILINEAR) {
  722. const int16_t c0 = vp9_filter_kernels[BILINEAR][phase_scaler][3];
  723. const int16_t c1 = vp9_filter_kernels[BILINEAR][phase_scaler][4];
  724. const __m128i c0c1 = _mm_set1_epi16(c0 | (c1 << 8)); // c0 and c1 >= 0
  725. scale_plane_4_to_1_bilinear(src->y_buffer, src->y_stride, dst->y_buffer,
  726. dst->y_stride, dst_w, dst_h, c0c1);
  727. scale_plane_4_to_1_bilinear(src->u_buffer, src->uv_stride, dst->u_buffer,
  728. dst->uv_stride, dst_uv_w, dst_uv_h, c0c1);
  729. scale_plane_4_to_1_bilinear(src->v_buffer, src->uv_stride, dst->v_buffer,
  730. dst->uv_stride, dst_uv_w, dst_uv_h, c0c1);
  731. } else {
  732. const int buffer_stride = (dst_w + 1) & ~1;
  733. const int buffer_height = (4 * dst_h + SUBPEL_TAPS - 2 + 7) & ~7;
  734. // When dst_w is 1 or 2, we need extra padding to avoid heap read overflow
  735. const int extra_padding = 16;
  736. uint8_t *const temp_buffer =
  737. (uint8_t *)malloc(buffer_stride * buffer_height + extra_padding);
  738. if (temp_buffer) {
  739. scale_plane_4_to_1_general(
  740. src->y_buffer, src->y_stride, dst->y_buffer, dst->y_stride, dst_w,
  741. dst_h, vp9_filter_kernels[filter_type][phase_scaler], temp_buffer);
  742. scale_plane_4_to_1_general(
  743. src->u_buffer, src->uv_stride, dst->u_buffer, dst->uv_stride,
  744. dst_uv_w, dst_uv_h, vp9_filter_kernels[filter_type][phase_scaler],
  745. temp_buffer);
  746. scale_plane_4_to_1_general(
  747. src->v_buffer, src->uv_stride, dst->v_buffer, dst->uv_stride,
  748. dst_uv_w, dst_uv_h, vp9_filter_kernels[filter_type][phase_scaler],
  749. temp_buffer);
  750. free(temp_buffer);
  751. } else {
  752. scaled = 0;
  753. }
  754. }
  755. } else if (4 * dst_w == 3 * src_w && 4 * dst_h == 3 * src_h) {
  756. // 4 to 3
  757. const int buffer_stride_hor = (dst_w + 5) - ((dst_w + 5) % 6) + 2;
  758. const int buffer_stride_ver = (dst_w + 7) & ~7;
  759. const int buffer_height = (4 * dst_h / 3 + SUBPEL_TAPS - 1 + 7) & ~7;
  760. // When the vertical filter reads more pixels than the horizontal filter
  761. // generated in each row, we need extra padding to avoid heap read overflow.
  762. // For example, the horizontal filter generates 18 pixels but the vertical
  763. // filter reads 24 pixels in a row. The difference is multiplied by 2 since
  764. // two rows are interlaced together in the optimization.
  765. const int extra_padding = (buffer_stride_ver > buffer_stride_hor)
  766. ? 2 * (buffer_stride_ver - buffer_stride_hor)
  767. : 0;
  768. const int buffer_size = buffer_stride_hor * buffer_height + extra_padding;
  769. uint8_t *const temp_buffer = (uint8_t *)malloc(buffer_size);
  770. if (temp_buffer) {
  771. scaled = 1;
  772. scale_plane_4_to_3_general(
  773. src->y_buffer, src->y_stride, dst->y_buffer, dst->y_stride, dst_w,
  774. dst_h, vp9_filter_kernels[filter_type], phase_scaler, temp_buffer);
  775. scale_plane_4_to_3_general(src->u_buffer, src->uv_stride, dst->u_buffer,
  776. dst->uv_stride, dst_uv_w, dst_uv_h,
  777. vp9_filter_kernels[filter_type], phase_scaler,
  778. temp_buffer);
  779. scale_plane_4_to_3_general(src->v_buffer, src->uv_stride, dst->v_buffer,
  780. dst->uv_stride, dst_uv_w, dst_uv_h,
  781. vp9_filter_kernels[filter_type], phase_scaler,
  782. temp_buffer);
  783. free(temp_buffer);
  784. }
  785. } else if (dst_w == src_w * 2 && dst_h == src_h * 2 && phase_scaler == 0) {
  786. // 1 to 2
  787. uint8_t *const temp_buffer = (uint8_t *)malloc(8 * ((src_w + 7) & ~7));
  788. if (temp_buffer) {
  789. scaled = 1;
  790. scale_plane_1_to_2_phase_0(
  791. src->y_buffer, src->y_stride, dst->y_buffer, dst->y_stride, src_w,
  792. src_h, vp9_filter_kernels[filter_type][8], temp_buffer);
  793. scale_plane_1_to_2_phase_0(src->u_buffer, src->uv_stride, dst->u_buffer,
  794. dst->uv_stride, src_w / 2, src_h / 2,
  795. vp9_filter_kernels[filter_type][8],
  796. temp_buffer);
  797. scale_plane_1_to_2_phase_0(src->v_buffer, src->uv_stride, dst->v_buffer,
  798. dst->uv_stride, src_w / 2, src_h / 2,
  799. vp9_filter_kernels[filter_type][8],
  800. temp_buffer);
  801. free(temp_buffer);
  802. }
  803. }
  804. if (scaled) {
  805. vpx_extend_frame_borders(dst);
  806. } else {
  807. // Call c version for all other scaling ratios.
  808. vp9_scale_and_extend_frame_c(src, dst, filter_type, phase_scaler);
  809. }
  810. }