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.

2494 lines
66KB

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