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.

2159 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. static inline int C_JPEG_TO_CCIR(int y) {
  694. y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
  695. if (y < 16)
  696. y = 16;
  697. return y;
  698. }
  699. #define RGB_TO_Y(r, g, b) \
  700. ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
  701. FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
  702. #define RGB_TO_U(r1, g1, b1, shift)\
  703. (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
  704. FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  705. #define RGB_TO_V(r1, g1, b1, shift)\
  706. (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
  707. FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  708. #define RGB_TO_Y_CCIR(r, g, b) \
  709. ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  710. FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  711. #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
  712. (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
  713. FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  714. #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
  715. (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
  716. FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  717. static uint8_t y_ccir_to_jpeg[256];
  718. static uint8_t y_jpeg_to_ccir[256];
  719. static uint8_t c_ccir_to_jpeg[256];
  720. static uint8_t c_jpeg_to_ccir[256];
  721. /* init various conversion tables */
  722. static void img_convert_init(void)
  723. {
  724. int i;
  725. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  726. for(i = 0;i < 256; i++) {
  727. y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
  728. y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
  729. c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
  730. c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
  731. }
  732. }
  733. /* apply to each pixel the given table */
  734. static void img_apply_table(uint8_t *dst, int dst_wrap,
  735. const uint8_t *src, int src_wrap,
  736. int width, int height, const uint8_t *table1)
  737. {
  738. int n;
  739. const uint8_t *s;
  740. uint8_t *d;
  741. const uint8_t *table;
  742. table = table1;
  743. for(;height > 0; height--) {
  744. s = src;
  745. d = dst;
  746. n = width;
  747. while (n >= 4) {
  748. d[0] = table[s[0]];
  749. d[1] = table[s[1]];
  750. d[2] = table[s[2]];
  751. d[3] = table[s[3]];
  752. d += 4;
  753. s += 4;
  754. n -= 4;
  755. }
  756. while (n > 0) {
  757. d[0] = table[s[0]];
  758. d++;
  759. s++;
  760. n--;
  761. }
  762. dst += dst_wrap;
  763. src += src_wrap;
  764. }
  765. }
  766. /* XXX: use generic filter ? */
  767. /* XXX: in most cases, the sampling position is incorrect */
  768. /* 4x1 -> 1x1 */
  769. static void shrink41(uint8_t *dst, int dst_wrap,
  770. const uint8_t *src, int src_wrap,
  771. int width, int height)
  772. {
  773. int w;
  774. const uint8_t *s;
  775. uint8_t *d;
  776. for(;height > 0; height--) {
  777. s = src;
  778. d = dst;
  779. for(w = width;w > 0; w--) {
  780. d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
  781. s += 4;
  782. d++;
  783. }
  784. src += src_wrap;
  785. dst += dst_wrap;
  786. }
  787. }
  788. /* 2x1 -> 1x1 */
  789. static void shrink21(uint8_t *dst, int dst_wrap,
  790. const uint8_t *src, int src_wrap,
  791. int width, int height)
  792. {
  793. int w;
  794. const uint8_t *s;
  795. uint8_t *d;
  796. for(;height > 0; height--) {
  797. s = src;
  798. d = dst;
  799. for(w = width;w > 0; w--) {
  800. d[0] = (s[0] + s[1]) >> 1;
  801. s += 2;
  802. d++;
  803. }
  804. src += src_wrap;
  805. dst += dst_wrap;
  806. }
  807. }
  808. /* 1x2 -> 1x1 */
  809. static void shrink12(uint8_t *dst, int dst_wrap,
  810. const uint8_t *src, int src_wrap,
  811. int width, int height)
  812. {
  813. int w;
  814. uint8_t *d;
  815. const uint8_t *s1, *s2;
  816. for(;height > 0; height--) {
  817. s1 = src;
  818. s2 = s1 + src_wrap;
  819. d = dst;
  820. for(w = width;w >= 4; w-=4) {
  821. d[0] = (s1[0] + s2[0]) >> 1;
  822. d[1] = (s1[1] + s2[1]) >> 1;
  823. d[2] = (s1[2] + s2[2]) >> 1;
  824. d[3] = (s1[3] + s2[3]) >> 1;
  825. s1 += 4;
  826. s2 += 4;
  827. d += 4;
  828. }
  829. for(;w > 0; w--) {
  830. d[0] = (s1[0] + s2[0]) >> 1;
  831. s1++;
  832. s2++;
  833. d++;
  834. }
  835. src += 2 * src_wrap;
  836. dst += dst_wrap;
  837. }
  838. }
  839. /* 2x2 -> 1x1 */
  840. static void shrink22(uint8_t *dst, int dst_wrap,
  841. const uint8_t *src, int src_wrap,
  842. int width, int height)
  843. {
  844. int w;
  845. const uint8_t *s1, *s2;
  846. uint8_t *d;
  847. for(;height > 0; height--) {
  848. s1 = src;
  849. s2 = s1 + src_wrap;
  850. d = dst;
  851. for(w = width;w >= 4; w-=4) {
  852. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  853. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  854. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  855. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  856. s1 += 8;
  857. s2 += 8;
  858. d += 4;
  859. }
  860. for(;w > 0; w--) {
  861. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  862. s1 += 2;
  863. s2 += 2;
  864. d++;
  865. }
  866. src += 2 * src_wrap;
  867. dst += dst_wrap;
  868. }
  869. }
  870. /* 4x4 -> 1x1 */
  871. static void shrink44(uint8_t *dst, int dst_wrap,
  872. const uint8_t *src, int src_wrap,
  873. int width, int height)
  874. {
  875. int w;
  876. const uint8_t *s1, *s2, *s3, *s4;
  877. uint8_t *d;
  878. for(;height > 0; height--) {
  879. s1 = src;
  880. s2 = s1 + src_wrap;
  881. s3 = s2 + src_wrap;
  882. s4 = s3 + src_wrap;
  883. d = dst;
  884. for(w = width;w > 0; w--) {
  885. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  886. s2[0] + s2[1] + s2[2] + s2[3] +
  887. s3[0] + s3[1] + s3[2] + s3[3] +
  888. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  889. s1 += 4;
  890. s2 += 4;
  891. s3 += 4;
  892. s4 += 4;
  893. d++;
  894. }
  895. src += 4 * src_wrap;
  896. dst += dst_wrap;
  897. }
  898. }
  899. static void grow21_line(uint8_t *dst, const uint8_t *src,
  900. int width)
  901. {
  902. int w;
  903. const uint8_t *s1;
  904. uint8_t *d;
  905. s1 = src;
  906. d = dst;
  907. for(w = width;w >= 4; w-=4) {
  908. d[1] = d[0] = s1[0];
  909. d[3] = d[2] = s1[1];
  910. s1 += 2;
  911. d += 4;
  912. }
  913. for(;w >= 2; w -= 2) {
  914. d[1] = d[0] = s1[0];
  915. s1 ++;
  916. d += 2;
  917. }
  918. /* only needed if width is not a multiple of two */
  919. /* XXX: veryfy that */
  920. if (w) {
  921. d[0] = s1[0];
  922. }
  923. }
  924. static void grow41_line(uint8_t *dst, const uint8_t *src,
  925. int width)
  926. {
  927. int w, v;
  928. const uint8_t *s1;
  929. uint8_t *d;
  930. s1 = src;
  931. d = dst;
  932. for(w = width;w >= 4; w-=4) {
  933. v = s1[0];
  934. d[0] = v;
  935. d[1] = v;
  936. d[2] = v;
  937. d[3] = v;
  938. s1 ++;
  939. d += 4;
  940. }
  941. }
  942. /* 1x1 -> 2x1 */
  943. static void grow21(uint8_t *dst, int dst_wrap,
  944. const uint8_t *src, int src_wrap,
  945. int width, int height)
  946. {
  947. for(;height > 0; height--) {
  948. grow21_line(dst, src, width);
  949. src += src_wrap;
  950. dst += dst_wrap;
  951. }
  952. }
  953. /* 1x1 -> 2x2 */
  954. static void grow22(uint8_t *dst, int dst_wrap,
  955. const uint8_t *src, int src_wrap,
  956. int width, int height)
  957. {
  958. for(;height > 0; height--) {
  959. grow21_line(dst, src, width);
  960. if (height%2)
  961. src += src_wrap;
  962. dst += dst_wrap;
  963. }
  964. }
  965. /* 1x1 -> 4x1 */
  966. static void grow41(uint8_t *dst, int dst_wrap,
  967. const uint8_t *src, int src_wrap,
  968. int width, int height)
  969. {
  970. for(;height > 0; height--) {
  971. grow41_line(dst, src, width);
  972. src += src_wrap;
  973. dst += dst_wrap;
  974. }
  975. }
  976. /* 1x1 -> 4x4 */
  977. static void grow44(uint8_t *dst, int dst_wrap,
  978. const uint8_t *src, int src_wrap,
  979. int width, int height)
  980. {
  981. for(;height > 0; height--) {
  982. grow41_line(dst, src, width);
  983. if ((height & 3) == 1)
  984. src += src_wrap;
  985. dst += dst_wrap;
  986. }
  987. }
  988. /* 1x2 -> 2x1 */
  989. static void conv411(uint8_t *dst, int dst_wrap,
  990. const uint8_t *src, int src_wrap,
  991. int width, int height)
  992. {
  993. int w, c;
  994. const uint8_t *s1, *s2;
  995. uint8_t *d;
  996. width>>=1;
  997. for(;height > 0; height--) {
  998. s1 = src;
  999. s2 = src + src_wrap;
  1000. d = dst;
  1001. for(w = width;w > 0; w--) {
  1002. c = (s1[0] + s2[0]) >> 1;
  1003. d[0] = c;
  1004. d[1] = c;
  1005. s1++;
  1006. s2++;
  1007. d += 2;
  1008. }
  1009. src += src_wrap * 2;
  1010. dst += dst_wrap;
  1011. }
  1012. }
  1013. /* XXX: add jpeg quantize code */
  1014. #define TRANSP_INDEX (6*6*6)
  1015. /* this is maybe slow, but allows for extensions */
  1016. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  1017. {
  1018. return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
  1019. }
  1020. static void build_rgb_palette(uint8_t *palette, int has_alpha)
  1021. {
  1022. uint32_t *pal;
  1023. static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
  1024. int i, r, g, b;
  1025. pal = (uint32_t *)palette;
  1026. i = 0;
  1027. for(r = 0; r < 6; r++) {
  1028. for(g = 0; g < 6; g++) {
  1029. for(b = 0; b < 6; b++) {
  1030. pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
  1031. (pal_value[g] << 8) | pal_value[b];
  1032. }
  1033. }
  1034. }
  1035. if (has_alpha)
  1036. pal[i++] = 0;
  1037. while (i < 256)
  1038. pal[i++] = 0xff000000;
  1039. }
  1040. /* copy bit n to bits 0 ... n - 1 */
  1041. static inline unsigned int bitcopy_n(unsigned int a, int n)
  1042. {
  1043. int mask;
  1044. mask = (1 << n) - 1;
  1045. return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
  1046. }
  1047. /* rgb555 handling */
  1048. #define RGB_NAME rgb555
  1049. #define RGB_IN(r, g, b, s)\
  1050. {\
  1051. unsigned int v = ((const uint16_t *)(s))[0];\
  1052. r = bitcopy_n(v >> (10 - 3), 3);\
  1053. g = bitcopy_n(v >> (5 - 3), 3);\
  1054. b = bitcopy_n(v << 3, 3);\
  1055. }
  1056. #define RGBA_IN(r, g, b, a, s)\
  1057. {\
  1058. unsigned int v = ((const uint16_t *)(s))[0];\
  1059. r = bitcopy_n(v >> (10 - 3), 3);\
  1060. g = bitcopy_n(v >> (5 - 3), 3);\
  1061. b = bitcopy_n(v << 3, 3);\
  1062. a = (-(v >> 15)) & 0xff;\
  1063. }
  1064. #define RGBA_OUT(d, r, g, b, a)\
  1065. {\
  1066. ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
  1067. ((a << 8) & 0x8000);\
  1068. }
  1069. #define BPP 2
  1070. #include "imgconvert_template.h"
  1071. /* rgb565 handling */
  1072. #define RGB_NAME rgb565
  1073. #define RGB_IN(r, g, b, s)\
  1074. {\
  1075. unsigned int v = ((const uint16_t *)(s))[0];\
  1076. r = bitcopy_n(v >> (11 - 3), 3);\
  1077. g = bitcopy_n(v >> (5 - 2), 2);\
  1078. b = bitcopy_n(v << 3, 3);\
  1079. }
  1080. #define RGB_OUT(d, r, g, b)\
  1081. {\
  1082. ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
  1083. }
  1084. #define BPP 2
  1085. #include "imgconvert_template.h"
  1086. /* bgr24 handling */
  1087. #define RGB_NAME bgr24
  1088. #define RGB_IN(r, g, b, s)\
  1089. {\
  1090. b = (s)[0];\
  1091. g = (s)[1];\
  1092. r = (s)[2];\
  1093. }
  1094. #define RGB_OUT(d, r, g, b)\
  1095. {\
  1096. (d)[0] = b;\
  1097. (d)[1] = g;\
  1098. (d)[2] = r;\
  1099. }
  1100. #define BPP 3
  1101. #include "imgconvert_template.h"
  1102. #undef RGB_IN
  1103. #undef RGB_OUT
  1104. #undef BPP
  1105. /* rgb24 handling */
  1106. #define RGB_NAME rgb24
  1107. #define FMT_RGB24
  1108. #define RGB_IN(r, g, b, s)\
  1109. {\
  1110. r = (s)[0];\
  1111. g = (s)[1];\
  1112. b = (s)[2];\
  1113. }
  1114. #define RGB_OUT(d, r, g, b)\
  1115. {\
  1116. (d)[0] = r;\
  1117. (d)[1] = g;\
  1118. (d)[2] = b;\
  1119. }
  1120. #define BPP 3
  1121. #include "imgconvert_template.h"
  1122. /* rgba32 handling */
  1123. #define RGB_NAME rgba32
  1124. #define FMT_RGBA32
  1125. #define RGB_IN(r, g, b, s)\
  1126. {\
  1127. unsigned int v = ((const uint32_t *)(s))[0];\
  1128. r = (v >> 16) & 0xff;\
  1129. g = (v >> 8) & 0xff;\
  1130. b = v & 0xff;\
  1131. }
  1132. #define RGBA_IN(r, g, b, a, s)\
  1133. {\
  1134. unsigned int v = ((const uint32_t *)(s))[0];\
  1135. a = (v >> 24) & 0xff;\
  1136. r = (v >> 16) & 0xff;\
  1137. g = (v >> 8) & 0xff;\
  1138. b = v & 0xff;\
  1139. }
  1140. #define RGBA_OUT(d, r, g, b, a)\
  1141. {\
  1142. ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
  1143. }
  1144. #define BPP 4
  1145. #include "imgconvert_template.h"
  1146. static void mono_to_gray(AVPicture *dst, AVPicture *src,
  1147. int width, int height, int xor_mask)
  1148. {
  1149. const unsigned char *p;
  1150. unsigned char *q;
  1151. int v, dst_wrap, src_wrap;
  1152. int y, w;
  1153. p = src->data[0];
  1154. src_wrap = src->linesize[0] - ((width + 7) >> 3);
  1155. q = dst->data[0];
  1156. dst_wrap = dst->linesize[0] - width;
  1157. for(y=0;y<height;y++) {
  1158. w = width;
  1159. while (w >= 8) {
  1160. v = *p++ ^ xor_mask;
  1161. q[0] = -(v >> 7);
  1162. q[1] = -((v >> 6) & 1);
  1163. q[2] = -((v >> 5) & 1);
  1164. q[3] = -((v >> 4) & 1);
  1165. q[4] = -((v >> 3) & 1);
  1166. q[5] = -((v >> 2) & 1);
  1167. q[6] = -((v >> 1) & 1);
  1168. q[7] = -((v >> 0) & 1);
  1169. w -= 8;
  1170. q += 8;
  1171. }
  1172. if (w > 0) {
  1173. v = *p++ ^ xor_mask;
  1174. do {
  1175. q[0] = -((v >> 7) & 1);
  1176. q++;
  1177. v <<= 1;
  1178. } while (--w);
  1179. }
  1180. p += src_wrap;
  1181. q += dst_wrap;
  1182. }
  1183. }
  1184. static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
  1185. int width, int height)
  1186. {
  1187. mono_to_gray(dst, src, width, height, 0xff);
  1188. }
  1189. static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
  1190. int width, int height)
  1191. {
  1192. mono_to_gray(dst, src, width, height, 0x00);
  1193. }
  1194. static void gray_to_mono(AVPicture *dst, AVPicture *src,
  1195. int width, int height, int xor_mask)
  1196. {
  1197. int n;
  1198. const uint8_t *s;
  1199. uint8_t *d;
  1200. int j, b, v, n1, src_wrap, dst_wrap, y;
  1201. s = src->data[0];
  1202. src_wrap = src->linesize[0] - width;
  1203. d = dst->data[0];
  1204. dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
  1205. for(y=0;y<height;y++) {
  1206. n = width;
  1207. while (n >= 8) {
  1208. v = 0;
  1209. for(j=0;j<8;j++) {
  1210. b = s[0];
  1211. s++;
  1212. v = (v << 1) | (b >> 7);
  1213. }
  1214. d[0] = v ^ xor_mask;
  1215. d++;
  1216. n -= 8;
  1217. }
  1218. if (n > 0) {
  1219. n1 = n;
  1220. v = 0;
  1221. while (n > 0) {
  1222. b = s[0];
  1223. s++;
  1224. v = (v << 1) | (b >> 7);
  1225. n--;
  1226. }
  1227. d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
  1228. d++;
  1229. }
  1230. s += src_wrap;
  1231. d += dst_wrap;
  1232. }
  1233. }
  1234. static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
  1235. int width, int height)
  1236. {
  1237. gray_to_mono(dst, src, width, height, 0xff);
  1238. }
  1239. static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
  1240. int width, int height)
  1241. {
  1242. gray_to_mono(dst, src, width, height, 0x00);
  1243. }
  1244. typedef struct ConvertEntry {
  1245. void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
  1246. } ConvertEntry;
  1247. /* Add each new convertion function in this table. In order to be able
  1248. to convert from any format to any format, the following constraints
  1249. must be satisfied:
  1250. - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
  1251. - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
  1252. - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
  1253. - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
  1254. PIX_FMT_RGB24.
  1255. - PIX_FMT_422 must convert to and from PIX_FMT_422P.
  1256. The other conversion functions are just optimisations for common cases.
  1257. */
  1258. static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
  1259. [PIX_FMT_YUV420P] = {
  1260. [PIX_FMT_RGB555] = {
  1261. .convert = yuv420p_to_rgb555
  1262. },
  1263. [PIX_FMT_RGB565] = {
  1264. .convert = yuv420p_to_rgb565
  1265. },
  1266. [PIX_FMT_BGR24] = {
  1267. .convert = yuv420p_to_bgr24
  1268. },
  1269. [PIX_FMT_RGB24] = {
  1270. .convert = yuv420p_to_rgb24
  1271. },
  1272. [PIX_FMT_RGBA32] = {
  1273. .convert = yuv420p_to_rgba32
  1274. },
  1275. },
  1276. [PIX_FMT_YUV422P] = {
  1277. [PIX_FMT_YUV422] = {
  1278. .convert = yuv422p_to_yuv422,
  1279. },
  1280. },
  1281. [PIX_FMT_YUV444P] = {
  1282. [PIX_FMT_RGB24] = {
  1283. .convert = yuv444p_to_rgb24
  1284. },
  1285. },
  1286. [PIX_FMT_YUVJ420P] = {
  1287. [PIX_FMT_RGB555] = {
  1288. .convert = yuvj420p_to_rgb555
  1289. },
  1290. [PIX_FMT_RGB565] = {
  1291. .convert = yuvj420p_to_rgb565
  1292. },
  1293. [PIX_FMT_BGR24] = {
  1294. .convert = yuvj420p_to_bgr24
  1295. },
  1296. [PIX_FMT_RGB24] = {
  1297. .convert = yuvj420p_to_rgb24
  1298. },
  1299. [PIX_FMT_RGBA32] = {
  1300. .convert = yuvj420p_to_rgba32
  1301. },
  1302. },
  1303. [PIX_FMT_YUVJ444P] = {
  1304. [PIX_FMT_RGB24] = {
  1305. .convert = yuvj444p_to_rgb24
  1306. },
  1307. },
  1308. [PIX_FMT_YUV422] = {
  1309. [PIX_FMT_YUV420P] = {
  1310. .convert = yuv422_to_yuv420p,
  1311. },
  1312. [PIX_FMT_YUV422P] = {
  1313. .convert = yuv422_to_yuv422p,
  1314. },
  1315. },
  1316. [PIX_FMT_RGB24] = {
  1317. [PIX_FMT_YUV420P] = {
  1318. .convert = rgb24_to_yuv420p
  1319. },
  1320. [PIX_FMT_RGB565] = {
  1321. .convert = rgb24_to_rgb565
  1322. },
  1323. [PIX_FMT_RGB555] = {
  1324. .convert = rgb24_to_rgb555
  1325. },
  1326. [PIX_FMT_RGBA32] = {
  1327. .convert = rgb24_to_rgba32
  1328. },
  1329. [PIX_FMT_BGR24] = {
  1330. .convert = rgb24_to_bgr24
  1331. },
  1332. [PIX_FMT_GRAY8] = {
  1333. .convert = rgb24_to_gray
  1334. },
  1335. [PIX_FMT_PAL8] = {
  1336. .convert = rgb24_to_pal8
  1337. },
  1338. [PIX_FMT_YUV444P] = {
  1339. .convert = rgb24_to_yuv444p
  1340. },
  1341. [PIX_FMT_YUVJ420P] = {
  1342. .convert = rgb24_to_yuvj420p
  1343. },
  1344. [PIX_FMT_YUVJ444P] = {
  1345. .convert = rgb24_to_yuvj444p
  1346. },
  1347. },
  1348. [PIX_FMT_RGBA32] = {
  1349. [PIX_FMT_RGB24] = {
  1350. .convert = rgba32_to_rgb24
  1351. },
  1352. [PIX_FMT_RGB555] = {
  1353. .convert = rgba32_to_rgb555
  1354. },
  1355. [PIX_FMT_PAL8] = {
  1356. .convert = rgba32_to_pal8
  1357. },
  1358. [PIX_FMT_YUV420P] = {
  1359. .convert = rgba32_to_yuv420p
  1360. },
  1361. [PIX_FMT_GRAY8] = {
  1362. .convert = rgba32_to_gray
  1363. },
  1364. },
  1365. [PIX_FMT_BGR24] = {
  1366. [PIX_FMT_RGB24] = {
  1367. .convert = bgr24_to_rgb24
  1368. },
  1369. [PIX_FMT_YUV420P] = {
  1370. .convert = bgr24_to_yuv420p
  1371. },
  1372. [PIX_FMT_GRAY8] = {
  1373. .convert = bgr24_to_gray
  1374. },
  1375. },
  1376. [PIX_FMT_RGB555] = {
  1377. [PIX_FMT_RGB24] = {
  1378. .convert = rgb555_to_rgb24
  1379. },
  1380. [PIX_FMT_RGBA32] = {
  1381. .convert = rgb555_to_rgba32
  1382. },
  1383. [PIX_FMT_YUV420P] = {
  1384. .convert = rgb555_to_yuv420p
  1385. },
  1386. [PIX_FMT_GRAY8] = {
  1387. .convert = rgb555_to_gray
  1388. },
  1389. },
  1390. [PIX_FMT_RGB565] = {
  1391. [PIX_FMT_RGB24] = {
  1392. .convert = rgb565_to_rgb24
  1393. },
  1394. [PIX_FMT_YUV420P] = {
  1395. .convert = rgb565_to_yuv420p
  1396. },
  1397. [PIX_FMT_GRAY8] = {
  1398. .convert = rgb565_to_gray
  1399. },
  1400. },
  1401. [PIX_FMT_GRAY8] = {
  1402. [PIX_FMT_RGB555] = {
  1403. .convert = gray_to_rgb555
  1404. },
  1405. [PIX_FMT_RGB565] = {
  1406. .convert = gray_to_rgb565
  1407. },
  1408. [PIX_FMT_RGB24] = {
  1409. .convert = gray_to_rgb24
  1410. },
  1411. [PIX_FMT_BGR24] = {
  1412. .convert = gray_to_bgr24
  1413. },
  1414. [PIX_FMT_RGBA32] = {
  1415. .convert = gray_to_rgba32
  1416. },
  1417. [PIX_FMT_MONOWHITE] = {
  1418. .convert = gray_to_monowhite
  1419. },
  1420. [PIX_FMT_MONOBLACK] = {
  1421. .convert = gray_to_monoblack
  1422. },
  1423. },
  1424. [PIX_FMT_MONOWHITE] = {
  1425. [PIX_FMT_GRAY8] = {
  1426. .convert = monowhite_to_gray
  1427. },
  1428. },
  1429. [PIX_FMT_MONOBLACK] = {
  1430. [PIX_FMT_GRAY8] = {
  1431. .convert = monoblack_to_gray
  1432. },
  1433. },
  1434. [PIX_FMT_PAL8] = {
  1435. [PIX_FMT_RGB555] = {
  1436. .convert = pal8_to_rgb555
  1437. },
  1438. [PIX_FMT_RGB565] = {
  1439. .convert = pal8_to_rgb565
  1440. },
  1441. [PIX_FMT_BGR24] = {
  1442. .convert = pal8_to_bgr24
  1443. },
  1444. [PIX_FMT_RGB24] = {
  1445. .convert = pal8_to_rgb24
  1446. },
  1447. [PIX_FMT_RGBA32] = {
  1448. .convert = pal8_to_rgba32
  1449. },
  1450. },
  1451. };
  1452. static int avpicture_alloc(AVPicture *picture,
  1453. int pix_fmt, int width, int height)
  1454. {
  1455. unsigned int size;
  1456. void *ptr;
  1457. size = avpicture_get_size(pix_fmt, width, height);
  1458. ptr = av_malloc(size);
  1459. if (!ptr)
  1460. goto fail;
  1461. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1462. return 0;
  1463. fail:
  1464. memset(picture, 0, sizeof(AVPicture));
  1465. return -1;
  1466. }
  1467. static void avpicture_free(AVPicture *picture)
  1468. {
  1469. av_free(picture->data[0]);
  1470. }
  1471. /* return true if yuv planar */
  1472. static inline int is_yuv_planar(PixFmtInfo *ps)
  1473. {
  1474. return (ps->color_type == FF_COLOR_YUV ||
  1475. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1476. ps->pixel_type == FF_PIXEL_PLANAR;
  1477. }
  1478. /* XXX: always use linesize. Return -1 if not supported */
  1479. int img_convert(AVPicture *dst, int dst_pix_fmt,
  1480. AVPicture *src, int src_pix_fmt,
  1481. int src_width, int src_height)
  1482. {
  1483. static int inited;
  1484. int i, ret, dst_width, dst_height, int_pix_fmt;
  1485. PixFmtInfo *src_pix, *dst_pix;
  1486. ConvertEntry *ce;
  1487. AVPicture tmp1, *tmp = &tmp1;
  1488. if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
  1489. dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
  1490. return -1;
  1491. if (src_width <= 0 || src_height <= 0)
  1492. return 0;
  1493. if (!inited) {
  1494. inited = 1;
  1495. img_convert_init();
  1496. }
  1497. dst_width = src_width;
  1498. dst_height = src_height;
  1499. dst_pix = &pix_fmt_info[dst_pix_fmt];
  1500. src_pix = &pix_fmt_info[src_pix_fmt];
  1501. if (src_pix_fmt == dst_pix_fmt) {
  1502. /* no conversion needed: just copy */
  1503. img_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
  1504. return 0;
  1505. }
  1506. ce = &convert_table[src_pix_fmt][dst_pix_fmt];
  1507. if (ce->convert) {
  1508. /* specific convertion routine */
  1509. ce->convert(dst, src, dst_width, dst_height);
  1510. return 0;
  1511. }
  1512. /* gray to YUV */
  1513. if (is_yuv_planar(dst_pix) &&
  1514. src_pix_fmt == PIX_FMT_GRAY8) {
  1515. int w, h, y;
  1516. uint8_t *d;
  1517. if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
  1518. img_copy_plane(dst->data[0], dst->linesize[0],
  1519. src->data[0], src->linesize[0],
  1520. dst_width, dst_height);
  1521. } else {
  1522. img_apply_table(dst->data[0], dst->linesize[0],
  1523. src->data[0], src->linesize[0],
  1524. dst_width, dst_height,
  1525. y_jpeg_to_ccir);
  1526. }
  1527. /* fill U and V with 128 */
  1528. w = dst_width;
  1529. h = dst_height;
  1530. w >>= dst_pix->x_chroma_shift;
  1531. h >>= dst_pix->y_chroma_shift;
  1532. for(i = 1; i <= 2; i++) {
  1533. d = dst->data[i];
  1534. for(y = 0; y< h; y++) {
  1535. memset(d, 128, w);
  1536. d += dst->linesize[i];
  1537. }
  1538. }
  1539. return 0;
  1540. }
  1541. /* YUV to gray */
  1542. if (is_yuv_planar(src_pix) &&
  1543. dst_pix_fmt == PIX_FMT_GRAY8) {
  1544. if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
  1545. img_copy_plane(dst->data[0], dst->linesize[0],
  1546. src->data[0], src->linesize[0],
  1547. dst_width, dst_height);
  1548. } else {
  1549. img_apply_table(dst->data[0], dst->linesize[0],
  1550. src->data[0], src->linesize[0],
  1551. dst_width, dst_height,
  1552. y_ccir_to_jpeg);
  1553. }
  1554. return 0;
  1555. }
  1556. /* YUV to YUV planar */
  1557. if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
  1558. int x_shift, y_shift, w, h, xy_shift;
  1559. void (*resize_func)(uint8_t *dst, int dst_wrap,
  1560. const uint8_t *src, int src_wrap,
  1561. int width, int height);
  1562. /* compute chroma size of the smallest dimensions */
  1563. w = dst_width;
  1564. h = dst_height;
  1565. if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
  1566. w >>= dst_pix->x_chroma_shift;
  1567. else
  1568. w >>= src_pix->x_chroma_shift;
  1569. if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
  1570. h >>= dst_pix->y_chroma_shift;
  1571. else
  1572. h >>= src_pix->y_chroma_shift;
  1573. x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
  1574. y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
  1575. xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
  1576. /* there must be filters for conversion at least from and to
  1577. YUV444 format */
  1578. switch(xy_shift) {
  1579. case 0x00:
  1580. resize_func = img_copy_plane;
  1581. break;
  1582. case 0x10:
  1583. resize_func = shrink21;
  1584. break;
  1585. case 0x20:
  1586. resize_func = shrink41;
  1587. break;
  1588. case 0x01:
  1589. resize_func = shrink12;
  1590. break;
  1591. case 0x11:
  1592. resize_func = shrink22;
  1593. break;
  1594. case 0x22:
  1595. resize_func = shrink44;
  1596. break;
  1597. case 0xf0:
  1598. resize_func = grow21;
  1599. break;
  1600. case 0xe0:
  1601. resize_func = grow41;
  1602. break;
  1603. case 0xff:
  1604. resize_func = grow22;
  1605. break;
  1606. case 0xee:
  1607. resize_func = grow44;
  1608. break;
  1609. case 0xf1:
  1610. resize_func = conv411;
  1611. break;
  1612. default:
  1613. /* currently not handled */
  1614. goto no_chroma_filter;
  1615. }
  1616. img_copy_plane(dst->data[0], dst->linesize[0],
  1617. src->data[0], src->linesize[0],
  1618. dst_width, dst_height);
  1619. for(i = 1;i <= 2; i++)
  1620. resize_func(dst->data[i], dst->linesize[i],
  1621. src->data[i], src->linesize[i],
  1622. dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
  1623. /* if yuv color space conversion is needed, we do it here on
  1624. the destination image */
  1625. if (dst_pix->color_type != src_pix->color_type) {
  1626. const uint8_t *y_table, *c_table;
  1627. if (dst_pix->color_type == FF_COLOR_YUV) {
  1628. y_table = y_jpeg_to_ccir;
  1629. c_table = c_jpeg_to_ccir;
  1630. } else {
  1631. y_table = y_ccir_to_jpeg;
  1632. c_table = c_ccir_to_jpeg;
  1633. }
  1634. img_apply_table(dst->data[0], dst->linesize[0],
  1635. dst->data[0], dst->linesize[0],
  1636. dst_width, dst_height,
  1637. y_table);
  1638. for(i = 1;i <= 2; i++)
  1639. img_apply_table(dst->data[i], dst->linesize[i],
  1640. dst->data[i], dst->linesize[i],
  1641. dst_width>>dst_pix->x_chroma_shift,
  1642. dst_height>>dst_pix->y_chroma_shift,
  1643. c_table);
  1644. }
  1645. return 0;
  1646. }
  1647. no_chroma_filter:
  1648. /* try to use an intermediate format */
  1649. if (src_pix_fmt == PIX_FMT_YUV422 ||
  1650. dst_pix_fmt == PIX_FMT_YUV422) {
  1651. /* specific case: convert to YUV422P first */
  1652. int_pix_fmt = PIX_FMT_YUV422P;
  1653. } else if ((src_pix->color_type == FF_COLOR_GRAY &&
  1654. src_pix_fmt != PIX_FMT_GRAY8) ||
  1655. (dst_pix->color_type == FF_COLOR_GRAY &&
  1656. dst_pix_fmt != PIX_FMT_GRAY8)) {
  1657. /* gray8 is the normalized format */
  1658. int_pix_fmt = PIX_FMT_GRAY8;
  1659. } else if ((is_yuv_planar(src_pix) &&
  1660. src_pix_fmt != PIX_FMT_YUV444P &&
  1661. src_pix_fmt != PIX_FMT_YUVJ444P)) {
  1662. /* yuv444 is the normalized format */
  1663. if (src_pix->color_type == FF_COLOR_YUV_JPEG)
  1664. int_pix_fmt = PIX_FMT_YUVJ444P;
  1665. else
  1666. int_pix_fmt = PIX_FMT_YUV444P;
  1667. } else if ((is_yuv_planar(dst_pix) &&
  1668. dst_pix_fmt != PIX_FMT_YUV444P &&
  1669. dst_pix_fmt != PIX_FMT_YUVJ444P)) {
  1670. /* yuv444 is the normalized format */
  1671. if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
  1672. int_pix_fmt = PIX_FMT_YUVJ444P;
  1673. else
  1674. int_pix_fmt = PIX_FMT_YUV444P;
  1675. } else {
  1676. /* the two formats are rgb or gray8 or yuv[j]444p */
  1677. if (src_pix->is_alpha && dst_pix->is_alpha)
  1678. int_pix_fmt = PIX_FMT_RGBA32;
  1679. else
  1680. int_pix_fmt = PIX_FMT_RGB24;
  1681. }
  1682. if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1683. return -1;
  1684. ret = -1;
  1685. if (img_convert(tmp, int_pix_fmt,
  1686. src, src_pix_fmt, src_width, src_height) < 0)
  1687. goto fail1;
  1688. if (img_convert(dst, dst_pix_fmt,
  1689. tmp, int_pix_fmt, dst_width, dst_height) < 0)
  1690. goto fail1;
  1691. ret = 0;
  1692. fail1:
  1693. avpicture_free(tmp);
  1694. return ret;
  1695. }
  1696. /* NOTE: we scan all the pixels to have an exact information */
  1697. static int get_alpha_info_pal8(AVPicture *src, int width, int height)
  1698. {
  1699. const unsigned char *p;
  1700. int src_wrap, ret, x, y;
  1701. unsigned int a;
  1702. uint32_t *palette = (uint32_t *)src->data[1];
  1703. p = src->data[0];
  1704. src_wrap = src->linesize[0] - width;
  1705. ret = 0;
  1706. for(y=0;y<height;y++) {
  1707. for(x=0;x<width;x++) {
  1708. a = palette[p[0]] >> 24;
  1709. if (a == 0x00) {
  1710. ret |= FF_ALPHA_TRANSP;
  1711. } else if (a != 0xff) {
  1712. ret |= FF_ALPHA_SEMI_TRANSP;
  1713. }
  1714. p++;
  1715. }
  1716. p += src_wrap;
  1717. }
  1718. return ret;
  1719. }
  1720. /**
  1721. * Tell if an image really has transparent alpha values.
  1722. * @return ored mask of FF_ALPHA_xxx constants
  1723. */
  1724. int img_get_alpha_info(AVPicture *src, int pix_fmt, int width, int height)
  1725. {
  1726. PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1727. int ret;
  1728. pf = &pix_fmt_info[pix_fmt];
  1729. /* no alpha can be represented in format */
  1730. if (!pf->is_alpha)
  1731. return 0;
  1732. switch(pix_fmt) {
  1733. case PIX_FMT_RGBA32:
  1734. ret = get_alpha_info_rgba32(src, width, height);
  1735. break;
  1736. case PIX_FMT_RGB555:
  1737. ret = get_alpha_info_rgb555(src, width, height);
  1738. break;
  1739. case PIX_FMT_PAL8:
  1740. ret = get_alpha_info_pal8(src, width, height);
  1741. break;
  1742. default:
  1743. /* we do not know, so everything is indicated */
  1744. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1745. break;
  1746. }
  1747. return ret;
  1748. }
  1749. #ifdef HAVE_MMX
  1750. #define DEINT_INPLACE_LINE_LUM \
  1751. movd_m2r(lum_m4[0],mm0);\
  1752. movd_m2r(lum_m3[0],mm1);\
  1753. movd_m2r(lum_m2[0],mm2);\
  1754. movd_m2r(lum_m1[0],mm3);\
  1755. movd_m2r(lum[0],mm4);\
  1756. punpcklbw_r2r(mm7,mm0);\
  1757. movd_r2m(mm2,lum_m4[0]);\
  1758. punpcklbw_r2r(mm7,mm1);\
  1759. punpcklbw_r2r(mm7,mm2);\
  1760. punpcklbw_r2r(mm7,mm3);\
  1761. punpcklbw_r2r(mm7,mm4);\
  1762. paddw_r2r(mm3,mm1);\
  1763. psllw_i2r(1,mm2);\
  1764. paddw_r2r(mm4,mm0);\
  1765. psllw_i2r(2,mm1);\
  1766. paddw_r2r(mm6,mm2);\
  1767. paddw_r2r(mm2,mm1);\
  1768. psubusw_r2r(mm0,mm1);\
  1769. psrlw_i2r(3,mm1);\
  1770. packuswb_r2r(mm7,mm1);\
  1771. movd_r2m(mm1,lum_m2[0]);
  1772. #define DEINT_LINE_LUM \
  1773. movd_m2r(lum_m4[0],mm0);\
  1774. movd_m2r(lum_m3[0],mm1);\
  1775. movd_m2r(lum_m2[0],mm2);\
  1776. movd_m2r(lum_m1[0],mm3);\
  1777. movd_m2r(lum[0],mm4);\
  1778. punpcklbw_r2r(mm7,mm0);\
  1779. punpcklbw_r2r(mm7,mm1);\
  1780. punpcklbw_r2r(mm7,mm2);\
  1781. punpcklbw_r2r(mm7,mm3);\
  1782. punpcklbw_r2r(mm7,mm4);\
  1783. paddw_r2r(mm3,mm1);\
  1784. psllw_i2r(1,mm2);\
  1785. paddw_r2r(mm4,mm0);\
  1786. psllw_i2r(2,mm1);\
  1787. paddw_r2r(mm6,mm2);\
  1788. paddw_r2r(mm2,mm1);\
  1789. psubusw_r2r(mm0,mm1);\
  1790. psrlw_i2r(3,mm1);\
  1791. packuswb_r2r(mm7,mm1);\
  1792. movd_r2m(mm1,dst[0]);
  1793. #endif
  1794. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1795. 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,
  1796. int size)
  1797. {
  1798. #ifndef HAVE_MMX
  1799. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1800. int sum;
  1801. for(;size > 0;size--) {
  1802. sum = -lum_m4[0];
  1803. sum += lum_m3[0] << 2;
  1804. sum += lum_m2[0] << 1;
  1805. sum += lum_m1[0] << 2;
  1806. sum += -lum[0];
  1807. dst[0] = cm[(sum + 4) >> 3];
  1808. lum_m4++;
  1809. lum_m3++;
  1810. lum_m2++;
  1811. lum_m1++;
  1812. lum++;
  1813. dst++;
  1814. }
  1815. #else
  1816. {
  1817. mmx_t rounder;
  1818. rounder.uw[0]=4;
  1819. rounder.uw[1]=4;
  1820. rounder.uw[2]=4;
  1821. rounder.uw[3]=4;
  1822. pxor_r2r(mm7,mm7);
  1823. movq_m2r(rounder,mm6);
  1824. }
  1825. for (;size > 3; size-=4) {
  1826. DEINT_LINE_LUM
  1827. lum_m4+=4;
  1828. lum_m3+=4;
  1829. lum_m2+=4;
  1830. lum_m1+=4;
  1831. lum+=4;
  1832. dst+=4;
  1833. }
  1834. #endif
  1835. }
  1836. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1837. int size)
  1838. {
  1839. #ifndef HAVE_MMX
  1840. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  1841. int sum;
  1842. for(;size > 0;size--) {
  1843. sum = -lum_m4[0];
  1844. sum += lum_m3[0] << 2;
  1845. sum += lum_m2[0] << 1;
  1846. lum_m4[0]=lum_m2[0];
  1847. sum += lum_m1[0] << 2;
  1848. sum += -lum[0];
  1849. lum_m2[0] = cm[(sum + 4) >> 3];
  1850. lum_m4++;
  1851. lum_m3++;
  1852. lum_m2++;
  1853. lum_m1++;
  1854. lum++;
  1855. }
  1856. #else
  1857. {
  1858. mmx_t rounder;
  1859. rounder.uw[0]=4;
  1860. rounder.uw[1]=4;
  1861. rounder.uw[2]=4;
  1862. rounder.uw[3]=4;
  1863. pxor_r2r(mm7,mm7);
  1864. movq_m2r(rounder,mm6);
  1865. }
  1866. for (;size > 3; size-=4) {
  1867. DEINT_INPLACE_LINE_LUM
  1868. lum_m4+=4;
  1869. lum_m3+=4;
  1870. lum_m2+=4;
  1871. lum_m1+=4;
  1872. lum+=4;
  1873. }
  1874. #endif
  1875. }
  1876. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1877. top field is copied as is, but the bottom field is deinterlaced
  1878. against the top field. */
  1879. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1880. uint8_t *src1, int src_wrap,
  1881. int width, int height)
  1882. {
  1883. uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1884. int y;
  1885. src_m2 = src1;
  1886. src_m1 = src1;
  1887. src_0=&src_m1[src_wrap];
  1888. src_p1=&src_0[src_wrap];
  1889. src_p2=&src_p1[src_wrap];
  1890. for(y=0;y<(height-2);y+=2) {
  1891. memcpy(dst,src_m1,width);
  1892. dst += dst_wrap;
  1893. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1894. src_m2 = src_0;
  1895. src_m1 = src_p1;
  1896. src_0 = src_p2;
  1897. src_p1 += 2*src_wrap;
  1898. src_p2 += 2*src_wrap;
  1899. dst += dst_wrap;
  1900. }
  1901. memcpy(dst,src_m1,width);
  1902. dst += dst_wrap;
  1903. /* do last line */
  1904. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1905. }
  1906. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1907. int width, int height)
  1908. {
  1909. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1910. int y;
  1911. uint8_t *buf;
  1912. buf = (uint8_t*)av_malloc(width);
  1913. src_m1 = src1;
  1914. memcpy(buf,src_m1,width);
  1915. src_0=&src_m1[src_wrap];
  1916. src_p1=&src_0[src_wrap];
  1917. src_p2=&src_p1[src_wrap];
  1918. for(y=0;y<(height-2);y+=2) {
  1919. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1920. src_m1 = src_p1;
  1921. src_0 = src_p2;
  1922. src_p1 += 2*src_wrap;
  1923. src_p2 += 2*src_wrap;
  1924. }
  1925. /* do last line */
  1926. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1927. av_free(buf);
  1928. }
  1929. /* deinterlace - if not supported return -1 */
  1930. int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
  1931. int pix_fmt, int width, int height)
  1932. {
  1933. int i;
  1934. if (pix_fmt != PIX_FMT_YUV420P &&
  1935. pix_fmt != PIX_FMT_YUV422P &&
  1936. pix_fmt != PIX_FMT_YUV444P)
  1937. return -1;
  1938. if ((width & 3) != 0 || (height & 3) != 0)
  1939. return -1;
  1940. for(i=0;i<3;i++) {
  1941. if (i == 1) {
  1942. switch(pix_fmt) {
  1943. case PIX_FMT_YUV420P:
  1944. width >>= 1;
  1945. height >>= 1;
  1946. break;
  1947. case PIX_FMT_YUV422P:
  1948. width >>= 1;
  1949. break;
  1950. default:
  1951. break;
  1952. }
  1953. }
  1954. if (src == dst) {
  1955. deinterlace_bottom_field_inplace(src->data[i], src->linesize[i],
  1956. width, height);
  1957. } else {
  1958. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1959. src->data[i], src->linesize[i],
  1960. width, height);
  1961. }
  1962. }
  1963. #ifdef HAVE_MMX
  1964. emms();
  1965. #endif
  1966. return 0;
  1967. }
  1968. #undef FIX