vp9_resize.c 33 KB

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