rescaler.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Rescaling functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include "./rescaler.h"
  16. #include "../dsp/dsp.h"
  17. //------------------------------------------------------------------------------
  18. // Implementations of critical functions ImportRow / ExportRow
  19. void (*WebPRescalerImportRow)(WebPRescaler* const wrk,
  20. const uint8_t* const src, int channel) = NULL;
  21. void (*WebPRescalerExportRow)(WebPRescaler* const wrk, int x_out) = NULL;
  22. #define RFIX 30
  23. #define MULT_FIX(x, y) (((int64_t)(x) * (y) + (1 << (RFIX - 1))) >> RFIX)
  24. static void ImportRowC(WebPRescaler* const wrk,
  25. const uint8_t* const src, int channel) {
  26. const int x_stride = wrk->num_channels;
  27. const int x_out_max = wrk->dst_width * wrk->num_channels;
  28. int x_in = channel;
  29. int x_out;
  30. int accum = 0;
  31. if (!wrk->x_expand) {
  32. int sum = 0;
  33. for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
  34. accum += wrk->x_add;
  35. for (; accum > 0; accum -= wrk->x_sub) {
  36. sum += src[x_in];
  37. x_in += x_stride;
  38. }
  39. { // Emit next horizontal pixel.
  40. const int32_t base = src[x_in];
  41. const int32_t frac = base * (-accum);
  42. x_in += x_stride;
  43. wrk->frow[x_out] = (sum + base) * wrk->x_sub - frac;
  44. // fresh fractional start for next pixel
  45. sum = (int)MULT_FIX(frac, wrk->fx_scale);
  46. }
  47. }
  48. } else { // simple bilinear interpolation
  49. int left = src[channel], right = src[channel];
  50. for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
  51. if (accum < 0) {
  52. left = right;
  53. x_in += x_stride;
  54. right = src[x_in];
  55. accum += wrk->x_add;
  56. }
  57. wrk->frow[x_out] = right * wrk->x_add + (left - right) * accum;
  58. accum -= wrk->x_sub;
  59. }
  60. }
  61. // Accumulate the contribution of the new row.
  62. for (x_out = channel; x_out < x_out_max; x_out += x_stride) {
  63. wrk->irow[x_out] += wrk->frow[x_out];
  64. }
  65. }
  66. static void ExportRowC(WebPRescaler* const wrk, int x_out) {
  67. if (wrk->y_accum <= 0) {
  68. uint8_t* const dst = wrk->dst;
  69. int32_t* const irow = wrk->irow;
  70. const int32_t* const frow = wrk->frow;
  71. const int yscale = wrk->fy_scale * (-wrk->y_accum);
  72. const int x_out_max = wrk->dst_width * wrk->num_channels;
  73. for (; x_out < x_out_max; ++x_out) {
  74. const int frac = (int)MULT_FIX(frow[x_out], yscale);
  75. const int v = (int)MULT_FIX(irow[x_out] - frac, wrk->fxy_scale);
  76. dst[x_out] = (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
  77. irow[x_out] = frac; // new fractional start
  78. }
  79. wrk->y_accum += wrk->y_add;
  80. wrk->dst += wrk->dst_stride;
  81. }
  82. }
  83. //------------------------------------------------------------------------------
  84. // MIPS version
  85. #if defined(WEBP_USE_MIPS32)
  86. static void ImportRowMIPS(WebPRescaler* const wrk,
  87. const uint8_t* const src, int channel) {
  88. const int x_stride = wrk->num_channels;
  89. const int x_out_max = wrk->dst_width * wrk->num_channels;
  90. const int fx_scale = wrk->fx_scale;
  91. const int x_add = wrk->x_add;
  92. const int x_sub = wrk->x_sub;
  93. int* frow = wrk->frow + channel;
  94. int* irow = wrk->irow + channel;
  95. const uint8_t* src1 = src + channel;
  96. int temp1, temp2, temp3;
  97. int base, frac, sum;
  98. int accum, accum1;
  99. const int x_stride1 = x_stride << 2;
  100. int loop_c = x_out_max - channel;
  101. if (!wrk->x_expand) {
  102. __asm__ volatile (
  103. "li %[temp1], 0x8000 \n\t"
  104. "li %[temp2], 0x10000 \n\t"
  105. "li %[sum], 0 \n\t"
  106. "li %[accum], 0 \n\t"
  107. "1: \n\t"
  108. "addu %[accum], %[accum], %[x_add] \n\t"
  109. "blez %[accum], 3f \n\t"
  110. "2: \n\t"
  111. "lbu %[temp3], 0(%[src1]) \n\t"
  112. "subu %[accum], %[accum], %[x_sub] \n\t"
  113. "addu %[src1], %[src1], %[x_stride] \n\t"
  114. "addu %[sum], %[sum], %[temp3] \n\t"
  115. "bgtz %[accum], 2b \n\t"
  116. "3: \n\t"
  117. "lbu %[base], 0(%[src1]) \n\t"
  118. "addu %[src1], %[src1], %[x_stride] \n\t"
  119. "negu %[accum1], %[accum] \n\t"
  120. "mul %[frac], %[base], %[accum1] \n\t"
  121. "addu %[temp3], %[sum], %[base] \n\t"
  122. "mul %[temp3], %[temp3], %[x_sub] \n\t"
  123. "lw %[base], 0(%[irow]) \n\t"
  124. "subu %[loop_c], %[loop_c], %[x_stride] \n\t"
  125. "sll %[accum1], %[frac], 2 \n\t"
  126. "mult %[temp1], %[temp2] \n\t"
  127. "madd %[accum1], %[fx_scale] \n\t"
  128. "mfhi %[sum] \n\t"
  129. "subu %[temp3], %[temp3], %[frac] \n\t"
  130. "sw %[temp3], 0(%[frow]) \n\t"
  131. "add %[base], %[base], %[temp3] \n\t"
  132. "sw %[base], 0(%[irow]) \n\t"
  133. "addu %[irow], %[irow], %[x_stride1] \n\t"
  134. "addu %[frow], %[frow], %[x_stride1] \n\t"
  135. "bgtz %[loop_c], 1b \n\t"
  136. : [accum] "=&r" (accum), [src1] "+r" (src1), [temp3] "=&r" (temp3),
  137. [sum] "=&r" (sum), [base] "=&r" (base), [frac] "=&r" (frac),
  138. [frow] "+r" (frow), [irow] "+r" (irow), [accum1] "=&r" (accum1),
  139. [temp2] "=&r" (temp2), [temp1] "=&r" (temp1)
  140. : [x_stride] "r" (x_stride), [fx_scale] "r" (fx_scale),
  141. [x_sub] "r" (x_sub), [x_add] "r" (x_add),
  142. [loop_c] "r" (loop_c), [x_stride1] "r" (x_stride1)
  143. : "memory", "hi", "lo"
  144. );
  145. } else {
  146. __asm__ volatile (
  147. "lbu %[temp1], 0(%[src1]) \n\t"
  148. "move %[temp2], %[temp1] \n\t"
  149. "li %[accum], 0 \n\t"
  150. "1: \n\t"
  151. "bgez %[accum], 2f \n\t"
  152. "move %[temp2], %[temp1] \n\t"
  153. "addu %[src1], %[x_stride] \n\t"
  154. "lbu %[temp1], 0(%[src1]) \n\t"
  155. "addu %[accum], %[x_add] \n\t"
  156. "2: \n\t"
  157. "subu %[temp3], %[temp2], %[temp1] \n\t"
  158. "mul %[temp3], %[temp3], %[accum] \n\t"
  159. "mul %[base], %[temp1], %[x_add] \n\t"
  160. "subu %[accum], %[accum], %[x_sub] \n\t"
  161. "lw %[frac], 0(%[irow]) \n\t"
  162. "subu %[loop_c], %[loop_c], %[x_stride] \n\t"
  163. "addu %[temp3], %[base], %[temp3] \n\t"
  164. "sw %[temp3], 0(%[frow]) \n\t"
  165. "addu %[frow], %[x_stride1] \n\t"
  166. "addu %[frac], %[temp3] \n\t"
  167. "sw %[frac], 0(%[irow]) \n\t"
  168. "addu %[irow], %[x_stride1] \n\t"
  169. "bgtz %[loop_c], 1b \n\t"
  170. : [src1] "+r" (src1), [accum] "=&r" (accum), [temp1] "=&r" (temp1),
  171. [temp2] "=&r" (temp2), [temp3] "=&r" (temp3), [base] "=&r" (base),
  172. [frac] "=&r" (frac), [frow] "+r" (frow), [irow] "+r" (irow)
  173. : [x_stride] "r" (x_stride), [x_add] "r" (x_add), [x_sub] "r" (x_sub),
  174. [x_stride1] "r" (x_stride1), [loop_c] "r" (loop_c)
  175. : "memory", "hi", "lo"
  176. );
  177. }
  178. }
  179. static void ExportRowMIPS(WebPRescaler* const wrk, int x_out) {
  180. if (wrk->y_accum <= 0) {
  181. uint8_t* const dst = wrk->dst;
  182. int32_t* const irow = wrk->irow;
  183. const int32_t* const frow = wrk->frow;
  184. const int yscale = wrk->fy_scale * (-wrk->y_accum);
  185. const int x_out_max = wrk->dst_width * wrk->num_channels;
  186. // if wrk->fxy_scale can fit into 32 bits use optimized code,
  187. // otherwise use C code
  188. if ((wrk->fxy_scale >> 32) == 0) {
  189. int temp0, temp1, temp3, temp4, temp5, temp6, temp7, loop_end;
  190. const int temp2 = (int)(wrk->fxy_scale);
  191. const int temp8 = x_out_max << 2;
  192. uint8_t* dst_t = (uint8_t*)dst;
  193. int32_t* irow_t = (int32_t*)irow;
  194. const int32_t* frow_t = (const int32_t*)frow;
  195. __asm__ volatile(
  196. "addiu %[temp6], $zero, -256 \n\t"
  197. "addiu %[temp7], $zero, 255 \n\t"
  198. "li %[temp3], 0x10000 \n\t"
  199. "li %[temp4], 0x8000 \n\t"
  200. "addu %[loop_end], %[frow_t], %[temp8] \n\t"
  201. "1: \n\t"
  202. "lw %[temp0], 0(%[frow_t]) \n\t"
  203. "mult %[temp3], %[temp4] \n\t"
  204. "addiu %[frow_t], %[frow_t], 4 \n\t"
  205. "sll %[temp0], %[temp0], 2 \n\t"
  206. "madd %[temp0], %[yscale] \n\t"
  207. "mfhi %[temp1] \n\t"
  208. "lw %[temp0], 0(%[irow_t]) \n\t"
  209. "addiu %[dst_t], %[dst_t], 1 \n\t"
  210. "addiu %[irow_t], %[irow_t], 4 \n\t"
  211. "subu %[temp0], %[temp0], %[temp1] \n\t"
  212. "mult %[temp3], %[temp4] \n\t"
  213. "sll %[temp0], %[temp0], 2 \n\t"
  214. "madd %[temp0], %[temp2] \n\t"
  215. "mfhi %[temp5] \n\t"
  216. "sw %[temp1], -4(%[irow_t]) \n\t"
  217. "and %[temp0], %[temp5], %[temp6] \n\t"
  218. "slti %[temp1], %[temp5], 0 \n\t"
  219. "beqz %[temp0], 2f \n\t"
  220. "xor %[temp5], %[temp5], %[temp5] \n\t"
  221. "movz %[temp5], %[temp7], %[temp1] \n\t"
  222. "2: \n\t"
  223. "sb %[temp5], -1(%[dst_t]) \n\t"
  224. "bne %[frow_t], %[loop_end], 1b \n\t"
  225. : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1), [temp3]"=&r"(temp3),
  226. [temp4]"=&r"(temp4), [temp5]"=&r"(temp5), [temp6]"=&r"(temp6),
  227. [temp7]"=&r"(temp7), [frow_t]"+r"(frow_t), [irow_t]"+r"(irow_t),
  228. [dst_t]"+r"(dst_t), [loop_end]"=&r"(loop_end)
  229. : [temp2]"r"(temp2), [yscale]"r"(yscale), [temp8]"r"(temp8)
  230. : "memory", "hi", "lo"
  231. );
  232. wrk->y_accum += wrk->y_add;
  233. wrk->dst += wrk->dst_stride;
  234. } else {
  235. ExportRowC(wrk, x_out);
  236. }
  237. }
  238. }
  239. #endif // WEBP_USE_MIPS32
  240. //------------------------------------------------------------------------------
  241. void WebPRescalerInit(WebPRescaler* const wrk, int src_width, int src_height,
  242. uint8_t* const dst, int dst_width, int dst_height,
  243. int dst_stride, int num_channels, int x_add, int x_sub,
  244. int y_add, int y_sub, int32_t* const work) {
  245. wrk->x_expand = (src_width < dst_width);
  246. wrk->src_width = src_width;
  247. wrk->src_height = src_height;
  248. wrk->dst_width = dst_width;
  249. wrk->dst_height = dst_height;
  250. wrk->dst = dst;
  251. wrk->dst_stride = dst_stride;
  252. wrk->num_channels = num_channels;
  253. // for 'x_expand', we use bilinear interpolation
  254. wrk->x_add = wrk->x_expand ? (x_sub - 1) : x_add - x_sub;
  255. wrk->x_sub = wrk->x_expand ? (x_add - 1) : x_sub;
  256. wrk->y_accum = y_add;
  257. wrk->y_add = y_add;
  258. wrk->y_sub = y_sub;
  259. wrk->fx_scale = (1 << RFIX) / x_sub;
  260. wrk->fy_scale = (1 << RFIX) / y_sub;
  261. wrk->fxy_scale = wrk->x_expand ?
  262. ((int64_t)dst_height << RFIX) / (x_sub * src_height) :
  263. ((int64_t)dst_height << RFIX) / (x_add * src_height);
  264. wrk->irow = work;
  265. wrk->frow = work + num_channels * dst_width;
  266. if (WebPRescalerImportRow == NULL) {
  267. WebPRescalerImportRow = ImportRowC;
  268. WebPRescalerExportRow = ExportRowC;
  269. if (VP8GetCPUInfo != NULL) {
  270. #if defined(WEBP_USE_MIPS32)
  271. if (VP8GetCPUInfo(kMIPS32)) {
  272. WebPRescalerImportRow = ImportRowMIPS;
  273. WebPRescalerExportRow = ExportRowMIPS;
  274. }
  275. #endif
  276. }
  277. }
  278. }
  279. #undef MULT_FIX
  280. #undef RFIX
  281. //------------------------------------------------------------------------------
  282. // all-in-one calls
  283. int WebPRescaleNeededLines(const WebPRescaler* const wrk, int max_num_lines) {
  284. const int num_lines = (wrk->y_accum + wrk->y_sub - 1) / wrk->y_sub;
  285. return (num_lines > max_num_lines) ? max_num_lines : num_lines;
  286. }
  287. int WebPRescalerImport(WebPRescaler* const wrk, int num_lines,
  288. const uint8_t* src, int src_stride) {
  289. int total_imported = 0;
  290. while (total_imported < num_lines && wrk->y_accum > 0) {
  291. int channel;
  292. for (channel = 0; channel < wrk->num_channels; ++channel) {
  293. WebPRescalerImportRow(wrk, src, channel);
  294. }
  295. src += src_stride;
  296. ++total_imported;
  297. wrk->y_accum -= wrk->y_sub;
  298. }
  299. return total_imported;
  300. }
  301. int WebPRescalerExport(WebPRescaler* const rescaler) {
  302. int total_exported = 0;
  303. while (WebPRescalerHasPendingOutput(rescaler)) {
  304. WebPRescalerExportRow(rescaler, 0);
  305. ++total_exported;
  306. }
  307. return total_exported;
  308. }
  309. //------------------------------------------------------------------------------