vp9_resize.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * Copyright (c) 2014 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 <assert.h>
  11. #include <limits.h>
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "./vpx_config.h"
  17. #if CONFIG_VP9_HIGHBITDEPTH
  18. #include "vpx_dsp/vpx_dsp_common.h"
  19. #endif // CONFIG_VP9_HIGHBITDEPTH
  20. #include "vpx_ports/mem.h"
  21. #include "vp9/common/vp9_common.h"
  22. #include "vp9/encoder/vp9_resize.h"
  23. #define FILTER_BITS 7
  24. #define INTERP_TAPS 8
  25. #define SUBPEL_BITS 5
  26. #define SUBPEL_MASK ((1 << SUBPEL_BITS) - 1)
  27. #define INTERP_PRECISION_BITS 32
  28. typedef int16_t interp_kernel[INTERP_TAPS];
  29. // Filters for interpolation (0.5-band) - note this also filters integer pels.
  30. static const interp_kernel filteredinterp_filters500[(1 << SUBPEL_BITS)] = {
  31. { -3, 0, 35, 64, 35, 0, -3, 0 }, { -3, -1, 34, 64, 36, 1, -3, 0 },
  32. { -3, -1, 32, 64, 38, 1, -3, 0 }, { -2, -2, 31, 63, 39, 2, -3, 0 },
  33. { -2, -2, 29, 63, 41, 2, -3, 0 }, { -2, -2, 28, 63, 42, 3, -4, 0 },
  34. { -2, -3, 27, 63, 43, 4, -4, 0 }, { -2, -3, 25, 62, 45, 5, -4, 0 },
  35. { -2, -3, 24, 62, 46, 5, -4, 0 }, { -2, -3, 23, 61, 47, 6, -4, 0 },
  36. { -2, -3, 21, 60, 49, 7, -4, 0 }, { -1, -4, 20, 60, 50, 8, -4, -1 },
  37. { -1, -4, 19, 59, 51, 9, -4, -1 }, { -1, -4, 17, 58, 52, 10, -4, 0 },
  38. { -1, -4, 16, 57, 53, 12, -4, -1 }, { -1, -4, 15, 56, 54, 13, -4, -1 },
  39. { -1, -4, 14, 55, 55, 14, -4, -1 }, { -1, -4, 13, 54, 56, 15, -4, -1 },
  40. { -1, -4, 12, 53, 57, 16, -4, -1 }, { 0, -4, 10, 52, 58, 17, -4, -1 },
  41. { -1, -4, 9, 51, 59, 19, -4, -1 }, { -1, -4, 8, 50, 60, 20, -4, -1 },
  42. { 0, -4, 7, 49, 60, 21, -3, -2 }, { 0, -4, 6, 47, 61, 23, -3, -2 },
  43. { 0, -4, 5, 46, 62, 24, -3, -2 }, { 0, -4, 5, 45, 62, 25, -3, -2 },
  44. { 0, -4, 4, 43, 63, 27, -3, -2 }, { 0, -4, 3, 42, 63, 28, -2, -2 },
  45. { 0, -3, 2, 41, 63, 29, -2, -2 }, { 0, -3, 2, 39, 63, 31, -2, -2 },
  46. { 0, -3, 1, 38, 64, 32, -1, -3 }, { 0, -3, 1, 36, 64, 34, -1, -3 }
  47. };
  48. // Filters for interpolation (0.625-band) - note this also filters integer pels.
  49. static const interp_kernel filteredinterp_filters625[(1 << SUBPEL_BITS)] = {
  50. { -1, -8, 33, 80, 33, -8, -1, 0 }, { -1, -8, 30, 80, 35, -8, -1, 1 },
  51. { -1, -8, 28, 80, 37, -7, -2, 1 }, { 0, -8, 26, 79, 39, -7, -2, 1 },
  52. { 0, -8, 24, 79, 41, -7, -2, 1 }, { 0, -8, 22, 78, 43, -6, -2, 1 },
  53. { 0, -8, 20, 78, 45, -5, -3, 1 }, { 0, -8, 18, 77, 48, -5, -3, 1 },
  54. { 0, -8, 16, 76, 50, -4, -3, 1 }, { 0, -8, 15, 75, 52, -3, -4, 1 },
  55. { 0, -7, 13, 74, 54, -3, -4, 1 }, { 0, -7, 11, 73, 56, -2, -4, 1 },
  56. { 0, -7, 10, 71, 58, -1, -4, 1 }, { 1, -7, 8, 70, 60, 0, -5, 1 },
  57. { 1, -6, 6, 68, 62, 1, -5, 1 }, { 1, -6, 5, 67, 63, 2, -5, 1 },
  58. { 1, -6, 4, 65, 65, 4, -6, 1 }, { 1, -5, 2, 63, 67, 5, -6, 1 },
  59. { 1, -5, 1, 62, 68, 6, -6, 1 }, { 1, -5, 0, 60, 70, 8, -7, 1 },
  60. { 1, -4, -1, 58, 71, 10, -7, 0 }, { 1, -4, -2, 56, 73, 11, -7, 0 },
  61. { 1, -4, -3, 54, 74, 13, -7, 0 }, { 1, -4, -3, 52, 75, 15, -8, 0 },
  62. { 1, -3, -4, 50, 76, 16, -8, 0 }, { 1, -3, -5, 48, 77, 18, -8, 0 },
  63. { 1, -3, -5, 45, 78, 20, -8, 0 }, { 1, -2, -6, 43, 78, 22, -8, 0 },
  64. { 1, -2, -7, 41, 79, 24, -8, 0 }, { 1, -2, -7, 39, 79, 26, -8, 0 },
  65. { 1, -2, -7, 37, 80, 28, -8, -1 }, { 1, -1, -8, 35, 80, 30, -8, -1 },
  66. };
  67. // Filters for interpolation (0.75-band) - note this also filters integer pels.
  68. static const interp_kernel filteredinterp_filters750[(1 << SUBPEL_BITS)] = {
  69. { 2, -11, 25, 96, 25, -11, 2, 0 }, { 2, -11, 22, 96, 28, -11, 2, 0 },
  70. { 2, -10, 19, 95, 31, -11, 2, 0 }, { 2, -10, 17, 95, 34, -12, 2, 0 },
  71. { 2, -9, 14, 94, 37, -12, 2, 0 }, { 2, -8, 12, 93, 40, -12, 1, 0 },
  72. { 2, -8, 9, 92, 43, -12, 1, 1 }, { 2, -7, 7, 91, 46, -12, 1, 0 },
  73. { 2, -7, 5, 90, 49, -12, 1, 0 }, { 2, -6, 3, 88, 52, -12, 0, 1 },
  74. { 2, -5, 1, 86, 55, -12, 0, 1 }, { 2, -5, -1, 84, 58, -11, 0, 1 },
  75. { 2, -4, -2, 82, 61, -11, -1, 1 }, { 2, -4, -4, 80, 64, -10, -1, 1 },
  76. { 1, -3, -5, 77, 67, -9, -1, 1 }, { 1, -3, -6, 75, 70, -8, -2, 1 },
  77. { 1, -2, -7, 72, 72, -7, -2, 1 }, { 1, -2, -8, 70, 75, -6, -3, 1 },
  78. { 1, -1, -9, 67, 77, -5, -3, 1 }, { 1, -1, -10, 64, 80, -4, -4, 2 },
  79. { 1, -1, -11, 61, 82, -2, -4, 2 }, { 1, 0, -11, 58, 84, -1, -5, 2 },
  80. { 1, 0, -12, 55, 86, 1, -5, 2 }, { 1, 0, -12, 52, 88, 3, -6, 2 },
  81. { 0, 1, -12, 49, 90, 5, -7, 2 }, { 0, 1, -12, 46, 91, 7, -7, 2 },
  82. { 1, 1, -12, 43, 92, 9, -8, 2 }, { 0, 1, -12, 40, 93, 12, -8, 2 },
  83. { 0, 2, -12, 37, 94, 14, -9, 2 }, { 0, 2, -12, 34, 95, 17, -10, 2 },
  84. { 0, 2, -11, 31, 95, 19, -10, 2 }, { 0, 2, -11, 28, 96, 22, -11, 2 }
  85. };
  86. // Filters for interpolation (0.875-band) - note this also filters integer pels.
  87. static const interp_kernel filteredinterp_filters875[(1 << SUBPEL_BITS)] = {
  88. { 3, -8, 13, 112, 13, -8, 3, 0 }, { 3, -7, 10, 112, 17, -9, 3, -1 },
  89. { 2, -6, 7, 111, 21, -9, 3, -1 }, { 2, -5, 4, 111, 24, -10, 3, -1 },
  90. { 2, -4, 1, 110, 28, -11, 3, -1 }, { 1, -3, -1, 108, 32, -12, 4, -1 },
  91. { 1, -2, -3, 106, 36, -13, 4, -1 }, { 1, -1, -6, 105, 40, -14, 4, -1 },
  92. { 1, -1, -7, 102, 44, -14, 4, -1 }, { 1, 0, -9, 100, 48, -15, 4, -1 },
  93. { 1, 1, -11, 97, 53, -16, 4, -1 }, { 0, 1, -12, 95, 57, -16, 4, -1 },
  94. { 0, 2, -13, 91, 61, -16, 4, -1 }, { 0, 2, -14, 88, 65, -16, 4, -1 },
  95. { 0, 3, -15, 84, 69, -17, 4, 0 }, { 0, 3, -16, 81, 73, -16, 3, 0 },
  96. { 0, 3, -16, 77, 77, -16, 3, 0 }, { 0, 3, -16, 73, 81, -16, 3, 0 },
  97. { 0, 4, -17, 69, 84, -15, 3, 0 }, { -1, 4, -16, 65, 88, -14, 2, 0 },
  98. { -1, 4, -16, 61, 91, -13, 2, 0 }, { -1, 4, -16, 57, 95, -12, 1, 0 },
  99. { -1, 4, -16, 53, 97, -11, 1, 1 }, { -1, 4, -15, 48, 100, -9, 0, 1 },
  100. { -1, 4, -14, 44, 102, -7, -1, 1 }, { -1, 4, -14, 40, 105, -6, -1, 1 },
  101. { -1, 4, -13, 36, 106, -3, -2, 1 }, { -1, 4, -12, 32, 108, -1, -3, 1 },
  102. { -1, 3, -11, 28, 110, 1, -4, 2 }, { -1, 3, -10, 24, 111, 4, -5, 2 },
  103. { -1, 3, -9, 21, 111, 7, -6, 2 }, { -1, 3, -9, 17, 112, 10, -7, 3 }
  104. };
  105. // Filters for interpolation (full-band) - no filtering for integer pixels
  106. static const interp_kernel filteredinterp_filters1000[(1 << SUBPEL_BITS)] = {
  107. { 0, 0, 0, 128, 0, 0, 0, 0 }, { 0, 1, -3, 128, 3, -1, 0, 0 },
  108. { -1, 2, -6, 127, 7, -2, 1, 0 }, { -1, 3, -9, 126, 12, -4, 1, 0 },
  109. { -1, 4, -12, 125, 16, -5, 1, 0 }, { -1, 4, -14, 123, 20, -6, 2, 0 },
  110. { -1, 5, -15, 120, 25, -8, 2, 0 }, { -1, 5, -17, 118, 30, -9, 3, -1 },
  111. { -1, 6, -18, 114, 35, -10, 3, -1 }, { -1, 6, -19, 111, 41, -12, 3, -1 },
  112. { -1, 6, -20, 107, 46, -13, 4, -1 }, { -1, 6, -21, 103, 52, -14, 4, -1 },
  113. { -1, 6, -21, 99, 57, -16, 5, -1 }, { -1, 6, -21, 94, 63, -17, 5, -1 },
  114. { -1, 6, -20, 89, 68, -18, 5, -1 }, { -1, 6, -20, 84, 73, -19, 6, -1 },
  115. { -1, 6, -20, 79, 79, -20, 6, -1 }, { -1, 6, -19, 73, 84, -20, 6, -1 },
  116. { -1, 5, -18, 68, 89, -20, 6, -1 }, { -1, 5, -17, 63, 94, -21, 6, -1 },
  117. { -1, 5, -16, 57, 99, -21, 6, -1 }, { -1, 4, -14, 52, 103, -21, 6, -1 },
  118. { -1, 4, -13, 46, 107, -20, 6, -1 }, { -1, 3, -12, 41, 111, -19, 6, -1 },
  119. { -1, 3, -10, 35, 114, -18, 6, -1 }, { -1, 3, -9, 30, 118, -17, 5, -1 },
  120. { 0, 2, -8, 25, 120, -15, 5, -1 }, { 0, 2, -6, 20, 123, -14, 4, -1 },
  121. { 0, 1, -5, 16, 125, -12, 4, -1 }, { 0, 1, -4, 12, 126, -9, 3, -1 },
  122. { 0, 1, -2, 7, 127, -6, 2, -1 }, { 0, 0, -1, 3, 128, -3, 1, 0 }
  123. };
  124. // Filters for factor of 2 downsampling.
  125. static const int16_t vp9_down2_symeven_half_filter[] = { 56, 12, -3, -1 };
  126. static const int16_t vp9_down2_symodd_half_filter[] = { 64, 35, 0, -3 };
  127. static const interp_kernel *choose_interp_filter(int inlength, int outlength) {
  128. int outlength16 = outlength * 16;
  129. if (outlength16 >= inlength * 16)
  130. return filteredinterp_filters1000;
  131. else if (outlength16 >= inlength * 13)
  132. return filteredinterp_filters875;
  133. else if (outlength16 >= inlength * 11)
  134. return filteredinterp_filters750;
  135. else if (outlength16 >= inlength * 9)
  136. return filteredinterp_filters625;
  137. else
  138. return filteredinterp_filters500;
  139. }
  140. static void interpolate(const uint8_t *const input, int inlength,
  141. uint8_t *output, int outlength) {
  142. const int64_t delta =
  143. (((uint64_t)inlength << 32) + outlength / 2) / outlength;
  144. const int64_t offset =
  145. inlength > outlength
  146. ? (((int64_t)(inlength - outlength) << 31) + outlength / 2) /
  147. outlength
  148. : -(((int64_t)(outlength - inlength) << 31) + outlength / 2) /
  149. outlength;
  150. uint8_t *optr = output;
  151. int x, x1, x2, sum, k, int_pel, sub_pel;
  152. int64_t y;
  153. const interp_kernel *interp_filters =
  154. choose_interp_filter(inlength, outlength);
  155. x = 0;
  156. y = offset;
  157. while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
  158. x++;
  159. y += delta;
  160. }
  161. x1 = x;
  162. x = outlength - 1;
  163. y = delta * x + offset;
  164. while ((y >> INTERP_PRECISION_BITS) + (int64_t)(INTERP_TAPS / 2) >=
  165. inlength) {
  166. x--;
  167. y -= delta;
  168. }
  169. x2 = x;
  170. if (x1 > x2) {
  171. for (x = 0, y = offset; x < outlength; ++x, y += delta) {
  172. const int16_t *filter;
  173. int_pel = y >> INTERP_PRECISION_BITS;
  174. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  175. filter = interp_filters[sub_pel];
  176. sum = 0;
  177. for (k = 0; k < INTERP_TAPS; ++k) {
  178. const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
  179. sum += filter[k] *
  180. input[(pk < 0 ? 0 : (pk >= inlength ? inlength - 1 : pk))];
  181. }
  182. *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
  183. }
  184. } else {
  185. // Initial part.
  186. for (x = 0, y = offset; x < x1; ++x, y += delta) {
  187. const int16_t *filter;
  188. int_pel = y >> INTERP_PRECISION_BITS;
  189. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  190. filter = interp_filters[sub_pel];
  191. sum = 0;
  192. for (k = 0; k < INTERP_TAPS; ++k)
  193. sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0
  194. ? 0
  195. : int_pel - INTERP_TAPS / 2 + 1 + k)];
  196. *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
  197. }
  198. // Middle part.
  199. for (; x <= x2; ++x, y += delta) {
  200. const int16_t *filter;
  201. int_pel = y >> INTERP_PRECISION_BITS;
  202. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  203. filter = interp_filters[sub_pel];
  204. sum = 0;
  205. for (k = 0; k < INTERP_TAPS; ++k)
  206. sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
  207. *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
  208. }
  209. // End part.
  210. for (; x < outlength; ++x, y += delta) {
  211. const int16_t *filter;
  212. int_pel = y >> INTERP_PRECISION_BITS;
  213. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  214. filter = interp_filters[sub_pel];
  215. sum = 0;
  216. for (k = 0; k < INTERP_TAPS; ++k)
  217. sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k >= inlength
  218. ? inlength - 1
  219. : int_pel - INTERP_TAPS / 2 + 1 + k)];
  220. *optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
  221. }
  222. }
  223. }
  224. static void down2_symeven(const uint8_t *const input, int length,
  225. uint8_t *output) {
  226. // Actual filter len = 2 * filter_len_half.
  227. const int16_t *filter = vp9_down2_symeven_half_filter;
  228. const int filter_len_half = sizeof(vp9_down2_symeven_half_filter) / 2;
  229. int i, j;
  230. uint8_t *optr = output;
  231. int l1 = filter_len_half;
  232. int l2 = (length - filter_len_half);
  233. l1 += (l1 & 1);
  234. l2 += (l2 & 1);
  235. if (l1 > l2) {
  236. // Short input length.
  237. for (i = 0; i < length; i += 2) {
  238. int sum = (1 << (FILTER_BITS - 1));
  239. for (j = 0; j < filter_len_half; ++j) {
  240. sum += (input[(i - j < 0 ? 0 : i - j)] +
  241. input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
  242. filter[j];
  243. }
  244. sum >>= FILTER_BITS;
  245. *optr++ = clip_pixel(sum);
  246. }
  247. } else {
  248. // Initial part.
  249. for (i = 0; i < l1; i += 2) {
  250. int sum = (1 << (FILTER_BITS - 1));
  251. for (j = 0; j < filter_len_half; ++j) {
  252. sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + 1 + j]) * filter[j];
  253. }
  254. sum >>= FILTER_BITS;
  255. *optr++ = clip_pixel(sum);
  256. }
  257. // Middle part.
  258. for (; i < l2; i += 2) {
  259. int sum = (1 << (FILTER_BITS - 1));
  260. for (j = 0; j < filter_len_half; ++j) {
  261. sum += (input[i - j] + input[i + 1 + j]) * filter[j];
  262. }
  263. sum >>= FILTER_BITS;
  264. *optr++ = clip_pixel(sum);
  265. }
  266. // End part.
  267. for (; i < length; i += 2) {
  268. int sum = (1 << (FILTER_BITS - 1));
  269. for (j = 0; j < filter_len_half; ++j) {
  270. sum += (input[i - j] +
  271. input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
  272. filter[j];
  273. }
  274. sum >>= FILTER_BITS;
  275. *optr++ = clip_pixel(sum);
  276. }
  277. }
  278. }
  279. static void down2_symodd(const uint8_t *const input, int length,
  280. uint8_t *output) {
  281. // Actual filter len = 2 * filter_len_half - 1.
  282. const int16_t *filter = vp9_down2_symodd_half_filter;
  283. const int filter_len_half = sizeof(vp9_down2_symodd_half_filter) / 2;
  284. int i, j;
  285. uint8_t *optr = output;
  286. int l1 = filter_len_half - 1;
  287. int l2 = (length - filter_len_half + 1);
  288. l1 += (l1 & 1);
  289. l2 += (l2 & 1);
  290. if (l1 > l2) {
  291. // Short input length.
  292. for (i = 0; i < length; i += 2) {
  293. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  294. for (j = 1; j < filter_len_half; ++j) {
  295. sum += (input[(i - j < 0 ? 0 : i - j)] +
  296. input[(i + j >= length ? length - 1 : i + j)]) *
  297. filter[j];
  298. }
  299. sum >>= FILTER_BITS;
  300. *optr++ = clip_pixel(sum);
  301. }
  302. } else {
  303. // Initial part.
  304. for (i = 0; i < l1; i += 2) {
  305. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  306. for (j = 1; j < filter_len_half; ++j) {
  307. sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
  308. }
  309. sum >>= FILTER_BITS;
  310. *optr++ = clip_pixel(sum);
  311. }
  312. // Middle part.
  313. for (; i < l2; i += 2) {
  314. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  315. for (j = 1; j < filter_len_half; ++j) {
  316. sum += (input[i - j] + input[i + j]) * filter[j];
  317. }
  318. sum >>= FILTER_BITS;
  319. *optr++ = clip_pixel(sum);
  320. }
  321. // End part.
  322. for (; i < length; i += 2) {
  323. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  324. for (j = 1; j < filter_len_half; ++j) {
  325. sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
  326. filter[j];
  327. }
  328. sum >>= FILTER_BITS;
  329. *optr++ = clip_pixel(sum);
  330. }
  331. }
  332. }
  333. static int get_down2_length(int length, int steps) {
  334. int s;
  335. for (s = 0; s < steps; ++s) length = (length + 1) >> 1;
  336. return length;
  337. }
  338. static int get_down2_steps(int in_length, int out_length) {
  339. int steps = 0;
  340. int proj_in_length;
  341. while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) {
  342. ++steps;
  343. in_length = proj_in_length;
  344. }
  345. return steps;
  346. }
  347. static void resize_multistep(const uint8_t *const input, int length,
  348. uint8_t *output, int olength, uint8_t *otmp) {
  349. int steps;
  350. if (length == olength) {
  351. memcpy(output, input, sizeof(output[0]) * length);
  352. return;
  353. }
  354. steps = get_down2_steps(length, olength);
  355. if (steps > 0) {
  356. int s;
  357. uint8_t *out = NULL;
  358. uint8_t *otmp2;
  359. int filteredlength = length;
  360. assert(otmp != NULL);
  361. otmp2 = otmp + get_down2_length(length, 1);
  362. for (s = 0; s < steps; ++s) {
  363. const int proj_filteredlength = get_down2_length(filteredlength, 1);
  364. const uint8_t *const in = (s == 0 ? input : out);
  365. if (s == steps - 1 && proj_filteredlength == olength)
  366. out = output;
  367. else
  368. out = (s & 1 ? otmp2 : otmp);
  369. if (filteredlength & 1)
  370. down2_symodd(in, filteredlength, out);
  371. else
  372. down2_symeven(in, filteredlength, out);
  373. filteredlength = proj_filteredlength;
  374. }
  375. if (filteredlength != olength) {
  376. interpolate(out, filteredlength, output, olength);
  377. }
  378. } else {
  379. interpolate(input, length, output, olength);
  380. }
  381. }
  382. static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) {
  383. int i;
  384. uint8_t *iptr = img;
  385. uint8_t *aptr = arr;
  386. for (i = 0; i < len; ++i, iptr += stride) {
  387. *aptr++ = *iptr;
  388. }
  389. }
  390. static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) {
  391. int i;
  392. uint8_t *iptr = img;
  393. uint8_t *aptr = arr;
  394. for (i = 0; i < len; ++i, iptr += stride) {
  395. *iptr = *aptr++;
  396. }
  397. }
  398. void vp9_resize_plane(const uint8_t *const input, int height, int width,
  399. int in_stride, uint8_t *output, int height2, int width2,
  400. int out_stride) {
  401. int i;
  402. uint8_t *intbuf = (uint8_t *)malloc(sizeof(uint8_t) * width2 * height);
  403. uint8_t *tmpbuf =
  404. (uint8_t *)malloc(sizeof(uint8_t) * (width < height ? height : width));
  405. uint8_t *arrbuf = (uint8_t *)malloc(sizeof(uint8_t) * height);
  406. uint8_t *arrbuf2 = (uint8_t *)malloc(sizeof(uint8_t) * height2);
  407. if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
  408. goto Error;
  409. assert(width > 0);
  410. assert(height > 0);
  411. assert(width2 > 0);
  412. assert(height2 > 0);
  413. for (i = 0; i < height; ++i)
  414. resize_multistep(input + in_stride * i, width, intbuf + width2 * i, width2,
  415. tmpbuf);
  416. for (i = 0; i < width2; ++i) {
  417. fill_col_to_arr(intbuf + i, width2, height, arrbuf);
  418. resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf);
  419. fill_arr_to_col(output + i, out_stride, height2, arrbuf2);
  420. }
  421. Error:
  422. free(intbuf);
  423. free(tmpbuf);
  424. free(arrbuf);
  425. free(arrbuf2);
  426. }
  427. #if CONFIG_VP9_HIGHBITDEPTH
  428. static void highbd_interpolate(const uint16_t *const input, int inlength,
  429. uint16_t *output, int outlength, int bd) {
  430. const int64_t delta =
  431. (((uint64_t)inlength << 32) + outlength / 2) / outlength;
  432. const int64_t offset =
  433. inlength > outlength
  434. ? (((int64_t)(inlength - outlength) << 31) + outlength / 2) /
  435. outlength
  436. : -(((int64_t)(outlength - inlength) << 31) + outlength / 2) /
  437. outlength;
  438. uint16_t *optr = output;
  439. int x, x1, x2, sum, k, int_pel, sub_pel;
  440. int64_t y;
  441. const interp_kernel *interp_filters =
  442. choose_interp_filter(inlength, outlength);
  443. x = 0;
  444. y = offset;
  445. while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
  446. x++;
  447. y += delta;
  448. }
  449. x1 = x;
  450. x = outlength - 1;
  451. y = delta * x + offset;
  452. while ((y >> INTERP_PRECISION_BITS) + (int64_t)(INTERP_TAPS / 2) >=
  453. inlength) {
  454. x--;
  455. y -= delta;
  456. }
  457. x2 = x;
  458. if (x1 > x2) {
  459. for (x = 0, y = offset; x < outlength; ++x, y += delta) {
  460. const int16_t *filter;
  461. int_pel = y >> INTERP_PRECISION_BITS;
  462. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  463. filter = interp_filters[sub_pel];
  464. sum = 0;
  465. for (k = 0; k < INTERP_TAPS; ++k) {
  466. const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
  467. sum += filter[k] *
  468. input[(pk < 0 ? 0 : (pk >= inlength ? inlength - 1 : pk))];
  469. }
  470. *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
  471. }
  472. } else {
  473. // Initial part.
  474. for (x = 0, y = offset; x < x1; ++x, y += delta) {
  475. const int16_t *filter;
  476. int_pel = y >> INTERP_PRECISION_BITS;
  477. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  478. filter = interp_filters[sub_pel];
  479. sum = 0;
  480. for (k = 0; k < INTERP_TAPS; ++k)
  481. sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0
  482. ? 0
  483. : int_pel - INTERP_TAPS / 2 + 1 + k)];
  484. *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
  485. }
  486. // Middle part.
  487. for (; x <= x2; ++x, y += delta) {
  488. const int16_t *filter;
  489. int_pel = y >> INTERP_PRECISION_BITS;
  490. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  491. filter = interp_filters[sub_pel];
  492. sum = 0;
  493. for (k = 0; k < INTERP_TAPS; ++k)
  494. sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
  495. *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
  496. }
  497. // End part.
  498. for (; x < outlength; ++x, y += delta) {
  499. const int16_t *filter;
  500. int_pel = y >> INTERP_PRECISION_BITS;
  501. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  502. filter = interp_filters[sub_pel];
  503. sum = 0;
  504. for (k = 0; k < INTERP_TAPS; ++k)
  505. sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k >= inlength
  506. ? inlength - 1
  507. : int_pel - INTERP_TAPS / 2 + 1 + k)];
  508. *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
  509. }
  510. }
  511. }
  512. static void highbd_down2_symeven(const uint16_t *const input, int length,
  513. uint16_t *output, int bd) {
  514. // Actual filter len = 2 * filter_len_half.
  515. static const int16_t *filter = vp9_down2_symeven_half_filter;
  516. const int filter_len_half = sizeof(vp9_down2_symeven_half_filter) / 2;
  517. int i, j;
  518. uint16_t *optr = output;
  519. int l1 = filter_len_half;
  520. int l2 = (length - filter_len_half);
  521. l1 += (l1 & 1);
  522. l2 += (l2 & 1);
  523. if (l1 > l2) {
  524. // Short input length.
  525. for (i = 0; i < length; i += 2) {
  526. int sum = (1 << (FILTER_BITS - 1));
  527. for (j = 0; j < filter_len_half; ++j) {
  528. sum += (input[(i - j < 0 ? 0 : i - j)] +
  529. input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
  530. filter[j];
  531. }
  532. sum >>= FILTER_BITS;
  533. *optr++ = clip_pixel_highbd(sum, bd);
  534. }
  535. } else {
  536. // Initial part.
  537. for (i = 0; i < l1; i += 2) {
  538. int sum = (1 << (FILTER_BITS - 1));
  539. for (j = 0; j < filter_len_half; ++j) {
  540. sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + 1 + j]) * filter[j];
  541. }
  542. sum >>= FILTER_BITS;
  543. *optr++ = clip_pixel_highbd(sum, bd);
  544. }
  545. // Middle part.
  546. for (; i < l2; i += 2) {
  547. int sum = (1 << (FILTER_BITS - 1));
  548. for (j = 0; j < filter_len_half; ++j) {
  549. sum += (input[i - j] + input[i + 1 + j]) * filter[j];
  550. }
  551. sum >>= FILTER_BITS;
  552. *optr++ = clip_pixel_highbd(sum, bd);
  553. }
  554. // End part.
  555. for (; i < length; i += 2) {
  556. int sum = (1 << (FILTER_BITS - 1));
  557. for (j = 0; j < filter_len_half; ++j) {
  558. sum += (input[i - j] +
  559. input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
  560. filter[j];
  561. }
  562. sum >>= FILTER_BITS;
  563. *optr++ = clip_pixel_highbd(sum, bd);
  564. }
  565. }
  566. }
  567. static void highbd_down2_symodd(const uint16_t *const input, int length,
  568. uint16_t *output, int bd) {
  569. // Actual filter len = 2 * filter_len_half - 1.
  570. static const int16_t *filter = vp9_down2_symodd_half_filter;
  571. const int filter_len_half = sizeof(vp9_down2_symodd_half_filter) / 2;
  572. int i, j;
  573. uint16_t *optr = output;
  574. int l1 = filter_len_half - 1;
  575. int l2 = (length - filter_len_half + 1);
  576. l1 += (l1 & 1);
  577. l2 += (l2 & 1);
  578. if (l1 > l2) {
  579. // Short input length.
  580. for (i = 0; i < length; i += 2) {
  581. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  582. for (j = 1; j < filter_len_half; ++j) {
  583. sum += (input[(i - j < 0 ? 0 : i - j)] +
  584. input[(i + j >= length ? length - 1 : i + j)]) *
  585. filter[j];
  586. }
  587. sum >>= FILTER_BITS;
  588. *optr++ = clip_pixel_highbd(sum, bd);
  589. }
  590. } else {
  591. // Initial part.
  592. for (i = 0; i < l1; i += 2) {
  593. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  594. for (j = 1; j < filter_len_half; ++j) {
  595. sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
  596. }
  597. sum >>= FILTER_BITS;
  598. *optr++ = clip_pixel_highbd(sum, bd);
  599. }
  600. // Middle part.
  601. for (; i < l2; i += 2) {
  602. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  603. for (j = 1; j < filter_len_half; ++j) {
  604. sum += (input[i - j] + input[i + j]) * filter[j];
  605. }
  606. sum >>= FILTER_BITS;
  607. *optr++ = clip_pixel_highbd(sum, bd);
  608. }
  609. // End part.
  610. for (; i < length; i += 2) {
  611. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  612. for (j = 1; j < filter_len_half; ++j) {
  613. sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
  614. filter[j];
  615. }
  616. sum >>= FILTER_BITS;
  617. *optr++ = clip_pixel_highbd(sum, bd);
  618. }
  619. }
  620. }
  621. static void highbd_resize_multistep(const uint16_t *const input, int length,
  622. uint16_t *output, int olength,
  623. uint16_t *otmp, int bd) {
  624. int steps;
  625. if (length == olength) {
  626. memcpy(output, input, sizeof(output[0]) * length);
  627. return;
  628. }
  629. steps = get_down2_steps(length, olength);
  630. if (steps > 0) {
  631. int s;
  632. uint16_t *out = NULL;
  633. uint16_t *otmp2;
  634. int filteredlength = length;
  635. assert(otmp != NULL);
  636. otmp2 = otmp + get_down2_length(length, 1);
  637. for (s = 0; s < steps; ++s) {
  638. const int proj_filteredlength = get_down2_length(filteredlength, 1);
  639. const uint16_t *const in = (s == 0 ? input : out);
  640. if (s == steps - 1 && proj_filteredlength == olength)
  641. out = output;
  642. else
  643. out = (s & 1 ? otmp2 : otmp);
  644. if (filteredlength & 1)
  645. highbd_down2_symodd(in, filteredlength, out, bd);
  646. else
  647. highbd_down2_symeven(in, filteredlength, out, bd);
  648. filteredlength = proj_filteredlength;
  649. }
  650. if (filteredlength != olength) {
  651. highbd_interpolate(out, filteredlength, output, olength, bd);
  652. }
  653. } else {
  654. highbd_interpolate(input, length, output, olength, bd);
  655. }
  656. }
  657. static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
  658. uint16_t *arr) {
  659. int i;
  660. uint16_t *iptr = img;
  661. uint16_t *aptr = arr;
  662. for (i = 0; i < len; ++i, iptr += stride) {
  663. *aptr++ = *iptr;
  664. }
  665. }
  666. static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
  667. uint16_t *arr) {
  668. int i;
  669. uint16_t *iptr = img;
  670. uint16_t *aptr = arr;
  671. for (i = 0; i < len; ++i, iptr += stride) {
  672. *iptr = *aptr++;
  673. }
  674. }
  675. void vp9_highbd_resize_plane(const uint8_t *const input, int height, int width,
  676. int in_stride, uint8_t *output, int height2,
  677. int width2, int out_stride, int bd) {
  678. int i;
  679. uint16_t *intbuf = (uint16_t *)malloc(sizeof(uint16_t) * width2 * height);
  680. uint16_t *tmpbuf =
  681. (uint16_t *)malloc(sizeof(uint16_t) * (width < height ? height : width));
  682. uint16_t *arrbuf = (uint16_t *)malloc(sizeof(uint16_t) * height);
  683. uint16_t *arrbuf2 = (uint16_t *)malloc(sizeof(uint16_t) * height2);
  684. if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
  685. goto Error;
  686. for (i = 0; i < height; ++i) {
  687. highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width,
  688. intbuf + width2 * i, width2, tmpbuf, bd);
  689. }
  690. for (i = 0; i < width2; ++i) {
  691. highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf);
  692. highbd_resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf, bd);
  693. highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
  694. arrbuf2);
  695. }
  696. Error:
  697. free(intbuf);
  698. free(tmpbuf);
  699. free(arrbuf);
  700. free(arrbuf2);
  701. }
  702. #endif // CONFIG_VP9_HIGHBITDEPTH
  703. void vp9_resize_frame420(const uint8_t *const y, int y_stride,
  704. const uint8_t *const u, const uint8_t *const v,
  705. int uv_stride, int height, int width, uint8_t *oy,
  706. int oy_stride, uint8_t *ou, uint8_t *ov,
  707. int ouv_stride, int oheight, int owidth) {
  708. vp9_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
  709. vp9_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2,
  710. owidth / 2, ouv_stride);
  711. vp9_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2,
  712. owidth / 2, ouv_stride);
  713. }
  714. void vp9_resize_frame422(const uint8_t *const y, int y_stride,
  715. const uint8_t *const u, const uint8_t *const v,
  716. int uv_stride, int height, int width, uint8_t *oy,
  717. int oy_stride, uint8_t *ou, uint8_t *ov,
  718. int ouv_stride, int oheight, int owidth) {
  719. vp9_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
  720. vp9_resize_plane(u, height, width / 2, uv_stride, ou, oheight, owidth / 2,
  721. ouv_stride);
  722. vp9_resize_plane(v, height, width / 2, uv_stride, ov, oheight, owidth / 2,
  723. ouv_stride);
  724. }
  725. void vp9_resize_frame444(const uint8_t *const y, int y_stride,
  726. const uint8_t *const u, const uint8_t *const v,
  727. int uv_stride, int height, int width, uint8_t *oy,
  728. int oy_stride, uint8_t *ou, uint8_t *ov,
  729. int ouv_stride, int oheight, int owidth) {
  730. vp9_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
  731. vp9_resize_plane(u, height, width, uv_stride, ou, oheight, owidth,
  732. ouv_stride);
  733. vp9_resize_plane(v, height, width, uv_stride, ov, oheight, owidth,
  734. ouv_stride);
  735. }
  736. #if CONFIG_VP9_HIGHBITDEPTH
  737. void vp9_highbd_resize_frame420(const uint8_t *const y, int y_stride,
  738. const uint8_t *const u, const uint8_t *const v,
  739. int uv_stride, int height, int width,
  740. uint8_t *oy, int oy_stride, uint8_t *ou,
  741. uint8_t *ov, int ouv_stride, int oheight,
  742. int owidth, int bd) {
  743. vp9_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
  744. oy_stride, bd);
  745. vp9_highbd_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2,
  746. owidth / 2, ouv_stride, bd);
  747. vp9_highbd_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2,
  748. owidth / 2, ouv_stride, bd);
  749. }
  750. void vp9_highbd_resize_frame422(const uint8_t *const y, int y_stride,
  751. const uint8_t *const u, const uint8_t *const v,
  752. int uv_stride, int height, int width,
  753. uint8_t *oy, int oy_stride, uint8_t *ou,
  754. uint8_t *ov, int ouv_stride, int oheight,
  755. int owidth, int bd) {
  756. vp9_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
  757. oy_stride, bd);
  758. vp9_highbd_resize_plane(u, height, width / 2, uv_stride, ou, oheight,
  759. owidth / 2, ouv_stride, bd);
  760. vp9_highbd_resize_plane(v, height, width / 2, uv_stride, ov, oheight,
  761. owidth / 2, ouv_stride, bd);
  762. }
  763. void vp9_highbd_resize_frame444(const uint8_t *const y, int y_stride,
  764. const uint8_t *const u, const uint8_t *const v,
  765. int uv_stride, int height, int width,
  766. uint8_t *oy, int oy_stride, uint8_t *ou,
  767. uint8_t *ov, int ouv_stride, int oheight,
  768. int owidth, int bd) {
  769. vp9_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
  770. oy_stride, bd);
  771. vp9_highbd_resize_plane(u, height, width, uv_stride, ou, oheight, owidth,
  772. ouv_stride, bd);
  773. vp9_highbd_resize_plane(v, height, width, uv_stride, ov, oheight, owidth,
  774. ouv_stride, bd);
  775. }
  776. #endif // CONFIG_VP9_HIGHBITDEPTH