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.

1516 lines
52KB

  1. /*
  2. * Misc image convertion routines
  3. * Copyright (c) 2001, 2002, 2003 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. #ifdef HAVE_MMX
  25. #include "i386/mmx.h"
  26. #endif
  27. typedef struct PixFmtInfo {
  28. const char *name;
  29. UINT8 nb_components; /* number of components in AVPicture array */
  30. UINT8 is_yuv : 1; /* true if YUV instead of RGB color space */
  31. UINT8 is_packed : 1; /* true if multiple components in same word */
  32. UINT8 is_paletted : 1; /* true if paletted */
  33. UINT8 is_alpha : 1; /* true if alpha can be specified */
  34. UINT8 is_gray : 1; /* true if gray or monochrome format */
  35. UINT8 x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
  36. UINT8 y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
  37. } PixFmtInfo;
  38. /* this table gives more information about formats */
  39. static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  40. /* YUV formats */
  41. [PIX_FMT_YUV420P] = {
  42. .name = "yuv420p",
  43. .nb_components = 3, .is_yuv = 1,
  44. .x_chroma_shift = 1, .y_chroma_shift = 1,
  45. },
  46. [PIX_FMT_YUV422P] = {
  47. .name = "yuv422p",
  48. .nb_components = 3, .is_yuv = 1,
  49. .x_chroma_shift = 1, .y_chroma_shift = 0,
  50. },
  51. [PIX_FMT_YUV444P] = {
  52. .name = "yuv444p",
  53. .nb_components = 3, .is_yuv = 1,
  54. .x_chroma_shift = 0, .y_chroma_shift = 0,
  55. },
  56. [PIX_FMT_YUV422] = {
  57. .name = "yuv422",
  58. .nb_components = 1, .is_yuv = 1, .is_packed = 1,
  59. .x_chroma_shift = 1, .y_chroma_shift = 0,
  60. },
  61. [PIX_FMT_YUV410P] = {
  62. .name = "yuv410p",
  63. .nb_components = 3, .is_yuv = 1,
  64. .x_chroma_shift = 2, .y_chroma_shift = 2,
  65. },
  66. [PIX_FMT_YUV411P] = {
  67. .name = "yuv411p",
  68. .nb_components = 3, .is_yuv = 1,
  69. .x_chroma_shift = 2, .y_chroma_shift = 0,
  70. },
  71. /* RGB formats */
  72. [PIX_FMT_RGB24] = {
  73. .name = "rgb24",
  74. .nb_components = 1, .is_packed = 1,
  75. },
  76. [PIX_FMT_BGR24] = {
  77. .name = "bgr24",
  78. .nb_components = 1, .is_packed = 1,
  79. },
  80. [PIX_FMT_RGBA32] = {
  81. .name = "rgba32",
  82. .nb_components = 1, .is_packed = 1, .is_alpha = 1,
  83. },
  84. [PIX_FMT_RGB565] = {
  85. .name = "rgb565",
  86. .nb_components = 1, .is_packed = 1,
  87. },
  88. [PIX_FMT_RGB555] = {
  89. .name = "rgb555",
  90. .nb_components = 1, .is_packed = 1, .is_alpha = 1,
  91. },
  92. /* gray / mono formats */
  93. [PIX_FMT_GRAY8] = {
  94. .name = "gray",
  95. .nb_components = 1, .is_gray = 1,
  96. },
  97. [PIX_FMT_MONOWHITE] = {
  98. .name = "monow",
  99. .nb_components = 1, .is_packed = 1, .is_gray = 1,
  100. },
  101. [PIX_FMT_MONOBLACK] = {
  102. .name = "monob",
  103. .nb_components = 1, .is_packed = 1, .is_gray = 1,
  104. },
  105. };
  106. void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
  107. {
  108. if (pix_fmt_info[pix_fmt].is_yuv) {
  109. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  110. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  111. } else {
  112. *h_shift=0;
  113. *v_shift=0;
  114. }
  115. }
  116. const char *avcodec_get_pix_fmt_name(int pix_fmt)
  117. {
  118. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  119. return "???";
  120. else
  121. return pix_fmt_info[pix_fmt].name;
  122. }
  123. /* Picture field are filled with 'ptr' addresses. Also return size */
  124. int avpicture_fill(AVPicture *picture, UINT8 *ptr,
  125. int pix_fmt, int width, int height)
  126. {
  127. int size, w2, h2, size2;
  128. PixFmtInfo *pinfo;
  129. pinfo = &pix_fmt_info[pix_fmt];
  130. size = width * height;
  131. switch(pix_fmt) {
  132. case PIX_FMT_YUV420P:
  133. case PIX_FMT_YUV422P:
  134. case PIX_FMT_YUV444P:
  135. case PIX_FMT_YUV410P:
  136. case PIX_FMT_YUV411P:
  137. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  138. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  139. size2 = w2 * h2;
  140. picture->data[0] = ptr;
  141. picture->data[1] = picture->data[0] + size;
  142. picture->data[2] = picture->data[1] + size2;
  143. picture->linesize[0] = width;
  144. picture->linesize[1] = w2;
  145. picture->linesize[2] = w2;
  146. return size + 2 * size2;
  147. case PIX_FMT_RGB24:
  148. case PIX_FMT_BGR24:
  149. picture->data[0] = ptr;
  150. picture->data[1] = NULL;
  151. picture->data[2] = NULL;
  152. picture->linesize[0] = width * 3;
  153. return size * 3;
  154. case PIX_FMT_RGBA32:
  155. picture->data[0] = ptr;
  156. picture->data[1] = NULL;
  157. picture->data[2] = NULL;
  158. picture->linesize[0] = width * 4;
  159. return size * 4;
  160. case PIX_FMT_RGB555:
  161. case PIX_FMT_RGB565:
  162. case PIX_FMT_YUV422:
  163. picture->data[0] = ptr;
  164. picture->data[1] = NULL;
  165. picture->data[2] = NULL;
  166. picture->linesize[0] = width * 2;
  167. return size * 2;
  168. case PIX_FMT_GRAY8:
  169. picture->data[0] = ptr;
  170. picture->data[1] = NULL;
  171. picture->data[2] = NULL;
  172. picture->linesize[0] = width;
  173. return size;
  174. case PIX_FMT_MONOWHITE:
  175. case PIX_FMT_MONOBLACK:
  176. picture->data[0] = ptr;
  177. picture->data[1] = NULL;
  178. picture->data[2] = NULL;
  179. picture->linesize[0] = (width + 7) >> 3;
  180. return picture->linesize[0] * height;
  181. default:
  182. picture->data[0] = NULL;
  183. picture->data[1] = NULL;
  184. picture->data[2] = NULL;
  185. return -1;
  186. }
  187. }
  188. int avpicture_get_size(int pix_fmt, int width, int height)
  189. {
  190. AVPicture dummy_pict;
  191. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  192. }
  193. /* XXX: totally non optimized */
  194. static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
  195. int width, int height)
  196. {
  197. UINT8 *lum, *cb, *cr;
  198. int x, y;
  199. const UINT8 *p;
  200. lum = dst->data[0];
  201. cb = dst->data[1];
  202. cr = dst->data[2];
  203. p = src->data[0];
  204. for(y=0;y<height;y+=2) {
  205. for(x=0;x<width;x+=2) {
  206. lum[0] = p[0];
  207. cb[0] = p[1];
  208. lum[1] = p[2];
  209. cr[0] = p[3];
  210. p += 4;
  211. lum += 2;
  212. cb++;
  213. cr++;
  214. }
  215. for(x=0;x<width;x+=2) {
  216. lum[0] = p[0];
  217. lum[1] = p[2];
  218. p += 4;
  219. lum += 2;
  220. }
  221. }
  222. }
  223. #define SCALEBITS 8
  224. #define ONE_HALF (1 << (SCALEBITS - 1))
  225. #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
  226. /* XXX: use generic filter ? */
  227. /* 1x2 -> 1x1 */
  228. static void shrink2(UINT8 *dst, int dst_wrap,
  229. UINT8 *src, int src_wrap,
  230. int width, int height)
  231. {
  232. int w;
  233. UINT8 *s1, *s2, *d;
  234. for(;height > 0; height--) {
  235. s1 = src;
  236. s2 = s1 + src_wrap;
  237. d = dst;
  238. for(w = width;w >= 4; w-=4) {
  239. d[0] = (s1[0] + s2[0]) >> 1;
  240. d[1] = (s1[1] + s2[1]) >> 1;
  241. d[2] = (s1[2] + s2[2]) >> 1;
  242. d[3] = (s1[3] + s2[3]) >> 1;
  243. s1 += 4;
  244. s2 += 4;
  245. d += 4;
  246. }
  247. for(;w > 0; w--) {
  248. d[0] = (s1[0] + s2[0]) >> 1;
  249. s1++;
  250. s2++;
  251. d++;
  252. }
  253. src += 2 * src_wrap;
  254. dst += dst_wrap;
  255. }
  256. }
  257. /* 2x2 -> 1x1 */
  258. static void shrink22(UINT8 *dst, int dst_wrap,
  259. UINT8 *src, int src_wrap,
  260. int width, int height)
  261. {
  262. int w;
  263. UINT8 *s1, *s2, *d;
  264. for(;height > 0; height--) {
  265. s1 = src;
  266. s2 = s1 + src_wrap;
  267. d = dst;
  268. for(w = width;w >= 4; w-=4) {
  269. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
  270. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
  271. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
  272. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
  273. s1 += 8;
  274. s2 += 8;
  275. d += 4;
  276. }
  277. for(;w > 0; w--) {
  278. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
  279. s1 += 2;
  280. s2 += 2;
  281. d++;
  282. }
  283. src += 2 * src_wrap;
  284. dst += dst_wrap;
  285. }
  286. }
  287. /* 1x1 -> 2x2 */
  288. static void grow22(UINT8 *dst, int dst_wrap,
  289. UINT8 *src, int src_wrap,
  290. int width, int height)
  291. {
  292. int w;
  293. UINT8 *s1, *d;
  294. for(;height > 0; height--) {
  295. s1 = src;
  296. d = dst;
  297. for(w = width;w >= 4; w-=4) {
  298. d[1] = d[0] = s1[0];
  299. d[3] = d[2] = s1[1];
  300. s1 += 2;
  301. d += 4;
  302. }
  303. for(;w > 0; w--) {
  304. d[0] = s1[0];
  305. s1 ++;
  306. d++;
  307. }
  308. if (height%2)
  309. src += src_wrap;
  310. dst += dst_wrap;
  311. }
  312. }
  313. /* 1x2 -> 2x1 */
  314. static void conv411(UINT8 *dst, int dst_wrap,
  315. UINT8 *src, int src_wrap,
  316. int width, int height)
  317. {
  318. int w, c;
  319. UINT8 *s1, *s2, *d;
  320. for(;height > 0; height--) {
  321. s1 = src;
  322. s2 = src + src_wrap;
  323. d = dst;
  324. for(w = width;w > 0; w--) {
  325. c = (s1[0] + s2[0]) >> 1;
  326. d[0] = c;
  327. d[1] = c;
  328. s1++;
  329. s2++;
  330. d += 2;
  331. }
  332. src += src_wrap * 2;
  333. dst += dst_wrap;
  334. }
  335. }
  336. static void img_copy(UINT8 *dst, int dst_wrap,
  337. UINT8 *src, int src_wrap,
  338. int width, int height)
  339. {
  340. for(;height > 0; height--) {
  341. memcpy(dst, src, width);
  342. dst += dst_wrap;
  343. src += src_wrap;
  344. }
  345. }
  346. #define SCALE_BITS 10
  347. #define C_Y (76309 >> (16 - SCALE_BITS))
  348. #define C_RV (117504 >> (16 - SCALE_BITS))
  349. #define C_BU (138453 >> (16 - SCALE_BITS))
  350. #define C_GU (13954 >> (16 - SCALE_BITS))
  351. #define C_GV (34903 >> (16 - SCALE_BITS))
  352. #define YUV_TO_RGB2(r, g, b, y1)\
  353. {\
  354. y = (y1 - 16) * C_Y;\
  355. r = cm[(y + r_add) >> SCALE_BITS];\
  356. g = cm[(y + g_add) >> SCALE_BITS];\
  357. b = cm[(y + b_add) >> SCALE_BITS];\
  358. }
  359. /* XXX: no chroma interpolating is done */
  360. #define RGB_FUNCTIONS(rgb_name) \
  361. \
  362. static void yuv420p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
  363. int width, int height) \
  364. { \
  365. UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; \
  366. int w, y, cb, cr, r_add, g_add, b_add, width2; \
  367. UINT8 *cm = cropTbl + MAX_NEG_CROP; \
  368. unsigned int r, g, b; \
  369. \
  370. d = dst->data[0]; \
  371. y1_ptr = src->data[0]; \
  372. cb_ptr = src->data[1]; \
  373. cr_ptr = src->data[2]; \
  374. width2 = (width + 1) >> 1; \
  375. for(;height >= 2; height -= 2) { \
  376. d1 = d; \
  377. d2 = d + dst->linesize[0]; \
  378. y2_ptr = y1_ptr + src->linesize[0]; \
  379. for(w = width; w >= 2; w -= 2) { \
  380. cb = cb_ptr[0] - 128; \
  381. cr = cr_ptr[0] - 128; \
  382. r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
  383. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
  384. b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
  385. \
  386. /* output 4 pixels */ \
  387. YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
  388. RGB_OUT(d1, r, g, b); \
  389. \
  390. YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
  391. RGB_OUT(d1 + BPP, r, g, b); \
  392. \
  393. YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
  394. RGB_OUT(d2, r, g, b); \
  395. \
  396. YUV_TO_RGB2(r, g, b, y2_ptr[1]); \
  397. RGB_OUT(d2 + BPP, r, g, b); \
  398. \
  399. d1 += 2 * BPP; \
  400. d2 += 2 * BPP; \
  401. \
  402. y1_ptr += 2; \
  403. y2_ptr += 2; \
  404. cb_ptr++; \
  405. cr_ptr++; \
  406. } \
  407. /* handle odd width */ \
  408. if (w) { \
  409. cb = cb_ptr[0] - 128; \
  410. cr = cr_ptr[0] - 128; \
  411. r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
  412. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
  413. b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
  414. \
  415. YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
  416. RGB_OUT(d1, r, g, b); \
  417. \
  418. YUV_TO_RGB2(r, g, b, y2_ptr[0]); \
  419. RGB_OUT(d2, r, g, b); \
  420. d1 += BPP; \
  421. d2 += BPP; \
  422. y1_ptr++; \
  423. y2_ptr++; \
  424. cb_ptr++; \
  425. cr_ptr++; \
  426. } \
  427. d += 2 * dst->linesize[0]; \
  428. y1_ptr += 2 * src->linesize[0] - width; \
  429. cb_ptr += src->linesize[1] - width2; \
  430. cr_ptr += src->linesize[2] - width2; \
  431. } \
  432. /* handle odd height */ \
  433. if (height) { \
  434. d1 = d; \
  435. for(w = width; w >= 2; w -= 2) { \
  436. cb = cb_ptr[0] - 128; \
  437. cr = cr_ptr[0] - 128; \
  438. r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
  439. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
  440. b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
  441. \
  442. /* output 2 pixels */ \
  443. YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
  444. RGB_OUT(d1, r, g, b); \
  445. \
  446. YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
  447. RGB_OUT(d1 + BPP, r, g, b); \
  448. \
  449. d1 += 2 * BPP; \
  450. \
  451. y1_ptr += 2; \
  452. cb_ptr++; \
  453. cr_ptr++; \
  454. } \
  455. /* handle width */ \
  456. if (w) { \
  457. cb = cb_ptr[0] - 128; \
  458. cr = cr_ptr[0] - 128; \
  459. r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
  460. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
  461. b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
  462. \
  463. /* output 2 pixels */ \
  464. YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
  465. RGB_OUT(d1, r, g, b); \
  466. d1 += BPP; \
  467. \
  468. y1_ptr++; \
  469. cb_ptr++; \
  470. cr_ptr++; \
  471. } \
  472. } \
  473. } \
  474. \
  475. /* XXX: no chroma interpolating is done */ \
  476. static void yuv422p_to_ ## rgb_name (AVPicture *dst, AVPicture *src, \
  477. int width, int height) \
  478. { \
  479. UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1; \
  480. int w, y, cb, cr, r_add, g_add, b_add, width2; \
  481. UINT8 *cm = cropTbl + MAX_NEG_CROP; \
  482. unsigned int r, g, b; \
  483. \
  484. d = dst->data[0]; \
  485. y1_ptr = src->data[0]; \
  486. cb_ptr = src->data[1]; \
  487. cr_ptr = src->data[2]; \
  488. width2 = (width + 1) >> 1; \
  489. for(;height > 0; height --) { \
  490. d1 = d; \
  491. for(w = width; w >= 2; w -= 2) { \
  492. cb = cb_ptr[0] - 128; \
  493. cr = cr_ptr[0] - 128; \
  494. r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
  495. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
  496. b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
  497. \
  498. /* output 2 pixels */ \
  499. YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
  500. RGB_OUT(d1, r, g, b); \
  501. \
  502. YUV_TO_RGB2(r, g, b, y1_ptr[1]); \
  503. RGB_OUT(d1 + BPP, r, g, b); \
  504. \
  505. d1 += 2 * BPP; \
  506. \
  507. y1_ptr += 2; \
  508. cb_ptr++; \
  509. cr_ptr++; \
  510. } \
  511. /* handle width */ \
  512. if (w) { \
  513. cb = cb_ptr[0] - 128; \
  514. cr = cr_ptr[0] - 128; \
  515. r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); \
  516. g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); \
  517. b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); \
  518. \
  519. /* output 2 pixels */ \
  520. YUV_TO_RGB2(r, g, b, y1_ptr[0]); \
  521. RGB_OUT(d1, r, g, b); \
  522. d1 += BPP; \
  523. \
  524. y1_ptr++; \
  525. cb_ptr++; \
  526. cr_ptr++; \
  527. } \
  528. d += dst->linesize[0]; \
  529. y1_ptr += src->linesize[0] - width; \
  530. cb_ptr += src->linesize[1] - width2; \
  531. cr_ptr += src->linesize[2] - width2; \
  532. } \
  533. } \
  534. \
  535. static void rgb_name ## _to_yuv420p(AVPicture *dst, AVPicture *src, \
  536. int width, int height) \
  537. { \
  538. int wrap, wrap3, x, y; \
  539. int r, g, b, r1, g1, b1; \
  540. UINT8 *lum, *cb, *cr; \
  541. const UINT8 *p; \
  542. \
  543. lum = dst->data[0]; \
  544. cb = dst->data[1]; \
  545. cr = dst->data[2]; \
  546. \
  547. wrap = width; \
  548. wrap3 = width * BPP; \
  549. p = src->data[0]; \
  550. for(y=0;y<height;y+=2) { \
  551. for(x=0;x<width;x+=2) { \
  552. RGB_IN(r, g, b, p); \
  553. r1 = r; \
  554. g1 = g; \
  555. b1 = b; \
  556. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
  557. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
  558. RGB_IN(r, g, b, p + BPP); \
  559. r1 += r; \
  560. g1 += g; \
  561. b1 += b; \
  562. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
  563. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
  564. p += wrap3; \
  565. lum += wrap; \
  566. \
  567. RGB_IN(r, g, b, p); \
  568. r1 += r; \
  569. g1 += g; \
  570. b1 += b; \
  571. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
  572. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
  573. \
  574. RGB_IN(r, g, b, p + BPP); \
  575. r1 += r; \
  576. g1 += g; \
  577. b1 += b; \
  578. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + \
  579. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
  580. \
  581. cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
  582. FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> \
  583. (SCALEBITS + 2)) + 128; \
  584. cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
  585. FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> \
  586. (SCALEBITS + 2)) + 128; \
  587. \
  588. cb++; \
  589. cr++; \
  590. p += -wrap3 + 2 * BPP; \
  591. lum += -wrap + 2; \
  592. } \
  593. p += wrap3; \
  594. lum += wrap; \
  595. } \
  596. } \
  597. \
  598. static void rgb_name ## _to_gray(AVPicture *dst, AVPicture *src, \
  599. int width, int height) \
  600. { \
  601. const unsigned char *p; \
  602. unsigned char *q; \
  603. int r, g, b, dst_wrap, src_wrap; \
  604. int x, y; \
  605. \
  606. p = src->data[0]; \
  607. src_wrap = src->linesize[0] - BPP * width; \
  608. \
  609. q = dst->data[0]; \
  610. dst_wrap = dst->linesize[0] - width; \
  611. \
  612. for(y=0;y<height;y++) { \
  613. for(x=0;x<width;x++) { \
  614. RGB_IN(r, g, b, p); \
  615. q[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
  616. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
  617. q++; \
  618. p += BPP; \
  619. } \
  620. p += src_wrap; \
  621. q += dst_wrap; \
  622. } \
  623. } \
  624. \
  625. static void gray_to_ ## rgb_name(AVPicture *dst, AVPicture *src, \
  626. int width, int height) \
  627. { \
  628. const unsigned char *p; \
  629. unsigned char *q; \
  630. int r, dst_wrap, src_wrap; \
  631. int x, y; \
  632. \
  633. p = src->data[0]; \
  634. src_wrap = src->linesize[0] - width; \
  635. \
  636. q = dst->data[0]; \
  637. dst_wrap = dst->linesize[0] - BPP * width; \
  638. \
  639. for(y=0;y<height;y++) { \
  640. for(x=0;x<width;x++) { \
  641. r = p[0]; \
  642. RGB_OUT(q, r, r, r); \
  643. q += BPP; \
  644. p ++; \
  645. } \
  646. p += src_wrap; \
  647. q += dst_wrap; \
  648. } \
  649. }
  650. /* copy bit n to bits 0 ... n - 1 */
  651. static inline unsigned int bitcopy_n(unsigned int a, int n)
  652. {
  653. int mask;
  654. mask = (1 << n) - 1;
  655. return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
  656. }
  657. /* rgb555 handling */
  658. #define RGB_IN(r, g, b, s)\
  659. {\
  660. unsigned int v = ((UINT16 *)(s))[0];\
  661. r = bitcopy_n(v >> (10 - 3), 3);\
  662. g = bitcopy_n(v >> (5 - 3), 3);\
  663. b = bitcopy_n(v << 3, 3);\
  664. }
  665. #define RGB_OUT(d, r, g, b)\
  666. {\
  667. ((UINT16 *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;\
  668. }
  669. #define BPP 2
  670. RGB_FUNCTIONS(rgb555)
  671. #undef RGB_IN
  672. #undef RGB_OUT
  673. #undef BPP
  674. /* rgb565 handling */
  675. #define RGB_IN(r, g, b, s)\
  676. {\
  677. unsigned int v = ((UINT16 *)(s))[0];\
  678. r = bitcopy_n(v >> (11 - 3), 3);\
  679. g = bitcopy_n(v >> (5 - 2), 2);\
  680. b = bitcopy_n(v << 3, 3);\
  681. }
  682. #define RGB_OUT(d, r, g, b)\
  683. {\
  684. ((UINT16 *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
  685. }
  686. #define BPP 2
  687. RGB_FUNCTIONS(rgb565)
  688. #undef RGB_IN
  689. #undef RGB_OUT
  690. #undef BPP
  691. /* bgr24 handling */
  692. #define RGB_IN(r, g, b, s)\
  693. {\
  694. b = (s)[0];\
  695. g = (s)[1];\
  696. r = (s)[2];\
  697. }
  698. #define RGB_OUT(d, r, g, b)\
  699. {\
  700. (d)[0] = b;\
  701. (d)[1] = g;\
  702. (d)[2] = r;\
  703. }
  704. #define BPP 3
  705. RGB_FUNCTIONS(bgr24)
  706. #undef RGB_IN
  707. #undef RGB_OUT
  708. #undef BPP
  709. /* rgb24 handling */
  710. #define RGB_IN(r, g, b, s)\
  711. {\
  712. r = (s)[0];\
  713. g = (s)[1];\
  714. b = (s)[2];\
  715. }
  716. #define RGB_OUT(d, r, g, b)\
  717. {\
  718. (d)[0] = r;\
  719. (d)[1] = g;\
  720. (d)[2] = b;\
  721. }
  722. #define BPP 3
  723. RGB_FUNCTIONS(rgb24)
  724. #undef RGB_IN
  725. #undef RGB_OUT
  726. #undef BPP
  727. /* rgba32 handling */
  728. #define RGB_IN(r, g, b, s)\
  729. {\
  730. unsigned int v = ((UINT32 *)(s))[0];\
  731. r = (v >> 16) & 0xff;\
  732. g = (v >> 8) & 0xff;\
  733. b = v & 0xff;\
  734. }
  735. #define RGB_OUT(d, r, g, b)\
  736. {\
  737. ((UINT32 *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;\
  738. }
  739. #define BPP 4
  740. RGB_FUNCTIONS(rgba32)
  741. #undef RGB_IN
  742. #undef RGB_OUT
  743. #undef BPP
  744. static void rgb24_to_rgb565(AVPicture *dst, AVPicture *src,
  745. int width, int height)
  746. {
  747. const unsigned char *p;
  748. unsigned char *q;
  749. int r, g, b, dst_wrap, src_wrap;
  750. int x, y;
  751. p = src->data[0];
  752. src_wrap = src->linesize[0] - 3 * width;
  753. q = dst->data[0];
  754. dst_wrap = dst->linesize[0] - 2 * width;
  755. for(y=0;y<height;y++) {
  756. for(x=0;x<width;x++) {
  757. r = p[0];
  758. g = p[1];
  759. b = p[2];
  760. ((unsigned short *)q)[0] =
  761. ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  762. q += 2;
  763. p += 3;
  764. }
  765. p += src_wrap;
  766. q += dst_wrap;
  767. }
  768. }
  769. /* NOTE: we also add a dummy alpha bit */
  770. static void rgb24_to_rgb555(AVPicture *dst, AVPicture *src,
  771. int width, int height)
  772. {
  773. const unsigned char *p;
  774. unsigned char *q;
  775. int r, g, b, dst_wrap, src_wrap;
  776. int x, y;
  777. p = src->data[0];
  778. src_wrap = src->linesize[0] - 3 * width;
  779. q = dst->data[0];
  780. dst_wrap = dst->linesize[0] - 2 * width;
  781. for(y=0;y<height;y++) {
  782. for(x=0;x<width;x++) {
  783. r = p[0];
  784. g = p[1];
  785. b = p[2];
  786. ((unsigned short *)q)[0] =
  787. ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;
  788. q += 2;
  789. p += 3;
  790. }
  791. p += src_wrap;
  792. q += dst_wrap;
  793. }
  794. }
  795. static void mono_to_gray(AVPicture *dst, AVPicture *src,
  796. int width, int height, int xor_mask)
  797. {
  798. const unsigned char *p;
  799. unsigned char *q;
  800. int v, dst_wrap, src_wrap;
  801. int y, w;
  802. p = src->data[0];
  803. src_wrap = src->linesize[0] - ((width + 7) >> 3);
  804. q = dst->data[0];
  805. dst_wrap = dst->linesize[0] - width;
  806. for(y=0;y<height;y++) {
  807. w = width;
  808. while (w >= 8) {
  809. v = *p++ ^ xor_mask;
  810. q[0] = -(v >> 7);
  811. q[1] = -((v >> 6) & 1);
  812. q[2] = -((v >> 5) & 1);
  813. q[3] = -((v >> 4) & 1);
  814. q[4] = -((v >> 3) & 1);
  815. q[5] = -((v >> 2) & 1);
  816. q[6] = -((v >> 1) & 1);
  817. q[7] = -((v >> 0) & 1);
  818. w -= 8;
  819. q += 8;
  820. }
  821. if (w > 0) {
  822. v = *p++ ^ xor_mask;
  823. do {
  824. q[0] = -((v >> 7) & 1);
  825. q++;
  826. v <<= 1;
  827. } while (--w);
  828. }
  829. p += src_wrap;
  830. q += dst_wrap;
  831. }
  832. }
  833. static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
  834. int width, int height)
  835. {
  836. mono_to_gray(dst, src, width, height, 0xff);
  837. }
  838. static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
  839. int width, int height)
  840. {
  841. mono_to_gray(dst, src, width, height, 0x00);
  842. }
  843. static void gray_to_mono(AVPicture *dst, AVPicture *src,
  844. int width, int height, int xor_mask)
  845. {
  846. int n;
  847. const UINT8 *s;
  848. UINT8 *d;
  849. int j, b, v, n1, src_wrap, dst_wrap, y;
  850. s = src->data[0];
  851. src_wrap = src->linesize[0] - width;
  852. d = dst->data[0];
  853. dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
  854. printf("%d %d\n", width, height);
  855. for(y=0;y<height;y++) {
  856. n = width;
  857. while (n >= 8) {
  858. v = 0;
  859. for(j=0;j<8;j++) {
  860. b = s[0];
  861. s++;
  862. v = (v << 1) | (b >> 7);
  863. }
  864. d[0] = v ^ xor_mask;
  865. d++;
  866. n -= 8;
  867. }
  868. if (n > 0) {
  869. n1 = n;
  870. v = 0;
  871. while (n > 0) {
  872. b = s[0];
  873. s++;
  874. v = (v << 1) | (b >> 7);
  875. n--;
  876. }
  877. d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
  878. d++;
  879. }
  880. s += src_wrap;
  881. d += dst_wrap;
  882. }
  883. }
  884. static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
  885. int width, int height)
  886. {
  887. gray_to_mono(dst, src, width, height, 0xff);
  888. }
  889. static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
  890. int width, int height)
  891. {
  892. gray_to_mono(dst, src, width, height, 0x00);
  893. }
  894. typedef struct ConvertEntry {
  895. void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
  896. } ConvertEntry;
  897. /* add each new convertion function in this table */
  898. /* constraints;
  899. - all non YUV modes must convert at least to and from PIX_FMT_RGB24
  900. */
  901. static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
  902. [PIX_FMT_YUV420P] = {
  903. [PIX_FMT_RGB555] = {
  904. .convert = yuv420p_to_rgb555
  905. },
  906. [PIX_FMT_RGB565] = {
  907. .convert = yuv420p_to_rgb565
  908. },
  909. [PIX_FMT_BGR24] = {
  910. .convert = yuv420p_to_bgr24
  911. },
  912. [PIX_FMT_RGB24] = {
  913. .convert = yuv420p_to_rgb24
  914. },
  915. [PIX_FMT_RGBA32] = {
  916. .convert = yuv420p_to_rgba32
  917. },
  918. },
  919. [PIX_FMT_YUV422P] = {
  920. [PIX_FMT_RGB555] = {
  921. .convert = yuv422p_to_rgb555
  922. },
  923. [PIX_FMT_RGB565] = {
  924. .convert = yuv422p_to_rgb565
  925. },
  926. [PIX_FMT_BGR24] = {
  927. .convert = yuv422p_to_bgr24
  928. },
  929. [PIX_FMT_RGB24] = {
  930. .convert = yuv422p_to_rgb24
  931. },
  932. [PIX_FMT_RGBA32] = {
  933. .convert = yuv422p_to_rgba32
  934. },
  935. },
  936. [PIX_FMT_YUV422] = {
  937. [PIX_FMT_YUV420P] = {
  938. .convert = yuv422_to_yuv420p,
  939. },
  940. },
  941. [PIX_FMT_RGB24] = {
  942. [PIX_FMT_YUV420P] = {
  943. .convert = rgb24_to_yuv420p
  944. },
  945. [PIX_FMT_RGB565] = {
  946. .convert = rgb24_to_rgb565
  947. },
  948. [PIX_FMT_RGB555] = {
  949. .convert = rgb24_to_rgb555
  950. },
  951. [PIX_FMT_GRAY8] = {
  952. .convert = rgb24_to_gray
  953. },
  954. },
  955. [PIX_FMT_RGBA32] = {
  956. [PIX_FMT_YUV420P] = {
  957. .convert = rgba32_to_yuv420p
  958. },
  959. [PIX_FMT_GRAY8] = {
  960. .convert = rgba32_to_gray
  961. },
  962. },
  963. [PIX_FMT_BGR24] = {
  964. [PIX_FMT_YUV420P] = {
  965. .convert = bgr24_to_yuv420p
  966. },
  967. [PIX_FMT_GRAY8] = {
  968. .convert = bgr24_to_gray
  969. },
  970. },
  971. [PIX_FMT_RGB555] = {
  972. [PIX_FMT_YUV420P] = {
  973. .convert = rgb555_to_yuv420p
  974. },
  975. [PIX_FMT_GRAY8] = {
  976. .convert = rgb555_to_gray
  977. },
  978. },
  979. [PIX_FMT_RGB565] = {
  980. [PIX_FMT_YUV420P] = {
  981. .convert = rgb565_to_yuv420p
  982. },
  983. [PIX_FMT_GRAY8] = {
  984. .convert = rgb565_to_gray
  985. },
  986. },
  987. [PIX_FMT_GRAY8] = {
  988. [PIX_FMT_RGB555] = {
  989. .convert = gray_to_rgb555
  990. },
  991. [PIX_FMT_RGB565] = {
  992. .convert = gray_to_rgb565
  993. },
  994. [PIX_FMT_RGB24] = {
  995. .convert = gray_to_rgb24
  996. },
  997. [PIX_FMT_BGR24] = {
  998. .convert = gray_to_bgr24
  999. },
  1000. [PIX_FMT_RGBA32] = {
  1001. .convert = gray_to_rgba32
  1002. },
  1003. [PIX_FMT_MONOWHITE] = {
  1004. .convert = gray_to_monowhite
  1005. },
  1006. [PIX_FMT_MONOBLACK] = {
  1007. .convert = gray_to_monoblack
  1008. },
  1009. },
  1010. [PIX_FMT_MONOWHITE] = {
  1011. [PIX_FMT_GRAY8] = {
  1012. .convert = monowhite_to_gray
  1013. },
  1014. },
  1015. [PIX_FMT_MONOBLACK] = {
  1016. [PIX_FMT_GRAY8] = {
  1017. .convert = monoblack_to_gray
  1018. },
  1019. },
  1020. };
  1021. static int avpicture_alloc(AVPicture *picture,
  1022. int pix_fmt, int width, int height)
  1023. {
  1024. int size;
  1025. void *ptr;
  1026. size = avpicture_get_size(pix_fmt, width, height);
  1027. if (size < 0)
  1028. goto fail;
  1029. ptr = av_malloc(size);
  1030. if (!ptr)
  1031. goto fail;
  1032. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1033. return 0;
  1034. fail:
  1035. memset(picture, 0, sizeof(AVPicture));
  1036. return -1;
  1037. }
  1038. static void avpicture_free(AVPicture *picture)
  1039. {
  1040. av_free(picture->data[0]);
  1041. }
  1042. /* XXX: always use linesize. Return -1 if not supported */
  1043. int img_convert(AVPicture *dst, int dst_pix_fmt,
  1044. AVPicture *src, int src_pix_fmt,
  1045. int src_width, int src_height)
  1046. {
  1047. int i, ret, dst_width, dst_height, int_pix_fmt;
  1048. PixFmtInfo *src_pix, *dst_pix;
  1049. ConvertEntry *ce;
  1050. AVPicture tmp1, *tmp = &tmp1;
  1051. if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
  1052. dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
  1053. return -1;
  1054. if (src_width <= 0 || src_height <= 0)
  1055. return 0;
  1056. dst_width = src_width;
  1057. dst_height = src_height;
  1058. dst_pix = &pix_fmt_info[dst_pix_fmt];
  1059. src_pix = &pix_fmt_info[src_pix_fmt];
  1060. if (src_pix_fmt == dst_pix_fmt) {
  1061. /* XXX: incorrect */
  1062. /* same format: just copy */
  1063. for(i = 0; i < dst_pix->nb_components; i++) {
  1064. int w, h;
  1065. w = dst_width;
  1066. h = dst_height;
  1067. if (dst_pix->is_yuv && (i == 1 || i == 2)) {
  1068. w >>= dst_pix->x_chroma_shift;
  1069. h >>= dst_pix->y_chroma_shift;
  1070. }
  1071. img_copy(dst->data[i], dst->linesize[i],
  1072. src->data[i], src->linesize[i],
  1073. w, h);
  1074. }
  1075. return 0;
  1076. }
  1077. ce = &convert_table[src_pix_fmt][dst_pix_fmt];
  1078. if (ce->convert) {
  1079. /* specific convertion routine */
  1080. ce->convert(dst, src, dst_width, dst_height);
  1081. return 0;
  1082. }
  1083. /* gray to YUV */
  1084. if (dst_pix->is_yuv && src_pix_fmt == PIX_FMT_GRAY8) {
  1085. int w, h, y;
  1086. uint8_t *d;
  1087. img_copy(dst->data[0], dst->linesize[0],
  1088. src->data[0], src->linesize[0],
  1089. dst_width, dst_height);
  1090. /* fill U and V with 128 */
  1091. w = dst_width;
  1092. h = dst_height;
  1093. w >>= dst_pix->x_chroma_shift;
  1094. h >>= dst_pix->y_chroma_shift;
  1095. for(i = 1; i <= 2; i++) {
  1096. d = dst->data[i];
  1097. for(y = 0; y< h; y++) {
  1098. memset(d, 128, w);
  1099. d += dst->linesize[i];
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. /* YUV to gray */
  1105. if (src_pix->is_yuv && dst_pix_fmt == PIX_FMT_GRAY8) {
  1106. img_copy(dst->data[0], dst->linesize[0],
  1107. src->data[0], src->linesize[0],
  1108. dst_width, dst_height);
  1109. return 0;
  1110. }
  1111. /* YUV to YUV */
  1112. if (dst_pix->is_yuv && src_pix->is_yuv) {
  1113. int x_shift, y_shift, w, h;
  1114. void (*resize_func)(UINT8 *dst, int dst_wrap,
  1115. UINT8 *src, int src_wrap,
  1116. int width, int height);
  1117. /* compute chroma size of the smallest dimensions */
  1118. w = dst_width;
  1119. h = dst_height;
  1120. if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
  1121. w >>= dst_pix->x_chroma_shift;
  1122. else
  1123. w >>= src_pix->x_chroma_shift;
  1124. if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
  1125. h >>= dst_pix->y_chroma_shift;
  1126. else
  1127. h >>= src_pix->y_chroma_shift;
  1128. x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
  1129. y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
  1130. if (x_shift == 0 && y_shift == 0) {
  1131. resize_func = img_copy; /* should never happen */
  1132. } else if (x_shift == 0 && y_shift == 1) {
  1133. resize_func = shrink2;
  1134. } else if (x_shift == 1 && y_shift == 1) {
  1135. resize_func = shrink22;
  1136. } else if (x_shift == -1 && y_shift == -1) {
  1137. resize_func = grow22;
  1138. } else if (x_shift == -1 && y_shift == 1) {
  1139. resize_func = conv411;
  1140. } else {
  1141. /* currently not handled */
  1142. return -1;
  1143. }
  1144. img_copy(dst->data[0], dst->linesize[0],
  1145. src->data[0], src->linesize[0],
  1146. dst_width, dst_height);
  1147. for(i = 1;i <= 2; i++)
  1148. resize_func(dst->data[i], dst->linesize[i],
  1149. src->data[i], src->linesize[i],
  1150. w, h);
  1151. return 0;
  1152. }
  1153. /* try to use an intermediate format */
  1154. if (src_pix_fmt == PIX_FMT_MONOWHITE ||
  1155. src_pix_fmt == PIX_FMT_MONOBLACK ||
  1156. dst_pix_fmt == PIX_FMT_MONOWHITE ||
  1157. dst_pix_fmt == PIX_FMT_MONOBLACK) {
  1158. int_pix_fmt = PIX_FMT_GRAY8;
  1159. } else {
  1160. int_pix_fmt = PIX_FMT_RGB24;
  1161. }
  1162. if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1163. return -1;
  1164. ret = -1;
  1165. if (img_convert(tmp, int_pix_fmt,
  1166. src, src_pix_fmt, src_width, src_height) < 0)
  1167. goto fail1;
  1168. if (img_convert(dst, dst_pix_fmt,
  1169. tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1170. goto fail1;
  1171. ret = 0;
  1172. fail1:
  1173. avpicture_free(tmp);
  1174. return ret;
  1175. }
  1176. #ifdef HAVE_MMX
  1177. #define DEINT_INPLACE_LINE_LUM \
  1178. movd_m2r(lum_m4[0],mm0);\
  1179. movd_m2r(lum_m3[0],mm1);\
  1180. movd_m2r(lum_m2[0],mm2);\
  1181. movd_m2r(lum_m1[0],mm3);\
  1182. movd_m2r(lum[0],mm4);\
  1183. punpcklbw_r2r(mm7,mm0);\
  1184. movd_r2m(mm2,lum_m4[0]);\
  1185. punpcklbw_r2r(mm7,mm1);\
  1186. punpcklbw_r2r(mm7,mm2);\
  1187. punpcklbw_r2r(mm7,mm3);\
  1188. punpcklbw_r2r(mm7,mm4);\
  1189. paddw_r2r(mm3,mm1);\
  1190. psllw_i2r(1,mm2);\
  1191. paddw_r2r(mm4,mm0);\
  1192. psllw_i2r(2,mm1);\
  1193. paddw_r2r(mm6,mm2);\
  1194. paddw_r2r(mm2,mm1);\
  1195. psubusw_r2r(mm0,mm1);\
  1196. psrlw_i2r(3,mm1);\
  1197. packuswb_r2r(mm7,mm1);\
  1198. movd_r2m(mm1,lum_m2[0]);
  1199. #define DEINT_LINE_LUM \
  1200. movd_m2r(lum_m4[0],mm0);\
  1201. movd_m2r(lum_m3[0],mm1);\
  1202. movd_m2r(lum_m2[0],mm2);\
  1203. movd_m2r(lum_m1[0],mm3);\
  1204. movd_m2r(lum[0],mm4);\
  1205. punpcklbw_r2r(mm7,mm0);\
  1206. punpcklbw_r2r(mm7,mm1);\
  1207. punpcklbw_r2r(mm7,mm2);\
  1208. punpcklbw_r2r(mm7,mm3);\
  1209. punpcklbw_r2r(mm7,mm4);\
  1210. paddw_r2r(mm3,mm1);\
  1211. psllw_i2r(1,mm2);\
  1212. paddw_r2r(mm4,mm0);\
  1213. psllw_i2r(2,mm1);\
  1214. paddw_r2r(mm6,mm2);\
  1215. paddw_r2r(mm2,mm1);\
  1216. psubusw_r2r(mm0,mm1);\
  1217. psrlw_i2r(3,mm1);\
  1218. packuswb_r2r(mm7,mm1);\
  1219. movd_r2m(mm1,dst[0]);
  1220. #endif
  1221. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1222. static void deinterlace_line(UINT8 *dst, UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
  1223. int size)
  1224. {
  1225. #ifndef HAVE_MMX
  1226. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  1227. int sum;
  1228. for(;size > 0;size--) {
  1229. sum = -lum_m4[0];
  1230. sum += lum_m3[0] << 2;
  1231. sum += lum_m2[0] << 1;
  1232. sum += lum_m1[0] << 2;
  1233. sum += -lum[0];
  1234. dst[0] = cm[(sum + 4) >> 3];
  1235. lum_m4++;
  1236. lum_m3++;
  1237. lum_m2++;
  1238. lum_m1++;
  1239. lum++;
  1240. dst++;
  1241. }
  1242. #else
  1243. {
  1244. mmx_t rounder;
  1245. rounder.uw[0]=4;
  1246. rounder.uw[1]=4;
  1247. rounder.uw[2]=4;
  1248. rounder.uw[3]=4;
  1249. pxor_r2r(mm7,mm7);
  1250. movq_m2r(rounder,mm6);
  1251. }
  1252. for (;size > 3; size-=4) {
  1253. DEINT_LINE_LUM
  1254. lum_m4+=4;
  1255. lum_m3+=4;
  1256. lum_m2+=4;
  1257. lum_m1+=4;
  1258. lum+=4;
  1259. dst+=4;
  1260. }
  1261. #endif
  1262. }
  1263. static void deinterlace_line_inplace(UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
  1264. int size)
  1265. {
  1266. #ifndef HAVE_MMX
  1267. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  1268. int sum;
  1269. for(;size > 0;size--) {
  1270. sum = -lum_m4[0];
  1271. sum += lum_m3[0] << 2;
  1272. sum += lum_m2[0] << 1;
  1273. lum_m4[0]=lum_m2[0];
  1274. sum += lum_m1[0] << 2;
  1275. sum += -lum[0];
  1276. lum_m2[0] = cm[(sum + 4) >> 3];
  1277. lum_m4++;
  1278. lum_m3++;
  1279. lum_m2++;
  1280. lum_m1++;
  1281. lum++;
  1282. }
  1283. #else
  1284. {
  1285. mmx_t rounder;
  1286. rounder.uw[0]=4;
  1287. rounder.uw[1]=4;
  1288. rounder.uw[2]=4;
  1289. rounder.uw[3]=4;
  1290. pxor_r2r(mm7,mm7);
  1291. movq_m2r(rounder,mm6);
  1292. }
  1293. for (;size > 3; size-=4) {
  1294. DEINT_INPLACE_LINE_LUM
  1295. lum_m4+=4;
  1296. lum_m3+=4;
  1297. lum_m2+=4;
  1298. lum_m1+=4;
  1299. lum+=4;
  1300. }
  1301. #endif
  1302. }
  1303. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1304. top field is copied as is, but the bottom field is deinterlaced
  1305. against the top field. */
  1306. static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
  1307. UINT8 *src1, int src_wrap,
  1308. int width, int height)
  1309. {
  1310. UINT8 *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1311. int y;
  1312. src_m2 = src1;
  1313. src_m1 = src1;
  1314. src_0=&src_m1[src_wrap];
  1315. src_p1=&src_0[src_wrap];
  1316. src_p2=&src_p1[src_wrap];
  1317. for(y=0;y<(height-2);y+=2) {
  1318. memcpy(dst,src_m1,width);
  1319. dst += dst_wrap;
  1320. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1321. src_m2 = src_0;
  1322. src_m1 = src_p1;
  1323. src_0 = src_p2;
  1324. src_p1 += 2*src_wrap;
  1325. src_p2 += 2*src_wrap;
  1326. dst += dst_wrap;
  1327. }
  1328. memcpy(dst,src_m1,width);
  1329. dst += dst_wrap;
  1330. /* do last line */
  1331. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1332. }
  1333. static void deinterlace_bottom_field_inplace(UINT8 *src1, int src_wrap,
  1334. int width, int height)
  1335. {
  1336. UINT8 *src_m1, *src_0, *src_p1, *src_p2;
  1337. int y;
  1338. UINT8 *buf;
  1339. buf = (UINT8*)av_malloc(width);
  1340. src_m1 = src1;
  1341. memcpy(buf,src_m1,width);
  1342. src_0=&src_m1[src_wrap];
  1343. src_p1=&src_0[src_wrap];
  1344. src_p2=&src_p1[src_wrap];
  1345. for(y=0;y<(height-2);y+=2) {
  1346. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1347. src_m1 = src_p1;
  1348. src_0 = src_p2;
  1349. src_p1 += 2*src_wrap;
  1350. src_p2 += 2*src_wrap;
  1351. }
  1352. /* do last line */
  1353. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1354. av_free(buf);
  1355. }
  1356. /* deinterlace - if not supported return -1 */
  1357. int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
  1358. int pix_fmt, int width, int height)
  1359. {
  1360. int i;
  1361. if (pix_fmt != PIX_FMT_YUV420P &&
  1362. pix_fmt != PIX_FMT_YUV422P &&
  1363. pix_fmt != PIX_FMT_YUV444P)
  1364. return -1;
  1365. if ((width & 3) != 0 || (height & 3) != 0)
  1366. return -1;
  1367. for(i=0;i<3;i++) {
  1368. if (i == 1) {
  1369. switch(pix_fmt) {
  1370. case PIX_FMT_YUV420P:
  1371. width >>= 1;
  1372. height >>= 1;
  1373. break;
  1374. case PIX_FMT_YUV422P:
  1375. width >>= 1;
  1376. break;
  1377. default:
  1378. break;
  1379. }
  1380. }
  1381. if (src == dst) {
  1382. deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
  1383. width, height);
  1384. } else {
  1385. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1386. src->data[i], src->linesize[i],
  1387. width, height);
  1388. }
  1389. }
  1390. #ifdef HAVE_MMX
  1391. emms();
  1392. #endif
  1393. return 0;
  1394. }
  1395. #undef FIX