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.

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