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.

2185 lines
58KB

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