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.

907 lines
27KB

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