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.

951 lines
29KB

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