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.

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