imgresample.c 25 KB

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