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.

551 lines
16KB

  1. /*
  2. * Misc image convertion routines
  3. * Copyright (c) 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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. static void img_copy(UINT8 *dst, int dst_wrap,
  230. UINT8 *src, int src_wrap,
  231. int width, int height)
  232. {
  233. for(;height > 0; height--) {
  234. memcpy(dst, src, width);
  235. dst += dst_wrap;
  236. src += src_wrap;
  237. }
  238. }
  239. #define SCALE_BITS 10
  240. #define C_Y (76309 >> (16 - SCALE_BITS))
  241. #define C_RV (117504 >> (16 - SCALE_BITS))
  242. #define C_BU (138453 >> (16 - SCALE_BITS))
  243. #define C_GU (13954 >> (16 - SCALE_BITS))
  244. #define C_GV (34903 >> (16 - SCALE_BITS))
  245. #define RGBOUT(r, g, b, y1)\
  246. {\
  247. y = (y1 - 16) * C_Y;\
  248. r = cm[(y + r_add) >> SCALE_BITS];\
  249. g = cm[(y + g_add) >> SCALE_BITS];\
  250. b = cm[(y + b_add) >> SCALE_BITS];\
  251. }
  252. /* XXX: no chroma interpolating is done */
  253. static void yuv420p_to_rgb24(AVPicture *dst, AVPicture *src,
  254. int width, int height)
  255. {
  256. UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
  257. int w, y, cb, cr, r_add, g_add, b_add, width2;
  258. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  259. d = dst->data[0];
  260. y1_ptr = src->data[0];
  261. cb_ptr = src->data[1];
  262. cr_ptr = src->data[2];
  263. width2 = width >> 1;
  264. for(;height > 0; height -= 2) {
  265. d1 = d;
  266. d2 = d + dst->linesize[0];
  267. y2_ptr = y1_ptr + src->linesize[0];
  268. for(w = width2; w > 0; w --) {
  269. cb = cb_ptr[0] - 128;
  270. cr = cr_ptr[0] - 128;
  271. r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
  272. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
  273. b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
  274. /* output 4 pixels */
  275. RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
  276. RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
  277. RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]);
  278. RGBOUT(d2[3], d2[4], d2[5], y2_ptr[1]);
  279. d1 += 6;
  280. d2 += 6;
  281. y1_ptr += 2;
  282. y2_ptr += 2;
  283. cb_ptr++;
  284. cr_ptr++;
  285. }
  286. d += 2 * dst->linesize[0];
  287. y1_ptr += 2 * src->linesize[0] - width;
  288. cb_ptr += src->linesize[1] - width2;
  289. cr_ptr += src->linesize[2] - width2;
  290. }
  291. }
  292. /* XXX: no chroma interpolating is done */
  293. static void yuv422p_to_rgb24(AVPicture *dst, AVPicture *src,
  294. int width, int height)
  295. {
  296. UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1;
  297. int w, y, cb, cr, r_add, g_add, b_add, width2;
  298. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  299. d = dst->data[0];
  300. y1_ptr = src->data[0];
  301. cb_ptr = src->data[1];
  302. cr_ptr = src->data[2];
  303. width2 = width >> 1;
  304. for(;height > 0; height --) {
  305. d1 = d;
  306. for(w = width2; w > 0; w --) {
  307. cb = cb_ptr[0] - 128;
  308. cr = cr_ptr[0] - 128;
  309. r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
  310. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
  311. b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
  312. /* output 2 pixels */
  313. RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
  314. RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
  315. d1 += 6;
  316. y1_ptr += 2;
  317. cb_ptr++;
  318. cr_ptr++;
  319. }
  320. d += dst->linesize[0];
  321. y1_ptr += src->linesize[0] - width;
  322. cb_ptr += src->linesize[1] - width2;
  323. cr_ptr += src->linesize[2] - width2;
  324. }
  325. }
  326. /* XXX: always use linesize. Return -1 if not supported */
  327. int img_convert(AVPicture *dst, int dst_pix_fmt,
  328. AVPicture *src, int pix_fmt,
  329. int width, int height)
  330. {
  331. int i;
  332. assert(pix_fmt != PIX_FMT_ANY && dst_pix_fmt != PIX_FMT_ANY);
  333. if (dst_pix_fmt == pix_fmt) {
  334. switch(pix_fmt) {
  335. case PIX_FMT_YUV420P:
  336. for(i=0;i<3;i++) {
  337. if (i == 1) {
  338. width >>= 1;
  339. height >>= 1;
  340. }
  341. img_copy(dst->data[i], dst->linesize[i],
  342. src->data[i], src->linesize[i],
  343. width, height);
  344. }
  345. break;
  346. default:
  347. return -1;
  348. }
  349. } else if (dst_pix_fmt == PIX_FMT_YUV420P) {
  350. switch(pix_fmt) {
  351. case PIX_FMT_YUV420P:
  352. for(i=0;i<3;i++) {
  353. img_copy(dst->data[i], dst->linesize[i],
  354. src->data[i], src->linesize[i],
  355. width, height);
  356. }
  357. break;
  358. case PIX_FMT_YUV422P:
  359. img_copy(dst->data[0], dst->linesize[0],
  360. src->data[0], src->linesize[0],
  361. width, height);
  362. width >>= 1;
  363. height >>= 1;
  364. for(i=1;i<3;i++) {
  365. shrink2(dst->data[i], dst->linesize[i],
  366. src->data[i], src->linesize[i],
  367. width, height);
  368. }
  369. break;
  370. case PIX_FMT_YUV444P:
  371. img_copy(dst->data[0], dst->linesize[0],
  372. src->data[0], src->linesize[0],
  373. width, height);
  374. width >>= 1;
  375. height >>= 1;
  376. for(i=1;i<3;i++) {
  377. shrink22(dst->data[i], dst->linesize[i],
  378. src->data[i], src->linesize[i],
  379. width, height);
  380. }
  381. break;
  382. case PIX_FMT_YUV422:
  383. yuv422_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
  384. src->data[0], width, height);
  385. break;
  386. case PIX_FMT_RGB24:
  387. rgb24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
  388. src->data[0], width, height);
  389. break;
  390. case PIX_FMT_BGR24:
  391. bgr24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
  392. src->data[0], width, height);
  393. break;
  394. default:
  395. return -1;
  396. }
  397. } else if (dst_pix_fmt == PIX_FMT_RGB24) {
  398. switch(pix_fmt) {
  399. case PIX_FMT_YUV420P:
  400. yuv420p_to_rgb24(dst, src, width, height);
  401. break;
  402. case PIX_FMT_YUV422P:
  403. yuv422p_to_rgb24(dst, src, width, height);
  404. break;
  405. default:
  406. return -1;
  407. }
  408. } else {
  409. return -1;
  410. }
  411. return 0;
  412. }
  413. /* filter parameters: [-1 4 2 4 -1] // 8 */
  414. static void deinterlace_line(UINT8 *dst, UINT8 *src, int src_wrap,
  415. int size)
  416. {
  417. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  418. int sum;
  419. UINT8 *s;
  420. for(;size > 0;size--) {
  421. s = src;
  422. sum = -s[0];
  423. s += src_wrap;
  424. sum += s[0] << 2;
  425. s += src_wrap;
  426. sum += s[0] << 1;
  427. s += src_wrap;
  428. sum += s[0] << 2;
  429. s += src_wrap;
  430. sum += -s[0];
  431. dst[0] = cm[(sum + 4) >> 3];
  432. dst++;
  433. src++;
  434. }
  435. }
  436. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  437. top field is copied as is, but the bottom field is deinterlaced
  438. against the top field. */
  439. static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
  440. UINT8 *src1, int src_wrap,
  441. int width, int height)
  442. {
  443. UINT8 *src, *ptr;
  444. int y, y1, i;
  445. UINT8 *buf;
  446. buf = (UINT8*)av_malloc(5 * width);
  447. src = src1;
  448. for(y=0;y<height;y+=2) {
  449. /* copy top field line */
  450. memcpy(dst, src, width);
  451. dst += dst_wrap;
  452. src += (1 - 2) * src_wrap;
  453. y1 = y - 2;
  454. if (y1 >= 0 && (y1 + 4) < height) {
  455. /* fast case : no edges */
  456. deinterlace_line(dst, src, src_wrap, width);
  457. } else {
  458. /* in order to use the same function, we use an intermediate buffer */
  459. ptr = buf;
  460. for(i=0;i<5;i++) {
  461. if (y1 < 0)
  462. memcpy(ptr, src1, width);
  463. else if (y1 >= height)
  464. memcpy(ptr, src1 + (height - 1) * src_wrap, width);
  465. else
  466. memcpy(ptr, src1 + y1 * src_wrap, width);
  467. y1++;
  468. ptr += width;
  469. }
  470. deinterlace_line(dst, buf, width, width);
  471. }
  472. dst += dst_wrap;
  473. src += (2 + 1) * src_wrap;
  474. }
  475. av_free(buf);
  476. }
  477. /* deinterlace, return -1 if format not handled */
  478. int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
  479. int pix_fmt, int width, int height)
  480. {
  481. int i;
  482. if (pix_fmt != PIX_FMT_YUV420P &&
  483. pix_fmt != PIX_FMT_YUV422P &&
  484. pix_fmt != PIX_FMT_YUV444P)
  485. return -1;
  486. if ((width & 1) != 0 || (height & 3) != 0)
  487. return -1;
  488. for(i=0;i<3;i++) {
  489. if (i == 1) {
  490. switch(pix_fmt) {
  491. case PIX_FMT_YUV420P:
  492. width >>= 1;
  493. height >>= 1;
  494. break;
  495. case PIX_FMT_YUV422P:
  496. width >>= 1;
  497. break;
  498. default:
  499. break;
  500. }
  501. }
  502. deinterlace_bottom_field(dst->data[i], dst->linesize[i],
  503. src->data[i], src->linesize[i],
  504. width, height);
  505. }
  506. return 0;
  507. }