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.

2853 lines
77KB

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