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.

592 lines
17KB

  1. /*
  2. * Misc image convertion routines
  3. * Copyright (c) 2001, 2002 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "dsputil.h"
  21. #ifdef USE_FASTMEMCPY
  22. #include "fastmemcpy.h"
  23. #endif
  24. /* XXX: totally non optimized */
  25. static void yuv422_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
  26. UINT8 *src, int width, int height)
  27. {
  28. int x, y;
  29. UINT8 *p = src;
  30. for(y=0;y<height;y+=2) {
  31. for(x=0;x<width;x+=2) {
  32. lum[0] = p[0];
  33. cb[0] = p[1];
  34. lum[1] = p[2];
  35. cr[0] = p[3];
  36. p += 4;
  37. lum += 2;
  38. cb++;
  39. cr++;
  40. }
  41. for(x=0;x<width;x+=2) {
  42. lum[0] = p[0];
  43. lum[1] = p[2];
  44. p += 4;
  45. lum += 2;
  46. }
  47. }
  48. }
  49. #define SCALEBITS 8
  50. #define ONE_HALF (1 << (SCALEBITS - 1))
  51. #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
  52. static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
  53. UINT8 *src, int width, int height)
  54. {
  55. int wrap, wrap3, x, y;
  56. int r, g, b, r1, g1, b1;
  57. UINT8 *p;
  58. wrap = width;
  59. wrap3 = width * 3;
  60. p = src;
  61. for(y=0;y<height;y+=2) {
  62. for(x=0;x<width;x+=2) {
  63. r = p[0];
  64. g = p[1];
  65. b = p[2];
  66. r1 = r;
  67. g1 = g;
  68. b1 = b;
  69. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  70. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  71. r = p[3];
  72. g = p[4];
  73. b = p[5];
  74. r1 += r;
  75. g1 += g;
  76. b1 += b;
  77. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  78. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  79. p += wrap3;
  80. lum += wrap;
  81. r = p[0];
  82. g = p[1];
  83. b = p[2];
  84. r1 += r;
  85. g1 += g;
  86. b1 += b;
  87. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  88. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  89. r = p[3];
  90. g = p[4];
  91. b = p[5];
  92. r1 += r;
  93. g1 += g;
  94. b1 += b;
  95. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  96. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  97. cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
  98. FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  99. cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
  100. FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  101. cb++;
  102. cr++;
  103. p += -wrap3 + 2 * 3;
  104. lum += -wrap + 2;
  105. }
  106. p += wrap3;
  107. lum += wrap;
  108. }
  109. }
  110. static void bgr24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
  111. UINT8 *src, int width, int height)
  112. {
  113. int wrap, wrap3, x, y;
  114. int r, g, b, r1, g1, b1;
  115. UINT8 *p;
  116. wrap = width;
  117. wrap3 = width * 3;
  118. p = src;
  119. for(y=0;y<height;y+=2) {
  120. for(x=0;x<width;x+=2) {
  121. b = p[0];
  122. g = p[1];
  123. r = p[2];
  124. r1 = r;
  125. g1 = g;
  126. b1 = b;
  127. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  128. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  129. b = p[3];
  130. g = p[4];
  131. r = p[5];
  132. r1 += r;
  133. g1 += g;
  134. b1 += b;
  135. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  136. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  137. p += wrap3;
  138. lum += wrap;
  139. b = p[0];
  140. g = p[1];
  141. r = p[2];
  142. r1 += r;
  143. g1 += g;
  144. b1 += b;
  145. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  146. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  147. b = p[3];
  148. g = p[4];
  149. r = p[5];
  150. r1 += r;
  151. g1 += g;
  152. b1 += b;
  153. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  154. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  155. cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
  156. FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  157. cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
  158. FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  159. cb++;
  160. cr++;
  161. p += -wrap3 + 2 * 3;
  162. lum += -wrap + 2;
  163. }
  164. p += wrap3;
  165. lum += wrap;
  166. }
  167. }
  168. /* XXX: use generic filter ? */
  169. /* 1x2 -> 1x1 */
  170. static void shrink2(UINT8 *dst, int dst_wrap,
  171. UINT8 *src, int src_wrap,
  172. int width, int height)
  173. {
  174. int w;
  175. UINT8 *s1, *s2, *d;
  176. for(;height > 0; height--) {
  177. s1 = src;
  178. s2 = s1 + src_wrap;
  179. d = dst;
  180. for(w = width;w >= 4; w-=4) {
  181. d[0] = (s1[0] + s2[0]) >> 1;
  182. d[1] = (s1[1] + s2[1]) >> 1;
  183. d[2] = (s1[2] + s2[2]) >> 1;
  184. d[3] = (s1[3] + s2[3]) >> 1;
  185. s1 += 4;
  186. s2 += 4;
  187. d += 4;
  188. }
  189. for(;w > 0; w--) {
  190. d[0] = (s1[0] + s2[0]) >> 1;
  191. s1++;
  192. s2++;
  193. d++;
  194. }
  195. src += 2 * src_wrap;
  196. dst += dst_wrap;
  197. }
  198. }
  199. /* 2x2 -> 1x1 */
  200. static void shrink22(UINT8 *dst, int dst_wrap,
  201. UINT8 *src, int src_wrap,
  202. int width, int height)
  203. {
  204. int w;
  205. UINT8 *s1, *s2, *d;
  206. for(;height > 0; height--) {
  207. s1 = src;
  208. s2 = s1 + src_wrap;
  209. d = dst;
  210. for(w = width;w >= 4; w-=4) {
  211. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
  212. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
  213. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
  214. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
  215. s1 += 8;
  216. s2 += 8;
  217. d += 4;
  218. }
  219. for(;w > 0; w--) {
  220. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
  221. s1 += 2;
  222. s2 += 2;
  223. d++;
  224. }
  225. src += 2 * src_wrap;
  226. dst += dst_wrap;
  227. }
  228. }
  229. /* 1x1 -> 2x2 */
  230. static void grow22(UINT8 *dst, int dst_wrap,
  231. UINT8 *src, int src_wrap,
  232. int width, int height)
  233. {
  234. int w;
  235. UINT8 *s1, *d;
  236. for(;height > 0; height--) {
  237. s1 = src;
  238. d = dst;
  239. for(w = width;w >= 4; w-=4) {
  240. d[1] = d[0] = s1[0];
  241. d[3] = d[2] = s1[1];
  242. s1 += 2;
  243. d += 4;
  244. }
  245. for(;w > 0; w--) {
  246. d[0] = s1[0];
  247. s1 ++;
  248. d++;
  249. }
  250. if (height%2)
  251. src += src_wrap;
  252. dst += dst_wrap;
  253. }
  254. }
  255. static void img_copy(UINT8 *dst, int dst_wrap,
  256. UINT8 *src, int src_wrap,
  257. int width, int height)
  258. {
  259. for(;height > 0; height--) {
  260. memcpy(dst, src, width);
  261. dst += dst_wrap;
  262. src += src_wrap;
  263. }
  264. }
  265. #define SCALE_BITS 10
  266. #define C_Y (76309 >> (16 - SCALE_BITS))
  267. #define C_RV (117504 >> (16 - SCALE_BITS))
  268. #define C_BU (138453 >> (16 - SCALE_BITS))
  269. #define C_GU (13954 >> (16 - SCALE_BITS))
  270. #define C_GV (34903 >> (16 - SCALE_BITS))
  271. #define RGBOUT(r, g, b, y1)\
  272. {\
  273. y = (y1 - 16) * C_Y;\
  274. r = cm[(y + r_add) >> SCALE_BITS];\
  275. g = cm[(y + g_add) >> SCALE_BITS];\
  276. b = cm[(y + b_add) >> SCALE_BITS];\
  277. }
  278. /* XXX: no chroma interpolating is done */
  279. static void yuv420p_to_rgb24(AVPicture *dst, AVPicture *src,
  280. int width, int height)
  281. {
  282. UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
  283. int w, y, cb, cr, r_add, g_add, b_add, width2;
  284. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  285. d = dst->data[0];
  286. y1_ptr = src->data[0];
  287. cb_ptr = src->data[1];
  288. cr_ptr = src->data[2];
  289. width2 = width >> 1;
  290. for(;height > 0; height -= 2) {
  291. d1 = d;
  292. d2 = d + dst->linesize[0];
  293. y2_ptr = y1_ptr + src->linesize[0];
  294. for(w = width2; w > 0; w --) {
  295. cb = cb_ptr[0] - 128;
  296. cr = cr_ptr[0] - 128;
  297. r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
  298. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
  299. b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
  300. /* output 4 pixels */
  301. RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
  302. RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
  303. RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]);
  304. RGBOUT(d2[3], d2[4], d2[5], y2_ptr[1]);
  305. d1 += 6;
  306. d2 += 6;
  307. y1_ptr += 2;
  308. y2_ptr += 2;
  309. cb_ptr++;
  310. cr_ptr++;
  311. }
  312. d += 2 * dst->linesize[0];
  313. y1_ptr += 2 * src->linesize[0] - width;
  314. cb_ptr += src->linesize[1] - width2;
  315. cr_ptr += src->linesize[2] - width2;
  316. }
  317. }
  318. /* XXX: no chroma interpolating is done */
  319. static void yuv422p_to_rgb24(AVPicture *dst, AVPicture *src,
  320. int width, int height)
  321. {
  322. UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1;
  323. int w, y, cb, cr, r_add, g_add, b_add, width2;
  324. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  325. d = dst->data[0];
  326. y1_ptr = src->data[0];
  327. cb_ptr = src->data[1];
  328. cr_ptr = src->data[2];
  329. width2 = width >> 1;
  330. for(;height > 0; height --) {
  331. d1 = d;
  332. for(w = width2; w > 0; w --) {
  333. cb = cb_ptr[0] - 128;
  334. cr = cr_ptr[0] - 128;
  335. r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
  336. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
  337. b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
  338. /* output 2 pixels */
  339. RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
  340. RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
  341. d1 += 6;
  342. y1_ptr += 2;
  343. cb_ptr++;
  344. cr_ptr++;
  345. }
  346. d += dst->linesize[0];
  347. y1_ptr += src->linesize[0] - width;
  348. cb_ptr += src->linesize[1] - width2;
  349. cr_ptr += src->linesize[2] - width2;
  350. }
  351. }
  352. /* XXX: always use linesize. Return -1 if not supported */
  353. int img_convert(AVPicture *dst, int dst_pix_fmt,
  354. AVPicture *src, int pix_fmt,
  355. int width, int height)
  356. {
  357. int i;
  358. assert(pix_fmt != PIX_FMT_ANY && dst_pix_fmt != PIX_FMT_ANY);
  359. if (dst_pix_fmt == pix_fmt) {
  360. switch(pix_fmt) {
  361. case PIX_FMT_YUV420P:
  362. for(i=0;i<3;i++) {
  363. if (i == 1) {
  364. width >>= 1;
  365. height >>= 1;
  366. }
  367. img_copy(dst->data[i], dst->linesize[i],
  368. src->data[i], src->linesize[i],
  369. width, height);
  370. }
  371. break;
  372. default:
  373. return -1;
  374. }
  375. } else if (dst_pix_fmt == PIX_FMT_YUV420P) {
  376. switch(pix_fmt) {
  377. case PIX_FMT_YUV410P:
  378. img_copy(dst->data[0], dst->linesize[0],
  379. src->data[0], src->linesize[0],
  380. width, height);
  381. grow22(dst->data[1], dst->linesize[1],
  382. src->data[1], src->linesize[1],
  383. width/2, height/2);
  384. grow22(dst->data[2], dst->linesize[2],
  385. src->data[2], src->linesize[2],
  386. width/2, height/2);
  387. break;
  388. case PIX_FMT_YUV420P:
  389. for(i=0;i<3;i++) {
  390. img_copy(dst->data[i], dst->linesize[i],
  391. src->data[i], src->linesize[i],
  392. width, height);
  393. }
  394. break;
  395. case PIX_FMT_YUV422P:
  396. img_copy(dst->data[0], dst->linesize[0],
  397. src->data[0], src->linesize[0],
  398. width, height);
  399. width >>= 1;
  400. height >>= 1;
  401. for(i=1;i<3;i++) {
  402. shrink2(dst->data[i], dst->linesize[i],
  403. src->data[i], src->linesize[i],
  404. width, height);
  405. }
  406. break;
  407. case PIX_FMT_YUV444P:
  408. img_copy(dst->data[0], dst->linesize[0],
  409. src->data[0], src->linesize[0],
  410. width, height);
  411. width >>= 1;
  412. height >>= 1;
  413. for(i=1;i<3;i++) {
  414. shrink22(dst->data[i], dst->linesize[i],
  415. src->data[i], src->linesize[i],
  416. width, height);
  417. }
  418. break;
  419. case PIX_FMT_YUV422:
  420. yuv422_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
  421. src->data[0], width, height);
  422. break;
  423. case PIX_FMT_RGB24:
  424. rgb24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
  425. src->data[0], width, height);
  426. break;
  427. case PIX_FMT_BGR24:
  428. bgr24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
  429. src->data[0], width, height);
  430. break;
  431. default:
  432. return -1;
  433. }
  434. } else if (dst_pix_fmt == PIX_FMT_RGB24) {
  435. switch(pix_fmt) {
  436. case PIX_FMT_YUV420P:
  437. yuv420p_to_rgb24(dst, src, width, height);
  438. break;
  439. case PIX_FMT_YUV422P:
  440. yuv422p_to_rgb24(dst, src, width, height);
  441. break;
  442. default:
  443. return -1;
  444. }
  445. } else {
  446. return -1;
  447. }
  448. return 0;
  449. }
  450. /* filter parameters: [-1 4 2 4 -1] // 8 */
  451. static void deinterlace_line(UINT8 *dst, UINT8 *src, int src_wrap,
  452. int size)
  453. {
  454. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  455. int sum;
  456. UINT8 *s;
  457. for(;size > 0;size--) {
  458. s = src;
  459. sum = -s[0];
  460. s += src_wrap;
  461. sum += s[0] << 2;
  462. s += src_wrap;
  463. sum += s[0] << 1;
  464. s += src_wrap;
  465. sum += s[0] << 2;
  466. s += src_wrap;
  467. sum += -s[0];
  468. dst[0] = cm[(sum + 4) >> 3];
  469. dst++;
  470. src++;
  471. }
  472. }
  473. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  474. top field is copied as is, but the bottom field is deinterlaced
  475. against the top field. */
  476. static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
  477. UINT8 *src1, int src_wrap,
  478. int width, int height)
  479. {
  480. UINT8 *src, *ptr;
  481. int y, y1, i;
  482. UINT8 *buf;
  483. buf = (UINT8*)av_malloc(5 * width);
  484. src = src1;
  485. for(y=0;y<height;y+=2) {
  486. /* copy top field line */
  487. memcpy(dst, src, width);
  488. dst += dst_wrap;
  489. src += (1 - 2) * src_wrap;
  490. y1 = y - 2;
  491. if (y1 >= 0 && (y1 + 4) < height) {
  492. /* fast case : no edges */
  493. deinterlace_line(dst, src, src_wrap, width);
  494. } else {
  495. /* in order to use the same function, we use an intermediate buffer */
  496. ptr = buf;
  497. for(i=0;i<5;i++) {
  498. if (y1 < 0)
  499. memcpy(ptr, src1, width);
  500. else if (y1 >= height)
  501. memcpy(ptr, src1 + (height - 1) * src_wrap, width);
  502. else
  503. memcpy(ptr, src1 + y1 * src_wrap, width);
  504. y1++;
  505. ptr += width;
  506. }
  507. deinterlace_line(dst, buf, width, width);
  508. }
  509. dst += dst_wrap;
  510. src += (2 + 1) * src_wrap;
  511. }
  512. av_free(buf);
  513. }
  514. /* deinterlace, return -1 if format not handled */
  515. int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
  516. int pix_fmt, int width, int height)
  517. {
  518. int i;
  519. if (pix_fmt != PIX_FMT_YUV420P &&
  520. pix_fmt != PIX_FMT_YUV422P &&
  521. pix_fmt != PIX_FMT_YUV444P)
  522. return -1;
  523. if ((width & 1) != 0 || (height & 3) != 0)
  524. return -1;
  525. for(i=0;i<3;i++) {
  526. if (i == 1) {
  527. switch(pix_fmt) {
  528. case PIX_FMT_YUV420P:
  529. width >>= 1;
  530. height >>= 1;
  531. break;
  532. case PIX_FMT_YUV422P:
  533. width >>= 1;
  534. break;
  535. default:
  536. break;
  537. }
  538. }
  539. deinterlace_bottom_field(dst->data[i], dst->linesize[i],
  540. src->data[i], src->linesize[i],
  541. width, height);
  542. }
  543. return 0;
  544. }
  545. #undef FIX