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.

2156 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) {
  305. if (pix_fmt == PIX_FMT_YUV422 || pix_fmt == PIX_FMT_RGB565 ||
  306. pix_fmt == PIX_FMT_RGB555)
  307. w = width * 2;
  308. else
  309. w = width * (pf->depth * pf->nb_channels / 8);
  310. data_planes = 1;
  311. h = height;
  312. } else {
  313. data_planes = pf->nb_channels;
  314. w = width;
  315. h = height;
  316. }
  317. for (i=0; i<data_planes; i++) {
  318. if (i == 1) {
  319. w = width >> pf->x_chroma_shift;
  320. h = height >> pf->y_chroma_shift;
  321. }
  322. s = src->data[i];
  323. for(j=0; j<h; j++) {
  324. memcpy(dest, s, w);
  325. dest += w;
  326. s += src->linesize[i];
  327. }
  328. }
  329. return size;
  330. }
  331. int avpicture_get_size(int pix_fmt, int width, int height)
  332. {
  333. AVPicture dummy_pict;
  334. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  335. }
  336. /**
  337. * compute the loss when converting from a pixel format to another
  338. */
  339. int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
  340. int has_alpha)
  341. {
  342. const PixFmtInfo *pf, *ps;
  343. int loss;
  344. ps = &pix_fmt_info[src_pix_fmt];
  345. pf = &pix_fmt_info[dst_pix_fmt];
  346. /* compute loss */
  347. loss = 0;
  348. pf = &pix_fmt_info[dst_pix_fmt];
  349. if (pf->depth < ps->depth ||
  350. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  351. loss |= FF_LOSS_DEPTH;
  352. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  353. pf->y_chroma_shift > ps->y_chroma_shift)
  354. loss |= FF_LOSS_RESOLUTION;
  355. switch(pf->color_type) {
  356. case FF_COLOR_RGB:
  357. if (ps->color_type != FF_COLOR_RGB &&
  358. ps->color_type != FF_COLOR_GRAY)
  359. loss |= FF_LOSS_COLORSPACE;
  360. break;
  361. case FF_COLOR_GRAY:
  362. if (ps->color_type != FF_COLOR_GRAY)
  363. loss |= FF_LOSS_COLORSPACE;
  364. break;
  365. case FF_COLOR_YUV:
  366. if (ps->color_type != FF_COLOR_YUV)
  367. loss |= FF_LOSS_COLORSPACE;
  368. break;
  369. case FF_COLOR_YUV_JPEG:
  370. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  371. ps->color_type != FF_COLOR_YUV &&
  372. ps->color_type != FF_COLOR_GRAY)
  373. loss |= FF_LOSS_COLORSPACE;
  374. break;
  375. default:
  376. /* fail safe test */
  377. if (ps->color_type != pf->color_type)
  378. loss |= FF_LOSS_COLORSPACE;
  379. break;
  380. }
  381. if (pf->color_type == FF_COLOR_GRAY &&
  382. ps->color_type != FF_COLOR_GRAY)
  383. loss |= FF_LOSS_CHROMA;
  384. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  385. loss |= FF_LOSS_ALPHA;
  386. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  387. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  388. loss |= FF_LOSS_COLORQUANT;
  389. return loss;
  390. }
  391. static int avg_bits_per_pixel(int pix_fmt)
  392. {
  393. int bits;
  394. const PixFmtInfo *pf;
  395. pf = &pix_fmt_info[pix_fmt];
  396. switch(pf->pixel_type) {
  397. case FF_PIXEL_PACKED:
  398. switch(pix_fmt) {
  399. case PIX_FMT_YUV422:
  400. case PIX_FMT_RGB565:
  401. case PIX_FMT_RGB555:
  402. bits = 16;
  403. break;
  404. default:
  405. bits = pf->depth * pf->nb_channels;
  406. break;
  407. }
  408. break;
  409. case FF_PIXEL_PLANAR:
  410. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  411. bits = pf->depth * pf->nb_channels;
  412. } else {
  413. bits = pf->depth + ((2 * pf->depth) >>
  414. (pf->x_chroma_shift + pf->y_chroma_shift));
  415. }
  416. break;
  417. case FF_PIXEL_PALETTE:
  418. bits = 8;
  419. break;
  420. default:
  421. bits = -1;
  422. break;
  423. }
  424. return bits;
  425. }
  426. static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
  427. int src_pix_fmt,
  428. int has_alpha,
  429. int loss_mask)
  430. {
  431. int dist, i, loss, min_dist, dst_pix_fmt;
  432. /* find exact color match with smallest size */
  433. dst_pix_fmt = -1;
  434. min_dist = 0x7fffffff;
  435. for(i = 0;i < PIX_FMT_NB; i++) {
  436. if (pix_fmt_mask & (1 << i)) {
  437. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  438. if (loss == 0) {
  439. dist = avg_bits_per_pixel(i);
  440. if (dist < min_dist) {
  441. min_dist = dist;
  442. dst_pix_fmt = i;
  443. }
  444. }
  445. }
  446. }
  447. return dst_pix_fmt;
  448. }
  449. /**
  450. * find best pixel format to convert to. Return -1 if none found
  451. */
  452. int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
  453. int has_alpha, int *loss_ptr)
  454. {
  455. int dst_pix_fmt, loss_mask, i;
  456. static const int loss_mask_order[] = {
  457. ~0, /* no loss first */
  458. ~FF_LOSS_ALPHA,
  459. ~FF_LOSS_RESOLUTION,
  460. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  461. ~FF_LOSS_COLORQUANT,
  462. ~FF_LOSS_DEPTH,
  463. 0,
  464. };
  465. /* try with successive loss */
  466. i = 0;
  467. for(;;) {
  468. loss_mask = loss_mask_order[i++];
  469. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  470. has_alpha, loss_mask);
  471. if (dst_pix_fmt >= 0)
  472. goto found;
  473. if (loss_mask == 0)
  474. break;
  475. }
  476. return -1;
  477. found:
  478. if (loss_ptr)
  479. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  480. return dst_pix_fmt;
  481. }
  482. static void img_copy_plane(uint8_t *dst, int dst_wrap,
  483. const uint8_t *src, int src_wrap,
  484. int width, int height)
  485. {
  486. for(;height > 0; height--) {
  487. memcpy(dst, src, width);
  488. dst += dst_wrap;
  489. src += src_wrap;
  490. }
  491. }
  492. /**
  493. * Copy image 'src' to 'dst'.
  494. */
  495. void img_copy(AVPicture *dst, AVPicture *src,
  496. int pix_fmt, int width, int height)
  497. {
  498. int bwidth, bits, i;
  499. PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  500. pf = &pix_fmt_info[pix_fmt];
  501. switch(pf->pixel_type) {
  502. case FF_PIXEL_PACKED:
  503. switch(pix_fmt) {
  504. case PIX_FMT_YUV422:
  505. case PIX_FMT_RGB565:
  506. case PIX_FMT_RGB555:
  507. bits = 16;
  508. break;
  509. default:
  510. bits = pf->depth * pf->nb_channels;
  511. break;
  512. }
  513. bwidth = (width * bits + 7) >> 3;
  514. img_copy_plane(dst->data[0], dst->linesize[0],
  515. src->data[0], src->linesize[0],
  516. bwidth, height);
  517. break;
  518. case FF_PIXEL_PLANAR:
  519. for(i = 0; i < pf->nb_channels; i++) {
  520. int w, h;
  521. w = width;
  522. h = height;
  523. if (i == 1 || i == 2) {
  524. w >>= pf->x_chroma_shift;
  525. h >>= pf->y_chroma_shift;
  526. }
  527. bwidth = (w * pf->depth + 7) >> 3;
  528. img_copy_plane(dst->data[i], dst->linesize[i],
  529. src->data[i], src->linesize[i],
  530. bwidth, h);
  531. }
  532. break;
  533. case FF_PIXEL_PALETTE:
  534. img_copy_plane(dst->data[0], dst->linesize[0],
  535. src->data[0], src->linesize[0],
  536. width, height);
  537. /* copy the palette */
  538. img_copy_plane(dst->data[1], dst->linesize[1],
  539. src->data[1], src->linesize[1],
  540. 4, 256);
  541. break;
  542. }
  543. }
  544. /* XXX: totally non optimized */
  545. static void yuv422_to_yuv420p(AVPicture *dst, AVPicture *src,
  546. int width, int height)
  547. {
  548. const uint8_t *p, *p1;
  549. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  550. int x;
  551. p1 = src->data[0];
  552. lum1 = dst->data[0];
  553. cb1 = dst->data[1];
  554. cr1 = dst->data[2];
  555. for(;height >= 2; height -= 2) {
  556. p = p1;
  557. lum = lum1;
  558. cb = cb1;
  559. cr = cr1;
  560. for(x=0;x<width;x+=2) {
  561. lum[0] = p[0];
  562. cb[0] = p[1];
  563. lum[1] = p[2];
  564. cr[0] = p[3];
  565. p += 4;
  566. lum += 2;
  567. cb++;
  568. cr++;
  569. }
  570. p1 += src->linesize[0];
  571. lum1 += dst->linesize[0];
  572. p = p1;
  573. lum = lum1;
  574. for(x=0;x<width;x+=2) {
  575. lum[0] = p[0];
  576. lum[1] = p[2];
  577. p += 4;
  578. lum += 2;
  579. }
  580. p1 += src->linesize[0];
  581. lum1 += dst->linesize[0];
  582. cb1 += dst->linesize[1];
  583. cr1 += dst->linesize[2];
  584. }
  585. }
  586. static void yuv422_to_yuv422p(AVPicture *dst, AVPicture *src,
  587. int width, int height)
  588. {
  589. const uint8_t *p, *p1;
  590. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  591. int w;
  592. p1 = src->data[0];
  593. lum1 = dst->data[0];
  594. cb1 = dst->data[1];
  595. cr1 = dst->data[2];
  596. for(;height > 0; height--) {
  597. p = p1;
  598. lum = lum1;
  599. cb = cb1;
  600. cr = cr1;
  601. for(w = width; w >= 2; w -= 2) {
  602. lum[0] = p[0];
  603. cb[0] = p[1];
  604. lum[1] = p[2];
  605. cr[0] = p[3];
  606. p += 4;
  607. lum += 2;
  608. cb++;
  609. cr++;
  610. }
  611. p1 += src->linesize[0];
  612. lum1 += dst->linesize[0];
  613. cb1 += dst->linesize[1];
  614. cr1 += dst->linesize[2];
  615. }
  616. }
  617. static void yuv422p_to_yuv422(AVPicture *dst, AVPicture *src,
  618. int width, int height)
  619. {
  620. uint8_t *p, *p1;
  621. const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  622. int w;
  623. p1 = dst->data[0];
  624. lum1 = src->data[0];
  625. cb1 = src->data[1];
  626. cr1 = src->data[2];
  627. for(;height > 0; height--) {
  628. p = p1;
  629. lum = lum1;
  630. cb = cb1;
  631. cr = cr1;
  632. for(w = width; w >= 2; w -= 2) {
  633. p[0] = lum[0];
  634. p[1] = cb[0];
  635. p[2] = lum[1];
  636. p[3] = cr[0];
  637. p += 4;
  638. lum += 2;
  639. cb++;
  640. cr++;
  641. }
  642. p1 += dst->linesize[0];
  643. lum1 += src->linesize[0];
  644. cb1 += src->linesize[1];
  645. cr1 += src->linesize[2];
  646. }
  647. }
  648. #define SCALEBITS 10
  649. #define ONE_HALF (1 << (SCALEBITS - 1))
  650. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  651. #define YUV_TO_RGB1_CCIR(cb1, cr1)\
  652. {\
  653. cb = (cb1) - 128;\
  654. cr = (cr1) - 128;\
  655. r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
  656. g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
  657. ONE_HALF;\
  658. b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
  659. }
  660. #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
  661. {\
  662. y = ((y1) - 16) * FIX(255.0/219.0);\
  663. r = cm[(y + r_add) >> SCALEBITS];\
  664. g = cm[(y + g_add) >> SCALEBITS];\
  665. b = cm[(y + b_add) >> SCALEBITS];\
  666. }
  667. #define YUV_TO_RGB1(cb1, cr1)\
  668. {\
  669. cb = (cb1) - 128;\
  670. cr = (cr1) - 128;\
  671. r_add = FIX(1.40200) * cr + ONE_HALF;\
  672. g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
  673. b_add = FIX(1.77200) * cb + ONE_HALF;\
  674. }
  675. #define YUV_TO_RGB2(r, g, b, y1)\
  676. {\
  677. y = (y1) << SCALEBITS;\
  678. r = cm[(y + r_add) >> SCALEBITS];\
  679. g = cm[(y + g_add) >> SCALEBITS];\
  680. b = cm[(y + b_add) >> SCALEBITS];\
  681. }
  682. #define Y_CCIR_TO_JPEG(y)\
  683. cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
  684. #define Y_JPEG_TO_CCIR(y)\
  685. (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  686. #define C_CCIR_TO_JPEG(y)\
  687. cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
  688. /* NOTE: the clamp is really necessary! */
  689. #define C_JPEG_TO_CCIR(y)\
  690. ({\
  691. int __y;\
  692. __y = ((((y) - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);\
  693. if (__y < 16)\
  694. __y = 16;\
  695. __y;\
  696. })
  697. #define RGB_TO_Y(r, g, b) \
  698. ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
  699. FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
  700. #define RGB_TO_U(r1, g1, b1, shift)\
  701. (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
  702. FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  703. #define RGB_TO_V(r1, g1, b1, shift)\
  704. (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
  705. FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  706. #define RGB_TO_Y_CCIR(r, g, b) \
  707. ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  708. FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  709. #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
  710. (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
  711. FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  712. #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
  713. (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
  714. FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  715. static uint8_t y_ccir_to_jpeg[256];
  716. static uint8_t y_jpeg_to_ccir[256];
  717. static uint8_t c_ccir_to_jpeg[256];
  718. static uint8_t c_jpeg_to_ccir[256];
  719. /* init various conversion tables */
  720. static void img_convert_init(void)
  721. {
  722. int i;
  723. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  724. for(i = 0;i < 256; i++) {
  725. y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
  726. y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
  727. c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
  728. c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
  729. }
  730. }
  731. /* apply to each pixel the given table */
  732. static void img_apply_table(uint8_t *dst, int dst_wrap,
  733. const uint8_t *src, int src_wrap,
  734. int width, int height, const uint8_t *table1)
  735. {
  736. int n;
  737. const uint8_t *s;
  738. uint8_t *d;
  739. const uint8_t *table;
  740. table = table1;
  741. for(;height > 0; height--) {
  742. s = src;
  743. d = dst;
  744. n = width;
  745. while (n >= 4) {
  746. d[0] = table[s[0]];
  747. d[1] = table[s[1]];
  748. d[2] = table[s[2]];
  749. d[3] = table[s[3]];
  750. d += 4;
  751. s += 4;
  752. n -= 4;
  753. }
  754. while (n > 0) {
  755. d[0] = table[s[0]];
  756. d++;
  757. s++;
  758. n--;
  759. }
  760. dst += dst_wrap;
  761. src += src_wrap;
  762. }
  763. }
  764. /* XXX: use generic filter ? */
  765. /* XXX: in most cases, the sampling position is incorrect */
  766. /* 4x1 -> 1x1 */
  767. static void shrink41(uint8_t *dst, int dst_wrap,
  768. const uint8_t *src, int src_wrap,
  769. int width, int height)
  770. {
  771. int w;
  772. const uint8_t *s;
  773. uint8_t *d;
  774. for(;height > 0; height--) {
  775. s = src;
  776. d = dst;
  777. for(w = width;w > 0; w--) {
  778. d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
  779. s += 4;
  780. d++;
  781. }
  782. src += src_wrap;
  783. dst += dst_wrap;
  784. }
  785. }
  786. /* 2x1 -> 1x1 */
  787. static void shrink21(uint8_t *dst, int dst_wrap,
  788. const uint8_t *src, int src_wrap,
  789. int width, int height)
  790. {
  791. int w;
  792. const uint8_t *s;
  793. uint8_t *d;
  794. for(;height > 0; height--) {
  795. s = src;
  796. d = dst;
  797. for(w = width;w > 0; w--) {
  798. d[0] = (s[0] + s[1]) >> 1;
  799. s += 2;
  800. d++;
  801. }
  802. src += src_wrap;
  803. dst += dst_wrap;
  804. }
  805. }
  806. /* 1x2 -> 1x1 */
  807. static void shrink12(uint8_t *dst, int dst_wrap,
  808. const uint8_t *src, int src_wrap,
  809. int width, int height)
  810. {
  811. int w;
  812. uint8_t *d;
  813. const uint8_t *s1, *s2;
  814. for(;height > 0; height--) {
  815. s1 = src;
  816. s2 = s1 + src_wrap;
  817. d = dst;
  818. for(w = width;w >= 4; w-=4) {
  819. d[0] = (s1[0] + s2[0]) >> 1;
  820. d[1] = (s1[1] + s2[1]) >> 1;
  821. d[2] = (s1[2] + s2[2]) >> 1;
  822. d[3] = (s1[3] + s2[3]) >> 1;
  823. s1 += 4;
  824. s2 += 4;
  825. d += 4;
  826. }
  827. for(;w > 0; w--) {
  828. d[0] = (s1[0] + s2[0]) >> 1;
  829. s1++;
  830. s2++;
  831. d++;
  832. }
  833. src += 2 * src_wrap;
  834. dst += dst_wrap;
  835. }
  836. }
  837. /* 2x2 -> 1x1 */
  838. static void shrink22(uint8_t *dst, int dst_wrap,
  839. const uint8_t *src, int src_wrap,
  840. int width, int height)
  841. {
  842. int w;
  843. const uint8_t *s1, *s2;
  844. uint8_t *d;
  845. for(;height > 0; height--) {
  846. s1 = src;
  847. s2 = s1 + src_wrap;
  848. d = dst;
  849. for(w = width;w >= 4; w-=4) {
  850. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  851. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  852. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  853. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  854. s1 += 8;
  855. s2 += 8;
  856. d += 4;
  857. }
  858. for(;w > 0; w--) {
  859. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  860. s1 += 2;
  861. s2 += 2;
  862. d++;
  863. }
  864. src += 2 * src_wrap;
  865. dst += dst_wrap;
  866. }
  867. }
  868. /* 4x4 -> 1x1 */
  869. static void shrink44(uint8_t *dst, int dst_wrap,
  870. const uint8_t *src, int src_wrap,
  871. int width, int height)
  872. {
  873. int w;
  874. const uint8_t *s1, *s2, *s3, *s4;
  875. uint8_t *d;
  876. for(;height > 0; height--) {
  877. s1 = src;
  878. s2 = s1 + src_wrap;
  879. s3 = s2 + src_wrap;
  880. s4 = s3 + src_wrap;
  881. d = dst;
  882. for(w = width;w > 0; w--) {
  883. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  884. s2[0] + s2[1] + s2[2] + s2[3] +
  885. s3[0] + s3[1] + s3[2] + s3[3] +
  886. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  887. s1 += 4;
  888. s2 += 4;
  889. s3 += 4;
  890. s4 += 4;
  891. d++;
  892. }
  893. src += 4 * src_wrap;
  894. dst += dst_wrap;
  895. }
  896. }
  897. static void grow21_line(uint8_t *dst, const uint8_t *src,
  898. int width)
  899. {
  900. int w;
  901. const uint8_t *s1;
  902. uint8_t *d;
  903. s1 = src;
  904. d = dst;
  905. for(w = width;w >= 4; w-=4) {
  906. d[1] = d[0] = s1[0];
  907. d[3] = d[2] = s1[1];
  908. s1 += 2;
  909. d += 4;
  910. }
  911. for(;w >= 2; w -= 2) {
  912. d[1] = d[0] = s1[0];
  913. s1 ++;
  914. d += 2;
  915. }
  916. /* only needed if width is not a multiple of two */
  917. /* XXX: veryfy that */
  918. if (w) {
  919. d[0] = s1[0];
  920. }
  921. }
  922. static void grow41_line(uint8_t *dst, const uint8_t *src,
  923. int width)
  924. {
  925. int w, v;
  926. const uint8_t *s1;
  927. uint8_t *d;
  928. s1 = src;
  929. d = dst;
  930. for(w = width;w >= 4; w-=4) {
  931. v = s1[0];
  932. d[0] = v;
  933. d[1] = v;
  934. d[2] = v;
  935. d[3] = v;
  936. s1 ++;
  937. d += 4;
  938. }
  939. }
  940. /* 1x1 -> 2x1 */
  941. static void grow21(uint8_t *dst, int dst_wrap,
  942. const uint8_t *src, int src_wrap,
  943. int width, int height)
  944. {
  945. for(;height > 0; height--) {
  946. grow21_line(dst, src, width);
  947. src += src_wrap;
  948. dst += dst_wrap;
  949. }
  950. }
  951. /* 1x1 -> 2x2 */
  952. static void grow22(uint8_t *dst, int dst_wrap,
  953. const uint8_t *src, int src_wrap,
  954. int width, int height)
  955. {
  956. for(;height > 0; height--) {
  957. grow21_line(dst, src, width);
  958. if (height%2)
  959. src += src_wrap;
  960. dst += dst_wrap;
  961. }
  962. }
  963. /* 1x1 -> 4x1 */
  964. static void grow41(uint8_t *dst, int dst_wrap,
  965. const uint8_t *src, int src_wrap,
  966. int width, int height)
  967. {
  968. for(;height > 0; height--) {
  969. grow41_line(dst, src, width);
  970. src += src_wrap;
  971. dst += dst_wrap;
  972. }
  973. }
  974. /* 1x1 -> 4x4 */
  975. static void grow44(uint8_t *dst, int dst_wrap,
  976. const uint8_t *src, int src_wrap,
  977. int width, int height)
  978. {
  979. for(;height > 0; height--) {
  980. grow41_line(dst, src, width);
  981. if ((height & 3) == 1)
  982. src += src_wrap;
  983. dst += dst_wrap;
  984. }
  985. }
  986. /* 1x2 -> 2x1 */
  987. static void conv411(uint8_t *dst, int dst_wrap,
  988. const uint8_t *src, int src_wrap,
  989. int width, int height)
  990. {
  991. int w, c;
  992. const uint8_t *s1, *s2;
  993. uint8_t *d;
  994. width>>=1;
  995. for(;height > 0; height--) {
  996. s1 = src;
  997. s2 = src + src_wrap;
  998. d = dst;
  999. for(w = width;w > 0; w--) {
  1000. c = (s1[0] + s2[0]) >> 1;
  1001. d[0] = c;
  1002. d[1] = c;
  1003. s1++;
  1004. s2++;
  1005. d += 2;
  1006. }
  1007. src += src_wrap * 2;
  1008. dst += dst_wrap;
  1009. }
  1010. }
  1011. /* XXX: add jpeg quantize code */
  1012. #define TRANSP_INDEX (6*6*6)
  1013. /* this is maybe slow, but allows for extensions */
  1014. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  1015. {
  1016. return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
  1017. }
  1018. static void build_rgb_palette(uint8_t *palette, int has_alpha)
  1019. {
  1020. uint32_t *pal;
  1021. static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
  1022. int i, r, g, b;
  1023. pal = (uint32_t *)palette;
  1024. i = 0;
  1025. for(r = 0; r < 6; r++) {
  1026. for(g = 0; g < 6; g++) {
  1027. for(b = 0; b < 6; b++) {
  1028. pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
  1029. (pal_value[g] << 8) | pal_value[b];
  1030. }
  1031. }
  1032. }
  1033. if (has_alpha)
  1034. pal[i++] = 0;
  1035. while (i < 256)
  1036. pal[i++] = 0xff000000;
  1037. }
  1038. /* copy bit n to bits 0 ... n - 1 */
  1039. static inline unsigned int bitcopy_n(unsigned int a, int n)
  1040. {
  1041. int mask;
  1042. mask = (1 << n) - 1;
  1043. return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
  1044. }
  1045. /* rgb555 handling */
  1046. #define RGB_NAME rgb555
  1047. #define RGB_IN(r, g, b, s)\
  1048. {\
  1049. unsigned int v = ((const uint16_t *)(s))[0];\
  1050. r = bitcopy_n(v >> (10 - 3), 3);\
  1051. g = bitcopy_n(v >> (5 - 3), 3);\
  1052. b = bitcopy_n(v << 3, 3);\
  1053. }
  1054. #define RGBA_IN(r, g, b, a, s)\
  1055. {\
  1056. unsigned int v = ((const uint16_t *)(s))[0];\
  1057. r = bitcopy_n(v >> (10 - 3), 3);\
  1058. g = bitcopy_n(v >> (5 - 3), 3);\
  1059. b = bitcopy_n(v << 3, 3);\
  1060. a = (-(v >> 15)) & 0xff;\
  1061. }
  1062. #define RGBA_OUT(d, r, g, b, a)\
  1063. {\
  1064. ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
  1065. ((a << 8) & 0x8000);\
  1066. }
  1067. #define BPP 2
  1068. #include "imgconvert_template.h"
  1069. /* rgb565 handling */
  1070. #define RGB_NAME rgb565
  1071. #define RGB_IN(r, g, b, s)\
  1072. {\
  1073. unsigned int v = ((const uint16_t *)(s))[0];\
  1074. r = bitcopy_n(v >> (11 - 3), 3);\
  1075. g = bitcopy_n(v >> (5 - 2), 2);\
  1076. b = bitcopy_n(v << 3, 3);\
  1077. }
  1078. #define RGB_OUT(d, r, g, b)\
  1079. {\
  1080. ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
  1081. }
  1082. #define BPP 2
  1083. #include "imgconvert_template.h"
  1084. /* bgr24 handling */
  1085. #define RGB_NAME bgr24
  1086. #define RGB_IN(r, g, b, s)\
  1087. {\
  1088. b = (s)[0];\
  1089. g = (s)[1];\
  1090. r = (s)[2];\
  1091. }
  1092. #define RGB_OUT(d, r, g, b)\
  1093. {\
  1094. (d)[0] = b;\
  1095. (d)[1] = g;\
  1096. (d)[2] = r;\
  1097. }
  1098. #define BPP 3
  1099. #include "imgconvert_template.h"
  1100. #undef RGB_IN
  1101. #undef RGB_OUT
  1102. #undef BPP
  1103. /* rgb24 handling */
  1104. #define RGB_NAME rgb24
  1105. #define FMT_RGB24
  1106. #define RGB_IN(r, g, b, s)\
  1107. {\
  1108. r = (s)[0];\
  1109. g = (s)[1];\
  1110. b = (s)[2];\
  1111. }
  1112. #define RGB_OUT(d, r, g, b)\
  1113. {\
  1114. (d)[0] = r;\
  1115. (d)[1] = g;\
  1116. (d)[2] = b;\
  1117. }
  1118. #define BPP 3
  1119. #include "imgconvert_template.h"
  1120. /* rgba32 handling */
  1121. #define RGB_NAME rgba32
  1122. #define FMT_RGBA32
  1123. #define RGB_IN(r, g, b, s)\
  1124. {\
  1125. unsigned int v = ((const uint32_t *)(s))[0];\
  1126. r = (v >> 16) & 0xff;\
  1127. g = (v >> 8) & 0xff;\
  1128. b = v & 0xff;\
  1129. }
  1130. #define RGBA_IN(r, g, b, a, s)\
  1131. {\
  1132. unsigned int v = ((const uint32_t *)(s))[0];\
  1133. a = (v >> 24) & 0xff;\
  1134. r = (v >> 16) & 0xff;\
  1135. g = (v >> 8) & 0xff;\
  1136. b = v & 0xff;\
  1137. }
  1138. #define RGBA_OUT(d, r, g, b, a)\
  1139. {\
  1140. ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
  1141. }
  1142. #define BPP 4
  1143. #include "imgconvert_template.h"
  1144. static void mono_to_gray(AVPicture *dst, AVPicture *src,
  1145. int width, int height, int xor_mask)
  1146. {
  1147. const unsigned char *p;
  1148. unsigned char *q;
  1149. int v, dst_wrap, src_wrap;
  1150. int y, w;
  1151. p = src->data[0];
  1152. src_wrap = src->linesize[0] - ((width + 7) >> 3);
  1153. q = dst->data[0];
  1154. dst_wrap = dst->linesize[0] - width;
  1155. for(y=0;y<height;y++) {
  1156. w = width;
  1157. while (w >= 8) {
  1158. v = *p++ ^ xor_mask;
  1159. q[0] = -(v >> 7);
  1160. q[1] = -((v >> 6) & 1);
  1161. q[2] = -((v >> 5) & 1);
  1162. q[3] = -((v >> 4) & 1);
  1163. q[4] = -((v >> 3) & 1);
  1164. q[5] = -((v >> 2) & 1);
  1165. q[6] = -((v >> 1) & 1);
  1166. q[7] = -((v >> 0) & 1);
  1167. w -= 8;
  1168. q += 8;
  1169. }
  1170. if (w > 0) {
  1171. v = *p++ ^ xor_mask;
  1172. do {
  1173. q[0] = -((v >> 7) & 1);
  1174. q++;
  1175. v <<= 1;
  1176. } while (--w);
  1177. }
  1178. p += src_wrap;
  1179. q += dst_wrap;
  1180. }
  1181. }
  1182. static void monowhite_to_gray(AVPicture *dst, AVPicture *src,
  1183. int width, int height)
  1184. {
  1185. mono_to_gray(dst, src, width, height, 0xff);
  1186. }
  1187. static void monoblack_to_gray(AVPicture *dst, AVPicture *src,
  1188. int width, int height)
  1189. {
  1190. mono_to_gray(dst, src, width, height, 0x00);
  1191. }
  1192. static void gray_to_mono(AVPicture *dst, AVPicture *src,
  1193. int width, int height, int xor_mask)
  1194. {
  1195. int n;
  1196. const uint8_t *s;
  1197. uint8_t *d;
  1198. int j, b, v, n1, src_wrap, dst_wrap, y;
  1199. s = src->data[0];
  1200. src_wrap = src->linesize[0] - width;
  1201. d = dst->data[0];
  1202. dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
  1203. for(y=0;y<height;y++) {
  1204. n = width;
  1205. while (n >= 8) {
  1206. v = 0;
  1207. for(j=0;j<8;j++) {
  1208. b = s[0];
  1209. s++;
  1210. v = (v << 1) | (b >> 7);
  1211. }
  1212. d[0] = v ^ xor_mask;
  1213. d++;
  1214. n -= 8;
  1215. }
  1216. if (n > 0) {
  1217. n1 = n;
  1218. v = 0;
  1219. while (n > 0) {
  1220. b = s[0];
  1221. s++;
  1222. v = (v << 1) | (b >> 7);
  1223. n--;
  1224. }
  1225. d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
  1226. d++;
  1227. }
  1228. s += src_wrap;
  1229. d += dst_wrap;
  1230. }
  1231. }
  1232. static void gray_to_monowhite(AVPicture *dst, AVPicture *src,
  1233. int width, int height)
  1234. {
  1235. gray_to_mono(dst, src, width, height, 0xff);
  1236. }
  1237. static void gray_to_monoblack(AVPicture *dst, AVPicture *src,
  1238. int width, int height)
  1239. {
  1240. gray_to_mono(dst, src, width, height, 0x00);
  1241. }
  1242. typedef struct ConvertEntry {
  1243. void (*convert)(AVPicture *dst, AVPicture *src, int width, int height);
  1244. } ConvertEntry;
  1245. /* Add each new convertion function in this table. In order to be able
  1246. to convert from any format to any format, the following constraints
  1247. must be satisfied:
  1248. - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
  1249. - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
  1250. - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
  1251. - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
  1252. PIX_FMT_RGB24.
  1253. - PIX_FMT_422 must convert to and from PIX_FMT_422P.
  1254. The other conversion functions are just optimisations for common cases.
  1255. */
  1256. static ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
  1257. [PIX_FMT_YUV420P] = {
  1258. [PIX_FMT_RGB555] = {
  1259. .convert = yuv420p_to_rgb555
  1260. },
  1261. [PIX_FMT_RGB565] = {
  1262. .convert = yuv420p_to_rgb565
  1263. },
  1264. [PIX_FMT_BGR24] = {
  1265. .convert = yuv420p_to_bgr24
  1266. },
  1267. [PIX_FMT_RGB24] = {
  1268. .convert = yuv420p_to_rgb24
  1269. },
  1270. [PIX_FMT_RGBA32] = {
  1271. .convert = yuv420p_to_rgba32
  1272. },
  1273. },
  1274. [PIX_FMT_YUV422P] = {
  1275. [PIX_FMT_YUV422] = {
  1276. .convert = yuv422p_to_yuv422,
  1277. },
  1278. },
  1279. [PIX_FMT_YUV444P] = {
  1280. [PIX_FMT_RGB24] = {
  1281. .convert = yuv444p_to_rgb24
  1282. },
  1283. },
  1284. [PIX_FMT_YUVJ420P] = {
  1285. [PIX_FMT_RGB555] = {
  1286. .convert = yuvj420p_to_rgb555
  1287. },
  1288. [PIX_FMT_RGB565] = {
  1289. .convert = yuvj420p_to_rgb565
  1290. },
  1291. [PIX_FMT_BGR24] = {
  1292. .convert = yuvj420p_to_bgr24
  1293. },
  1294. [PIX_FMT_RGB24] = {
  1295. .convert = yuvj420p_to_rgb24
  1296. },
  1297. [PIX_FMT_RGBA32] = {
  1298. .convert = yuvj420p_to_rgba32
  1299. },
  1300. },
  1301. [PIX_FMT_YUVJ444P] = {
  1302. [PIX_FMT_RGB24] = {
  1303. .convert = yuvj444p_to_rgb24
  1304. },
  1305. },
  1306. [PIX_FMT_YUV422] = {
  1307. [PIX_FMT_YUV420P] = {
  1308. .convert = yuv422_to_yuv420p,
  1309. },
  1310. [PIX_FMT_YUV422P] = {
  1311. .convert = yuv422_to_yuv422p,
  1312. },
  1313. },
  1314. [PIX_FMT_RGB24] = {
  1315. [PIX_FMT_YUV420P] = {
  1316. .convert = rgb24_to_yuv420p
  1317. },
  1318. [PIX_FMT_RGB565] = {
  1319. .convert = rgb24_to_rgb565
  1320. },
  1321. [PIX_FMT_RGB555] = {
  1322. .convert = rgb24_to_rgb555
  1323. },
  1324. [PIX_FMT_RGBA32] = {
  1325. .convert = rgb24_to_rgba32
  1326. },
  1327. [PIX_FMT_BGR24] = {
  1328. .convert = rgb24_to_bgr24
  1329. },
  1330. [PIX_FMT_GRAY8] = {
  1331. .convert = rgb24_to_gray
  1332. },
  1333. [PIX_FMT_PAL8] = {
  1334. .convert = rgb24_to_pal8
  1335. },
  1336. [PIX_FMT_YUV444P] = {
  1337. .convert = rgb24_to_yuv444p
  1338. },
  1339. [PIX_FMT_YUVJ420P] = {
  1340. .convert = rgb24_to_yuvj420p
  1341. },
  1342. [PIX_FMT_YUVJ444P] = {
  1343. .convert = rgb24_to_yuvj444p
  1344. },
  1345. },
  1346. [PIX_FMT_RGBA32] = {
  1347. [PIX_FMT_RGB24] = {
  1348. .convert = rgba32_to_rgb24
  1349. },
  1350. [PIX_FMT_RGB555] = {
  1351. .convert = rgba32_to_rgb555
  1352. },
  1353. [PIX_FMT_PAL8] = {
  1354. .convert = rgba32_to_pal8
  1355. },
  1356. [PIX_FMT_YUV420P] = {
  1357. .convert = rgba32_to_yuv420p
  1358. },
  1359. [PIX_FMT_GRAY8] = {
  1360. .convert = rgba32_to_gray
  1361. },
  1362. },
  1363. [PIX_FMT_BGR24] = {
  1364. [PIX_FMT_RGB24] = {
  1365. .convert = bgr24_to_rgb24
  1366. },
  1367. [PIX_FMT_YUV420P] = {
  1368. .convert = bgr24_to_yuv420p
  1369. },
  1370. [PIX_FMT_GRAY8] = {
  1371. .convert = bgr24_to_gray
  1372. },
  1373. },
  1374. [PIX_FMT_RGB555] = {
  1375. [PIX_FMT_RGB24] = {
  1376. .convert = rgb555_to_rgb24
  1377. },
  1378. [PIX_FMT_RGBA32] = {
  1379. .convert = rgb555_to_rgba32
  1380. },
  1381. [PIX_FMT_YUV420P] = {
  1382. .convert = rgb555_to_yuv420p
  1383. },
  1384. [PIX_FMT_GRAY8] = {
  1385. .convert = rgb555_to_gray
  1386. },
  1387. },
  1388. [PIX_FMT_RGB565] = {
  1389. [PIX_FMT_RGB24] = {
  1390. .convert = rgb565_to_rgb24
  1391. },
  1392. [PIX_FMT_YUV420P] = {
  1393. .convert = rgb565_to_yuv420p
  1394. },
  1395. [PIX_FMT_GRAY8] = {
  1396. .convert = rgb565_to_gray
  1397. },
  1398. },
  1399. [PIX_FMT_GRAY8] = {
  1400. [PIX_FMT_RGB555] = {
  1401. .convert = gray_to_rgb555
  1402. },
  1403. [PIX_FMT_RGB565] = {
  1404. .convert = gray_to_rgb565
  1405. },
  1406. [PIX_FMT_RGB24] = {
  1407. .convert = gray_to_rgb24
  1408. },
  1409. [PIX_FMT_BGR24] = {
  1410. .convert = gray_to_bgr24
  1411. },
  1412. [PIX_FMT_RGBA32] = {
  1413. .convert = gray_to_rgba32
  1414. },
  1415. [PIX_FMT_MONOWHITE] = {
  1416. .convert = gray_to_monowhite
  1417. },
  1418. [PIX_FMT_MONOBLACK] = {
  1419. .convert = gray_to_monoblack
  1420. },
  1421. },
  1422. [PIX_FMT_MONOWHITE] = {
  1423. [PIX_FMT_GRAY8] = {
  1424. .convert = monowhite_to_gray
  1425. },
  1426. },
  1427. [PIX_FMT_MONOBLACK] = {
  1428. [PIX_FMT_GRAY8] = {
  1429. .convert = monoblack_to_gray
  1430. },
  1431. },
  1432. [PIX_FMT_PAL8] = {
  1433. [PIX_FMT_RGB555] = {
  1434. .convert = pal8_to_rgb555
  1435. },
  1436. [PIX_FMT_RGB565] = {
  1437. .convert = pal8_to_rgb565
  1438. },
  1439. [PIX_FMT_BGR24] = {
  1440. .convert = pal8_to_bgr24
  1441. },
  1442. [PIX_FMT_RGB24] = {
  1443. .convert = pal8_to_rgb24
  1444. },
  1445. [PIX_FMT_RGBA32] = {
  1446. .convert = pal8_to_rgba32
  1447. },
  1448. },
  1449. };
  1450. static int avpicture_alloc(AVPicture *picture,
  1451. int pix_fmt, int width, int height)
  1452. {
  1453. unsigned int size;
  1454. void *ptr;
  1455. size = avpicture_get_size(pix_fmt, width, height);
  1456. if (size < 0)
  1457. goto fail;
  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