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.

2521 lines
67KB

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