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.

833 lines
25KB

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