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.

2880 lines
78KB

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