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.

2162 lines
57KB

  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. /**
  20. * @file imgconvert.c
  21. * Misc image convertion routines.
  22. */
  23. /* TODO:
  24. * - write 'ffimg' program to test all the image related stuff
  25. * - move all api to slice based system
  26. * - integrate deinterlacing, postprocessing and scaling in the conversion process
  27. */
  28. #include "avcodec.h"
  29. #include "dsputil.h"
  30. #ifdef USE_FASTMEMCPY
  31. #include "fastmemcpy.h"
  32. #endif
  33. #ifdef HAVE_MMX
  34. #include "i386/mmx.h"
  35. #endif
  36. #define xglue(x, y) x ## y
  37. #define glue(x, y) xglue(x, y)
  38. #define FF_COLOR_RGB 0 /* RGB color space */
  39. #define FF_COLOR_GRAY 1 /* gray color space */
  40. #define FF_COLOR_YUV 2 /* YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  41. #define FF_COLOR_YUV_JPEG 3 /* YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  42. #define FF_PIXEL_PLANAR 0 /* each channel has one component in AVPicture */
  43. #define FF_PIXEL_PACKED 1 /* only one components containing all the channels */
  44. #define FF_PIXEL_PALETTE 2 /* one components containing indexes for a palette */
  45. typedef struct PixFmtInfo {
  46. const char *name;
  47. uint8_t nb_channels; /* number of channels (including alpha) */
  48. uint8_t color_type; /* color type (see FF_COLOR_xxx constants) */
  49. uint8_t pixel_type; /* pixel storage type (see FF_PIXEL_xxx constants) */
  50. uint8_t is_alpha : 1; /* true if alpha can be specified */
  51. uint8_t x_chroma_shift; /* X chroma subsampling factor is 2 ^ shift */
  52. uint8_t y_chroma_shift; /* Y chroma subsampling factor is 2 ^ shift */
  53. uint8_t depth; /* bit depth of the color components */
  54. } PixFmtInfo;
  55. /* this table gives more information about formats */
  56. static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  57. /* YUV formats */
  58. [PIX_FMT_YUV420P] = {
  59. .name = "yuv420p",
  60. .nb_channels = 3,
  61. .color_type = FF_COLOR_YUV,
  62. .pixel_type = FF_PIXEL_PLANAR,
  63. .depth = 8,
  64. .x_chroma_shift = 1, .y_chroma_shift = 1,
  65. },
  66. [PIX_FMT_YUV422P] = {
  67. .name = "yuv422p",
  68. .nb_channels = 3,
  69. .color_type = FF_COLOR_YUV,
  70. .pixel_type = FF_PIXEL_PLANAR,
  71. .depth = 8,
  72. .x_chroma_shift = 1, .y_chroma_shift = 0,
  73. },
  74. [PIX_FMT_YUV444P] = {
  75. .name = "yuv444p",
  76. .nb_channels = 3,
  77. .color_type = FF_COLOR_YUV,
  78. .pixel_type = FF_PIXEL_PLANAR,
  79. .depth = 8,
  80. .x_chroma_shift = 0, .y_chroma_shift = 0,
  81. },
  82. [PIX_FMT_YUV422] = {
  83. .name = "yuv422",
  84. .nb_channels = 1,
  85. .color_type = FF_COLOR_YUV,
  86. .pixel_type = FF_PIXEL_PACKED,
  87. .depth = 8,
  88. .x_chroma_shift = 1, .y_chroma_shift = 0,
  89. },
  90. [PIX_FMT_YUV410P] = {
  91. .name = "yuv410p",
  92. .nb_channels = 3,
  93. .color_type = FF_COLOR_YUV,
  94. .pixel_type = FF_PIXEL_PLANAR,
  95. .depth = 8,
  96. .x_chroma_shift = 2, .y_chroma_shift = 2,
  97. },
  98. [PIX_FMT_YUV411P] = {
  99. .name = "yuv411p",
  100. .nb_channels = 3,
  101. .color_type = FF_COLOR_YUV,
  102. .pixel_type = FF_PIXEL_PLANAR,
  103. .depth = 8,
  104. .x_chroma_shift = 2, .y_chroma_shift = 0,
  105. },
  106. /* JPEG YUV */
  107. [PIX_FMT_YUVJ420P] = {
  108. .name = "yuvj420p",
  109. .nb_channels = 3,
  110. .color_type = FF_COLOR_YUV_JPEG,
  111. .pixel_type = FF_PIXEL_PLANAR,
  112. .depth = 8,
  113. .x_chroma_shift = 1, .y_chroma_shift = 1,
  114. },
  115. [PIX_FMT_YUVJ422P] = {
  116. .name = "yuvj422p",
  117. .nb_channels = 3,
  118. .color_type = FF_COLOR_YUV_JPEG,
  119. .pixel_type = FF_PIXEL_PLANAR,
  120. .depth = 8,
  121. .x_chroma_shift = 1, .y_chroma_shift = 0,
  122. },
  123. [PIX_FMT_YUVJ444P] = {
  124. .name = "yuvj444p",
  125. .nb_channels = 3,
  126. .color_type = FF_COLOR_YUV_JPEG,
  127. .pixel_type = FF_PIXEL_PLANAR,
  128. .depth = 8,
  129. .x_chroma_shift = 0, .y_chroma_shift = 0,
  130. },
  131. /* RGB formats */
  132. [PIX_FMT_RGB24] = {
  133. .name = "rgb24",
  134. .nb_channels = 3,
  135. .color_type = FF_COLOR_RGB,
  136. .pixel_type = FF_PIXEL_PACKED,
  137. .depth = 8,
  138. },
  139. [PIX_FMT_BGR24] = {
  140. .name = "bgr24",
  141. .nb_channels = 3,
  142. .color_type = FF_COLOR_RGB,
  143. .pixel_type = FF_PIXEL_PACKED,
  144. .depth = 8,
  145. },
  146. [PIX_FMT_RGBA32] = {
  147. .name = "rgba32",
  148. .nb_channels = 4, .is_alpha = 1,
  149. .color_type = FF_COLOR_RGB,
  150. .pixel_type = FF_PIXEL_PACKED,
  151. .depth = 8,
  152. },
  153. [PIX_FMT_RGB565] = {
  154. .name = "rgb565",
  155. .nb_channels = 3,
  156. .color_type = FF_COLOR_RGB,
  157. .pixel_type = FF_PIXEL_PACKED,
  158. .depth = 5,
  159. },
  160. [PIX_FMT_RGB555] = {
  161. .name = "rgb555",
  162. .nb_channels = 4, .is_alpha = 1,
  163. .color_type = FF_COLOR_RGB,
  164. .pixel_type = FF_PIXEL_PACKED,
  165. .depth = 5,
  166. },
  167. /* gray / mono formats */
  168. [PIX_FMT_GRAY8] = {
  169. .name = "gray",
  170. .nb_channels = 1,
  171. .color_type = FF_COLOR_GRAY,
  172. .pixel_type = FF_PIXEL_PLANAR,
  173. .depth = 8,
  174. },
  175. [PIX_FMT_MONOWHITE] = {
  176. .name = "monow",
  177. .nb_channels = 1,
  178. .color_type = FF_COLOR_GRAY,
  179. .pixel_type = FF_PIXEL_PLANAR,
  180. .depth = 1,
  181. },
  182. [PIX_FMT_MONOBLACK] = {
  183. .name = "monob",
  184. .nb_channels = 1,
  185. .color_type = FF_COLOR_GRAY,
  186. .pixel_type = FF_PIXEL_PLANAR,
  187. .depth = 1,
  188. },
  189. /* paletted formats */
  190. [PIX_FMT_PAL8] = {
  191. .name = "pal8",
  192. .nb_channels = 4, .is_alpha = 1,
  193. .color_type = FF_COLOR_RGB,
  194. .pixel_type = FF_PIXEL_PALETTE,
  195. .depth = 8,
  196. },
  197. };
  198. void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
  199. {
  200. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  201. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  202. }
  203. const char *avcodec_get_pix_fmt_name(int pix_fmt)
  204. {
  205. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  206. return "???";
  207. else
  208. return pix_fmt_info[pix_fmt].name;
  209. }
  210. enum PixelFormat avcodec_get_pix_fmt(const char* name)
  211. {
  212. int i;
  213. for (i=0; i < PIX_FMT_NB; i++)
  214. if (!strcmp(pix_fmt_info[i].name, name))
  215. break;
  216. return i;
  217. }
  218. /* Picture field are filled with 'ptr' addresses. Also return size */
  219. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  220. int pix_fmt, int width, int height)
  221. {
  222. int size, w2, h2, size2;
  223. PixFmtInfo *pinfo;
  224. pinfo = &pix_fmt_info[pix_fmt];
  225. size = width * height;
  226. switch(pix_fmt) {
  227. case PIX_FMT_YUV420P:
  228. case PIX_FMT_YUV422P:
  229. case PIX_FMT_YUV444P:
  230. case PIX_FMT_YUV410P:
  231. case PIX_FMT_YUV411P:
  232. case PIX_FMT_YUVJ420P:
  233. case PIX_FMT_YUVJ422P:
  234. case PIX_FMT_YUVJ444P:
  235. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  236. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  237. size2 = w2 * h2;
  238. picture->data[0] = ptr;
  239. picture->data[1] = picture->data[0] + size;
  240. picture->data[2] = picture->data[1] + size2;
  241. picture->linesize[0] = width;
  242. picture->linesize[1] = w2;
  243. picture->linesize[2] = w2;
  244. return size + 2 * size2;
  245. case PIX_FMT_RGB24:
  246. case PIX_FMT_BGR24:
  247. picture->data[0] = ptr;
  248. picture->data[1] = NULL;
  249. picture->data[2] = NULL;
  250. picture->linesize[0] = width * 3;
  251. return size * 3;
  252. case PIX_FMT_RGBA32:
  253. picture->data[0] = ptr;
  254. picture->data[1] = NULL;
  255. picture->data[2] = NULL;
  256. picture->linesize[0] = width * 4;
  257. return size * 4;
  258. case PIX_FMT_RGB555:
  259. case PIX_FMT_RGB565:
  260. case PIX_FMT_YUV422:
  261. picture->data[0] = ptr;
  262. picture->data[1] = NULL;
  263. picture->data[2] = NULL;
  264. picture->linesize[0] = width * 2;
  265. return size * 2;
  266. case PIX_FMT_GRAY8:
  267. picture->data[0] = ptr;
  268. picture->data[1] = NULL;
  269. picture->data[2] = NULL;
  270. picture->linesize[0] = width;
  271. return size;
  272. case PIX_FMT_MONOWHITE:
  273. case PIX_FMT_MONOBLACK:
  274. picture->data[0] = ptr;
  275. picture->data[1] = NULL;
  276. picture->data[2] = NULL;
  277. picture->linesize[0] = (width + 7) >> 3;
  278. return picture->linesize[0] * height;
  279. case PIX_FMT_PAL8:
  280. size2 = (size + 3) & ~3;
  281. picture->data[0] = ptr;
  282. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  283. picture->data[2] = NULL;
  284. picture->linesize[0] = width;
  285. picture->linesize[1] = 4;
  286. return size2 + 256 * 4;
  287. default:
  288. picture->data[0] = NULL;
  289. picture->data[1] = NULL;
  290. picture->data[2] = NULL;
  291. picture->data[3] = NULL;
  292. return -1;
  293. }
  294. }
  295. int avpicture_layout(AVPicture* src, int pix_fmt, int width, int height,
  296. unsigned char *dest, int dest_size)
  297. {
  298. PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  299. int i, j, w, h, data_planes;
  300. unsigned char* s;
  301. int size = avpicture_get_size(pix_fmt, width, height);
  302. if (size > dest_size)
  303. return -1;
  304. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  305. if (pix_fmt == PIX_FMT_YUV422 || pix_fmt == PIX_FMT_RGB565 ||
  306. pix_fmt == PIX_FMT_RGB555)
  307. w = width * 2;
  308. else if (pix_fmt == PIX_FMT_PAL8)
  309. w = width;
  310. else
  311. w = width * (pf->depth * pf->nb_channels / 8);
  312. data_planes = 1;
  313. h = height;
  314. } else {
  315. data_planes = pf->nb_channels;
  316. w = width;
  317. h = height;
  318. }
  319. for (i=0; i<data_planes; i++) {
  320. if (i == 1) {
  321. w = width >> pf->x_chroma_shift;
  322. h = height >> pf->y_chroma_shift;
  323. }
  324. s = src->data[i];
  325. for(j=0; j<h; j++) {
  326. memcpy(dest, s, w);
  327. dest += w;
  328. s += src->linesize[i];
  329. }
  330. }
  331. if (pf->pixel_type == FF_PIXEL_PALETTE)
  332. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  333. return size;
  334. }
  335. int avpicture_get_size(int pix_fmt, int width, int height)
  336. {
  337. AVPicture dummy_pict;
  338. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  339. }
  340. /**
  341. * compute the loss when converting from a pixel format to another
  342. */
  343. int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
  344. int has_alpha)
  345. {
  346. const PixFmtInfo *pf, *ps;
  347. int loss;
  348. ps = &pix_fmt_info[src_pix_fmt];
  349. pf = &pix_fmt_info[dst_pix_fmt];
  350. /* compute loss */
  351. loss = 0;
  352. pf = &pix_fmt_info[dst_pix_fmt];
  353. if (pf->depth < ps->depth ||
  354. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  355. loss |= FF_LOSS_DEPTH;
  356. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  357. pf->y_chroma_shift > ps->y_chroma_shift)
  358. loss |= FF_LOSS_RESOLUTION;
  359. switch(pf->color_type) {
  360. case FF_COLOR_RGB:
  361. if (ps->color_type != FF_COLOR_RGB &&
  362. ps->color_type != FF_COLOR_GRAY)
  363. loss |= FF_LOSS_COLORSPACE;
  364. break;
  365. case FF_COLOR_GRAY:
  366. if (ps->color_type != FF_COLOR_GRAY)
  367. loss |= FF_LOSS_COLORSPACE;
  368. break;
  369. case FF_COLOR_YUV:
  370. if (ps->color_type != FF_COLOR_YUV)
  371. loss |= FF_LOSS_COLORSPACE;
  372. break;
  373. case FF_COLOR_YUV_JPEG:
  374. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  375. ps->color_type != FF_COLOR_YUV &&
  376. ps->color_type != FF_COLOR_GRAY)
  377. loss |= FF_LOSS_COLORSPACE;
  378. break;
  379. default:
  380. /* fail safe test */
  381. if (ps->color_type != pf->color_type)
  382. loss |= FF_LOSS_COLORSPACE;
  383. break;
  384. }
  385. if (pf->color_type == FF_COLOR_GRAY &&
  386. ps->color_type != FF_COLOR_GRAY)
  387. loss |= FF_LOSS_CHROMA;
  388. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  389. loss |= FF_LOSS_ALPHA;
  390. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  391. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  392. loss |= FF_LOSS_COLORQUANT;
  393. return loss;
  394. }
  395. static int avg_bits_per_pixel(int pix_fmt)
  396. {
  397. int bits;
  398. const PixFmtInfo *pf;
  399. pf = &pix_fmt_info[pix_fmt];
  400. switch(pf->pixel_type) {
  401. case FF_PIXEL_PACKED:
  402. switch(pix_fmt) {
  403. case PIX_FMT_YUV422:
  404. case PIX_FMT_RGB565:
  405. case PIX_FMT_RGB555:
  406. bits = 16;
  407. break;
  408. default:
  409. bits = pf->depth * pf->nb_channels;
  410. break;
  411. }
  412. break;
  413. case FF_PIXEL_PLANAR:
  414. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  415. bits = pf->depth * pf->nb_channels;
  416. } else {
  417. bits = pf->depth + ((2 * pf->depth) >>
  418. (pf->x_chroma_shift + pf->y_chroma_shift));
  419. }
  420. break;
  421. case FF_PIXEL_PALETTE:
  422. bits = 8;
  423. break;
  424. default:
  425. bits = -1;
  426. break;
  427. }
  428. return bits;
  429. }
  430. static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
  431. int src_pix_fmt,
  432. int has_alpha,
  433. int loss_mask)
  434. {
  435. int dist, i, loss, min_dist, dst_pix_fmt;
  436. /* find exact color match with smallest size */
  437. dst_pix_fmt = -1;
  438. min_dist = 0x7fffffff;
  439. for(i = 0;i < PIX_FMT_NB; i++) {
  440. if (pix_fmt_mask & (1 << i)) {
  441. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  442. if (loss == 0) {
  443. dist = avg_bits_per_pixel(i);
  444. if (dist < min_dist) {
  445. min_dist = dist;
  446. dst_pix_fmt = i;
  447. }
  448. }
  449. }
  450. }
  451. return dst_pix_fmt;
  452. }
  453. /**
  454. * find best pixel format to convert to. Return -1 if none found
  455. */
  456. int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
  457. int has_alpha, int *loss_ptr)
  458. {
  459. int dst_pix_fmt, loss_mask, i;
  460. static const int loss_mask_order[] = {
  461. ~0, /* no loss first */
  462. ~FF_LOSS_ALPHA,
  463. ~FF_LOSS_RESOLUTION,
  464. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  465. ~FF_LOSS_COLORQUANT,
  466. ~FF_LOSS_DEPTH,
  467. 0,
  468. };
  469. /* try with successive loss */
  470. i = 0;
  471. for(;;) {
  472. loss_mask = loss_mask_order[i++];
  473. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  474. has_alpha, loss_mask);
  475. if (dst_pix_fmt >= 0)
  476. goto found;
  477. if (loss_mask == 0)
  478. break;
  479. }
  480. return -1;
  481. found:
  482. if (loss_ptr)
  483. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  484. return dst_pix_fmt;
  485. }
  486. static void img_copy_plane(uint8_t *dst, int dst_wrap,
  487. const uint8_t *src, int src_wrap,
  488. int width, int height)
  489. {
  490. for(;height > 0; height--) {
  491. memcpy(dst, src, width);
  492. dst += dst_wrap;
  493. src += src_wrap;
  494. }
  495. }
  496. /**
  497. * Copy image 'src' to 'dst'.
  498. */
  499. void img_copy(AVPicture *dst, AVPicture *src,
  500. int pix_fmt, int width, int height)
  501. {
  502. int bwidth, bits, i;
  503. PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  504. pf = &pix_fmt_info[pix_fmt];
  505. switch(pf->pixel_type) {
  506. case FF_PIXEL_PACKED:
  507. switch(pix_fmt) {
  508. case PIX_FMT_YUV422:
  509. case PIX_FMT_RGB565:
  510. case PIX_FMT_RGB555:
  511. bits = 16;
  512. break;
  513. default:
  514. bits = pf->depth * pf->nb_channels;
  515. break;
  516. }
  517. bwidth = (width * bits + 7) >> 3;
  518. img_copy_plane(dst->data[0], dst->linesize[0],
  519. src->data[0], src->linesize[0],
  520. bwidth, height);
  521. break;
  522. case FF_PIXEL_PLANAR:
  523. for(i = 0; i < pf->nb_channels; i++) {
  524. int w, h;
  525. w = width;
  526. h = height;
  527. if (i == 1 || i == 2) {
  528. w >>= pf->x_chroma_shift;
  529. h >>= pf->y_chroma_shift;
  530. }
  531. bwidth = (w * pf->depth + 7) >> 3;
  532. img_copy_plane(dst->data[i], dst->linesize[i],
  533. src->data[i], src->linesize[i],
  534. bwidth, h);
  535. }
  536. break;
  537. case FF_PIXEL_PALETTE:
  538. img_copy_plane(dst->data[0], dst->linesize[0],
  539. src->data[0], src->linesize[0],
  540. width, height);
  541. /* copy the palette */
  542. img_copy_plane(dst->data[1], dst->linesize[1],
  543. src->data[1], src->linesize[1],
  544. 4, 256);
  545. break;
  546. }
  547. }
  548. /* XXX: totally non optimized */
  549. static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
  550. int width, int height)
  551. {
  552. const uint8_t *p, *p1;
  553. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  554. int x;
  555. p1 = src->data[0];
  556. lum1 = dst->data[0];
  557. cb1 = dst->data[1];
  558. cr1 = dst->data[2];
  559. for(;height >= 2; height -= 2) {
  560. p = p1;
  561. lum = lum1;
  562. cb = cb1;
  563. cr = cr1;
  564. for(x=0;x<width;x+=2) {
  565. lum[0] = p[0];
  566. cb[0] = p[1];
  567. lum[1] = p[2];
  568. cr[0] = p[3];
  569. p += 4;
  570. lum += 2;
  571. cb++;
  572. cr++;
  573. }
  574. p1 += src->linesize[0];
  575. lum1 += dst->linesize[0];
  576. p = p1;
  577. lum = lum1;
  578. for(x=0;x<width;x+=2) {
  579. lum[0] = p[0];
  580. lum[1] = p[2];
  581. p += 4;
  582. lum += 2;
  583. }
  584. p1 += src->linesize[0];
  585. lum1 += dst->linesize[0];
  586. cb1 += dst->linesize[1];
  587. cr1 += dst->linesize[2];
  588. }
  589. }
  590. static void yuv422_to_yuv422p(AVPicture *dst, AVPicture *src,
  591. int width, int height)
  592. {
  593. const uint8_t *p, *p1;
  594. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  595. int w;
  596. p1 = src->data[0];
  597. lum1 = dst->data[0];
  598. cb1 = dst->data[1];
  599. cr1 = dst->data[2];
  600. for(;height > 0; height--) {
  601. p = p1;
  602. lum = lum1;
  603. cb = cb1;
  604. cr = cr1;
  605. for(w = width; w >= 2; w -= 2) {
  606. lum[0] = p[0];
  607. cb[0] = p[1];
  608. lum[1] = p[2];
  609. cr[0] = p[3];
  610. p += 4;
  611. lum += 2;
  612. cb++;
  613. cr++;
  614. }
  615. p1 += src->linesize[0];
  616. lum1 += dst->linesize[0];
  617. cb1 += dst->linesize[1];
  618. cr1 += dst->linesize[2];
  619. }
  620. }
  621. static void yuv422p_to_yuv422(AVPicture *dst, AVPicture *src,
  622. int width, int height)
  623. {
  624. uint8_t *p, *p1;
  625. const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  626. int w;
  627. p1 = dst->data[0];
  628. lum1 = src->data[0];
  629. cb1 = src->data[1];
  630. cr1 = src->data[2];
  631. for(;height > 0; height--) {
  632. p = p1;
  633. lum = lum1;
  634. cb = cb1;
  635. cr = cr1;
  636. for(w = width; w >= 2; w -= 2) {
  637. p[0] = lum[0];
  638. p[1] = cb[0];
  639. p[2] = lum[1];
  640. p[3] = cr[0];
  641. p += 4;
  642. lum += 2;
  643. cb++;
  644. cr++;
  645. }
  646. p1 += dst->linesize[0];
  647. lum1 += src->linesize[0];
  648. cb1 += src->linesize[1];
  649. cr1 += src->linesize[2];
  650. }
  651. }
  652. #define SCALEBITS 10
  653. #define ONE_HALF (1 << (SCALEBITS - 1))
  654. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  655. #define YUV_TO_RGB1_CCIR(cb1, cr1)\
  656. {\
  657. cb = (cb1) - 128;\
  658. cr = (cr1) - 128;\
  659. r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
  660. g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
  661. ONE_HALF;\
  662. b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
  663. }
  664. #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
  665. {\
  666. y = ((y1) - 16) * FIX(255.0/219.0);\
  667. r = cm[(y + r_add) >> SCALEBITS];\
  668. g = cm[(y + g_add) >> SCALEBITS];\
  669. b = cm[(y + b_add) >> SCALEBITS];\
  670. }
  671. #define YUV_TO_RGB1(cb1, cr1)\
  672. {\
  673. cb = (cb1) - 128;\
  674. cr = (cr1) - 128;\
  675. r_add = FIX(1.40200) * cr + ONE_HALF;\
  676. g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
  677. b_add = FIX(1.77200) * cb + ONE_HALF;\
  678. }
  679. #define YUV_TO_RGB2(r, g, b, y1)\
  680. {\
  681. y = (y1) << SCALEBITS;\
  682. r = cm[(y + r_add) >> SCALEBITS];\
  683. g = cm[(y + g_add) >> SCALEBITS];\
  684. b = cm[(y + b_add) >> SCALEBITS];\
  685. }
  686. #define Y_CCIR_TO_JPEG(y)\
  687. cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
  688. #define Y_JPEG_TO_CCIR(y)\
  689. (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  690. #define C_CCIR_TO_JPEG(y)\
  691. cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
  692. /* NOTE: the clamp is really necessary! */
  693. #define C_JPEG_TO_CCIR(y)\
  694. ({\
  695. int __y;\
  696. __y = ((((y) - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);\
  697. if (__y < 16)\
  698. __y = 16;\
  699. __y;\
  700. })
  701. #define RGB_TO_Y(r, g, b) \
  702. ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
  703. FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
  704. #define RGB_TO_U(r1, g1, b1, shift)\
  705. (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
  706. FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  707. #define RGB_TO_V(r1, g1, b1, shift)\
  708. (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
  709. FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  710. #define RGB_TO_Y_CCIR(r, g, b) \
  711. ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  712. FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  713. #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
  714. (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
  715. FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  716. #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
  717. (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
  718. FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  719. static uint8_t y_ccir_to_jpeg[256];
  720. static uint8_t y_jpeg_to_ccir[256];
  721. static uint8_t c_ccir_to_jpeg[256];
  722. static uint8_t c_jpeg_to_ccir[256];
  723. /* init various conversion tables */
  724. static void img_convert_init(void)
  725. {
  726. int i;
  727. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  728. for(i = 0;i < 256; i++) {
  729. y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
  730. y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
  731. c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
  732. c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
  733. }
  734. }
  735. /* apply to each pixel the given table */
  736. static void img_apply_table(uint8_t *dst, int dst_wrap,
  737. const uint8_t *src, int src_wrap,
  738. int width, int height, const uint8_t *table1)
  739. {
  740. int n;
  741. const uint8_t *s;
  742. uint8_t *d;
  743. const uint8_t *table;
  744. table = table1;
  745. for(;height > 0; height--) {
  746. s = src;
  747. d = dst;
  748. n = width;
  749. while (n >= 4) {
  750. d[0] = table[s[0]];
  751. d[1] = table[s[1]];
  752. d[2] = table[s[2]];
  753. d[3] = table[s[3]];
  754. d += 4;
  755. s += 4;
  756. n -= 4;
  757. }
  758. while (n > 0) {
  759. d[0] = table[s[0]];
  760. d++;
  761. s++;
  762. n--;
  763. }
  764. dst += dst_wrap;
  765. src += src_wrap;
  766. }
  767. }
  768. /* XXX: use generic filter ? */
  769. /* XXX: in most cases, the sampling position is incorrect */
  770. /* 4x1 -> 1x1 */
  771. static void shrink41(uint8_t *dst, int dst_wrap,
  772. const uint8_t *src, int src_wrap,
  773. int width, int height)
  774. {
  775. int w;
  776. const uint8_t *s;
  777. uint8_t *d;
  778. for(;height > 0; height--) {
  779. s = src;
  780. d = dst;
  781. for(w = width;w > 0; w--) {
  782. d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
  783. s += 4;
  784. d++;
  785. }
  786. src += src_wrap;
  787. dst += dst_wrap;
  788. }
  789. }
  790. /* 2x1 -> 1x1 */
  791. static void shrink21(uint8_t *dst, int dst_wrap,
  792. const uint8_t *src, int src_wrap,
  793. int width, int height)
  794. {
  795. int w;
  796. const uint8_t *s;
  797. uint8_t *d;
  798. for(;height > 0; height--) {
  799. s = src;
  800. d = dst;
  801. for(w = width;w > 0; w--) {
  802. d[0] = (s[0] + s[1]) >> 1;
  803. s += 2;
  804. d++;
  805. }
  806. src += src_wrap;
  807. dst += dst_wrap;
  808. }
  809. }
  810. /* 1x2 -> 1x1 */
  811. static void shrink12(uint8_t *dst, int dst_wrap,
  812. const uint8_t *src, int src_wrap,
  813. int width, int height)
  814. {
  815. int w;
  816. uint8_t *d;
  817. const uint8_t *s1, *s2;
  818. for(;height > 0; height--) {
  819. s1 = src;
  820. s2 = s1 + src_wrap;
  821. d = dst;
  822. for(w = width;w >= 4; w-=4) {
  823. d[0] = (s1[0] + s2[0]) >> 1;
  824. d[1] = (s1[1] + s2[1]) >> 1;
  825. d[2] = (s1[2] + s2[2]) >> 1;
  826. d[3] = (s1[3] + s2[3]) >> 1;
  827. s1 += 4;
  828. s2 += 4;
  829. d += 4;
  830. }
  831. for(;w > 0; w--) {
  832. d[0] = (s1[0] + s2[0]) >> 1;
  833. s1++;
  834. s2++;
  835. d++;
  836. }
  837. src += 2 * src_wrap;
  838. dst += dst_wrap;
  839. }
  840. }
  841. /* 2x2 -> 1x1 */
  842. static void shrink22(uint8_t *dst, int dst_wrap,
  843. const uint8_t *src, int src_wrap,
  844. int width, int height)
  845. {
  846. int w;
  847. const uint8_t *s1, *s2;
  848. uint8_t *d;
  849. for(;height > 0; height--) {
  850. s1 = src;
  851. s2 = s1 + src_wrap;
  852. d = dst;
  853. for(w = width;w >= 4; w-=4) {
  854. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  855. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  856. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  857. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  858. s1 += 8;
  859. s2 += 8;
  860. d += 4;
  861. }
  862. for(;w > 0; w--) {
  863. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  864. s1 += 2;
  865. s2 += 2;
  866. d++;
  867. }
  868. src += 2 * src_wrap;
  869. dst += dst_wrap;
  870. }
  871. }
  872. /* 4x4 -> 1x1 */
  873. static void shrink44(uint8_t *dst, int dst_wrap,
  874. const uint8_t *src, int src_wrap,
  875. int width, int height)
  876. {
  877. int w;
  878. const uint8_t *s1, *s2, *s3, *s4;
  879. uint8_t *d;
  880. for(;height > 0; height--) {
  881. s1 = src;
  882. s2 = s1 + src_wrap;
  883. s3 = s2 + src_wrap;
  884. s4 = s3 + src_wrap;
  885. d = dst;
  886. for(w = width;w > 0; w--) {
  887. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  888. s2[0] + s2[1] + s2[2] + s2[3] +
  889. s3[0] + s3[1] + s3[2] + s3[3] +
  890. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  891. s1 += 4;
  892. s2 += 4;
  893. s3 += 4;
  894. s4 += 4;
  895. d++;
  896. }
  897. src += 4 * src_wrap;
  898. dst += dst_wrap;
  899. }
  900. }
  901. static void grow21_line(uint8_t *dst, const uint8_t *src,
  902. int width)
  903. {
  904. int w;
  905. const uint8_t *s1;
  906. uint8_t *d;
  907. s1 = src;
  908. d = dst;
  909. for(w = width;w >= 4; w-=4) {
  910. d[1] = d[0] = s1[0];
  911. d[3] = d[2] = s1[1];
  912. s1 += 2;
  913. d += 4;
  914. }
  915. for(;w >= 2; w -= 2) {
  916. d[1] = d[0] = s1[0];
  917. s1 ++;
  918. d += 2;
  919. }
  920. /* only needed if width is not a multiple of two */
  921. /* XXX: veryfy that */
  922. if (w) {
  923. d[0] = s1[0];
  924. }
  925. }
  926. static void grow41_line(uint8_t *dst, const uint8_t *src,
  927. int width)
  928. {
  929. int w, v;
  930. const uint8_t *s1;
  931. uint8_t *d;
  932. s1 = src;
  933. d = dst;
  934. for(w = width;w >= 4; w-=4) {
  935. v = s1[0];
  936. d[0] = v;
  937. d[1] = v;
  938. d[2] = v;
  939. d[3] = v;
  940. s1 ++;
  941. d += 4;
  942. }
  943. }
  944. /* 1x1 -> 2x1 */
  945. static void grow21(uint8_t *dst, int dst_wrap,
  946. const uint8_t *src, int src_wrap,
  947. int width, int height)
  948. {
  949. for(;height > 0; height--) {
  950. grow21_line(dst, src, width);
  951. src += src_wrap;
  952. dst += dst_wrap;
  953. }
  954. }
  955. /* 1x1 -> 2x2 */
  956. static void grow22(uint8_t *dst, int dst_wrap,
  957. const uint8_t *src, int src_wrap,
  958. int width, int height)
  959. {
  960. for(;height > 0; height--) {
  961. grow21_line(dst, src, width);
  962. if (height%2)
  963. src += src_wrap;
  964. dst += dst_wrap;
  965. }
  966. }
  967. /* 1x1 -> 4x1 */
  968. static void grow41(uint8_t *dst, int dst_wrap,
  969. const uint8_t *src, int src_wrap,
  970. int width, int height)
  971. {
  972. for(;height > 0; height--) {
  973. grow41_line(dst, src, width);
  974. src += src_wrap;
  975. dst += dst_wrap;
  976. }
  977. }
  978. /* 1x1 -> 4x4 */
  979. static void grow44(uint8_t *dst, int dst_wrap,
  980. const uint8_t *src, int src_wrap,
  981. int width, int height)
  982. {
  983. for(;height > 0; height--) {
  984. grow41_line(dst, src, width);
  985. if ((height & 3) == 1)
  986. src += src_wrap;
  987. dst += dst_wrap;
  988. }
  989. }
  990. /* 1x2 -> 2x1 */
  991. static void conv411(uint8_t *dst, int dst_wrap,
  992. const uint8_t *src, int src_wrap,
  993. int width, int height)
  994. {
  995. int w, c;
  996. const uint8_t *s1, *s2;
  997. uint8_t *d;
  998. width>>=1;
  999. for(;height > 0; height--) {
  1000. s1 = src;
  1001. s2 = src + src_wrap;
  1002. d = dst;
  1003. for(w = width;w > 0; w--) {
  1004. c = (s1[0] + s2[0]) >> 1;
  1005. d[0] = c;
  1006. d[1] = c;
  1007. s1++;
  1008. s2++;
  1009. d += 2;
  1010. }
  1011. src += src_wrap * 2;
  1012. dst += dst_wrap;
  1013. }
  1014. }
  1015. /* XXX: add jpeg quantize code */
  1016. #define TRANSP_INDEX (6*6*6)
  1017. /* this is maybe slow, but allows for extensions */
  1018. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  1019. {
  1020. return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
  1021. }
  1022. static void build_rgb_palette(uint8_t *palette, int has_alpha)
  1023. {
  1024. uint32_t *pal;
  1025. static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
  1026. int i, r, g, b;
  1027. pal = (uint32_t *)palette;
  1028. i = 0;
  1029. for(r = 0; r < 6; r++) {
  1030. for(g = 0; g < 6; g++) {
  1031. for(b = 0; b < 6; b++) {
  1032. pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
  1033. (pal_value[g] << 8) | pal_value[b];
  1034. }
  1035. }
  1036. }
  1037. if (has_alpha)
  1038. pal[i++] = 0;
  1039. while (i < 256)
  1040. pal[i++] = 0xff000000;
  1041. }
  1042. /* copy bit n to bits 0 ... n - 1 */
  1043. static inline unsigned int bitcopy_n(unsigned int a, int n)
  1044. {
  1045. int mask;
  1046. mask = (1 << n) - 1;
  1047. return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
  1048. }
  1049. /* rgb555 handling */
  1050. #define RGB_NAME rgb555
  1051. #define RGB_IN(r, g, b, s)\
  1052. {\
  1053. unsigned int v = ((const uint16_t *)(s))[0];\
  1054. r = bitcopy_n(v >> (10 - 3), 3);\
  1055. g = bitcopy_n(v >> (5 - 3), 3);\
  1056. b = bitcopy_n(v << 3, 3);\
  1057. }
  1058. #define RGBA_IN(r, g, b, a, s)\
  1059. {\
  1060. unsigned int v = ((const uint16_t *)(s))[0];\
  1061. r = bitcopy_n(v >> (10 - 3), 3);\
  1062. g = bitcopy_n(v >> (5 - 3), 3);\
  1063. b = bitcopy_n(v << 3, 3);\
  1064. a = (-(v >> 15)) & 0xff;\
  1065. }
  1066. #define RGBA_OUT(d, r, g, b, a)\
  1067. {\
  1068. ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
  1069. ((a << 8) & 0x8000);\
  1070. }
  1071. #define BPP 2
  1072. #include "imgconvert_template.h"
  1073. /* rgb565 handling */
  1074. #define RGB_NAME rgb565
  1075. #define RGB_IN(r, g, b, s)\
  1076. {\
  1077. unsigned int v = ((const uint16_t *)(s))[0];\
  1078. r = bitcopy_n(v >> (11 - 3), 3);\
  1079. g = bitcopy_n(v >> (5 - 2), 2);\
  1080. b = bitcopy_n(v << 3, 3);\
  1081. }
  1082. #define RGB_OUT(d, r, g, b)\
  1083. {\
  1084. ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
  1085. }
  1086. #define BPP 2
  1087. #include "imgconvert_template.h"
  1088. /* bgr24 handling */
  1089. #define RGB_NAME bgr24
  1090. #define RGB_IN(r, g, b, s)\
  1091. {\
  1092. b = (s)[0];\
  1093. g = (s)[1];\
  1094. r = (s)[2];\
  1095. }
  1096. #define RGB_OUT(d, r, g, b)\
  1097. {\
  1098. (d)[0] = b;\
  1099. (d)[1] = g;\
  1100. (d)[2] = r;\
  1101. }
  1102. #define BPP 3
  1103. #include "imgconvert_template.h"
  1104. #undef RGB_IN
  1105. #undef RGB_OUT
  1106. #undef BPP
  1107. /* rgb24 handling */
  1108. #define RGB_NAME rgb24
  1109. #define FMT_RGB24
  1110. #define RGB_IN(r, g, b, s)\
  1111. {\
  1112. r = (s)[0];\
  1113. g = (s)[1];\
  1114. b = (s)[2];\
  1115. }
  1116. #define RGB_OUT(d, r, g, b)\
  1117. {\
  1118. (d)[0] = r;\
  1119. (d)[1] = g;\
  1120. (d)[2] = b;\
  1121. }
  1122. #define BPP 3
  1123. #include "imgconvert_template.h"
  1124. /* rgba32 handling */
  1125. #define RGB_NAME rgba32
  1126. #define FMT_RGBA32
  1127. #define RGB_IN(r, g, b, s)\
  1128. {\
  1129. unsigned int v = ((const uint32_t *)(s))[0];\
  1130. r = (v >> 16) & 0xff;\
  1131. g = (v >> 8) & 0xff;\
  1132. b = v & 0xff;\
  1133. }
  1134. #define RGBA_IN(r, g, b, a, s)\
  1135. {\
  1136. unsigned int v = ((const uint32_t *)(s))[0];\
  1137. a = (v >> 24) & 0xff;\
  1138. r = (v >> 16) & 0xff;\
  1139. g = (v >> 8) & 0xff;\
  1140. b = v & 0xff;\
  1141. }
  1142. #define RGBA_OUT(d, r, g, b, a)\
  1143. {\
  1144. ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
  1145. }
  1146. #define BPP 4
  1147. #include "imgconvert_template.h"
  1148. static void mono_to_gray(AVPicture *dst, AVPicture *src,
  1149. int width, int height, int xor_mask)
  1150. {
  1151. const unsigned char *p;
  1152. unsigned char *q;
  1153. int v, dst_wrap, src_wrap;
  1154. int y, w;
  1155. p = src->data[0];
  1156. src_wrap = src->linesize[0] - ((width + 7) >> 3);
  1157. q = dst->data[0];
  1158. dst_wrap = dst->linesize[0] - width;
  1159. for(y=0;y<height;y++) {
  1160. w = width;
  1161. while (w >= 8) {
  1162. v = *p++ ^ xor_mask;
  1163. q[0] = -(v >> 7);
  1164. q[1] = -((v >> 6) & 1);
  1165. q[2] = -((v >> 5) & 1);
  1166. q[3] = -((v >> 4) & 1);
  1167. q[4] = -((v >> 3) & 1);
  1168. q[5] = -((v >> 2) & 1);
  1169. q[6] = -((v >> 1) & 1);
  1170. q[7] = -((v >> 0) & 1);
  1171. w -= 8;
  1172. q += 8;
  1173. }
  1174. if (w > 0) {
  1175. v = *p++ ^ xor_mask;
  1176. do {
  1177. q[0] = -((v >> 7) & 1);
  1178. q++;
  1179. v <<= 1;
  1180. } while (--w);
  1181. }
  1182. p += src_wrap;
  1183. q += dst_wrap;
  1184. }
  1185. }
  1186. static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
  1187. int width, int height)
  1188. {
  1189. mono_to_gray(dst, src, width, height, 0xff);
  1190. }
  1191. static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
  1192. int width, int height)
  1193. {
  1194. mono_to_gray(dst, src, width, height, 0x00);
  1195. }
  1196. static void gray_to_mono(AVPicture *dst, AVPicture *src,
  1197. int width, int height, int xor_mask)
  1198. {
  1199. int n;
  1200. const uint8_t *s;
  1201. uint8_t *d;
  1202. int j, b, v, n1, src_wrap, dst_wrap, y;
  1203. s = src->data[0];
  1204. src_wrap = src->linesize[0] - width;
  1205. d = dst->data[0];
  1206. dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
  1207. for(y=0;y<height;y++) {
  1208. n = width;
  1209. while (n >= 8) {
  1210. v = 0;
  1211. for(j=0;j<8;j++) {
  1212. b = s[0];
  1213. s++;
  1214. v = (v << 1) | (b >> 7);
  1215. }
  1216. d[0] = v ^ xor_mask;
  1217. d++;
  1218. n -= 8;
  1219. }
  1220. if (n > 0) {
  1221. n1 = n;
  1222. v = 0;
  1223. while (n > 0) {
  1224. b = s[0];
  1225. s++;
  1226. v = (v << 1) | (b >> 7);
  1227. n--;
  1228. }
  1229. d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
  1230. d++;
  1231. }
  1232. s += src_wrap;
  1233. d += dst_wrap;
  1234. }
  1235. }
  1236. static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
  1237. int width, int height)
  1238. {
  1239. gray_to_mono(dst, src, width, height, 0xff);
  1240. }
  1241. static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
  1242. int width, int height)
  1243. {
  1244. gray_to_mono(dst, src, width, height, 0x00);
  1245. }
  1246. typedef struct ConvertEntry {
  1247. void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
  1248. } ConvertEntry;
  1249. /* Add each new convertion function in this table. In order to be able
  1250. to convert from any format to any format, the following constraints
  1251. must be satisfied:
  1252. - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
  1253. - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
  1254. - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
  1255. - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
  1256. PIX_FMT_RGB24.
  1257. - PIX_FMT_422 must convert to and from PIX_FMT_422P.
  1258. The other conversion functions are just optimisations for common cases.
  1259. */
  1260. static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
  1261. [PIX_FMT_YUV420P] = {
  1262. [PIX_FMT_RGB555] = {
  1263. .convert = yuv420p_to_rgb555
  1264. },
  1265. [PIX_FMT_RGB565] = {
  1266. .convert = yuv420p_to_rgb565
  1267. },
  1268. [PIX_FMT_BGR24] = {
  1269. .convert = yuv420p_to_bgr24
  1270. },
  1271. [PIX_FMT_RGB24] = {
  1272. .convert = yuv420p_to_rgb24
  1273. },
  1274. [PIX_FMT_RGBA32] = {
  1275. .convert = yuv420p_to_rgba32
  1276. },
  1277. },
  1278. [PIX_FMT_YUV422P] = {
  1279. [PIX_FMT_YUV422] = {
  1280. .convert = yuv422p_to_yuv422,
  1281. },
  1282. },
  1283. [PIX_FMT_YUV444P] = {
  1284. [PIX_FMT_RGB24] = {
  1285. .convert = yuv444p_to_rgb24
  1286. },
  1287. },
  1288. [PIX_FMT_YUVJ420P] = {
  1289. [PIX_FMT_RGB555] = {
  1290. .convert = yuvj420p_to_rgb555
  1291. },
  1292. [PIX_FMT_RGB565] = {
  1293. .convert = yuvj420p_to_rgb565
  1294. },
  1295. [PIX_FMT_BGR24] = {
  1296. .convert = yuvj420p_to_bgr24
  1297. },
  1298. [PIX_FMT_RGB24] = {
  1299. .convert = yuvj420p_to_rgb24
  1300. },
  1301. [PIX_FMT_RGBA32] = {
  1302. .convert = yuvj420p_to_rgba32
  1303. },
  1304. },
  1305. [PIX_FMT_YUVJ444P] = {
  1306. [PIX_FMT_RGB24] = {
  1307. .convert = yuvj444p_to_rgb24
  1308. },
  1309. },
  1310. [PIX_FMT_YUV422] = {
  1311. [PIX_FMT_YUV420P] = {
  1312. .convert = yuv422_to_yuv420p,
  1313. },
  1314. [PIX_FMT_YUV422P] = {
  1315. .convert = yuv422_to_yuv422p,
  1316. },
  1317. },
  1318. [PIX_FMT_RGB24] = {
  1319. [PIX_FMT_YUV420P] = {
  1320. .convert = rgb24_to_yuv420p
  1321. },
  1322. [PIX_FMT_RGB565] = {
  1323. .convert = rgb24_to_rgb565
  1324. },
  1325. [PIX_FMT_RGB555] = {
  1326. .convert = rgb24_to_rgb555
  1327. },
  1328. [PIX_FMT_RGBA32] = {
  1329. .convert = rgb24_to_rgba32
  1330. },
  1331. [PIX_FMT_BGR24] = {
  1332. .convert = rgb24_to_bgr24
  1333. },
  1334. [PIX_FMT_GRAY8] = {
  1335. .convert = rgb24_to_gray
  1336. },
  1337. [PIX_FMT_PAL8] = {
  1338. .convert = rgb24_to_pal8
  1339. },
  1340. [PIX_FMT_YUV444P] = {
  1341. .convert = rgb24_to_yuv444p
  1342. },
  1343. [PIX_FMT_YUVJ420P] = {
  1344. .convert = rgb24_to_yuvj420p
  1345. },
  1346. [PIX_FMT_YUVJ444P] = {
  1347. .convert = rgb24_to_yuvj444p
  1348. },
  1349. },
  1350. [PIX_FMT_RGBA32] = {
  1351. [PIX_FMT_RGB24] = {
  1352. .convert = rgba32_to_rgb24
  1353. },
  1354. [PIX_FMT_RGB555] = {
  1355. .convert = rgba32_to_rgb555
  1356. },
  1357. [PIX_FMT_PAL8] = {
  1358. .convert = rgba32_to_pal8
  1359. },
  1360. [PIX_FMT_YUV420P] = {
  1361. .convert = rgba32_to_yuv420p
  1362. },
  1363. [PIX_FMT_GRAY8] = {
  1364. .convert = rgba32_to_gray
  1365. },
  1366. },
  1367. [PIX_FMT_BGR24] = {
  1368. [PIX_FMT_RGB24] = {
  1369. .convert = bgr24_to_rgb24
  1370. },
  1371. [PIX_FMT_YUV420P] = {
  1372. .convert = bgr24_to_yuv420p
  1373. },
  1374. [PIX_FMT_GRAY8] = {
  1375. .convert = bgr24_to_gray
  1376. },
  1377. },
  1378. [PIX_FMT_RGB555] = {
  1379. [PIX_FMT_RGB24] = {
  1380. .convert = rgb555_to_rgb24
  1381. },
  1382. [PIX_FMT_RGBA32] = {
  1383. .convert = rgb555_to_rgba32
  1384. },
  1385. [PIX_FMT_YUV420P] = {
  1386. .convert = rgb555_to_yuv420p
  1387. },
  1388. [PIX_FMT_GRAY8] = {
  1389. .convert = rgb555_to_gray
  1390. },
  1391. },
  1392. [PIX_FMT_RGB565] = {
  1393. [PIX_FMT_RGB24] = {
  1394. .convert = rgb565_to_rgb24
  1395. },
  1396. [PIX_FMT_YUV420P] = {
  1397. .convert = rgb565_to_yuv420p
  1398. },
  1399. [PIX_FMT_GRAY8] = {
  1400. .convert = rgb565_to_gray
  1401. },
  1402. },
  1403. [PIX_FMT_GRAY8] = {
  1404. [PIX_FMT_RGB555] = {
  1405. .convert = gray_to_rgb555
  1406. },
  1407. [PIX_FMT_RGB565] = {
  1408. .convert = gray_to_rgb565
  1409. },
  1410. [PIX_FMT_RGB24] = {
  1411. .convert = gray_to_rgb24
  1412. },
  1413. [PIX_FMT_BGR24] = {
  1414. .convert = gray_to_bgr24
  1415. },
  1416. [PIX_FMT_RGBA32] = {
  1417. .convert = gray_to_rgba32
  1418. },
  1419. [PIX_FMT_MONOWHITE] = {
  1420. .convert = gray_to_monowhite
  1421. },
  1422. [PIX_FMT_MONOBLACK] = {
  1423. .convert = gray_to_monoblack
  1424. },
  1425. },
  1426. [PIX_FMT_MONOWHITE] = {
  1427. [PIX_FMT_GRAY8] = {
  1428. .convert = monowhite_to_gray
  1429. },
  1430. },
  1431. [PIX_FMT_MONOBLACK] = {
  1432. [PIX_FMT_GRAY8] = {
  1433. .convert = monoblack_to_gray
  1434. },
  1435. },
  1436. [PIX_FMT_PAL8] = {
  1437. [PIX_FMT_RGB555] = {
  1438. .convert = pal8_to_rgb555
  1439. },
  1440. [PIX_FMT_RGB565] = {
  1441. .convert = pal8_to_rgb565
  1442. },
  1443. [PIX_FMT_BGR24] = {
  1444. .convert = pal8_to_bgr24
  1445. },
  1446. [PIX_FMT_RGB24] = {
  1447. .convert = pal8_to_rgb24
  1448. },
  1449. [PIX_FMT_RGBA32] = {
  1450. .convert = pal8_to_rgba32
  1451. },
  1452. },
  1453. };
  1454. static int avpicture_alloc(AVPicture *picture,
  1455. int pix_fmt, int width, int height)
  1456. {
  1457. unsigned int size;
  1458. void *ptr;
  1459. size = avpicture_get_size(pix_fmt, width, height);
  1460. if (size < 0)
  1461. goto fail;
  1462. ptr = av_malloc(size);
  1463. if (!ptr)
  1464. goto fail;
  1465. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1466. return 0;
  1467. fail:
  1468. memset(picture, 0, sizeof(AVPicture));
  1469. return -1;
  1470. }
  1471. static void avpicture_free(AVPicture *picture)
  1472. {
  1473. av_free(picture->data[0]);
  1474. }
  1475. /* return true if yuv planar */
  1476. static inline int is_yuv_planar(PixFmtInfo *ps)
  1477. {
  1478. return (ps->color_type == FF_COLOR_YUV ||
  1479. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1480. ps->pixel_type == FF_PIXEL_PLANAR;
  1481. }
  1482. /* XXX: always use linesize. Return -1 if not supported */
  1483. int img_convert(AVPicture *dst, int dst_pix_fmt,
  1484. AVPicture *src, int src_pix_fmt,
  1485. int src_width, int src_height)
  1486. {
  1487. static int inited;
  1488. int i, ret, dst_width, dst_height, int_pix_fmt;
  1489. PixFmtInfo *src_pix, *dst_pix;
  1490. ConvertEntry *ce;
  1491. AVPicture tmp1, *tmp = &tmp1;
  1492. if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
  1493. dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
  1494. return -1;
  1495. if (src_width <= 0 || src_height <= 0)
  1496. return 0;
  1497. if (!inited) {
  1498. inited = 1;
  1499. img_convert_init();
  1500. }
  1501. dst_width = src_width;
  1502. dst_height = src_height;
  1503. dst_pix = &pix_fmt_info[dst_pix_fmt];
  1504. src_pix = &pix_fmt_info[src_pix_fmt];
  1505. if (src_pix_fmt == dst_pix_fmt) {
  1506. /* no conversion needed: just copy */
  1507. img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
  1508. return 0;
  1509. }
  1510. ce = &convert_table[src_pix_fmt][dst_pix_fmt];
  1511. if (ce->convert) {
  1512. /* specific convertion routine */
  1513. ce->convert(dst, src, dst_width, dst_height);
  1514. return 0;
  1515. }
  1516. /* gray to YUV */
  1517. if (is_yuv_planar(dst_pix) &&
  1518. src_pix_fmt == PIX_FMT_GRAY8) {
  1519. int w, h, y;
  1520. uint8_t *d;
  1521. if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
  1522. img_copy_plane(dst->data[0], dst->linesize[0],
  1523. src->data[0], src->linesize[0],
  1524. dst_width, dst_height);
  1525. } else {
  1526. img_apply_table(dst->data[0], dst->linesize[0],
  1527. src->data[0], src->linesize[0],
  1528. dst_width, dst_height,
  1529. y_jpeg_to_ccir);
  1530. }
  1531. /* fill U and V with 128 */
  1532. w = dst_width;
  1533. h = dst_height;
  1534. w >>= dst_pix->x_chroma_shift;
  1535. h >>= dst_pix->y_chroma_shift;
  1536. for(i = 1; i <= 2; i++) {
  1537. d = dst->data[i];
  1538. for(y = 0; y< h; y++) {
  1539. memset(d, 128, w);
  1540. d += dst->linesize[i];
  1541. }
  1542. }
  1543. return 0;
  1544. }
  1545. /* YUV to gray */
  1546. if (is_yuv_planar(src_pix) &&
  1547. dst_pix_fmt == PIX_FMT_GRAY8) {
  1548. if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
  1549. img_copy_plane(dst->data[0], dst->linesize[0],
  1550. src->data[0], src->linesize[0],
  1551. dst_width, dst_height);
  1552. } else {
  1553. img_apply_table(dst->data[0], dst->linesize[0],
  1554. src->data[0], src->linesize[0],
  1555. dst_width, dst_height,
  1556. y_ccir_to_jpeg);
  1557. }
  1558. return 0;
  1559. }
  1560. /* YUV to YUV planar */
  1561. if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
  1562. int x_shift, y_shift, w, h, xy_shift;
  1563. void (*resize_func)(uint8_t *dst, int dst_wrap,
  1564. const uint8_t *src, int src_wrap,
  1565. int width, int height);
  1566. /* compute chroma size of the smallest dimensions */
  1567. w = dst_width;
  1568. h = dst_height;
  1569. if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
  1570. w >>= dst_pix->x_chroma_shift;
  1571. else
  1572. w >>= src_pix->x_chroma_shift;
  1573. if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
  1574. h >>= dst_pix->y_chroma_shift;
  1575. else
  1576. h >>= src_pix->y_chroma_shift;
  1577. x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
  1578. y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
  1579. xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
  1580. /* there must be filters for conversion at least from and to
  1581. YUV444 format */
  1582. switch(xy_shift) {
  1583. case 0x00:
  1584. resize_func = img_copy_plane;
  1585. break;
  1586. case 0x10:
  1587. resize_func = shrink21;
  1588. break;
  1589. case 0x20:
  1590. resize_func = shrink41;
  1591. break;
  1592. case 0x01:
  1593. resize_func = shrink12;
  1594. break;
  1595. case 0x11:
  1596. resize_func = shrink22;
  1597. break;
  1598. case 0x22:
  1599. resize_func = shrink44;
  1600. break;
  1601. case 0xf0:
  1602. resize_func = grow21;
  1603. break;
  1604. case 0xe0:
  1605. resize_func = grow41;
  1606. break;
  1607. case 0xff:
  1608. resize_func = grow22;
  1609. break;
  1610. case 0xee:
  1611. resize_func = grow44;
  1612. break;
  1613. case 0xf1:
  1614. resize_func = conv411;
  1615. break;
  1616. default:
  1617. /* currently not handled */
  1618. goto no_chroma_filter;
  1619. }
  1620. img_copy_plane(dst->data[0], dst->linesize[0],
  1621. src->data[0], src->linesize[0],
  1622. dst_width, dst_height);
  1623. for(i = 1;i <= 2; i++)
  1624. resize_func(dst->data[i], dst->linesize[i],
  1625. src->data[i], src->linesize[i],
  1626. dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
  1627. /* if yuv color space conversion is needed, we do it here on
  1628. the destination image */
  1629. if (dst_pix->color_type != src_pix->color_type) {
  1630. const uint8_t *y_table, *c_table;
  1631. if (dst_pix->color_type == FF_COLOR_YUV) {
  1632. y_table = y_jpeg_to_ccir;
  1633. c_table = c_jpeg_to_ccir;
  1634. } else {
  1635. y_table = y_ccir_to_jpeg;
  1636. c_table = c_ccir_to_jpeg;
  1637. }
  1638. img_apply_table(dst->data[0], dst->linesize[0],
  1639. dst->data[0], dst->linesize[0],
  1640. dst_width, dst_height,
  1641. y_table);
  1642. for(i = 1;i <= 2; i++)
  1643. img_apply_table(dst->data[i], dst->linesize[i],
  1644. dst->data[i], dst->linesize[i],
  1645. dst_width>>dst_pix->x_chroma_shift,
  1646. dst_height>>dst_pix->y_chroma_shift,
  1647. c_table);
  1648. }
  1649. return 0;
  1650. }
  1651. no_chroma_filter:
  1652. /* try to use an intermediate format */
  1653. if (src_pix_fmt == PIX_FMT_YUV422 ||
  1654. dst_pix_fmt == PIX_FMT_YUV422) {
  1655. /* specific case: convert to YUV422P first */
  1656. int_pix_fmt = PIX_FMT_YUV422P;
  1657. } else if ((src_pix->color_type == FF_COLOR_GRAY &&
  1658. src_pix_fmt != PIX_FMT_GRAY8) ||
  1659. (dst_pix->color_type == FF_COLOR_GRAY &&
  1660. dst_pix_fmt != PIX_FMT_GRAY8)) {
  1661. /* gray8 is the normalized format */
  1662. int_pix_fmt = PIX_FMT_GRAY8;
  1663. } else if ((is_yuv_planar(src_pix) &&
  1664. src_pix_fmt != PIX_FMT_YUV444P &&
  1665. src_pix_fmt != PIX_FMT_YUVJ444P)) {
  1666. /* yuv444 is the normalized format */
  1667. if (src_pix->color_type == FF_COLOR_YUV_JPEG)
  1668. int_pix_fmt = PIX_FMT_YUVJ444P;
  1669. else
  1670. int_pix_fmt = PIX_FMT_YUV444P;
  1671. } else if ((is_yuv_planar(dst_pix) &&
  1672. dst_pix_fmt != PIX_FMT_YUV444P &&
  1673. dst_pix_fmt != PIX_FMT_YUVJ444P)) {
  1674. /* yuv444 is the normalized format */
  1675. if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
  1676. int_pix_fmt = PIX_FMT_YUVJ444P;
  1677. else
  1678. int_pix_fmt = PIX_FMT_YUV444P;
  1679. } else {
  1680. /* the two formats are rgb or gray8 or yuv[j]444p */
  1681. if (src_pix->is_alpha && dst_pix->is_alpha)
  1682. int_pix_fmt = PIX_FMT_RGBA32;
  1683. else
  1684. int_pix_fmt = PIX_FMT_RGB24;
  1685. }
  1686. if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1687. return -1;
  1688. ret = -1;
  1689. if (img_convert(tmp, int_pix_fmt,
  1690. src, src_pix_fmt, src_width, src_height) < 0)
  1691. goto fail1;
  1692. if (img_convert(dst, dst_pix_fmt,
  1693. tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1694. goto fail1;
  1695. ret = 0;
  1696. fail1:
  1697. avpicture_free(tmp);
  1698. return ret;
  1699. }
  1700. /* NOTE: we scan all the pixels to have an exact information */
  1701. static int get_alpha_info_pal8(AVPicture *src, int width, int height)
  1702. {
  1703. const unsigned char *p;
  1704. int src_wrap, ret, x, y;
  1705. unsigned int a;
  1706. uint32_t *palette = (uint32_t *)src->data[1];
  1707. p = src->data[0];
  1708. src_wrap = src->linesize[0] - width;
  1709. ret = 0;
  1710. for(y=0;y<height;y++) {
  1711. for(x=0;x<width;x++) {
  1712. a = palette[p[0]] >> 24;
  1713. if (a == 0x00) {
  1714. ret |= FF_ALPHA_TRANSP;
  1715. } else if (a != 0xff) {
  1716. ret |= FF_ALPHA_SEMI_TRANSP;
  1717. }
  1718. p++;
  1719. }
  1720. p += src_wrap;
  1721. }
  1722. return ret;
  1723. }
  1724. /**
  1725. * Tell if an image really has transparent alpha values.
  1726. * @return ored mask of FF_ALPHA_xxx constants
  1727. */
  1728. int img_get_alpha_info(AVPicture *src, int pix_fmt, int width, int height)
  1729. {
  1730. PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1731. int ret;
  1732. pf = &pix_fmt_info[pix_fmt];
  1733. /* no alpha can be represented in format */
  1734. if (!pf->is_alpha)
  1735. return 0;
  1736. switch(pix_fmt) {
  1737. case PIX_FMT_RGBA32:
  1738. ret = get_alpha_info_rgba32(src, width, height);
  1739. break;
  1740. case PIX_FMT_RGB555:
  1741. ret = get_alpha_info_rgb555(src, width, height);
  1742. break;
  1743. case PIX_FMT_PAL8:
  1744. ret = get_alpha_info_pal8(src, width, height);
  1745. break;
  1746. default:
  1747. /* we do not know, so everything is indicated */
  1748. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1749. break;
  1750. }
  1751. return ret;
  1752. }
  1753. #ifdef HAVE_MMX
  1754. #define DEINT_INPLACE_LINE_LUM \
  1755. movd_m2r(lum_m4[0],mm0);\
  1756. movd_m2r(lum_m3[0],mm1);\
  1757. movd_m2r(lum_m2[0],mm2);\
  1758. movd_m2r(lum_m1[0],mm3);\
  1759. movd_m2r(lum[0],mm4);\
  1760. punpcklbw_r2r(mm7,mm0);\
  1761. movd_r2m(mm2,lum_m4[0]);\
  1762. punpcklbw_r2r(mm7,mm1);\
  1763. punpcklbw_r2r(mm7,mm2);\
  1764. punpcklbw_r2r(mm7,mm3);\
  1765. punpcklbw_r2r(mm7,mm4);\
  1766. paddw_r2r(mm3,mm1);\
  1767. psllw_i2r(1,mm2);\
  1768. paddw_r2r(mm4,mm0);\
  1769. psllw_i2r(2,mm1);\
  1770. paddw_r2r(mm6,mm2);\
  1771. paddw_r2r(mm2,mm1);\
  1772. psubusw_r2r(mm0,mm1);\
  1773. psrlw_i2r(3,mm1);\
  1774. packuswb_r2r(mm7,mm1);\
  1775. movd_r2m(mm1,lum_m2[0]);
  1776. #define DEINT_LINE_LUM \
  1777. movd_m2r(lum_m4[0],mm0);\
  1778. movd_m2r(lum_m3[0],mm1);\
  1779. movd_m2r(lum_m2[0],mm2);\
  1780. movd_m2r(lum_m1[0],mm3);\
  1781. movd_m2r(lum[0],mm4);\
  1782. punpcklbw_r2r(mm7,mm0);\
  1783. punpcklbw_r2r(mm7,mm1);\
  1784. punpcklbw_r2r(mm7,mm2);\
  1785. punpcklbw_r2r(mm7,mm3);\
  1786. punpcklbw_r2r(mm7,mm4);\
  1787. paddw_r2r(mm3,mm1);\
  1788. psllw_i2r(1,mm2);\
  1789. paddw_r2r(mm4,mm0);\
  1790. psllw_i2r(2,mm1);\
  1791. paddw_r2r(mm6,mm2);\
  1792. paddw_r2r(mm2,mm1);\
  1793. psubusw_r2r(mm0,mm1);\
  1794. psrlw_i2r(3,mm1);\
  1795. packuswb_r2r(mm7,mm1);\
  1796. movd_r2m(mm1,dst[0]);
  1797. #endif
  1798. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1799. static void deinterlace_line(uint8_t *dst, uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1800. int size)
  1801. {
  1802. #ifndef HAVE_MMX
  1803. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1804. int sum;
  1805. for(;size > 0;size--) {
  1806. sum = -lum_m4[0];
  1807. sum += lum_m3[0] << 2;
  1808. sum += lum_m2[0] << 1;
  1809. sum += lum_m1[0] << 2;
  1810. sum += -lum[0];
  1811. dst[0] = cm[(sum + 4) >> 3];
  1812. lum_m4++;
  1813. lum_m3++;
  1814. lum_m2++;
  1815. lum_m1++;
  1816. lum++;
  1817. dst++;
  1818. }
  1819. #else
  1820. {
  1821. mmx_t rounder;
  1822. rounder.uw[0]=4;
  1823. rounder.uw[1]=4;
  1824. rounder.uw[2]=4;
  1825. rounder.uw[3]=4;
  1826. pxor_r2r(mm7,mm7);
  1827. movq_m2r(rounder,mm6);
  1828. }
  1829. for (;size > 3; size-=4) {
  1830. DEINT_LINE_LUM
  1831. lum_m4+=4;
  1832. lum_m3+=4;
  1833. lum_m2+=4;
  1834. lum_m1+=4;
  1835. lum+=4;
  1836. dst+=4;
  1837. }
  1838. #endif
  1839. }
  1840. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1841. int size)
  1842. {
  1843. #ifndef HAVE_MMX
  1844. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1845. int sum;
  1846. for(;size > 0;size--) {
  1847. sum = -lum_m4[0];
  1848. sum += lum_m3[0] << 2;
  1849. sum += lum_m2[0] << 1;
  1850. lum_m4[0]=lum_m2[0];
  1851. sum += lum_m1[0] << 2;
  1852. sum += -lum[0];
  1853. lum_m2[0] = cm[(sum + 4) >> 3];
  1854. lum_m4++;
  1855. lum_m3++;
  1856. lum_m2++;
  1857. lum_m1++;
  1858. lum++;
  1859. }
  1860. #else
  1861. {
  1862. mmx_t rounder;
  1863. rounder.uw[0]=4;
  1864. rounder.uw[1]=4;
  1865. rounder.uw[2]=4;
  1866. rounder.uw[3]=4;
  1867. pxor_r2r(mm7,mm7);
  1868. movq_m2r(rounder,mm6);
  1869. }
  1870. for (;size > 3; size-=4) {
  1871. DEINT_INPLACE_LINE_LUM
  1872. lum_m4+=4;
  1873. lum_m3+=4;
  1874. lum_m2+=4;
  1875. lum_m1+=4;
  1876. lum+=4;
  1877. }
  1878. #endif
  1879. }
  1880. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1881. top field is copied as is, but the bottom field is deinterlaced
  1882. against the top field. */
  1883. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1884. uint8_t *src1, int src_wrap,
  1885. int width, int height)
  1886. {
  1887. uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1888. int y;
  1889. src_m2 = src1;
  1890. src_m1 = src1;
  1891. src_0=&src_m1[src_wrap];
  1892. src_p1=&src_0[src_wrap];
  1893. src_p2=&src_p1[src_wrap];
  1894. for(y=0;y<(height-2);y+=2) {
  1895. memcpy(dst,src_m1,width);
  1896. dst += dst_wrap;
  1897. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1898. src_m2 = src_0;
  1899. src_m1 = src_p1;
  1900. src_0 = src_p2;
  1901. src_p1 += 2*src_wrap;
  1902. src_p2 += 2*src_wrap;
  1903. dst += dst_wrap;
  1904. }
  1905. memcpy(dst,src_m1,width);
  1906. dst += dst_wrap;
  1907. /* do last line */
  1908. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1909. }
  1910. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1911. int width, int height)
  1912. {
  1913. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1914. int y;
  1915. uint8_t *buf;
  1916. buf = (uint8_t*)av_malloc(width);
  1917. src_m1 = src1;
  1918. memcpy(buf,src_m1,width);
  1919. src_0=&src_m1[src_wrap];
  1920. src_p1=&src_0[src_wrap];
  1921. src_p2=&src_p1[src_wrap];
  1922. for(y=0;y<(height-2);y+=2) {
  1923. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1924. src_m1 = src_p1;
  1925. src_0 = src_p2;
  1926. src_p1 += 2*src_wrap;
  1927. src_p2 += 2*src_wrap;
  1928. }
  1929. /* do last line */
  1930. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1931. av_free(buf);
  1932. }
  1933. /* deinterlace - if not supported return -1 */
  1934. int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
  1935. int pix_fmt, int width, int height)
  1936. {
  1937. int i;
  1938. if (pix_fmt != PIX_FMT_YUV420P &&
  1939. pix_fmt != PIX_FMT_YUV422P &&
  1940. pix_fmt != PIX_FMT_YUV444P)
  1941. return -1;
  1942. if ((width & 3) != 0 || (height & 3) != 0)
  1943. return -1;
  1944. for(i=0;i<3;i++) {
  1945. if (i == 1) {
  1946. switch(pix_fmt) {
  1947. case PIX_FMT_YUV420P:
  1948. width >>= 1;
  1949. height >>= 1;
  1950. break;
  1951. case PIX_FMT_YUV422P:
  1952. width >>= 1;
  1953. break;
  1954. default:
  1955. break;
  1956. }
  1957. }
  1958. if (src == dst) {
  1959. deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
  1960. width, height);
  1961. } else {
  1962. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1963. src->data[i], src->linesize[i],
  1964. width, height);
  1965. }
  1966. }
  1967. #ifdef HAVE_MMX
  1968. emms();
  1969. #endif
  1970. return 0;
  1971. }
  1972. #undef FIX