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.

2924 lines
78KB

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