vp9_resize.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 *)calloc(width2 * height, sizeof(*intbuf));
  403. uint8_t *tmpbuf =
  404. (uint8_t *)calloc(width < height ? height : width, sizeof(*tmpbuf));
  405. uint8_t *arrbuf = (uint8_t *)calloc(height, sizeof(*arrbuf));
  406. uint8_t *arrbuf2 = (uint8_t *)calloc(height2, sizeof(*arrbuf2));
  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. assert(int_pel - INTERP_TAPS / 2 + 1 + k < inlength);
  482. sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0
  483. ? 0
  484. : int_pel - INTERP_TAPS / 2 + 1 + k)];
  485. }
  486. *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
  487. }
  488. // Middle part.
  489. for (; x <= x2; ++x, y += delta) {
  490. const int16_t *filter;
  491. int_pel = y >> INTERP_PRECISION_BITS;
  492. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  493. filter = interp_filters[sub_pel];
  494. sum = 0;
  495. for (k = 0; k < INTERP_TAPS; ++k)
  496. sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
  497. *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
  498. }
  499. // End part.
  500. for (; x < outlength; ++x, y += delta) {
  501. const int16_t *filter;
  502. int_pel = y >> INTERP_PRECISION_BITS;
  503. sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
  504. filter = interp_filters[sub_pel];
  505. sum = 0;
  506. for (k = 0; k < INTERP_TAPS; ++k)
  507. sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k >= inlength
  508. ? inlength - 1
  509. : int_pel - INTERP_TAPS / 2 + 1 + k)];
  510. *optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
  511. }
  512. }
  513. }
  514. static void highbd_down2_symeven(const uint16_t *const input, int length,
  515. uint16_t *output, int bd) {
  516. // Actual filter len = 2 * filter_len_half.
  517. static const int16_t *filter = vp9_down2_symeven_half_filter;
  518. const int filter_len_half = sizeof(vp9_down2_symeven_half_filter) / 2;
  519. int i, j;
  520. uint16_t *optr = output;
  521. int l1 = filter_len_half;
  522. int l2 = (length - filter_len_half);
  523. l1 += (l1 & 1);
  524. l2 += (l2 & 1);
  525. if (l1 > l2) {
  526. // Short input length.
  527. for (i = 0; i < length; i += 2) {
  528. int sum = (1 << (FILTER_BITS - 1));
  529. for (j = 0; j < filter_len_half; ++j) {
  530. sum += (input[(i - j < 0 ? 0 : i - j)] +
  531. input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
  532. filter[j];
  533. }
  534. sum >>= FILTER_BITS;
  535. *optr++ = clip_pixel_highbd(sum, bd);
  536. }
  537. } else {
  538. // Initial part.
  539. for (i = 0; i < l1; i += 2) {
  540. int sum = (1 << (FILTER_BITS - 1));
  541. for (j = 0; j < filter_len_half; ++j) {
  542. sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + 1 + j]) * filter[j];
  543. }
  544. sum >>= FILTER_BITS;
  545. *optr++ = clip_pixel_highbd(sum, bd);
  546. }
  547. // Middle part.
  548. for (; i < l2; i += 2) {
  549. int sum = (1 << (FILTER_BITS - 1));
  550. for (j = 0; j < filter_len_half; ++j) {
  551. sum += (input[i - j] + input[i + 1 + j]) * filter[j];
  552. }
  553. sum >>= FILTER_BITS;
  554. *optr++ = clip_pixel_highbd(sum, bd);
  555. }
  556. // End part.
  557. for (; i < length; i += 2) {
  558. int sum = (1 << (FILTER_BITS - 1));
  559. for (j = 0; j < filter_len_half; ++j) {
  560. sum += (input[i - j] +
  561. input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
  562. filter[j];
  563. }
  564. sum >>= FILTER_BITS;
  565. *optr++ = clip_pixel_highbd(sum, bd);
  566. }
  567. }
  568. }
  569. static void highbd_down2_symodd(const uint16_t *const input, int length,
  570. uint16_t *output, int bd) {
  571. // Actual filter len = 2 * filter_len_half - 1.
  572. static const int16_t *filter = vp9_down2_symodd_half_filter;
  573. const int filter_len_half = sizeof(vp9_down2_symodd_half_filter) / 2;
  574. int i, j;
  575. uint16_t *optr = output;
  576. int l1 = filter_len_half - 1;
  577. int l2 = (length - filter_len_half + 1);
  578. l1 += (l1 & 1);
  579. l2 += (l2 & 1);
  580. if (l1 > l2) {
  581. // Short input length.
  582. for (i = 0; i < length; i += 2) {
  583. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  584. for (j = 1; j < filter_len_half; ++j) {
  585. sum += (input[(i - j < 0 ? 0 : i - j)] +
  586. input[(i + j >= length ? length - 1 : i + j)]) *
  587. filter[j];
  588. }
  589. sum >>= FILTER_BITS;
  590. *optr++ = clip_pixel_highbd(sum, bd);
  591. }
  592. } else {
  593. // Initial part.
  594. for (i = 0; i < l1; i += 2) {
  595. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  596. for (j = 1; j < filter_len_half; ++j) {
  597. sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
  598. }
  599. sum >>= FILTER_BITS;
  600. *optr++ = clip_pixel_highbd(sum, bd);
  601. }
  602. // Middle part.
  603. for (; i < l2; i += 2) {
  604. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  605. for (j = 1; j < filter_len_half; ++j) {
  606. sum += (input[i - j] + input[i + j]) * filter[j];
  607. }
  608. sum >>= FILTER_BITS;
  609. *optr++ = clip_pixel_highbd(sum, bd);
  610. }
  611. // End part.
  612. for (; i < length; i += 2) {
  613. int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
  614. for (j = 1; j < filter_len_half; ++j) {
  615. sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
  616. filter[j];
  617. }
  618. sum >>= FILTER_BITS;
  619. *optr++ = clip_pixel_highbd(sum, bd);
  620. }
  621. }
  622. }
  623. static void highbd_resize_multistep(const uint16_t *const input, int length,
  624. uint16_t *output, int olength,
  625. uint16_t *otmp, int bd) {
  626. int steps;
  627. if (length == olength) {
  628. memcpy(output, input, sizeof(output[0]) * length);
  629. return;
  630. }
  631. steps = get_down2_steps(length, olength);
  632. if (steps > 0) {
  633. int s;
  634. uint16_t *out = NULL;
  635. uint16_t *otmp2;
  636. int filteredlength = length;
  637. assert(otmp != NULL);
  638. otmp2 = otmp + get_down2_length(length, 1);
  639. for (s = 0; s < steps; ++s) {
  640. const int proj_filteredlength = get_down2_length(filteredlength, 1);
  641. const uint16_t *const in = (s == 0 ? input : out);
  642. if (s == steps - 1 && proj_filteredlength == olength)
  643. out = output;
  644. else
  645. out = (s & 1 ? otmp2 : otmp);
  646. if (filteredlength & 1)
  647. highbd_down2_symodd(in, filteredlength, out, bd);
  648. else
  649. highbd_down2_symeven(in, filteredlength, out, bd);
  650. filteredlength = proj_filteredlength;
  651. }
  652. if (filteredlength != olength) {
  653. highbd_interpolate(out, filteredlength, output, olength, bd);
  654. }
  655. } else {
  656. highbd_interpolate(input, length, output, olength, bd);
  657. }
  658. }
  659. static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
  660. uint16_t *arr) {
  661. int i;
  662. uint16_t *iptr = img;
  663. uint16_t *aptr = arr;
  664. for (i = 0; i < len; ++i, iptr += stride) {
  665. *aptr++ = *iptr;
  666. }
  667. }
  668. static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
  669. uint16_t *arr) {
  670. int i;
  671. uint16_t *iptr = img;
  672. uint16_t *aptr = arr;
  673. for (i = 0; i < len; ++i, iptr += stride) {
  674. *iptr = *aptr++;
  675. }
  676. }
  677. void vp9_highbd_resize_plane(const uint8_t *const input, int height, int width,
  678. int in_stride, uint8_t *output, int height2,
  679. int width2, int out_stride, int bd) {
  680. int i;
  681. uint16_t *intbuf = (uint16_t *)malloc(sizeof(uint16_t) * width2 * height);
  682. uint16_t *tmpbuf =
  683. (uint16_t *)malloc(sizeof(uint16_t) * (width < height ? height : width));
  684. uint16_t *arrbuf = (uint16_t *)malloc(sizeof(uint16_t) * height);
  685. uint16_t *arrbuf2 = (uint16_t *)malloc(sizeof(uint16_t) * height2);
  686. if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
  687. goto Error;
  688. assert(width > 0);
  689. assert(height > 0);
  690. assert(width2 > 0);
  691. assert(height2 > 0);
  692. for (i = 0; i < height; ++i) {
  693. highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width,
  694. intbuf + width2 * i, width2, tmpbuf, bd);
  695. }
  696. for (i = 0; i < width2; ++i) {
  697. highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf);
  698. highbd_resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf, bd);
  699. highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
  700. arrbuf2);
  701. }
  702. Error:
  703. free(intbuf);
  704. free(tmpbuf);
  705. free(arrbuf);
  706. free(arrbuf2);
  707. }
  708. #endif // CONFIG_VP9_HIGHBITDEPTH
  709. void vp9_resize_frame420(const uint8_t *const y, int y_stride,
  710. const uint8_t *const u, const uint8_t *const v,
  711. int uv_stride, int height, int width, uint8_t *oy,
  712. int oy_stride, uint8_t *ou, uint8_t *ov,
  713. int ouv_stride, int oheight, int owidth) {
  714. vp9_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
  715. vp9_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2,
  716. owidth / 2, ouv_stride);
  717. vp9_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2,
  718. owidth / 2, ouv_stride);
  719. }
  720. void vp9_resize_frame422(const uint8_t *const y, int y_stride,
  721. const uint8_t *const u, const uint8_t *const v,
  722. int uv_stride, int height, int width, uint8_t *oy,
  723. int oy_stride, uint8_t *ou, uint8_t *ov,
  724. int ouv_stride, int oheight, int owidth) {
  725. vp9_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
  726. vp9_resize_plane(u, height, width / 2, uv_stride, ou, oheight, owidth / 2,
  727. ouv_stride);
  728. vp9_resize_plane(v, height, width / 2, uv_stride, ov, oheight, owidth / 2,
  729. ouv_stride);
  730. }
  731. void vp9_resize_frame444(const uint8_t *const y, int y_stride,
  732. const uint8_t *const u, const uint8_t *const v,
  733. int uv_stride, int height, int width, uint8_t *oy,
  734. int oy_stride, uint8_t *ou, uint8_t *ov,
  735. int ouv_stride, int oheight, int owidth) {
  736. vp9_resize_plane(y, height, width, y_stride, oy, oheight, owidth, oy_stride);
  737. vp9_resize_plane(u, height, width, uv_stride, ou, oheight, owidth,
  738. ouv_stride);
  739. vp9_resize_plane(v, height, width, uv_stride, ov, oheight, owidth,
  740. ouv_stride);
  741. }
  742. #if CONFIG_VP9_HIGHBITDEPTH
  743. void vp9_highbd_resize_frame420(const uint8_t *const y, int y_stride,
  744. const uint8_t *const u, const uint8_t *const v,
  745. int uv_stride, int height, int width,
  746. uint8_t *oy, int oy_stride, uint8_t *ou,
  747. uint8_t *ov, int ouv_stride, int oheight,
  748. int owidth, int bd) {
  749. vp9_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
  750. oy_stride, bd);
  751. vp9_highbd_resize_plane(u, height / 2, width / 2, uv_stride, ou, oheight / 2,
  752. owidth / 2, ouv_stride, bd);
  753. vp9_highbd_resize_plane(v, height / 2, width / 2, uv_stride, ov, oheight / 2,
  754. owidth / 2, ouv_stride, bd);
  755. }
  756. void vp9_highbd_resize_frame422(const uint8_t *const y, int y_stride,
  757. const uint8_t *const u, const uint8_t *const v,
  758. int uv_stride, int height, int width,
  759. uint8_t *oy, int oy_stride, uint8_t *ou,
  760. uint8_t *ov, int ouv_stride, int oheight,
  761. int owidth, int bd) {
  762. vp9_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
  763. oy_stride, bd);
  764. vp9_highbd_resize_plane(u, height, width / 2, uv_stride, ou, oheight,
  765. owidth / 2, ouv_stride, bd);
  766. vp9_highbd_resize_plane(v, height, width / 2, uv_stride, ov, oheight,
  767. owidth / 2, ouv_stride, bd);
  768. }
  769. void vp9_highbd_resize_frame444(const uint8_t *const y, int y_stride,
  770. const uint8_t *const u, const uint8_t *const v,
  771. int uv_stride, int height, int width,
  772. uint8_t *oy, int oy_stride, uint8_t *ou,
  773. uint8_t *ov, int ouv_stride, int oheight,
  774. int owidth, int bd) {
  775. vp9_highbd_resize_plane(y, height, width, y_stride, oy, oheight, owidth,
  776. oy_stride, bd);
  777. vp9_highbd_resize_plane(u, height, width, uv_stride, ou, oheight, owidth,
  778. ouv_stride, bd);
  779. vp9_highbd_resize_plane(v, height, width, uv_stride, ov, oheight, owidth,
  780. ouv_stride, bd);
  781. }
  782. #endif // CONFIG_VP9_HIGHBITDEPTH