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.

2888 lines
77KB

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