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.

1518 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 = dst->linesize[0]; \
  548. wrap3 = src->linesize[0]; \
  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 + (wrap3 - width * BPP); \
  594. lum += wrap + (wrap - width); \
  595. cb += dst->linesize[1] - width / 2; \
  596. cr += dst->linesize[2] - width / 2; \
  597. } \
  598. } \
  599. \
  600. static void rgb_name ## _to_gray(AVPicture *dst, AVPicture *src, \
  601. int width, int height) \
  602. { \
  603. const unsigned char *p; \
  604. unsigned char *q; \
  605. int r, g, b, dst_wrap, src_wrap; \
  606. int x, y; \
  607. \
  608. p = src->data[0]; \
  609. src_wrap = src->linesize[0] - BPP * width; \
  610. \
  611. q = dst->data[0]; \
  612. dst_wrap = dst->linesize[0] - width; \
  613. \
  614. for(y=0;y<height;y++) { \
  615. for(x=0;x<width;x++) { \
  616. RGB_IN(r, g, b, p); \
  617. q[0] = (FIX(0.29900) * r + FIX(0.58700) * g + \
  618. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; \
  619. q++; \
  620. p += BPP; \
  621. } \
  622. p += src_wrap; \
  623. q += dst_wrap; \
  624. } \
  625. } \
  626. \
  627. static void gray_to_ ## rgb_name(AVPicture *dst, AVPicture *src, \
  628. int width, int height) \
  629. { \
  630. const unsigned char *p; \
  631. unsigned char *q; \
  632. int r, dst_wrap, src_wrap; \
  633. int x, y; \
  634. \
  635. p = src->data[0]; \
  636. src_wrap = src->linesize[0] - width; \
  637. \
  638. q = dst->data[0]; \
  639. dst_wrap = dst->linesize[0] - BPP * width; \
  640. \
  641. for(y=0;y<height;y++) { \
  642. for(x=0;x<width;x++) { \
  643. r = p[0]; \
  644. RGB_OUT(q, r, r, r); \
  645. q += BPP; \
  646. p ++; \
  647. } \
  648. p += src_wrap; \
  649. q += dst_wrap; \
  650. } \
  651. }
  652. /* copy bit n to bits 0 ... n - 1 */
  653. static inline unsigned int bitcopy_n(unsigned int a, int n)
  654. {
  655. int mask;
  656. mask = (1 << n) - 1;
  657. return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
  658. }
  659. /* rgb555 handling */
  660. #define RGB_IN(r, g, b, s)\
  661. {\
  662. unsigned int v = ((UINT16 *)(s))[0];\
  663. r = bitcopy_n(v >> (10 - 3), 3);\
  664. g = bitcopy_n(v >> (5 - 3), 3);\
  665. b = bitcopy_n(v << 3, 3);\
  666. }
  667. #define RGB_OUT(d, r, g, b)\
  668. {\
  669. ((UINT16 *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;\
  670. }
  671. #define BPP 2
  672. RGB_FUNCTIONS(rgb555)
  673. #undef RGB_IN
  674. #undef RGB_OUT
  675. #undef BPP
  676. /* rgb565 handling */
  677. #define RGB_IN(r, g, b, s)\
  678. {\
  679. unsigned int v = ((UINT16 *)(s))[0];\
  680. r = bitcopy_n(v >> (11 - 3), 3);\
  681. g = bitcopy_n(v >> (5 - 2), 2);\
  682. b = bitcopy_n(v << 3, 3);\
  683. }
  684. #define RGB_OUT(d, r, g, b)\
  685. {\
  686. ((UINT16 *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
  687. }
  688. #define BPP 2
  689. RGB_FUNCTIONS(rgb565)
  690. #undef RGB_IN
  691. #undef RGB_OUT
  692. #undef BPP
  693. /* bgr24 handling */
  694. #define RGB_IN(r, g, b, s)\
  695. {\
  696. b = (s)[0];\
  697. g = (s)[1];\
  698. r = (s)[2];\
  699. }
  700. #define RGB_OUT(d, r, g, b)\
  701. {\
  702. (d)[0] = b;\
  703. (d)[1] = g;\
  704. (d)[2] = r;\
  705. }
  706. #define BPP 3
  707. RGB_FUNCTIONS(bgr24)
  708. #undef RGB_IN
  709. #undef RGB_OUT
  710. #undef BPP
  711. /* rgb24 handling */
  712. #define RGB_IN(r, g, b, s)\
  713. {\
  714. r = (s)[0];\
  715. g = (s)[1];\
  716. b = (s)[2];\
  717. }
  718. #define RGB_OUT(d, r, g, b)\
  719. {\
  720. (d)[0] = r;\
  721. (d)[1] = g;\
  722. (d)[2] = b;\
  723. }
  724. #define BPP 3
  725. RGB_FUNCTIONS(rgb24)
  726. #undef RGB_IN
  727. #undef RGB_OUT
  728. #undef BPP
  729. /* rgba32 handling */
  730. #define RGB_IN(r, g, b, s)\
  731. {\
  732. unsigned int v = ((UINT32 *)(s))[0];\
  733. r = (v >> 16) & 0xff;\
  734. g = (v >> 8) & 0xff;\
  735. b = v & 0xff;\
  736. }
  737. #define RGB_OUT(d, r, g, b)\
  738. {\
  739. ((UINT32 *)(d))[0] = (0xff << 24) | (r << 16) | (g << 8) | b;\
  740. }
  741. #define BPP 4
  742. RGB_FUNCTIONS(rgba32)
  743. #undef RGB_IN
  744. #undef RGB_OUT
  745. #undef BPP
  746. static void rgb24_to_rgb565(AVPicture *dst, AVPicture *src,
  747. int width, int height)
  748. {
  749. const unsigned char *p;
  750. unsigned char *q;
  751. int r, g, b, dst_wrap, src_wrap;
  752. int x, y;
  753. p = src->data[0];
  754. src_wrap = src->linesize[0] - 3 * width;
  755. q = dst->data[0];
  756. dst_wrap = dst->linesize[0] - 2 * width;
  757. for(y=0;y<height;y++) {
  758. for(x=0;x<width;x++) {
  759. r = p[0];
  760. g = p[1];
  761. b = p[2];
  762. ((unsigned short *)q)[0] =
  763. ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  764. q += 2;
  765. p += 3;
  766. }
  767. p += src_wrap;
  768. q += dst_wrap;
  769. }
  770. }
  771. /* NOTE: we also add a dummy alpha bit */
  772. static void rgb24_to_rgb555(AVPicture *dst, AVPicture *src,
  773. int width, int height)
  774. {
  775. const unsigned char *p;
  776. unsigned char *q;
  777. int r, g, b, dst_wrap, src_wrap;
  778. int x, y;
  779. p = src->data[0];
  780. src_wrap = src->linesize[0] - 3 * width;
  781. q = dst->data[0];
  782. dst_wrap = dst->linesize[0] - 2 * width;
  783. for(y=0;y<height;y++) {
  784. for(x=0;x<width;x++) {
  785. r = p[0];
  786. g = p[1];
  787. b = p[2];
  788. ((unsigned short *)q)[0] =
  789. ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | 0x8000;
  790. q += 2;
  791. p += 3;
  792. }
  793. p += src_wrap;
  794. q += dst_wrap;
  795. }
  796. }
  797. static void mono_to_gray(AVPicture *dst, AVPicture *src,
  798. int width, int height, int xor_mask)
  799. {
  800. const unsigned char *p;
  801. unsigned char *q;
  802. int v, dst_wrap, src_wrap;
  803. int y, w;
  804. p = src->data[0];
  805. src_wrap = src->linesize[0] - ((width + 7) >> 3);
  806. q = dst->data[0];
  807. dst_wrap = dst->linesize[0] - width;
  808. for(y=0;y<height;y++) {
  809. w = width;
  810. while (w >= 8) {
  811. v = *p++ ^ xor_mask;
  812. q[0] = -(v >> 7);
  813. q[1] = -((v >> 6) & 1);
  814. q[2] = -((v >> 5) & 1);
  815. q[3] = -((v >> 4) & 1);
  816. q[4] = -((v >> 3) & 1);
  817. q[5] = -((v >> 2) & 1);
  818. q[6] = -((v >> 1) & 1);
  819. q[7] = -((v >> 0) & 1);
  820. w -= 8;
  821. q += 8;
  822. }
  823. if (w > 0) {
  824. v = *p++ ^ xor_mask;
  825. do {
  826. q[0] = -((v >> 7) & 1);
  827. q++;
  828. v <<= 1;
  829. } while (--w);
  830. }
  831. p += src_wrap;
  832. q += dst_wrap;
  833. }
  834. }
  835. static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
  836. int width, int height)
  837. {
  838. mono_to_gray(dst, src, width, height, 0xff);
  839. }
  840. static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
  841. int width, int height)
  842. {
  843. mono_to_gray(dst, src, width, height, 0x00);
  844. }
  845. static void gray_to_mono(AVPicture *dst, AVPicture *src,
  846. int width, int height, int xor_mask)
  847. {
  848. int n;
  849. const UINT8 *s;
  850. UINT8 *d;
  851. int j, b, v, n1, src_wrap, dst_wrap, y;
  852. s = src->data[0];
  853. src_wrap = src->linesize[0] - width;
  854. d = dst->data[0];
  855. dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
  856. printf("%d %d\n", width, height);
  857. for(y=0;y<height;y++) {
  858. n = width;
  859. while (n >= 8) {
  860. v = 0;
  861. for(j=0;j<8;j++) {
  862. b = s[0];
  863. s++;
  864. v = (v << 1) | (b >> 7);
  865. }
  866. d[0] = v ^ xor_mask;
  867. d++;
  868. n -= 8;
  869. }
  870. if (n > 0) {
  871. n1 = n;
  872. v = 0;
  873. while (n > 0) {
  874. b = s[0];
  875. s++;
  876. v = (v << 1) | (b >> 7);
  877. n--;
  878. }
  879. d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
  880. d++;
  881. }
  882. s += src_wrap;
  883. d += dst_wrap;
  884. }
  885. }
  886. static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
  887. int width, int height)
  888. {
  889. gray_to_mono(dst, src, width, height, 0xff);
  890. }
  891. static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
  892. int width, int height)
  893. {
  894. gray_to_mono(dst, src, width, height, 0x00);
  895. }
  896. typedef struct ConvertEntry {
  897. void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
  898. } ConvertEntry;
  899. /* add each new convertion function in this table */
  900. /* constraints;
  901. - all non YUV modes must convert at least to and from PIX_FMT_RGB24
  902. */
  903. static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
  904. [PIX_FMT_YUV420P] = {
  905. [PIX_FMT_RGB555] = {
  906. .convert = yuv420p_to_rgb555
  907. },
  908. [PIX_FMT_RGB565] = {
  909. .convert = yuv420p_to_rgb565
  910. },
  911. [PIX_FMT_BGR24] = {
  912. .convert = yuv420p_to_bgr24
  913. },
  914. [PIX_FMT_RGB24] = {
  915. .convert = yuv420p_to_rgb24
  916. },
  917. [PIX_FMT_RGBA32] = {
  918. .convert = yuv420p_to_rgba32
  919. },
  920. },
  921. [PIX_FMT_YUV422P] = {
  922. [PIX_FMT_RGB555] = {
  923. .convert = yuv422p_to_rgb555
  924. },
  925. [PIX_FMT_RGB565] = {
  926. .convert = yuv422p_to_rgb565
  927. },
  928. [PIX_FMT_BGR24] = {
  929. .convert = yuv422p_to_bgr24
  930. },
  931. [PIX_FMT_RGB24] = {
  932. .convert = yuv422p_to_rgb24
  933. },
  934. [PIX_FMT_RGBA32] = {
  935. .convert = yuv422p_to_rgba32
  936. },
  937. },
  938. [PIX_FMT_YUV422] = {
  939. [PIX_FMT_YUV420P] = {
  940. .convert = yuv422_to_yuv420p,
  941. },
  942. },
  943. [PIX_FMT_RGB24] = {
  944. [PIX_FMT_YUV420P] = {
  945. .convert = rgb24_to_yuv420p
  946. },
  947. [PIX_FMT_RGB565] = {
  948. .convert = rgb24_to_rgb565
  949. },
  950. [PIX_FMT_RGB555] = {
  951. .convert = rgb24_to_rgb555
  952. },
  953. [PIX_FMT_GRAY8] = {
  954. .convert = rgb24_to_gray
  955. },
  956. },
  957. [PIX_FMT_RGBA32] = {
  958. [PIX_FMT_YUV420P] = {
  959. .convert = rgba32_to_yuv420p
  960. },
  961. [PIX_FMT_GRAY8] = {
  962. .convert = rgba32_to_gray
  963. },
  964. },
  965. [PIX_FMT_BGR24] = {
  966. [PIX_FMT_YUV420P] = {
  967. .convert = bgr24_to_yuv420p
  968. },
  969. [PIX_FMT_GRAY8] = {
  970. .convert = bgr24_to_gray
  971. },
  972. },
  973. [PIX_FMT_RGB555] = {
  974. [PIX_FMT_YUV420P] = {
  975. .convert = rgb555_to_yuv420p
  976. },
  977. [PIX_FMT_GRAY8] = {
  978. .convert = rgb555_to_gray
  979. },
  980. },
  981. [PIX_FMT_RGB565] = {
  982. [PIX_FMT_YUV420P] = {
  983. .convert = rgb565_to_yuv420p
  984. },
  985. [PIX_FMT_GRAY8] = {
  986. .convert = rgb565_to_gray
  987. },
  988. },
  989. [PIX_FMT_GRAY8] = {
  990. [PIX_FMT_RGB555] = {
  991. .convert = gray_to_rgb555
  992. },
  993. [PIX_FMT_RGB565] = {
  994. .convert = gray_to_rgb565
  995. },
  996. [PIX_FMT_RGB24] = {
  997. .convert = gray_to_rgb24
  998. },
  999. [PIX_FMT_BGR24] = {
  1000. .convert = gray_to_bgr24
  1001. },
  1002. [PIX_FMT_RGBA32] = {
  1003. .convert = gray_to_rgba32
  1004. },
  1005. [PIX_FMT_MONOWHITE] = {
  1006. .convert = gray_to_monowhite
  1007. },
  1008. [PIX_FMT_MONOBLACK] = {
  1009. .convert = gray_to_monoblack
  1010. },
  1011. },
  1012. [PIX_FMT_MONOWHITE] = {
  1013. [PIX_FMT_GRAY8] = {
  1014. .convert = monowhite_to_gray
  1015. },
  1016. },
  1017. [PIX_FMT_MONOBLACK] = {
  1018. [PIX_FMT_GRAY8] = {
  1019. .convert = monoblack_to_gray
  1020. },
  1021. },
  1022. };
  1023. static int avpicture_alloc(AVPicture *picture,
  1024. int pix_fmt, int width, int height)
  1025. {
  1026. int size;
  1027. void *ptr;
  1028. size = avpicture_get_size(pix_fmt, width, height);
  1029. if (size < 0)
  1030. goto fail;
  1031. ptr = av_malloc(size);
  1032. if (!ptr)
  1033. goto fail;
  1034. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1035. return 0;
  1036. fail:
  1037. memset(picture, 0, sizeof(AVPicture));
  1038. return -1;
  1039. }
  1040. static void avpicture_free(AVPicture *picture)
  1041. {
  1042. av_free(picture->data[0]);
  1043. }
  1044. /* XXX: always use linesize. Return -1 if not supported */
  1045. int img_convert(AVPicture *dst, int dst_pix_fmt,
  1046. AVPicture *src, int src_pix_fmt,
  1047. int src_width, int src_height)
  1048. {
  1049. int i, ret, dst_width, dst_height, int_pix_fmt;
  1050. PixFmtInfo *src_pix, *dst_pix;
  1051. ConvertEntry *ce;
  1052. AVPicture tmp1, *tmp = &tmp1;
  1053. if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
  1054. dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
  1055. return -1;
  1056. if (src_width <= 0 || src_height <= 0)
  1057. return 0;
  1058. dst_width = src_width;
  1059. dst_height = src_height;
  1060. dst_pix = &pix_fmt_info[dst_pix_fmt];
  1061. src_pix = &pix_fmt_info[src_pix_fmt];
  1062. if (src_pix_fmt == dst_pix_fmt) {
  1063. /* XXX: incorrect */
  1064. /* same format: just copy */
  1065. for(i = 0; i < dst_pix->nb_components; i++) {
  1066. int w, h;
  1067. w = dst_width;
  1068. h = dst_height;
  1069. if (dst_pix->is_yuv && (i == 1 || i == 2)) {
  1070. w >>= dst_pix->x_chroma_shift;
  1071. h >>= dst_pix->y_chroma_shift;
  1072. }
  1073. img_copy(dst->data[i], dst->linesize[i],
  1074. src->data[i], src->linesize[i],
  1075. w, h);
  1076. }
  1077. return 0;
  1078. }
  1079. ce = &convert_table[src_pix_fmt][dst_pix_fmt];
  1080. if (ce->convert) {
  1081. /* specific convertion routine */
  1082. ce->convert(dst, src, dst_width, dst_height);
  1083. return 0;
  1084. }
  1085. /* gray to YUV */
  1086. if (dst_pix->is_yuv && src_pix_fmt == PIX_FMT_GRAY8) {
  1087. int w, h, y;
  1088. uint8_t *d;
  1089. img_copy(dst->data[0], dst->linesize[0],
  1090. src->data[0], src->linesize[0],
  1091. dst_width, dst_height);
  1092. /* fill U and V with 128 */
  1093. w = dst_width;
  1094. h = dst_height;
  1095. w >>= dst_pix->x_chroma_shift;
  1096. h >>= dst_pix->y_chroma_shift;
  1097. for(i = 1; i <= 2; i++) {
  1098. d = dst->data[i];
  1099. for(y = 0; y< h; y++) {
  1100. memset(d, 128, w);
  1101. d += dst->linesize[i];
  1102. }
  1103. }
  1104. return 0;
  1105. }
  1106. /* YUV to gray */
  1107. if (src_pix->is_yuv && dst_pix_fmt == PIX_FMT_GRAY8) {
  1108. img_copy(dst->data[0], dst->linesize[0],
  1109. src->data[0], src->linesize[0],
  1110. dst_width, dst_height);
  1111. return 0;
  1112. }
  1113. /* YUV to YUV */
  1114. if (dst_pix->is_yuv && src_pix->is_yuv) {
  1115. int x_shift, y_shift, w, h;
  1116. void (*resize_func)(UINT8 *dst, int dst_wrap,
  1117. UINT8 *src, int src_wrap,
  1118. int width, int height);
  1119. /* compute chroma size of the smallest dimensions */
  1120. w = dst_width;
  1121. h = dst_height;
  1122. if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
  1123. w >>= dst_pix->x_chroma_shift;
  1124. else
  1125. w >>= src_pix->x_chroma_shift;
  1126. if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
  1127. h >>= dst_pix->y_chroma_shift;
  1128. else
  1129. h >>= src_pix->y_chroma_shift;
  1130. x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
  1131. y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
  1132. if (x_shift == 0 && y_shift == 0) {
  1133. resize_func = img_copy; /* should never happen */
  1134. } else if (x_shift == 0 && y_shift == 1) {
  1135. resize_func = shrink2;
  1136. } else if (x_shift == 1 && y_shift == 1) {
  1137. resize_func = shrink22;
  1138. } else if (x_shift == -1 && y_shift == -1) {
  1139. resize_func = grow22;
  1140. } else if (x_shift == -1 && y_shift == 1) {
  1141. resize_func = conv411;
  1142. } else {
  1143. /* currently not handled */
  1144. return -1;
  1145. }
  1146. img_copy(dst->data[0], dst->linesize[0],
  1147. src->data[0], src->linesize[0],
  1148. dst_width, dst_height);
  1149. for(i = 1;i <= 2; i++)
  1150. resize_func(dst->data[i], dst->linesize[i],
  1151. src->data[i], src->linesize[i],
  1152. w, h);
  1153. return 0;
  1154. }
  1155. /* try to use an intermediate format */
  1156. if (src_pix_fmt == PIX_FMT_MONOWHITE ||
  1157. src_pix_fmt == PIX_FMT_MONOBLACK ||
  1158. dst_pix_fmt == PIX_FMT_MONOWHITE ||
  1159. dst_pix_fmt == PIX_FMT_MONOBLACK) {
  1160. int_pix_fmt = PIX_FMT_GRAY8;
  1161. } else {
  1162. int_pix_fmt = PIX_FMT_RGB24;
  1163. }
  1164. if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1165. return -1;
  1166. ret = -1;
  1167. if (img_convert(tmp, int_pix_fmt,
  1168. src, src_pix_fmt, src_width, src_height) < 0)
  1169. goto fail1;
  1170. if (img_convert(dst, dst_pix_fmt,
  1171. tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1172. goto fail1;
  1173. ret = 0;
  1174. fail1:
  1175. avpicture_free(tmp);
  1176. return ret;
  1177. }
  1178. #ifdef HAVE_MMX
  1179. #define DEINT_INPLACE_LINE_LUM \
  1180. movd_m2r(lum_m4[0],mm0);\
  1181. movd_m2r(lum_m3[0],mm1);\
  1182. movd_m2r(lum_m2[0],mm2);\
  1183. movd_m2r(lum_m1[0],mm3);\
  1184. movd_m2r(lum[0],mm4);\
  1185. punpcklbw_r2r(mm7,mm0);\
  1186. movd_r2m(mm2,lum_m4[0]);\
  1187. punpcklbw_r2r(mm7,mm1);\
  1188. punpcklbw_r2r(mm7,mm2);\
  1189. punpcklbw_r2r(mm7,mm3);\
  1190. punpcklbw_r2r(mm7,mm4);\
  1191. paddw_r2r(mm3,mm1);\
  1192. psllw_i2r(1,mm2);\
  1193. paddw_r2r(mm4,mm0);\
  1194. psllw_i2r(2,mm1);\
  1195. paddw_r2r(mm6,mm2);\
  1196. paddw_r2r(mm2,mm1);\
  1197. psubusw_r2r(mm0,mm1);\
  1198. psrlw_i2r(3,mm1);\
  1199. packuswb_r2r(mm7,mm1);\
  1200. movd_r2m(mm1,lum_m2[0]);
  1201. #define DEINT_LINE_LUM \
  1202. movd_m2r(lum_m4[0],mm0);\
  1203. movd_m2r(lum_m3[0],mm1);\
  1204. movd_m2r(lum_m2[0],mm2);\
  1205. movd_m2r(lum_m1[0],mm3);\
  1206. movd_m2r(lum[0],mm4);\
  1207. punpcklbw_r2r(mm7,mm0);\
  1208. punpcklbw_r2r(mm7,mm1);\
  1209. punpcklbw_r2r(mm7,mm2);\
  1210. punpcklbw_r2r(mm7,mm3);\
  1211. punpcklbw_r2r(mm7,mm4);\
  1212. paddw_r2r(mm3,mm1);\
  1213. psllw_i2r(1,mm2);\
  1214. paddw_r2r(mm4,mm0);\
  1215. psllw_i2r(2,mm1);\
  1216. paddw_r2r(mm6,mm2);\
  1217. paddw_r2r(mm2,mm1);\
  1218. psubusw_r2r(mm0,mm1);\
  1219. psrlw_i2r(3,mm1);\
  1220. packuswb_r2r(mm7,mm1);\
  1221. movd_r2m(mm1,dst[0]);
  1222. #endif
  1223. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1224. static void deinterlace_line(UINT8 *dst, UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
  1225. int size)
  1226. {
  1227. #ifndef HAVE_MMX
  1228. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  1229. int sum;
  1230. for(;size > 0;size--) {
  1231. sum = -lum_m4[0];
  1232. sum += lum_m3[0] << 2;
  1233. sum += lum_m2[0] << 1;
  1234. sum += lum_m1[0] << 2;
  1235. sum += -lum[0];
  1236. dst[0] = cm[(sum + 4) >> 3];
  1237. lum_m4++;
  1238. lum_m3++;
  1239. lum_m2++;
  1240. lum_m1++;
  1241. lum++;
  1242. dst++;
  1243. }
  1244. #else
  1245. {
  1246. mmx_t rounder;
  1247. rounder.uw[0]=4;
  1248. rounder.uw[1]=4;
  1249. rounder.uw[2]=4;
  1250. rounder.uw[3]=4;
  1251. pxor_r2r(mm7,mm7);
  1252. movq_m2r(rounder,mm6);
  1253. }
  1254. for (;size > 3; size-=4) {
  1255. DEINT_LINE_LUM
  1256. lum_m4+=4;
  1257. lum_m3+=4;
  1258. lum_m2+=4;
  1259. lum_m1+=4;
  1260. lum+=4;
  1261. dst+=4;
  1262. }
  1263. #endif
  1264. }
  1265. static void deinterlace_line_inplace(UINT8 *lum_m4, UINT8 *lum_m3, UINT8 *lum_m2, UINT8 *lum_m1, UINT8 *lum,
  1266. int size)
  1267. {
  1268. #ifndef HAVE_MMX
  1269. UINT8 *cm = cropTbl + MAX_NEG_CROP;
  1270. int sum;
  1271. for(;size > 0;size--) {
  1272. sum = -lum_m4[0];
  1273. sum += lum_m3[0] << 2;
  1274. sum += lum_m2[0] << 1;
  1275. lum_m4[0]=lum_m2[0];
  1276. sum += lum_m1[0] << 2;
  1277. sum += -lum[0];
  1278. lum_m2[0] = cm[(sum + 4) >> 3];
  1279. lum_m4++;
  1280. lum_m3++;
  1281. lum_m2++;
  1282. lum_m1++;
  1283. lum++;
  1284. }
  1285. #else
  1286. {
  1287. mmx_t rounder;
  1288. rounder.uw[0]=4;
  1289. rounder.uw[1]=4;
  1290. rounder.uw[2]=4;
  1291. rounder.uw[3]=4;
  1292. pxor_r2r(mm7,mm7);
  1293. movq_m2r(rounder,mm6);
  1294. }
  1295. for (;size > 3; size-=4) {
  1296. DEINT_INPLACE_LINE_LUM
  1297. lum_m4+=4;
  1298. lum_m3+=4;
  1299. lum_m2+=4;
  1300. lum_m1+=4;
  1301. lum+=4;
  1302. }
  1303. #endif
  1304. }
  1305. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1306. top field is copied as is, but the bottom field is deinterlaced
  1307. against the top field. */
  1308. static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
  1309. UINT8 *src1, int src_wrap,
  1310. int width, int height)
  1311. {
  1312. UINT8 *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1313. int y;
  1314. src_m2 = src1;
  1315. src_m1 = src1;
  1316. src_0=&src_m1[src_wrap];
  1317. src_p1=&src_0[src_wrap];
  1318. src_p2=&src_p1[src_wrap];
  1319. for(y=0;y<(height-2);y+=2) {
  1320. memcpy(dst,src_m1,width);
  1321. dst += dst_wrap;
  1322. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1323. src_m2 = src_0;
  1324. src_m1 = src_p1;
  1325. src_0 = src_p2;
  1326. src_p1 += 2*src_wrap;
  1327. src_p2 += 2*src_wrap;
  1328. dst += dst_wrap;
  1329. }
  1330. memcpy(dst,src_m1,width);
  1331. dst += dst_wrap;
  1332. /* do last line */
  1333. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1334. }
  1335. static void deinterlace_bottom_field_inplace(UINT8 *src1, int src_wrap,
  1336. int width, int height)
  1337. {
  1338. UINT8 *src_m1, *src_0, *src_p1, *src_p2;
  1339. int y;
  1340. UINT8 *buf;
  1341. buf = (UINT8*)av_malloc(width);
  1342. src_m1 = src1;
  1343. memcpy(buf,src_m1,width);
  1344. src_0=&src_m1[src_wrap];
  1345. src_p1=&src_0[src_wrap];
  1346. src_p2=&src_p1[src_wrap];
  1347. for(y=0;y<(height-2);y+=2) {
  1348. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1349. src_m1 = src_p1;
  1350. src_0 = src_p2;
  1351. src_p1 += 2*src_wrap;
  1352. src_p2 += 2*src_wrap;
  1353. }
  1354. /* do last line */
  1355. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1356. av_free(buf);
  1357. }
  1358. /* deinterlace - if not supported return -1 */
  1359. int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
  1360. int pix_fmt, int width, int height)
  1361. {
  1362. int i;
  1363. if (pix_fmt != PIX_FMT_YUV420P &&
  1364. pix_fmt != PIX_FMT_YUV422P &&
  1365. pix_fmt != PIX_FMT_YUV444P)
  1366. return -1;
  1367. if ((width & 3) != 0 || (height & 3) != 0)
  1368. return -1;
  1369. for(i=0;i<3;i++) {
  1370. if (i == 1) {
  1371. switch(pix_fmt) {
  1372. case PIX_FMT_YUV420P:
  1373. width >>= 1;
  1374. height >>= 1;
  1375. break;
  1376. case PIX_FMT_YUV422P:
  1377. width >>= 1;
  1378. break;
  1379. default:
  1380. break;
  1381. }
  1382. }
  1383. if (src == dst) {
  1384. deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
  1385. width, height);
  1386. } else {
  1387. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1388. src->data[i], src->linesize[i],
  1389. width, height);
  1390. }
  1391. }
  1392. #ifdef HAVE_MMX
  1393. emms();
  1394. #endif
  1395. return 0;
  1396. }
  1397. #undef FIX