You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

762 lines
22KB

  1. /*
  2. * High quality image resampling with polyphase filters
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file imgresample.c
  21. * High quality image resampling with polyphase filters .
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #ifdef USE_FASTMEMCPY
  26. #include "fastmemcpy.h"
  27. #endif
  28. #define NB_COMPONENTS 3
  29. #define PHASE_BITS 4
  30. #define NB_PHASES (1 << PHASE_BITS)
  31. #define NB_TAPS 4
  32. #define FCENTER 1 /* index of the center of the filter */
  33. //#define TEST 1 /* Test it */
  34. #define POS_FRAC_BITS 16
  35. #define POS_FRAC (1 << POS_FRAC_BITS)
  36. /* 6 bits precision is needed for MMX */
  37. #define FILTER_BITS 8
  38. #define LINE_BUF_HEIGHT (NB_TAPS * 4)
  39. struct ImgReSampleContext {
  40. int iwidth, iheight, owidth, oheight;
  41. int topBand, bottomBand, leftBand, rightBand;
  42. int padtop, padbottom, padleft, padright;
  43. int pad_owidth, pad_oheight;
  44. int h_incr, v_incr;
  45. int16_t h_filters[NB_PHASES][NB_TAPS] __align8; /* horizontal filters */
  46. int16_t v_filters[NB_PHASES][NB_TAPS] __align8; /* vertical filters */
  47. uint8_t *line_buf;
  48. };
  49. void av_build_filter(int16_t *filter, double factor, int tap_count, int phase_count, int scale, int type);
  50. static inline int get_phase(int pos)
  51. {
  52. return ((pos) >> (POS_FRAC_BITS - PHASE_BITS)) & ((1 << PHASE_BITS) - 1);
  53. }
  54. /* This function must be optimized */
  55. static void h_resample_fast(uint8_t *dst, int dst_width, const uint8_t *src,
  56. int src_width, int src_start, int src_incr,
  57. int16_t *filters)
  58. {
  59. int src_pos, phase, sum, i;
  60. const uint8_t *s;
  61. int16_t *filter;
  62. src_pos = src_start;
  63. for(i=0;i<dst_width;i++) {
  64. #ifdef TEST
  65. /* test */
  66. if ((src_pos >> POS_FRAC_BITS) < 0 ||
  67. (src_pos >> POS_FRAC_BITS) > (src_width - NB_TAPS))
  68. av_abort();
  69. #endif
  70. s = src + (src_pos >> POS_FRAC_BITS);
  71. phase = get_phase(src_pos);
  72. filter = filters + phase * NB_TAPS;
  73. #if NB_TAPS == 4
  74. sum = s[0] * filter[0] +
  75. s[1] * filter[1] +
  76. s[2] * filter[2] +
  77. s[3] * filter[3];
  78. #else
  79. {
  80. int j;
  81. sum = 0;
  82. for(j=0;j<NB_TAPS;j++)
  83. sum += s[j] * filter[j];
  84. }
  85. #endif
  86. sum = sum >> FILTER_BITS;
  87. if (sum < 0)
  88. sum = 0;
  89. else if (sum > 255)
  90. sum = 255;
  91. dst[0] = sum;
  92. src_pos += src_incr;
  93. dst++;
  94. }
  95. }
  96. /* This function must be optimized */
  97. static void v_resample(uint8_t *dst, int dst_width, const uint8_t *src,
  98. int wrap, int16_t *filter)
  99. {
  100. int sum, i;
  101. const uint8_t *s;
  102. s = src;
  103. for(i=0;i<dst_width;i++) {
  104. #if NB_TAPS == 4
  105. sum = s[0 * wrap] * filter[0] +
  106. s[1 * wrap] * filter[1] +
  107. s[2 * wrap] * filter[2] +
  108. s[3 * wrap] * filter[3];
  109. #else
  110. {
  111. int j;
  112. uint8_t *s1 = s;
  113. sum = 0;
  114. for(j=0;j<NB_TAPS;j++) {
  115. sum += s1[0] * filter[j];
  116. s1 += wrap;
  117. }
  118. }
  119. #endif
  120. sum = sum >> FILTER_BITS;
  121. if (sum < 0)
  122. sum = 0;
  123. else if (sum > 255)
  124. sum = 255;
  125. dst[0] = sum;
  126. dst++;
  127. s++;
  128. }
  129. }
  130. #ifdef HAVE_MMX
  131. #include "i386/mmx.h"
  132. #define FILTER4(reg) \
  133. {\
  134. s = src + (src_pos >> POS_FRAC_BITS);\
  135. phase = get_phase(src_pos);\
  136. filter = filters + phase * NB_TAPS;\
  137. movq_m2r(*s, reg);\
  138. punpcklbw_r2r(mm7, reg);\
  139. movq_m2r(*filter, mm6);\
  140. pmaddwd_r2r(reg, mm6);\
  141. movq_r2r(mm6, reg);\
  142. psrlq_i2r(32, reg);\
  143. paddd_r2r(mm6, reg);\
  144. psrad_i2r(FILTER_BITS, reg);\
  145. src_pos += src_incr;\
  146. }
  147. #define DUMP(reg) movq_r2m(reg, tmp); printf(#reg "=%016Lx\n", tmp.uq);
  148. /* XXX: do four pixels at a time */
  149. static void h_resample_fast4_mmx(uint8_t *dst, int dst_width,
  150. const uint8_t *src, int src_width,
  151. int src_start, int src_incr, int16_t *filters)
  152. {
  153. int src_pos, phase;
  154. const uint8_t *s;
  155. int16_t *filter;
  156. mmx_t tmp;
  157. src_pos = src_start;
  158. pxor_r2r(mm7, mm7);
  159. while (dst_width >= 4) {
  160. FILTER4(mm0);
  161. FILTER4(mm1);
  162. FILTER4(mm2);
  163. FILTER4(mm3);
  164. packuswb_r2r(mm7, mm0);
  165. packuswb_r2r(mm7, mm1);
  166. packuswb_r2r(mm7, mm3);
  167. packuswb_r2r(mm7, mm2);
  168. movq_r2m(mm0, tmp);
  169. dst[0] = tmp.ub[0];
  170. movq_r2m(mm1, tmp);
  171. dst[1] = tmp.ub[0];
  172. movq_r2m(mm2, tmp);
  173. dst[2] = tmp.ub[0];
  174. movq_r2m(mm3, tmp);
  175. dst[3] = tmp.ub[0];
  176. dst += 4;
  177. dst_width -= 4;
  178. }
  179. while (dst_width > 0) {
  180. FILTER4(mm0);
  181. packuswb_r2r(mm7, mm0);
  182. movq_r2m(mm0, tmp);
  183. dst[0] = tmp.ub[0];
  184. dst++;
  185. dst_width--;
  186. }
  187. emms();
  188. }
  189. static void v_resample4_mmx(uint8_t *dst, int dst_width, const uint8_t *src,
  190. int wrap, int16_t *filter)
  191. {
  192. int sum, i, v;
  193. const uint8_t *s;
  194. mmx_t tmp;
  195. mmx_t coefs[4];
  196. for(i=0;i<4;i++) {
  197. v = filter[i];
  198. coefs[i].uw[0] = v;
  199. coefs[i].uw[1] = v;
  200. coefs[i].uw[2] = v;
  201. coefs[i].uw[3] = v;
  202. }
  203. pxor_r2r(mm7, mm7);
  204. s = src;
  205. while (dst_width >= 4) {
  206. movq_m2r(s[0 * wrap], mm0);
  207. punpcklbw_r2r(mm7, mm0);
  208. movq_m2r(s[1 * wrap], mm1);
  209. punpcklbw_r2r(mm7, mm1);
  210. movq_m2r(s[2 * wrap], mm2);
  211. punpcklbw_r2r(mm7, mm2);
  212. movq_m2r(s[3 * wrap], mm3);
  213. punpcklbw_r2r(mm7, mm3);
  214. pmullw_m2r(coefs[0], mm0);
  215. pmullw_m2r(coefs[1], mm1);
  216. pmullw_m2r(coefs[2], mm2);
  217. pmullw_m2r(coefs[3], mm3);
  218. paddw_r2r(mm1, mm0);
  219. paddw_r2r(mm3, mm2);
  220. paddw_r2r(mm2, mm0);
  221. psraw_i2r(FILTER_BITS, mm0);
  222. packuswb_r2r(mm7, mm0);
  223. movq_r2m(mm0, tmp);
  224. *(uint32_t *)dst = tmp.ud[0];
  225. dst += 4;
  226. s += 4;
  227. dst_width -= 4;
  228. }
  229. while (dst_width > 0) {
  230. sum = s[0 * wrap] * filter[0] +
  231. s[1 * wrap] * filter[1] +
  232. s[2 * wrap] * filter[2] +
  233. s[3 * wrap] * filter[3];
  234. sum = sum >> FILTER_BITS;
  235. if (sum < 0)
  236. sum = 0;
  237. else if (sum > 255)
  238. sum = 255;
  239. dst[0] = sum;
  240. dst++;
  241. s++;
  242. dst_width--;
  243. }
  244. emms();
  245. }
  246. #endif
  247. #ifdef HAVE_ALTIVEC
  248. typedef union {
  249. vector unsigned char v;
  250. unsigned char c[16];
  251. } vec_uc_t;
  252. typedef union {
  253. vector signed short v;
  254. signed short s[8];
  255. } vec_ss_t;
  256. void v_resample16_altivec(uint8_t *dst, int dst_width, const uint8_t *src,
  257. int wrap, int16_t *filter)
  258. {
  259. int sum, i;
  260. const uint8_t *s;
  261. vector unsigned char *tv, tmp, dstv, zero;
  262. vec_ss_t srchv[4], srclv[4], fv[4];
  263. vector signed short zeros, sumhv, sumlv;
  264. s = src;
  265. for(i=0;i<4;i++)
  266. {
  267. /*
  268. The vec_madds later on does an implicit >>15 on the result.
  269. Since FILTER_BITS is 8, and we have 15 bits of magnitude in
  270. a signed short, we have just enough bits to pre-shift our
  271. filter constants <<7 to compensate for vec_madds.
  272. */
  273. fv[i].s[0] = filter[i] << (15-FILTER_BITS);
  274. fv[i].v = vec_splat(fv[i].v, 0);
  275. }
  276. zero = vec_splat_u8(0);
  277. zeros = vec_splat_s16(0);
  278. /*
  279. When we're resampling, we'd ideally like both our input buffers,
  280. and output buffers to be 16-byte aligned, so we can do both aligned
  281. reads and writes. Sadly we can't always have this at the moment, so
  282. we opt for aligned writes, as unaligned writes have a huge overhead.
  283. To do this, do enough scalar resamples to get dst 16-byte aligned.
  284. */
  285. i = (-(int)dst) & 0xf;
  286. while(i>0) {
  287. sum = s[0 * wrap] * filter[0] +
  288. s[1 * wrap] * filter[1] +
  289. s[2 * wrap] * filter[2] +
  290. s[3 * wrap] * filter[3];
  291. sum = sum >> FILTER_BITS;
  292. if (sum<0) sum = 0; else if (sum>255) sum=255;
  293. dst[0] = sum;
  294. dst++;
  295. s++;
  296. dst_width--;
  297. i--;
  298. }
  299. /* Do our altivec resampling on 16 pixels at once. */
  300. while(dst_width>=16) {
  301. /*
  302. Read 16 (potentially unaligned) bytes from each of
  303. 4 lines into 4 vectors, and split them into shorts.
  304. Interleave the multipy/accumulate for the resample
  305. filter with the loads to hide the 3 cycle latency
  306. the vec_madds have.
  307. */
  308. tv = (vector unsigned char *) &s[0 * wrap];
  309. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[i * wrap]));
  310. srchv[0].v = (vector signed short) vec_mergeh(zero, tmp);
  311. srclv[0].v = (vector signed short) vec_mergel(zero, tmp);
  312. sumhv = vec_madds(srchv[0].v, fv[0].v, zeros);
  313. sumlv = vec_madds(srclv[0].v, fv[0].v, zeros);
  314. tv = (vector unsigned char *) &s[1 * wrap];
  315. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[1 * wrap]));
  316. srchv[1].v = (vector signed short) vec_mergeh(zero, tmp);
  317. srclv[1].v = (vector signed short) vec_mergel(zero, tmp);
  318. sumhv = vec_madds(srchv[1].v, fv[1].v, sumhv);
  319. sumlv = vec_madds(srclv[1].v, fv[1].v, sumlv);
  320. tv = (vector unsigned char *) &s[2 * wrap];
  321. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[2 * wrap]));
  322. srchv[2].v = (vector signed short) vec_mergeh(zero, tmp);
  323. srclv[2].v = (vector signed short) vec_mergel(zero, tmp);
  324. sumhv = vec_madds(srchv[2].v, fv[2].v, sumhv);
  325. sumlv = vec_madds(srclv[2].v, fv[2].v, sumlv);
  326. tv = (vector unsigned char *) &s[3 * wrap];
  327. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[3 * wrap]));
  328. srchv[3].v = (vector signed short) vec_mergeh(zero, tmp);
  329. srclv[3].v = (vector signed short) vec_mergel(zero, tmp);
  330. sumhv = vec_madds(srchv[3].v, fv[3].v, sumhv);
  331. sumlv = vec_madds(srclv[3].v, fv[3].v, sumlv);
  332. /*
  333. Pack the results into our destination vector,
  334. and do an aligned write of that back to memory.
  335. */
  336. dstv = vec_packsu(sumhv, sumlv) ;
  337. vec_st(dstv, 0, (vector unsigned char *) dst);
  338. dst+=16;
  339. s+=16;
  340. dst_width-=16;
  341. }
  342. /*
  343. If there are any leftover pixels, resample them
  344. with the slow scalar method.
  345. */
  346. while(dst_width>0) {
  347. sum = s[0 * wrap] * filter[0] +
  348. s[1 * wrap] * filter[1] +
  349. s[2 * wrap] * filter[2] +
  350. s[3 * wrap] * filter[3];
  351. sum = sum >> FILTER_BITS;
  352. if (sum<0) sum = 0; else if (sum>255) sum=255;
  353. dst[0] = sum;
  354. dst++;
  355. s++;
  356. dst_width--;
  357. }
  358. }
  359. #endif
  360. /* slow version to handle limit cases. Does not need optimisation */
  361. static void h_resample_slow(uint8_t *dst, int dst_width,
  362. const uint8_t *src, int src_width,
  363. int src_start, int src_incr, int16_t *filters)
  364. {
  365. int src_pos, phase, sum, j, v, i;
  366. const uint8_t *s, *src_end;
  367. int16_t *filter;
  368. src_end = src + src_width;
  369. src_pos = src_start;
  370. for(i=0;i<dst_width;i++) {
  371. s = src + (src_pos >> POS_FRAC_BITS);
  372. phase = get_phase(src_pos);
  373. filter = filters + phase * NB_TAPS;
  374. sum = 0;
  375. for(j=0;j<NB_TAPS;j++) {
  376. if (s < src)
  377. v = src[0];
  378. else if (s >= src_end)
  379. v = src_end[-1];
  380. else
  381. v = s[0];
  382. sum += v * filter[j];
  383. s++;
  384. }
  385. sum = sum >> FILTER_BITS;
  386. if (sum < 0)
  387. sum = 0;
  388. else if (sum > 255)
  389. sum = 255;
  390. dst[0] = sum;
  391. src_pos += src_incr;
  392. dst++;
  393. }
  394. }
  395. static void h_resample(uint8_t *dst, int dst_width, const uint8_t *src,
  396. int src_width, int src_start, int src_incr,
  397. int16_t *filters)
  398. {
  399. int n, src_end;
  400. if (src_start < 0) {
  401. n = (0 - src_start + src_incr - 1) / src_incr;
  402. h_resample_slow(dst, n, src, src_width, src_start, src_incr, filters);
  403. dst += n;
  404. dst_width -= n;
  405. src_start += n * src_incr;
  406. }
  407. src_end = src_start + dst_width * src_incr;
  408. if (src_end > ((src_width - NB_TAPS) << POS_FRAC_BITS)) {
  409. n = (((src_width - NB_TAPS + 1) << POS_FRAC_BITS) - 1 - src_start) /
  410. src_incr;
  411. } else {
  412. n = dst_width;
  413. }
  414. #ifdef HAVE_MMX
  415. if ((mm_flags & MM_MMX) && NB_TAPS == 4)
  416. h_resample_fast4_mmx(dst, n,
  417. src, src_width, src_start, src_incr, filters);
  418. else
  419. #endif
  420. h_resample_fast(dst, n,
  421. src, src_width, src_start, src_incr, filters);
  422. if (n < dst_width) {
  423. dst += n;
  424. dst_width -= n;
  425. src_start += n * src_incr;
  426. h_resample_slow(dst, dst_width,
  427. src, src_width, src_start, src_incr, filters);
  428. }
  429. }
  430. static void component_resample(ImgReSampleContext *s,
  431. uint8_t *output, int owrap, int owidth, int oheight,
  432. uint8_t *input, int iwrap, int iwidth, int iheight)
  433. {
  434. int src_y, src_y1, last_src_y, ring_y, phase_y, y1, y;
  435. uint8_t *new_line, *src_line;
  436. last_src_y = - FCENTER - 1;
  437. /* position of the bottom of the filter in the source image */
  438. src_y = (last_src_y + NB_TAPS) * POS_FRAC;
  439. ring_y = NB_TAPS; /* position in ring buffer */
  440. for(y=0;y<oheight;y++) {
  441. /* apply horizontal filter on new lines from input if needed */
  442. src_y1 = src_y >> POS_FRAC_BITS;
  443. while (last_src_y < src_y1) {
  444. if (++ring_y >= LINE_BUF_HEIGHT + NB_TAPS)
  445. ring_y = NB_TAPS;
  446. last_src_y++;
  447. /* handle limit conditions : replicate line (slightly
  448. inefficient because we filter multiple times) */
  449. y1 = last_src_y;
  450. if (y1 < 0) {
  451. y1 = 0;
  452. } else if (y1 >= iheight) {
  453. y1 = iheight - 1;
  454. }
  455. src_line = input + y1 * iwrap;
  456. new_line = s->line_buf + ring_y * owidth;
  457. /* apply filter and handle limit cases correctly */
  458. h_resample(new_line, owidth,
  459. src_line, iwidth, - FCENTER * POS_FRAC, s->h_incr,
  460. &s->h_filters[0][0]);
  461. /* handle ring buffer wraping */
  462. if (ring_y >= LINE_BUF_HEIGHT) {
  463. memcpy(s->line_buf + (ring_y - LINE_BUF_HEIGHT) * owidth,
  464. new_line, owidth);
  465. }
  466. }
  467. /* apply vertical filter */
  468. phase_y = get_phase(src_y);
  469. #ifdef HAVE_MMX
  470. /* desactivated MMX because loss of precision */
  471. if ((mm_flags & MM_MMX) && NB_TAPS == 4 && 0)
  472. v_resample4_mmx(output, owidth,
  473. s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
  474. &s->v_filters[phase_y][0]);
  475. else
  476. #endif
  477. #ifdef HAVE_ALTIVEC
  478. if ((mm_flags & MM_ALTIVEC) && NB_TAPS == 4 && FILTER_BITS <= 6)
  479. v_resample16_altivec(output, owidth,
  480. s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
  481. &s->v_filters[phase_y][0]);
  482. else
  483. #endif
  484. v_resample(output, owidth,
  485. s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
  486. &s->v_filters[phase_y][0]);
  487. src_y += s->v_incr;
  488. output += owrap;
  489. }
  490. }
  491. ImgReSampleContext *img_resample_init(int owidth, int oheight,
  492. int iwidth, int iheight)
  493. {
  494. return img_resample_full_init(owidth, oheight, iwidth, iheight,
  495. 0, 0, 0, 0, 0, 0, 0, 0);
  496. }
  497. ImgReSampleContext *img_resample_full_init(int owidth, int oheight,
  498. int iwidth, int iheight,
  499. int topBand, int bottomBand,
  500. int leftBand, int rightBand,
  501. int padtop, int padbottom,
  502. int padleft, int padright)
  503. {
  504. ImgReSampleContext *s;
  505. s = av_mallocz(sizeof(ImgReSampleContext));
  506. if (!s)
  507. return NULL;
  508. if((unsigned)owidth >= UINT_MAX / (LINE_BUF_HEIGHT + NB_TAPS))
  509. return NULL;
  510. s->line_buf = av_mallocz(owidth * (LINE_BUF_HEIGHT + NB_TAPS));
  511. if (!s->line_buf)
  512. goto fail;
  513. s->owidth = owidth;
  514. s->oheight = oheight;
  515. s->iwidth = iwidth;
  516. s->iheight = iheight;
  517. s->topBand = topBand;
  518. s->bottomBand = bottomBand;
  519. s->leftBand = leftBand;
  520. s->rightBand = rightBand;
  521. s->padtop = padtop;
  522. s->padbottom = padbottom;
  523. s->padleft = padleft;
  524. s->padright = padright;
  525. s->pad_owidth = owidth - (padleft + padright);
  526. s->pad_oheight = oheight - (padtop + padbottom);
  527. s->h_incr = ((iwidth - leftBand - rightBand) * POS_FRAC) / s->pad_owidth;
  528. s->v_incr = ((iheight - topBand - bottomBand) * POS_FRAC) / s->pad_oheight;
  529. av_build_filter(&s->h_filters[0][0], (float) s->pad_owidth /
  530. (float) (iwidth - leftBand - rightBand), NB_TAPS, NB_PHASES, 1<<FILTER_BITS, 0);
  531. av_build_filter(&s->v_filters[0][0], (float) s->pad_oheight /
  532. (float) (iheight - topBand - bottomBand), NB_TAPS, NB_PHASES, 1<<FILTER_BITS, 0);
  533. return s;
  534. fail:
  535. av_free(s);
  536. return NULL;
  537. }
  538. void img_resample(ImgReSampleContext *s,
  539. AVPicture *output, const AVPicture *input)
  540. {
  541. int i, shift;
  542. uint8_t* optr;
  543. for (i=0;i<3;i++) {
  544. shift = (i == 0) ? 0 : 1;
  545. optr = output->data[i] + (((output->linesize[i] *
  546. s->padtop) + s->padleft) >> shift);
  547. component_resample(s, optr, output->linesize[i],
  548. s->pad_owidth >> shift, s->pad_oheight >> shift,
  549. input->data[i] + (input->linesize[i] *
  550. (s->topBand >> shift)) + (s->leftBand >> shift),
  551. input->linesize[i], ((s->iwidth - s->leftBand -
  552. s->rightBand) >> shift),
  553. (s->iheight - s->topBand - s->bottomBand) >> shift);
  554. }
  555. }
  556. void img_resample_close(ImgReSampleContext *s)
  557. {
  558. av_free(s->line_buf);
  559. av_free(s);
  560. }
  561. #ifdef TEST
  562. #include <stdio.h>
  563. /* input */
  564. #define XSIZE 256
  565. #define YSIZE 256
  566. uint8_t img[XSIZE * YSIZE];
  567. /* output */
  568. #define XSIZE1 512
  569. #define YSIZE1 512
  570. uint8_t img1[XSIZE1 * YSIZE1];
  571. uint8_t img2[XSIZE1 * YSIZE1];
  572. void save_pgm(const char *filename, uint8_t *img, int xsize, int ysize)
  573. {
  574. FILE *f;
  575. f=fopen(filename,"w");
  576. fprintf(f,"P5\n%d %d\n%d\n", xsize, ysize, 255);
  577. fwrite(img,1, xsize * ysize,f);
  578. fclose(f);
  579. }
  580. static void dump_filter(int16_t *filter)
  581. {
  582. int i, ph;
  583. for(ph=0;ph<NB_PHASES;ph++) {
  584. av_log(NULL, AV_LOG_INFO, "%2d: ", ph);
  585. for(i=0;i<NB_TAPS;i++) {
  586. av_log(NULL, AV_LOG_INFO, " %5.2f", filter[ph * NB_TAPS + i] / 256.0);
  587. }
  588. av_log(NULL, AV_LOG_INFO, "\n");
  589. }
  590. }
  591. #ifdef HAVE_MMX
  592. int mm_flags;
  593. #endif
  594. int main(int argc, char **argv)
  595. {
  596. int x, y, v, i, xsize, ysize;
  597. ImgReSampleContext *s;
  598. float fact, factors[] = { 1/2.0, 3.0/4.0, 1.0, 4.0/3.0, 16.0/9.0, 2.0 };
  599. char buf[256];
  600. /* build test image */
  601. for(y=0;y<YSIZE;y++) {
  602. for(x=0;x<XSIZE;x++) {
  603. if (x < XSIZE/2 && y < YSIZE/2) {
  604. if (x < XSIZE/4 && y < YSIZE/4) {
  605. if ((x % 10) <= 6 &&
  606. (y % 10) <= 6)
  607. v = 0xff;
  608. else
  609. v = 0x00;
  610. } else if (x < XSIZE/4) {
  611. if (x & 1)
  612. v = 0xff;
  613. else
  614. v = 0;
  615. } else if (y < XSIZE/4) {
  616. if (y & 1)
  617. v = 0xff;
  618. else
  619. v = 0;
  620. } else {
  621. if (y < YSIZE*3/8) {
  622. if ((y+x) & 1)
  623. v = 0xff;
  624. else
  625. v = 0;
  626. } else {
  627. if (((x+3) % 4) <= 1 &&
  628. ((y+3) % 4) <= 1)
  629. v = 0xff;
  630. else
  631. v = 0x00;
  632. }
  633. }
  634. } else if (x < XSIZE/2) {
  635. v = ((x - (XSIZE/2)) * 255) / (XSIZE/2);
  636. } else if (y < XSIZE/2) {
  637. v = ((y - (XSIZE/2)) * 255) / (XSIZE/2);
  638. } else {
  639. v = ((x + y - XSIZE) * 255) / XSIZE;
  640. }
  641. img[(YSIZE - y) * XSIZE + (XSIZE - x)] = v;
  642. }
  643. }
  644. save_pgm("/tmp/in.pgm", img, XSIZE, YSIZE);
  645. for(i=0;i<sizeof(factors)/sizeof(float);i++) {
  646. fact = factors[i];
  647. xsize = (int)(XSIZE * fact);
  648. ysize = (int)((YSIZE - 100) * fact);
  649. s = img_resample_full_init(xsize, ysize, XSIZE, YSIZE, 50 ,50, 0, 0, 0, 0, 0, 0);
  650. av_log(NULL, AV_LOG_INFO, "Factor=%0.2f\n", fact);
  651. dump_filter(&s->h_filters[0][0]);
  652. component_resample(s, img1, xsize, xsize, ysize,
  653. img + 50 * XSIZE, XSIZE, XSIZE, YSIZE - 100);
  654. img_resample_close(s);
  655. snprintf(buf, sizeof(buf), "/tmp/out%d.pgm", i);
  656. save_pgm(buf, img1, xsize, ysize);
  657. }
  658. /* mmx test */
  659. #ifdef HAVE_MMX
  660. av_log(NULL, AV_LOG_INFO, "MMX test\n");
  661. fact = 0.72;
  662. xsize = (int)(XSIZE * fact);
  663. ysize = (int)(YSIZE * fact);
  664. mm_flags = MM_MMX;
  665. s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
  666. component_resample(s, img1, xsize, xsize, ysize,
  667. img, XSIZE, XSIZE, YSIZE);
  668. mm_flags = 0;
  669. s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
  670. component_resample(s, img2, xsize, xsize, ysize,
  671. img, XSIZE, XSIZE, YSIZE);
  672. if (memcmp(img1, img2, xsize * ysize) != 0) {
  673. av_log(NULL, AV_LOG_ERROR, "mmx error\n");
  674. exit(1);
  675. }
  676. av_log(NULL, AV_LOG_INFO, "MMX OK\n");
  677. #endif
  678. return 0;
  679. }
  680. #endif