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.

2893 lines
77KB

  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file imgconvert.c
  23. * misc image conversion routines
  24. */
  25. /* TODO:
  26. * - write 'ffimg' program to test all the image related stuff
  27. * - move all api to slice based system
  28. * - integrate deinterlacing, postprocessing and scaling in the conversion process
  29. */
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "colorspace.h"
  33. #ifdef HAVE_MMX
  34. #include "i386/mmx.h"
  35. #include "i386/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_UYYVYY411] = {
  257. .name = "uyyvyy411",
  258. .nb_channels = 1,
  259. .color_type = FF_COLOR_YUV,
  260. .pixel_type = FF_PIXEL_PACKED,
  261. .depth = 8,
  262. .x_chroma_shift = 2, .y_chroma_shift = 0,
  263. },
  264. [PIX_FMT_BGR32] = {
  265. .name = "bgr32",
  266. .nb_channels = 4, .is_alpha = 1,
  267. .color_type = FF_COLOR_RGB,
  268. .pixel_type = FF_PIXEL_PACKED,
  269. .depth = 8,
  270. .x_chroma_shift = 0, .y_chroma_shift = 0,
  271. },
  272. [PIX_FMT_BGR565] = {
  273. .name = "bgr565",
  274. .nb_channels = 3,
  275. .color_type = FF_COLOR_RGB,
  276. .pixel_type = FF_PIXEL_PACKED,
  277. .depth = 5,
  278. .x_chroma_shift = 0, .y_chroma_shift = 0,
  279. },
  280. [PIX_FMT_BGR555] = {
  281. .name = "bgr555",
  282. .nb_channels = 3,
  283. .color_type = FF_COLOR_RGB,
  284. .pixel_type = FF_PIXEL_PACKED,
  285. .depth = 5,
  286. .x_chroma_shift = 0, .y_chroma_shift = 0,
  287. },
  288. [PIX_FMT_RGB8] = {
  289. .name = "rgb8",
  290. .nb_channels = 1,
  291. .color_type = FF_COLOR_RGB,
  292. .pixel_type = FF_PIXEL_PACKED,
  293. .depth = 8,
  294. .x_chroma_shift = 0, .y_chroma_shift = 0,
  295. },
  296. [PIX_FMT_RGB4] = {
  297. .name = "rgb4",
  298. .nb_channels = 1,
  299. .color_type = FF_COLOR_RGB,
  300. .pixel_type = FF_PIXEL_PACKED,
  301. .depth = 4,
  302. .x_chroma_shift = 0, .y_chroma_shift = 0,
  303. },
  304. [PIX_FMT_RGB4_BYTE] = {
  305. .name = "rgb4_byte",
  306. .nb_channels = 1,
  307. .color_type = FF_COLOR_RGB,
  308. .pixel_type = FF_PIXEL_PACKED,
  309. .depth = 8,
  310. .x_chroma_shift = 0, .y_chroma_shift = 0,
  311. },
  312. [PIX_FMT_BGR8] = {
  313. .name = "bgr8",
  314. .nb_channels = 1,
  315. .color_type = FF_COLOR_RGB,
  316. .pixel_type = FF_PIXEL_PACKED,
  317. .depth = 8,
  318. .x_chroma_shift = 0, .y_chroma_shift = 0,
  319. },
  320. [PIX_FMT_BGR4] = {
  321. .name = "bgr4",
  322. .nb_channels = 1,
  323. .color_type = FF_COLOR_RGB,
  324. .pixel_type = FF_PIXEL_PACKED,
  325. .depth = 4,
  326. .x_chroma_shift = 0, .y_chroma_shift = 0,
  327. },
  328. [PIX_FMT_BGR4_BYTE] = {
  329. .name = "bgr4_byte",
  330. .nb_channels = 1,
  331. .color_type = FF_COLOR_RGB,
  332. .pixel_type = FF_PIXEL_PACKED,
  333. .depth = 8,
  334. .x_chroma_shift = 0, .y_chroma_shift = 0,
  335. },
  336. [PIX_FMT_NV12] = {
  337. .name = "nv12",
  338. .nb_channels = 2,
  339. .color_type = FF_COLOR_YUV,
  340. .pixel_type = FF_PIXEL_PLANAR,
  341. .depth = 8,
  342. .x_chroma_shift = 1, .y_chroma_shift = 1,
  343. },
  344. [PIX_FMT_NV21] = {
  345. .name = "nv12",
  346. .nb_channels = 2,
  347. .color_type = FF_COLOR_YUV,
  348. .pixel_type = FF_PIXEL_PLANAR,
  349. .depth = 8,
  350. .x_chroma_shift = 1, .y_chroma_shift = 1,
  351. },
  352. [PIX_FMT_BGR32_1] = {
  353. .name = "bgr32_1",
  354. .nb_channels = 4, .is_alpha = 1,
  355. .color_type = FF_COLOR_RGB,
  356. .pixel_type = FF_PIXEL_PACKED,
  357. .depth = 8,
  358. .x_chroma_shift = 0, .y_chroma_shift = 0,
  359. },
  360. [PIX_FMT_RGB32_1] = {
  361. .name = "rgb32_1",
  362. .nb_channels = 4, .is_alpha = 1,
  363. .color_type = FF_COLOR_RGB,
  364. .pixel_type = FF_PIXEL_PACKED,
  365. .depth = 8,
  366. .x_chroma_shift = 0, .y_chroma_shift = 0,
  367. },
  368. };
  369. void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
  370. {
  371. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  372. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  373. }
  374. const char *avcodec_get_pix_fmt_name(int pix_fmt)
  375. {
  376. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  377. return NULL;
  378. else
  379. return pix_fmt_info[pix_fmt].name;
  380. }
  381. enum PixelFormat avcodec_get_pix_fmt(const char* name)
  382. {
  383. int i;
  384. for (i=0; i < PIX_FMT_NB; i++)
  385. if (!strcmp(pix_fmt_info[i].name, name))
  386. return i;
  387. return PIX_FMT_NONE;
  388. }
  389. void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
  390. {
  391. /* print header */
  392. if (pix_fmt < 0)
  393. snprintf (buf, buf_size,
  394. "name " " nb_channels" " depth" " is_alpha"
  395. );
  396. else{
  397. PixFmtInfo info= pix_fmt_info[pix_fmt];
  398. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  399. snprintf (buf, buf_size,
  400. "%-10s" " %1d " " %2d " " %c ",
  401. info.name,
  402. info.nb_channels,
  403. info.depth,
  404. is_alpha_char
  405. );
  406. }
  407. }
  408. int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
  409. {
  410. int w2;
  411. const PixFmtInfo *pinfo;
  412. memset(picture->linesize, 0, sizeof(picture->linesize));
  413. pinfo = &pix_fmt_info[pix_fmt];
  414. switch(pix_fmt) {
  415. case PIX_FMT_YUV420P:
  416. case PIX_FMT_YUV422P:
  417. case PIX_FMT_YUV444P:
  418. case PIX_FMT_YUV410P:
  419. case PIX_FMT_YUV411P:
  420. case PIX_FMT_YUV440P:
  421. case PIX_FMT_YUVJ420P:
  422. case PIX_FMT_YUVJ422P:
  423. case PIX_FMT_YUVJ444P:
  424. case PIX_FMT_YUVJ440P:
  425. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  426. picture->linesize[0] = width;
  427. picture->linesize[1] = w2;
  428. picture->linesize[2] = w2;
  429. break;
  430. case PIX_FMT_YUVA420P:
  431. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  432. picture->linesize[0] = width;
  433. picture->linesize[1] = w2;
  434. picture->linesize[2] = w2;
  435. picture->linesize[3] = width;
  436. break;
  437. case PIX_FMT_NV12:
  438. case PIX_FMT_NV21:
  439. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  440. picture->linesize[0] = width;
  441. picture->linesize[1] = w2;
  442. break;
  443. case PIX_FMT_RGB24:
  444. case PIX_FMT_BGR24:
  445. picture->linesize[0] = width * 3;
  446. break;
  447. case PIX_FMT_RGB32:
  448. case PIX_FMT_BGR32:
  449. case PIX_FMT_RGB32_1:
  450. case PIX_FMT_BGR32_1:
  451. picture->linesize[0] = width * 4;
  452. break;
  453. case PIX_FMT_GRAY16BE:
  454. case PIX_FMT_GRAY16LE:
  455. case PIX_FMT_BGR555:
  456. case PIX_FMT_BGR565:
  457. case PIX_FMT_RGB555:
  458. case PIX_FMT_RGB565:
  459. case PIX_FMT_YUYV422:
  460. picture->linesize[0] = width * 2;
  461. break;
  462. case PIX_FMT_UYVY422:
  463. picture->linesize[0] = width * 2;
  464. break;
  465. case PIX_FMT_UYYVYY411:
  466. picture->linesize[0] = width + width/2;
  467. break;
  468. case PIX_FMT_RGB8:
  469. case PIX_FMT_BGR8:
  470. case PIX_FMT_RGB4_BYTE:
  471. case PIX_FMT_BGR4_BYTE:
  472. case PIX_FMT_GRAY8:
  473. picture->linesize[0] = width;
  474. break;
  475. case PIX_FMT_RGB4:
  476. case PIX_FMT_BGR4:
  477. picture->linesize[0] = width / 2;
  478. break;
  479. case PIX_FMT_MONOWHITE:
  480. case PIX_FMT_MONOBLACK:
  481. picture->linesize[0] = (width + 7) >> 3;
  482. break;
  483. case PIX_FMT_PAL8:
  484. picture->linesize[0] = width;
  485. picture->linesize[1] = 4;
  486. break;
  487. default:
  488. return -1;
  489. }
  490. return 0;
  491. }
  492. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
  493. int height)
  494. {
  495. int size, h2, size2;
  496. const PixFmtInfo *pinfo;
  497. pinfo = &pix_fmt_info[pix_fmt];
  498. size = picture->linesize[0] * height;
  499. switch(pix_fmt) {
  500. case PIX_FMT_YUV420P:
  501. case PIX_FMT_YUV422P:
  502. case PIX_FMT_YUV444P:
  503. case PIX_FMT_YUV410P:
  504. case PIX_FMT_YUV411P:
  505. case PIX_FMT_YUV440P:
  506. case PIX_FMT_YUVJ420P:
  507. case PIX_FMT_YUVJ422P:
  508. case PIX_FMT_YUVJ444P:
  509. case PIX_FMT_YUVJ440P:
  510. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  511. size2 = picture->linesize[1] * h2;
  512. picture->data[0] = ptr;
  513. picture->data[1] = picture->data[0] + size;
  514. picture->data[2] = picture->data[1] + size2;
  515. picture->data[3] = NULL;
  516. return size + 2 * size2;
  517. case PIX_FMT_YUVA420P:
  518. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  519. size2 = picture->linesize[1] * h2;
  520. picture->data[0] = ptr;
  521. picture->data[1] = picture->data[0] + size;
  522. picture->data[2] = picture->data[1] + size2;
  523. picture->data[3] = picture->data[1] + size2 + size2;
  524. return 2 * size + 2 * size2;
  525. case PIX_FMT_NV12:
  526. case PIX_FMT_NV21:
  527. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  528. size2 = picture->linesize[1] * h2 * 2;
  529. picture->data[0] = ptr;
  530. picture->data[1] = picture->data[0] + size;
  531. picture->data[2] = NULL;
  532. picture->data[3] = NULL;
  533. return size + 2 * size2;
  534. case PIX_FMT_RGB24:
  535. case PIX_FMT_BGR24:
  536. case PIX_FMT_RGB32:
  537. case PIX_FMT_BGR32:
  538. case PIX_FMT_RGB32_1:
  539. case PIX_FMT_BGR32_1:
  540. case PIX_FMT_GRAY16BE:
  541. case PIX_FMT_GRAY16LE:
  542. case PIX_FMT_BGR555:
  543. case PIX_FMT_BGR565:
  544. case PIX_FMT_RGB555:
  545. case PIX_FMT_RGB565:
  546. case PIX_FMT_YUYV422:
  547. case PIX_FMT_UYVY422:
  548. case PIX_FMT_UYYVYY411:
  549. case PIX_FMT_RGB8:
  550. case PIX_FMT_BGR8:
  551. case PIX_FMT_RGB4_BYTE:
  552. case PIX_FMT_BGR4_BYTE:
  553. case PIX_FMT_GRAY8:
  554. case PIX_FMT_RGB4:
  555. case PIX_FMT_BGR4:
  556. case PIX_FMT_MONOWHITE:
  557. case PIX_FMT_MONOBLACK:
  558. picture->data[0] = ptr;
  559. picture->data[1] = NULL;
  560. picture->data[2] = NULL;
  561. picture->data[3] = NULL;
  562. return size;
  563. case PIX_FMT_PAL8:
  564. size2 = (size + 3) & ~3;
  565. picture->data[0] = ptr;
  566. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  567. picture->data[2] = NULL;
  568. picture->data[3] = NULL;
  569. return size2 + 256 * 4;
  570. default:
  571. picture->data[0] = NULL;
  572. picture->data[1] = NULL;
  573. picture->data[2] = NULL;
  574. picture->data[3] = NULL;
  575. return -1;
  576. }
  577. }
  578. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  579. int pix_fmt, int width, int height)
  580. {
  581. if(avcodec_check_dimensions(NULL, width, height))
  582. return -1;
  583. if (ff_fill_linesize(picture, pix_fmt, width))
  584. return -1;
  585. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  586. }
  587. int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
  588. unsigned char *dest, int dest_size)
  589. {
  590. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  591. int i, j, w, h, data_planes;
  592. const unsigned char* s;
  593. int size = avpicture_get_size(pix_fmt, width, height);
  594. if (size > dest_size || size < 0)
  595. return -1;
  596. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  597. if (pix_fmt == PIX_FMT_YUYV422 ||
  598. pix_fmt == PIX_FMT_UYVY422 ||
  599. pix_fmt == PIX_FMT_BGR565 ||
  600. pix_fmt == PIX_FMT_BGR555 ||
  601. pix_fmt == PIX_FMT_RGB565 ||
  602. pix_fmt == PIX_FMT_RGB555)
  603. w = width * 2;
  604. else if (pix_fmt == PIX_FMT_UYYVYY411)
  605. w = width + width/2;
  606. else if (pix_fmt == PIX_FMT_PAL8)
  607. w = width;
  608. else
  609. w = width * (pf->depth * pf->nb_channels / 8);
  610. data_planes = 1;
  611. h = height;
  612. } else {
  613. data_planes = pf->nb_channels;
  614. w = (width*pf->depth + 7)/8;
  615. h = height;
  616. }
  617. for (i=0; i<data_planes; i++) {
  618. if (i == 1) {
  619. w = width >> pf->x_chroma_shift;
  620. h = height >> pf->y_chroma_shift;
  621. }
  622. s = src->data[i];
  623. for(j=0; j<h; j++) {
  624. memcpy(dest, s, w);
  625. dest += w;
  626. s += src->linesize[i];
  627. }
  628. }
  629. if (pf->pixel_type == FF_PIXEL_PALETTE)
  630. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  631. return size;
  632. }
  633. int avpicture_get_size(int pix_fmt, int width, int height)
  634. {
  635. AVPicture dummy_pict;
  636. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  637. }
  638. int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
  639. int has_alpha)
  640. {
  641. const PixFmtInfo *pf, *ps;
  642. int loss;
  643. ps = &pix_fmt_info[src_pix_fmt];
  644. pf = &pix_fmt_info[dst_pix_fmt];
  645. /* compute loss */
  646. loss = 0;
  647. pf = &pix_fmt_info[dst_pix_fmt];
  648. if (pf->depth < ps->depth ||
  649. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  650. loss |= FF_LOSS_DEPTH;
  651. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  652. pf->y_chroma_shift > ps->y_chroma_shift)
  653. loss |= FF_LOSS_RESOLUTION;
  654. switch(pf->color_type) {
  655. case FF_COLOR_RGB:
  656. if (ps->color_type != FF_COLOR_RGB &&
  657. ps->color_type != FF_COLOR_GRAY)
  658. loss |= FF_LOSS_COLORSPACE;
  659. break;
  660. case FF_COLOR_GRAY:
  661. if (ps->color_type != FF_COLOR_GRAY)
  662. loss |= FF_LOSS_COLORSPACE;
  663. break;
  664. case FF_COLOR_YUV:
  665. if (ps->color_type != FF_COLOR_YUV)
  666. loss |= FF_LOSS_COLORSPACE;
  667. break;
  668. case FF_COLOR_YUV_JPEG:
  669. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  670. ps->color_type != FF_COLOR_YUV &&
  671. ps->color_type != FF_COLOR_GRAY)
  672. loss |= FF_LOSS_COLORSPACE;
  673. break;
  674. default:
  675. /* fail safe test */
  676. if (ps->color_type != pf->color_type)
  677. loss |= FF_LOSS_COLORSPACE;
  678. break;
  679. }
  680. if (pf->color_type == FF_COLOR_GRAY &&
  681. ps->color_type != FF_COLOR_GRAY)
  682. loss |= FF_LOSS_CHROMA;
  683. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  684. loss |= FF_LOSS_ALPHA;
  685. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  686. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  687. loss |= FF_LOSS_COLORQUANT;
  688. return loss;
  689. }
  690. static int avg_bits_per_pixel(int pix_fmt)
  691. {
  692. int bits;
  693. const PixFmtInfo *pf;
  694. pf = &pix_fmt_info[pix_fmt];
  695. switch(pf->pixel_type) {
  696. case FF_PIXEL_PACKED:
  697. switch(pix_fmt) {
  698. case PIX_FMT_YUYV422:
  699. case PIX_FMT_UYVY422:
  700. case PIX_FMT_RGB565:
  701. case PIX_FMT_RGB555:
  702. case PIX_FMT_BGR565:
  703. case PIX_FMT_BGR555:
  704. bits = 16;
  705. break;
  706. case PIX_FMT_UYYVYY411:
  707. bits = 12;
  708. break;
  709. default:
  710. bits = pf->depth * pf->nb_channels;
  711. break;
  712. }
  713. break;
  714. case FF_PIXEL_PLANAR:
  715. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  716. bits = pf->depth * pf->nb_channels;
  717. } else {
  718. bits = pf->depth + ((2 * pf->depth) >>
  719. (pf->x_chroma_shift + pf->y_chroma_shift));
  720. }
  721. break;
  722. case FF_PIXEL_PALETTE:
  723. bits = 8;
  724. break;
  725. default:
  726. bits = -1;
  727. break;
  728. }
  729. return bits;
  730. }
  731. static int avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  732. int src_pix_fmt,
  733. int has_alpha,
  734. int loss_mask)
  735. {
  736. int dist, i, loss, min_dist, dst_pix_fmt;
  737. /* find exact color match with smallest size */
  738. dst_pix_fmt = -1;
  739. min_dist = 0x7fffffff;
  740. for(i = 0;i < PIX_FMT_NB; i++) {
  741. if (pix_fmt_mask & (1ULL << i)) {
  742. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  743. if (loss == 0) {
  744. dist = avg_bits_per_pixel(i);
  745. if (dist < min_dist) {
  746. min_dist = dist;
  747. dst_pix_fmt = i;
  748. }
  749. }
  750. }
  751. }
  752. return dst_pix_fmt;
  753. }
  754. int avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, int src_pix_fmt,
  755. int has_alpha, int *loss_ptr)
  756. {
  757. int dst_pix_fmt, loss_mask, i;
  758. static const int loss_mask_order[] = {
  759. ~0, /* no loss first */
  760. ~FF_LOSS_ALPHA,
  761. ~FF_LOSS_RESOLUTION,
  762. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  763. ~FF_LOSS_COLORQUANT,
  764. ~FF_LOSS_DEPTH,
  765. 0,
  766. };
  767. /* try with successive loss */
  768. i = 0;
  769. for(;;) {
  770. loss_mask = loss_mask_order[i++];
  771. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  772. has_alpha, loss_mask);
  773. if (dst_pix_fmt >= 0)
  774. goto found;
  775. if (loss_mask == 0)
  776. break;
  777. }
  778. return -1;
  779. found:
  780. if (loss_ptr)
  781. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  782. return dst_pix_fmt;
  783. }
  784. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  785. const uint8_t *src, int src_wrap,
  786. int width, int height)
  787. {
  788. if((!dst) || (!src))
  789. return;
  790. for(;height > 0; height--) {
  791. memcpy(dst, src, width);
  792. dst += dst_wrap;
  793. src += src_wrap;
  794. }
  795. }
  796. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  797. {
  798. int bits;
  799. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  800. pf = &pix_fmt_info[pix_fmt];
  801. switch(pf->pixel_type) {
  802. case FF_PIXEL_PACKED:
  803. switch(pix_fmt) {
  804. case PIX_FMT_YUYV422:
  805. case PIX_FMT_UYVY422:
  806. case PIX_FMT_RGB565:
  807. case PIX_FMT_RGB555:
  808. case PIX_FMT_BGR565:
  809. case PIX_FMT_BGR555:
  810. bits = 16;
  811. break;
  812. case PIX_FMT_UYYVYY411:
  813. bits = 12;
  814. break;
  815. default:
  816. bits = pf->depth * pf->nb_channels;
  817. break;
  818. }
  819. return (width * bits + 7) >> 3;
  820. break;
  821. case FF_PIXEL_PLANAR:
  822. if (plane == 1 || plane == 2)
  823. width= -((-width)>>pf->x_chroma_shift);
  824. return (width * pf->depth + 7) >> 3;
  825. break;
  826. case FF_PIXEL_PALETTE:
  827. if (plane == 0)
  828. return width;
  829. break;
  830. }
  831. return -1;
  832. }
  833. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  834. int pix_fmt, int width, int height)
  835. {
  836. int i;
  837. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  838. pf = &pix_fmt_info[pix_fmt];
  839. switch(pf->pixel_type) {
  840. case FF_PIXEL_PACKED:
  841. case FF_PIXEL_PLANAR:
  842. for(i = 0; i < pf->nb_channels; i++) {
  843. int h;
  844. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  845. h = height;
  846. if (i == 1 || i == 2) {
  847. h= -((-height)>>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. /* 2x2 -> 1x1 */
  1184. void ff_shrink22(uint8_t *dst, int dst_wrap,
  1185. const uint8_t *src, int src_wrap,
  1186. int width, int height)
  1187. {
  1188. int w;
  1189. const uint8_t *s1, *s2;
  1190. uint8_t *d;
  1191. for(;height > 0; height--) {
  1192. s1 = src;
  1193. s2 = s1 + src_wrap;
  1194. d = dst;
  1195. for(w = width;w >= 4; w-=4) {
  1196. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1197. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  1198. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  1199. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1200. s1 += 8;
  1201. s2 += 8;
  1202. d += 4;
  1203. }
  1204. for(;w > 0; w--) {
  1205. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1206. s1 += 2;
  1207. s2 += 2;
  1208. d++;
  1209. }
  1210. src += 2 * src_wrap;
  1211. dst += dst_wrap;
  1212. }
  1213. }
  1214. /* 4x4 -> 1x1 */
  1215. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1216. const uint8_t *src, int src_wrap,
  1217. int width, int height)
  1218. {
  1219. int w;
  1220. const uint8_t *s1, *s2, *s3, *s4;
  1221. uint8_t *d;
  1222. for(;height > 0; height--) {
  1223. s1 = src;
  1224. s2 = s1 + src_wrap;
  1225. s3 = s2 + src_wrap;
  1226. s4 = s3 + src_wrap;
  1227. d = dst;
  1228. for(w = width;w > 0; w--) {
  1229. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1230. s2[0] + s2[1] + s2[2] + s2[3] +
  1231. s3[0] + s3[1] + s3[2] + s3[3] +
  1232. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1233. s1 += 4;
  1234. s2 += 4;
  1235. s3 += 4;
  1236. s4 += 4;
  1237. d++;
  1238. }
  1239. src += 4 * src_wrap;
  1240. dst += dst_wrap;
  1241. }
  1242. }
  1243. /* 8x8 -> 1x1 */
  1244. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1245. const uint8_t *src, int src_wrap,
  1246. int width, int height)
  1247. {
  1248. int w, i;
  1249. for(;height > 0; height--) {
  1250. for(w = width;w > 0; w--) {
  1251. int tmp=0;
  1252. for(i=0; i<8; i++){
  1253. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1254. src += src_wrap;
  1255. }
  1256. *(dst++) = (tmp + 32)>>6;
  1257. src += 8 - 8*src_wrap;
  1258. }
  1259. src += 8*src_wrap - 8*width;
  1260. dst += dst_wrap - width;
  1261. }
  1262. }
  1263. /* XXX: add jpeg quantize code */
  1264. #define TRANSP_INDEX (6*6*6)
  1265. /* this is maybe slow, but allows for extensions */
  1266. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  1267. {
  1268. return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
  1269. }
  1270. static void build_rgb_palette(uint8_t *palette, int has_alpha)
  1271. {
  1272. uint32_t *pal;
  1273. static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
  1274. int i, r, g, b;
  1275. pal = (uint32_t *)palette;
  1276. i = 0;
  1277. for(r = 0; r < 6; r++) {
  1278. for(g = 0; g < 6; g++) {
  1279. for(b = 0; b < 6; b++) {
  1280. pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
  1281. (pal_value[g] << 8) | pal_value[b];
  1282. }
  1283. }
  1284. }
  1285. if (has_alpha)
  1286. pal[i++] = 0;
  1287. while (i < 256)
  1288. pal[i++] = 0xff000000;
  1289. }
  1290. /* copy bit n to bits 0 ... n - 1 */
  1291. static inline unsigned int bitcopy_n(unsigned int a, int n)
  1292. {
  1293. int mask;
  1294. mask = (1 << n) - 1;
  1295. return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
  1296. }
  1297. /* rgb555 handling */
  1298. #define RGB_NAME rgb555
  1299. #define RGB_IN(r, g, b, s)\
  1300. {\
  1301. unsigned int v = ((const uint16_t *)(s))[0];\
  1302. r = bitcopy_n(v >> (10 - 3), 3);\
  1303. g = bitcopy_n(v >> (5 - 3), 3);\
  1304. b = bitcopy_n(v << 3, 3);\
  1305. }
  1306. #define RGB_OUT(d, r, g, b)\
  1307. {\
  1308. ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
  1309. }
  1310. #define BPP 2
  1311. #include "imgconvert_template.c"
  1312. /* rgb565 handling */
  1313. #define RGB_NAME rgb565
  1314. #define RGB_IN(r, g, b, s)\
  1315. {\
  1316. unsigned int v = ((const uint16_t *)(s))[0];\
  1317. r = bitcopy_n(v >> (11 - 3), 3);\
  1318. g = bitcopy_n(v >> (5 - 2), 2);\
  1319. b = bitcopy_n(v << 3, 3);\
  1320. }
  1321. #define RGB_OUT(d, r, g, b)\
  1322. {\
  1323. ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
  1324. }
  1325. #define BPP 2
  1326. #include "imgconvert_template.c"
  1327. /* bgr24 handling */
  1328. #define RGB_NAME bgr24
  1329. #define RGB_IN(r, g, b, s)\
  1330. {\
  1331. b = (s)[0];\
  1332. g = (s)[1];\
  1333. r = (s)[2];\
  1334. }
  1335. #define RGB_OUT(d, r, g, b)\
  1336. {\
  1337. (d)[0] = b;\
  1338. (d)[1] = g;\
  1339. (d)[2] = r;\
  1340. }
  1341. #define BPP 3
  1342. #include "imgconvert_template.c"
  1343. #undef RGB_IN
  1344. #undef RGB_OUT
  1345. #undef BPP
  1346. /* rgb24 handling */
  1347. #define RGB_NAME rgb24
  1348. #define FMT_RGB24
  1349. #define RGB_IN(r, g, b, s)\
  1350. {\
  1351. r = (s)[0];\
  1352. g = (s)[1];\
  1353. b = (s)[2];\
  1354. }
  1355. #define RGB_OUT(d, r, g, b)\
  1356. {\
  1357. (d)[0] = r;\
  1358. (d)[1] = g;\
  1359. (d)[2] = b;\
  1360. }
  1361. #define BPP 3
  1362. #include "imgconvert_template.c"
  1363. /* rgb32 handling */
  1364. #define RGB_NAME rgb32
  1365. #define FMT_RGB32
  1366. #define RGB_IN(r, g, b, s)\
  1367. {\
  1368. unsigned int v = ((const uint32_t *)(s))[0];\
  1369. r = (v >> 16) & 0xff;\
  1370. g = (v >> 8) & 0xff;\
  1371. b = v & 0xff;\
  1372. }
  1373. #define RGBA_IN(r, g, b, a, s)\
  1374. {\
  1375. unsigned int v = ((const uint32_t *)(s))[0];\
  1376. a = (v >> 24) & 0xff;\
  1377. r = (v >> 16) & 0xff;\
  1378. g = (v >> 8) & 0xff;\
  1379. b = v & 0xff;\
  1380. }
  1381. #define RGBA_OUT(d, r, g, b, a)\
  1382. {\
  1383. ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
  1384. }
  1385. #define BPP 4
  1386. #include "imgconvert_template.c"
  1387. static void mono_to_gray(AVPicture *dst, const AVPicture *src,
  1388. int width, int height, int xor_mask)
  1389. {
  1390. const unsigned char *p;
  1391. unsigned char *q;
  1392. int v, dst_wrap, src_wrap;
  1393. int y, w;
  1394. p = src->data[0];
  1395. src_wrap = src->linesize[0] - ((width + 7) >> 3);
  1396. q = dst->data[0];
  1397. dst_wrap = dst->linesize[0] - width;
  1398. for(y=0;y<height;y++) {
  1399. w = width;
  1400. while (w >= 8) {
  1401. v = *p++ ^ xor_mask;
  1402. q[0] = -(v >> 7);
  1403. q[1] = -((v >> 6) & 1);
  1404. q[2] = -((v >> 5) & 1);
  1405. q[3] = -((v >> 4) & 1);
  1406. q[4] = -((v >> 3) & 1);
  1407. q[5] = -((v >> 2) & 1);
  1408. q[6] = -((v >> 1) & 1);
  1409. q[7] = -((v >> 0) & 1);
  1410. w -= 8;
  1411. q += 8;
  1412. }
  1413. if (w > 0) {
  1414. v = *p++ ^ xor_mask;
  1415. do {
  1416. q[0] = -((v >> 7) & 1);
  1417. q++;
  1418. v <<= 1;
  1419. } while (--w);
  1420. }
  1421. p += src_wrap;
  1422. q += dst_wrap;
  1423. }
  1424. }
  1425. static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
  1426. int width, int height)
  1427. {
  1428. mono_to_gray(dst, src, width, height, 0xff);
  1429. }
  1430. static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
  1431. int width, int height)
  1432. {
  1433. mono_to_gray(dst, src, width, height, 0x00);
  1434. }
  1435. static void gray_to_mono(AVPicture *dst, const AVPicture *src,
  1436. int width, int height, int xor_mask)
  1437. {
  1438. int n;
  1439. const uint8_t *s;
  1440. uint8_t *d;
  1441. int j, b, v, n1, src_wrap, dst_wrap, y;
  1442. s = src->data[0];
  1443. src_wrap = src->linesize[0] - width;
  1444. d = dst->data[0];
  1445. dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
  1446. for(y=0;y<height;y++) {
  1447. n = width;
  1448. while (n >= 8) {
  1449. v = 0;
  1450. for(j=0;j<8;j++) {
  1451. b = s[0];
  1452. s++;
  1453. v = (v << 1) | (b >> 7);
  1454. }
  1455. d[0] = v ^ xor_mask;
  1456. d++;
  1457. n -= 8;
  1458. }
  1459. if (n > 0) {
  1460. n1 = n;
  1461. v = 0;
  1462. while (n > 0) {
  1463. b = s[0];
  1464. s++;
  1465. v = (v << 1) | (b >> 7);
  1466. n--;
  1467. }
  1468. d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
  1469. d++;
  1470. }
  1471. s += src_wrap;
  1472. d += dst_wrap;
  1473. }
  1474. }
  1475. static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
  1476. int width, int height)
  1477. {
  1478. gray_to_mono(dst, src, width, height, 0xff);
  1479. }
  1480. static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
  1481. int width, int height)
  1482. {
  1483. gray_to_mono(dst, src, width, height, 0x00);
  1484. }
  1485. static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
  1486. int width, int height)
  1487. {
  1488. int x, y, src_wrap, dst_wrap;
  1489. uint8_t *s, *d;
  1490. s = src->data[0];
  1491. src_wrap = src->linesize[0] - width;
  1492. d = dst->data[0];
  1493. dst_wrap = dst->linesize[0] - width * 2;
  1494. for(y=0; y<height; y++){
  1495. for(x=0; x<width; x++){
  1496. *d++ = *s;
  1497. *d++ = *s++;
  1498. }
  1499. s += src_wrap;
  1500. d += dst_wrap;
  1501. }
  1502. }
  1503. static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
  1504. int width, int height)
  1505. {
  1506. int x, y, src_wrap, dst_wrap;
  1507. uint8_t *s, *d;
  1508. s = src->data[0];
  1509. src_wrap = src->linesize[0] - width * 2;
  1510. d = dst->data[0];
  1511. dst_wrap = dst->linesize[0] - width;
  1512. for(y=0; y<height; y++){
  1513. for(x=0; x<width; x++){
  1514. *d++ = *s;
  1515. s += 2;
  1516. }
  1517. s += src_wrap;
  1518. d += dst_wrap;
  1519. }
  1520. }
  1521. static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
  1522. int width, int height)
  1523. {
  1524. gray16_to_gray(dst, src, width, height);
  1525. }
  1526. static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
  1527. int width, int height)
  1528. {
  1529. AVPicture tmpsrc = *src;
  1530. tmpsrc.data[0]++;
  1531. gray16_to_gray(dst, &tmpsrc, width, height);
  1532. }
  1533. static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
  1534. int width, int height)
  1535. {
  1536. int x, y, src_wrap, dst_wrap;
  1537. uint16_t *s, *d;
  1538. s = (uint16_t*)src->data[0];
  1539. src_wrap = (src->linesize[0] - width * 2)/2;
  1540. d = (uint16_t*)dst->data[0];
  1541. dst_wrap = (dst->linesize[0] - width * 2)/2;
  1542. for(y=0; y<height; y++){
  1543. for(x=0; x<width; x++){
  1544. *d++ = bswap_16(*s++);
  1545. }
  1546. s += src_wrap;
  1547. d += dst_wrap;
  1548. }
  1549. }
  1550. typedef struct ConvertEntry {
  1551. void (*convert)(AVPicture *dst,
  1552. const AVPicture *src, int width, int height);
  1553. } ConvertEntry;
  1554. /* Add each new conversion function in this table. In order to be able
  1555. to convert from any format to any format, the following constraints
  1556. must be satisfied:
  1557. - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
  1558. - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
  1559. - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
  1560. - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
  1561. PIX_FMT_RGB24.
  1562. - PIX_FMT_422 must convert to and from PIX_FMT_422P.
  1563. The other conversion functions are just optimizations for common cases.
  1564. */
  1565. static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
  1566. [PIX_FMT_YUV420P] = {
  1567. [PIX_FMT_YUYV422] = {
  1568. .convert = yuv420p_to_yuyv422,
  1569. },
  1570. [PIX_FMT_RGB555] = {
  1571. .convert = yuv420p_to_rgb555
  1572. },
  1573. [PIX_FMT_RGB565] = {
  1574. .convert = yuv420p_to_rgb565
  1575. },
  1576. [PIX_FMT_BGR24] = {
  1577. .convert = yuv420p_to_bgr24
  1578. },
  1579. [PIX_FMT_RGB24] = {
  1580. .convert = yuv420p_to_rgb24
  1581. },
  1582. [PIX_FMT_RGB32] = {
  1583. .convert = yuv420p_to_rgb32
  1584. },
  1585. [PIX_FMT_UYVY422] = {
  1586. .convert = yuv420p_to_uyvy422,
  1587. },
  1588. },
  1589. [PIX_FMT_YUV422P] = {
  1590. [PIX_FMT_YUYV422] = {
  1591. .convert = yuv422p_to_yuyv422,
  1592. },
  1593. [PIX_FMT_UYVY422] = {
  1594. .convert = yuv422p_to_uyvy422,
  1595. },
  1596. },
  1597. [PIX_FMT_YUV444P] = {
  1598. [PIX_FMT_RGB24] = {
  1599. .convert = yuv444p_to_rgb24
  1600. },
  1601. },
  1602. [PIX_FMT_YUVJ420P] = {
  1603. [PIX_FMT_RGB555] = {
  1604. .convert = yuvj420p_to_rgb555
  1605. },
  1606. [PIX_FMT_RGB565] = {
  1607. .convert = yuvj420p_to_rgb565
  1608. },
  1609. [PIX_FMT_BGR24] = {
  1610. .convert = yuvj420p_to_bgr24
  1611. },
  1612. [PIX_FMT_RGB24] = {
  1613. .convert = yuvj420p_to_rgb24
  1614. },
  1615. [PIX_FMT_RGB32] = {
  1616. .convert = yuvj420p_to_rgb32
  1617. },
  1618. },
  1619. [PIX_FMT_YUVJ444P] = {
  1620. [PIX_FMT_RGB24] = {
  1621. .convert = yuvj444p_to_rgb24
  1622. },
  1623. },
  1624. [PIX_FMT_YUYV422] = {
  1625. [PIX_FMT_YUV420P] = {
  1626. .convert = yuyv422_to_yuv420p,
  1627. },
  1628. [PIX_FMT_YUV422P] = {
  1629. .convert = yuyv422_to_yuv422p,
  1630. },
  1631. },
  1632. [PIX_FMT_UYVY422] = {
  1633. [PIX_FMT_YUV420P] = {
  1634. .convert = uyvy422_to_yuv420p,
  1635. },
  1636. [PIX_FMT_YUV422P] = {
  1637. .convert = uyvy422_to_yuv422p,
  1638. },
  1639. },
  1640. [PIX_FMT_RGB24] = {
  1641. [PIX_FMT_YUV420P] = {
  1642. .convert = rgb24_to_yuv420p
  1643. },
  1644. [PIX_FMT_RGB565] = {
  1645. .convert = rgb24_to_rgb565
  1646. },
  1647. [PIX_FMT_RGB555] = {
  1648. .convert = rgb24_to_rgb555
  1649. },
  1650. [PIX_FMT_RGB32] = {
  1651. .convert = rgb24_to_rgb32
  1652. },
  1653. [PIX_FMT_BGR24] = {
  1654. .convert = rgb24_to_bgr24
  1655. },
  1656. [PIX_FMT_GRAY8] = {
  1657. .convert = rgb24_to_gray
  1658. },
  1659. [PIX_FMT_PAL8] = {
  1660. .convert = rgb24_to_pal8
  1661. },
  1662. [PIX_FMT_YUV444P] = {
  1663. .convert = rgb24_to_yuv444p
  1664. },
  1665. [PIX_FMT_YUVJ420P] = {
  1666. .convert = rgb24_to_yuvj420p
  1667. },
  1668. [PIX_FMT_YUVJ444P] = {
  1669. .convert = rgb24_to_yuvj444p
  1670. },
  1671. },
  1672. [PIX_FMT_RGB32] = {
  1673. [PIX_FMT_RGB24] = {
  1674. .convert = rgb32_to_rgb24
  1675. },
  1676. [PIX_FMT_BGR24] = {
  1677. .convert = rgb32_to_bgr24
  1678. },
  1679. [PIX_FMT_RGB565] = {
  1680. .convert = rgb32_to_rgb565
  1681. },
  1682. [PIX_FMT_RGB555] = {
  1683. .convert = rgb32_to_rgb555
  1684. },
  1685. [PIX_FMT_PAL8] = {
  1686. .convert = rgb32_to_pal8
  1687. },
  1688. [PIX_FMT_YUV420P] = {
  1689. .convert = rgb32_to_yuv420p
  1690. },
  1691. [PIX_FMT_GRAY8] = {
  1692. .convert = rgb32_to_gray
  1693. },
  1694. },
  1695. [PIX_FMT_BGR24] = {
  1696. [PIX_FMT_RGB32] = {
  1697. .convert = bgr24_to_rgb32
  1698. },
  1699. [PIX_FMT_RGB24] = {
  1700. .convert = bgr24_to_rgb24
  1701. },
  1702. [PIX_FMT_YUV420P] = {
  1703. .convert = bgr24_to_yuv420p
  1704. },
  1705. [PIX_FMT_GRAY8] = {
  1706. .convert = bgr24_to_gray
  1707. },
  1708. },
  1709. [PIX_FMT_RGB555] = {
  1710. [PIX_FMT_RGB24] = {
  1711. .convert = rgb555_to_rgb24
  1712. },
  1713. [PIX_FMT_RGB32] = {
  1714. .convert = rgb555_to_rgb32
  1715. },
  1716. [PIX_FMT_YUV420P] = {
  1717. .convert = rgb555_to_yuv420p
  1718. },
  1719. [PIX_FMT_GRAY8] = {
  1720. .convert = rgb555_to_gray
  1721. },
  1722. },
  1723. [PIX_FMT_RGB565] = {
  1724. [PIX_FMT_RGB32] = {
  1725. .convert = rgb565_to_rgb32
  1726. },
  1727. [PIX_FMT_RGB24] = {
  1728. .convert = rgb565_to_rgb24
  1729. },
  1730. [PIX_FMT_YUV420P] = {
  1731. .convert = rgb565_to_yuv420p
  1732. },
  1733. [PIX_FMT_GRAY8] = {
  1734. .convert = rgb565_to_gray
  1735. },
  1736. },
  1737. [PIX_FMT_GRAY16BE] = {
  1738. [PIX_FMT_GRAY8] = {
  1739. .convert = gray16be_to_gray
  1740. },
  1741. [PIX_FMT_GRAY16LE] = {
  1742. .convert = gray16_to_gray16
  1743. },
  1744. },
  1745. [PIX_FMT_GRAY16LE] = {
  1746. [PIX_FMT_GRAY8] = {
  1747. .convert = gray16le_to_gray
  1748. },
  1749. [PIX_FMT_GRAY16BE] = {
  1750. .convert = gray16_to_gray16
  1751. },
  1752. },
  1753. [PIX_FMT_GRAY8] = {
  1754. [PIX_FMT_RGB555] = {
  1755. .convert = gray_to_rgb555
  1756. },
  1757. [PIX_FMT_RGB565] = {
  1758. .convert = gray_to_rgb565
  1759. },
  1760. [PIX_FMT_RGB24] = {
  1761. .convert = gray_to_rgb24
  1762. },
  1763. [PIX_FMT_BGR24] = {
  1764. .convert = gray_to_bgr24
  1765. },
  1766. [PIX_FMT_RGB32] = {
  1767. .convert = gray_to_rgb32
  1768. },
  1769. [PIX_FMT_MONOWHITE] = {
  1770. .convert = gray_to_monowhite
  1771. },
  1772. [PIX_FMT_MONOBLACK] = {
  1773. .convert = gray_to_monoblack
  1774. },
  1775. [PIX_FMT_GRAY16LE] = {
  1776. .convert = gray_to_gray16
  1777. },
  1778. [PIX_FMT_GRAY16BE] = {
  1779. .convert = gray_to_gray16
  1780. },
  1781. },
  1782. [PIX_FMT_MONOWHITE] = {
  1783. [PIX_FMT_GRAY8] = {
  1784. .convert = monowhite_to_gray
  1785. },
  1786. },
  1787. [PIX_FMT_MONOBLACK] = {
  1788. [PIX_FMT_GRAY8] = {
  1789. .convert = monoblack_to_gray
  1790. },
  1791. },
  1792. [PIX_FMT_PAL8] = {
  1793. [PIX_FMT_RGB555] = {
  1794. .convert = pal8_to_rgb555
  1795. },
  1796. [PIX_FMT_RGB565] = {
  1797. .convert = pal8_to_rgb565
  1798. },
  1799. [PIX_FMT_BGR24] = {
  1800. .convert = pal8_to_bgr24
  1801. },
  1802. [PIX_FMT_RGB24] = {
  1803. .convert = pal8_to_rgb24
  1804. },
  1805. [PIX_FMT_RGB32] = {
  1806. .convert = pal8_to_rgb32
  1807. },
  1808. },
  1809. [PIX_FMT_UYYVYY411] = {
  1810. [PIX_FMT_YUV411P] = {
  1811. .convert = uyyvyy411_to_yuv411p,
  1812. },
  1813. },
  1814. };
  1815. int avpicture_alloc(AVPicture *picture,
  1816. int pix_fmt, int width, int height)
  1817. {
  1818. int size;
  1819. void *ptr;
  1820. size = avpicture_get_size(pix_fmt, width, height);
  1821. if(size<0)
  1822. goto fail;
  1823. ptr = av_malloc(size);
  1824. if (!ptr)
  1825. goto fail;
  1826. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1827. return 0;
  1828. fail:
  1829. memset(picture, 0, sizeof(AVPicture));
  1830. return -1;
  1831. }
  1832. void avpicture_free(AVPicture *picture)
  1833. {
  1834. av_free(picture->data[0]);
  1835. }
  1836. /* return true if yuv planar */
  1837. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1838. {
  1839. return (ps->color_type == FF_COLOR_YUV ||
  1840. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1841. ps->pixel_type == FF_PIXEL_PLANAR;
  1842. }
  1843. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1844. int pix_fmt, int top_band, int left_band)
  1845. {
  1846. int y_shift;
  1847. int x_shift;
  1848. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1849. return -1;
  1850. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1851. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1852. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1853. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1854. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1855. dst->linesize[0] = src->linesize[0];
  1856. dst->linesize[1] = src->linesize[1];
  1857. dst->linesize[2] = src->linesize[2];
  1858. return 0;
  1859. }
  1860. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1861. int pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1862. int *color)
  1863. {
  1864. uint8_t *optr;
  1865. int y_shift;
  1866. int x_shift;
  1867. int yheight;
  1868. int i, y;
  1869. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1870. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1871. for (i = 0; i < 3; i++) {
  1872. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1873. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1874. if (padtop || padleft) {
  1875. memset(dst->data[i], color[i],
  1876. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1877. }
  1878. if (padleft || padright) {
  1879. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1880. (dst->linesize[i] - (padright >> x_shift));
  1881. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1882. for (y = 0; y < yheight; y++) {
  1883. memset(optr, color[i], (padleft + padright) >> x_shift);
  1884. optr += dst->linesize[i];
  1885. }
  1886. }
  1887. if (src) { /* first line */
  1888. uint8_t *iptr = src->data[i];
  1889. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1890. (padleft >> x_shift);
  1891. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1892. iptr += src->linesize[i];
  1893. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1894. (dst->linesize[i] - (padright >> x_shift));
  1895. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1896. for (y = 0; y < yheight; y++) {
  1897. memset(optr, color[i], (padleft + padright) >> x_shift);
  1898. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1899. (width - padleft - padright) >> x_shift);
  1900. iptr += src->linesize[i];
  1901. optr += dst->linesize[i];
  1902. }
  1903. }
  1904. if (padbottom || padright) {
  1905. optr = dst->data[i] + dst->linesize[i] *
  1906. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1907. memset(optr, color[i],dst->linesize[i] *
  1908. (padbottom >> y_shift) + (padright >> x_shift));
  1909. }
  1910. }
  1911. return 0;
  1912. }
  1913. #ifndef CONFIG_SWSCALE
  1914. static uint8_t y_ccir_to_jpeg[256];
  1915. static uint8_t y_jpeg_to_ccir[256];
  1916. static uint8_t c_ccir_to_jpeg[256];
  1917. static uint8_t c_jpeg_to_ccir[256];
  1918. /* init various conversion tables */
  1919. static void img_convert_init(void)
  1920. {
  1921. int i;
  1922. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1923. for(i = 0;i < 256; i++) {
  1924. y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
  1925. y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
  1926. c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
  1927. c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
  1928. }
  1929. }
  1930. /* apply to each pixel the given table */
  1931. static void img_apply_table(uint8_t *dst, int dst_wrap,
  1932. const uint8_t *src, int src_wrap,
  1933. int width, int height, const uint8_t *table1)
  1934. {
  1935. int n;
  1936. const uint8_t *s;
  1937. uint8_t *d;
  1938. const uint8_t *table;
  1939. table = table1;
  1940. for(;height > 0; height--) {
  1941. s = src;
  1942. d = dst;
  1943. n = width;
  1944. while (n >= 4) {
  1945. d[0] = table[s[0]];
  1946. d[1] = table[s[1]];
  1947. d[2] = table[s[2]];
  1948. d[3] = table[s[3]];
  1949. d += 4;
  1950. s += 4;
  1951. n -= 4;
  1952. }
  1953. while (n > 0) {
  1954. d[0] = table[s[0]];
  1955. d++;
  1956. s++;
  1957. n--;
  1958. }
  1959. dst += dst_wrap;
  1960. src += src_wrap;
  1961. }
  1962. }
  1963. /* XXX: use generic filter ? */
  1964. /* XXX: in most cases, the sampling position is incorrect */
  1965. /* 4x1 -> 1x1 */
  1966. static void shrink41(uint8_t *dst, int dst_wrap,
  1967. const uint8_t *src, int src_wrap,
  1968. int width, int height)
  1969. {
  1970. int w;
  1971. const uint8_t *s;
  1972. uint8_t *d;
  1973. for(;height > 0; height--) {
  1974. s = src;
  1975. d = dst;
  1976. for(w = width;w > 0; w--) {
  1977. d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
  1978. s += 4;
  1979. d++;
  1980. }
  1981. src += src_wrap;
  1982. dst += dst_wrap;
  1983. }
  1984. }
  1985. /* 2x1 -> 1x1 */
  1986. static void shrink21(uint8_t *dst, int dst_wrap,
  1987. const uint8_t *src, int src_wrap,
  1988. int width, int height)
  1989. {
  1990. int w;
  1991. const uint8_t *s;
  1992. uint8_t *d;
  1993. for(;height > 0; height--) {
  1994. s = src;
  1995. d = dst;
  1996. for(w = width;w > 0; w--) {
  1997. d[0] = (s[0] + s[1]) >> 1;
  1998. s += 2;
  1999. d++;
  2000. }
  2001. src += src_wrap;
  2002. dst += dst_wrap;
  2003. }
  2004. }
  2005. /* 1x2 -> 1x1 */
  2006. static void shrink12(uint8_t *dst, int dst_wrap,
  2007. const uint8_t *src, int src_wrap,
  2008. int width, int height)
  2009. {
  2010. int w;
  2011. uint8_t *d;
  2012. const uint8_t *s1, *s2;
  2013. for(;height > 0; height--) {
  2014. s1 = src;
  2015. s2 = s1 + src_wrap;
  2016. d = dst;
  2017. for(w = width;w >= 4; w-=4) {
  2018. d[0] = (s1[0] + s2[0]) >> 1;
  2019. d[1] = (s1[1] + s2[1]) >> 1;
  2020. d[2] = (s1[2] + s2[2]) >> 1;
  2021. d[3] = (s1[3] + s2[3]) >> 1;
  2022. s1 += 4;
  2023. s2 += 4;
  2024. d += 4;
  2025. }
  2026. for(;w > 0; w--) {
  2027. d[0] = (s1[0] + s2[0]) >> 1;
  2028. s1++;
  2029. s2++;
  2030. d++;
  2031. }
  2032. src += 2 * src_wrap;
  2033. dst += dst_wrap;
  2034. }
  2035. }
  2036. static void grow21_line(uint8_t *dst, const uint8_t *src,
  2037. int width)
  2038. {
  2039. int w;
  2040. const uint8_t *s1;
  2041. uint8_t *d;
  2042. s1 = src;
  2043. d = dst;
  2044. for(w = width;w >= 4; w-=4) {
  2045. d[1] = d[0] = s1[0];
  2046. d[3] = d[2] = s1[1];
  2047. s1 += 2;
  2048. d += 4;
  2049. }
  2050. for(;w >= 2; w -= 2) {
  2051. d[1] = d[0] = s1[0];
  2052. s1 ++;
  2053. d += 2;
  2054. }
  2055. /* only needed if width is not a multiple of two */
  2056. /* XXX: veryfy that */
  2057. if (w) {
  2058. d[0] = s1[0];
  2059. }
  2060. }
  2061. static void grow41_line(uint8_t *dst, const uint8_t *src,
  2062. int width)
  2063. {
  2064. int w, v;
  2065. const uint8_t *s1;
  2066. uint8_t *d;
  2067. s1 = src;
  2068. d = dst;
  2069. for(w = width;w >= 4; w-=4) {
  2070. v = s1[0];
  2071. d[0] = v;
  2072. d[1] = v;
  2073. d[2] = v;
  2074. d[3] = v;
  2075. s1 ++;
  2076. d += 4;
  2077. }
  2078. }
  2079. /* 1x1 -> 2x1 */
  2080. static void grow21(uint8_t *dst, int dst_wrap,
  2081. const uint8_t *src, int src_wrap,
  2082. int width, int height)
  2083. {
  2084. for(;height > 0; height--) {
  2085. grow21_line(dst, src, width);
  2086. src += src_wrap;
  2087. dst += dst_wrap;
  2088. }
  2089. }
  2090. /* 1x1 -> 1x2 */
  2091. static void grow12(uint8_t *dst, int dst_wrap,
  2092. const uint8_t *src, int src_wrap,
  2093. int width, int height)
  2094. {
  2095. for(;height > 0; height-=2) {
  2096. memcpy(dst, src, width);
  2097. dst += dst_wrap;
  2098. memcpy(dst, src, width);
  2099. dst += dst_wrap;
  2100. src += src_wrap;
  2101. }
  2102. }
  2103. /* 1x1 -> 2x2 */
  2104. static void grow22(uint8_t *dst, int dst_wrap,
  2105. const uint8_t *src, int src_wrap,
  2106. int width, int height)
  2107. {
  2108. for(;height > 0; height--) {
  2109. grow21_line(dst, src, width);
  2110. if (height%2)
  2111. src += src_wrap;
  2112. dst += dst_wrap;
  2113. }
  2114. }
  2115. /* 1x1 -> 4x1 */
  2116. static void grow41(uint8_t *dst, int dst_wrap,
  2117. const uint8_t *src, int src_wrap,
  2118. int width, int height)
  2119. {
  2120. for(;height > 0; height--) {
  2121. grow41_line(dst, src, width);
  2122. src += src_wrap;
  2123. dst += dst_wrap;
  2124. }
  2125. }
  2126. /* 1x1 -> 4x4 */
  2127. static void grow44(uint8_t *dst, int dst_wrap,
  2128. const uint8_t *src, int src_wrap,
  2129. int width, int height)
  2130. {
  2131. for(;height > 0; height--) {
  2132. grow41_line(dst, src, width);
  2133. if ((height & 3) == 1)
  2134. src += src_wrap;
  2135. dst += dst_wrap;
  2136. }
  2137. }
  2138. /* 1x2 -> 2x1 */
  2139. static void conv411(uint8_t *dst, int dst_wrap,
  2140. const uint8_t *src, int src_wrap,
  2141. int width, int height)
  2142. {
  2143. int w, c;
  2144. const uint8_t *s1, *s2;
  2145. uint8_t *d;
  2146. width>>=1;
  2147. for(;height > 0; height--) {
  2148. s1 = src;
  2149. s2 = src + src_wrap;
  2150. d = dst;
  2151. for(w = width;w > 0; w--) {
  2152. c = (s1[0] + s2[0]) >> 1;
  2153. d[0] = c;
  2154. d[1] = c;
  2155. s1++;
  2156. s2++;
  2157. d += 2;
  2158. }
  2159. src += src_wrap * 2;
  2160. dst += dst_wrap;
  2161. }
  2162. }
  2163. /* XXX: always use linesize. Return -1 if not supported */
  2164. int img_convert(AVPicture *dst, int dst_pix_fmt,
  2165. const AVPicture *src, int src_pix_fmt,
  2166. int src_width, int src_height)
  2167. {
  2168. static int initialized;
  2169. int i, ret, dst_width, dst_height, int_pix_fmt;
  2170. const PixFmtInfo *src_pix, *dst_pix;
  2171. const ConvertEntry *ce;
  2172. AVPicture tmp1, *tmp = &tmp1;
  2173. if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
  2174. dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
  2175. return -1;
  2176. if (src_width <= 0 || src_height <= 0)
  2177. return 0;
  2178. if (!initialized) {
  2179. initialized = 1;
  2180. img_convert_init();
  2181. }
  2182. dst_width = src_width;
  2183. dst_height = src_height;
  2184. dst_pix = &pix_fmt_info[dst_pix_fmt];
  2185. src_pix = &pix_fmt_info[src_pix_fmt];
  2186. if (src_pix_fmt == dst_pix_fmt) {
  2187. /* no conversion needed: just copy */
  2188. av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
  2189. return 0;
  2190. }
  2191. ce = &convert_table[src_pix_fmt][dst_pix_fmt];
  2192. if (ce->convert) {
  2193. /* specific conversion routine */
  2194. ce->convert(dst, src, dst_width, dst_height);
  2195. return 0;
  2196. }
  2197. /* gray to YUV */
  2198. if (is_yuv_planar(dst_pix) &&
  2199. src_pix_fmt == PIX_FMT_GRAY8) {
  2200. int w, h, y;
  2201. uint8_t *d;
  2202. if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
  2203. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2204. src->data[0], src->linesize[0],
  2205. dst_width, dst_height);
  2206. } else {
  2207. img_apply_table(dst->data[0], dst->linesize[0],
  2208. src->data[0], src->linesize[0],
  2209. dst_width, dst_height,
  2210. y_jpeg_to_ccir);
  2211. }
  2212. /* fill U and V with 128 */
  2213. w = dst_width;
  2214. h = dst_height;
  2215. w >>= dst_pix->x_chroma_shift;
  2216. h >>= dst_pix->y_chroma_shift;
  2217. for(i = 1; i <= 2; i++) {
  2218. d = dst->data[i];
  2219. for(y = 0; y< h; y++) {
  2220. memset(d, 128, w);
  2221. d += dst->linesize[i];
  2222. }
  2223. }
  2224. return 0;
  2225. }
  2226. /* YUV to gray */
  2227. if (is_yuv_planar(src_pix) &&
  2228. dst_pix_fmt == PIX_FMT_GRAY8) {
  2229. if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
  2230. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2231. src->data[0], src->linesize[0],
  2232. dst_width, dst_height);
  2233. } else {
  2234. img_apply_table(dst->data[0], dst->linesize[0],
  2235. src->data[0], src->linesize[0],
  2236. dst_width, dst_height,
  2237. y_ccir_to_jpeg);
  2238. }
  2239. return 0;
  2240. }
  2241. /* YUV to YUV planar */
  2242. if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
  2243. int x_shift, y_shift, w, h, xy_shift;
  2244. void (*resize_func)(uint8_t *dst, int dst_wrap,
  2245. const uint8_t *src, int src_wrap,
  2246. int width, int height);
  2247. /* compute chroma size of the smallest dimensions */
  2248. w = dst_width;
  2249. h = dst_height;
  2250. if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
  2251. w >>= dst_pix->x_chroma_shift;
  2252. else
  2253. w >>= src_pix->x_chroma_shift;
  2254. if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
  2255. h >>= dst_pix->y_chroma_shift;
  2256. else
  2257. h >>= src_pix->y_chroma_shift;
  2258. x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
  2259. y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
  2260. xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
  2261. /* there must be filters for conversion at least from and to
  2262. YUV444 format */
  2263. switch(xy_shift) {
  2264. case 0x00:
  2265. resize_func = ff_img_copy_plane;
  2266. break;
  2267. case 0x10:
  2268. resize_func = shrink21;
  2269. break;
  2270. case 0x20:
  2271. resize_func = shrink41;
  2272. break;
  2273. case 0x01:
  2274. resize_func = shrink12;
  2275. break;
  2276. case 0x11:
  2277. resize_func = ff_shrink22;
  2278. break;
  2279. case 0x22:
  2280. resize_func = ff_shrink44;
  2281. break;
  2282. case 0xf0:
  2283. resize_func = grow21;
  2284. break;
  2285. case 0x0f:
  2286. resize_func = grow12;
  2287. break;
  2288. case 0xe0:
  2289. resize_func = grow41;
  2290. break;
  2291. case 0xff:
  2292. resize_func = grow22;
  2293. break;
  2294. case 0xee:
  2295. resize_func = grow44;
  2296. break;
  2297. case 0xf1:
  2298. resize_func = conv411;
  2299. break;
  2300. default:
  2301. /* currently not handled */
  2302. goto no_chroma_filter;
  2303. }
  2304. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2305. src->data[0], src->linesize[0],
  2306. dst_width, dst_height);
  2307. for(i = 1;i <= 2; i++)
  2308. resize_func(dst->data[i], dst->linesize[i],
  2309. src->data[i], src->linesize[i],
  2310. dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
  2311. /* if yuv color space conversion is needed, we do it here on
  2312. the destination image */
  2313. if (dst_pix->color_type != src_pix->color_type) {
  2314. const uint8_t *y_table, *c_table;
  2315. if (dst_pix->color_type == FF_COLOR_YUV) {
  2316. y_table = y_jpeg_to_ccir;
  2317. c_table = c_jpeg_to_ccir;
  2318. } else {
  2319. y_table = y_ccir_to_jpeg;
  2320. c_table = c_ccir_to_jpeg;
  2321. }
  2322. img_apply_table(dst->data[0], dst->linesize[0],
  2323. dst->data[0], dst->linesize[0],
  2324. dst_width, dst_height,
  2325. y_table);
  2326. for(i = 1;i <= 2; i++)
  2327. img_apply_table(dst->data[i], dst->linesize[i],
  2328. dst->data[i], dst->linesize[i],
  2329. dst_width>>dst_pix->x_chroma_shift,
  2330. dst_height>>dst_pix->y_chroma_shift,
  2331. c_table);
  2332. }
  2333. return 0;
  2334. }
  2335. no_chroma_filter:
  2336. /* try to use an intermediate format */
  2337. if (src_pix_fmt == PIX_FMT_YUYV422 ||
  2338. dst_pix_fmt == PIX_FMT_YUYV422) {
  2339. /* specific case: convert to YUV422P first */
  2340. int_pix_fmt = PIX_FMT_YUV422P;
  2341. } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
  2342. dst_pix_fmt == PIX_FMT_UYVY422) {
  2343. /* specific case: convert to YUV422P first */
  2344. int_pix_fmt = PIX_FMT_YUV422P;
  2345. } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
  2346. dst_pix_fmt == PIX_FMT_UYYVYY411) {
  2347. /* specific case: convert to YUV411P first */
  2348. int_pix_fmt = PIX_FMT_YUV411P;
  2349. } else if ((src_pix->color_type == FF_COLOR_GRAY &&
  2350. src_pix_fmt != PIX_FMT_GRAY8) ||
  2351. (dst_pix->color_type == FF_COLOR_GRAY &&
  2352. dst_pix_fmt != PIX_FMT_GRAY8)) {
  2353. /* gray8 is the normalized format */
  2354. int_pix_fmt = PIX_FMT_GRAY8;
  2355. } else if ((is_yuv_planar(src_pix) &&
  2356. src_pix_fmt != PIX_FMT_YUV444P &&
  2357. src_pix_fmt != PIX_FMT_YUVJ444P)) {
  2358. /* yuv444 is the normalized format */
  2359. if (src_pix->color_type == FF_COLOR_YUV_JPEG)
  2360. int_pix_fmt = PIX_FMT_YUVJ444P;
  2361. else
  2362. int_pix_fmt = PIX_FMT_YUV444P;
  2363. } else if ((is_yuv_planar(dst_pix) &&
  2364. dst_pix_fmt != PIX_FMT_YUV444P &&
  2365. dst_pix_fmt != PIX_FMT_YUVJ444P)) {
  2366. /* yuv444 is the normalized format */
  2367. if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
  2368. int_pix_fmt = PIX_FMT_YUVJ444P;
  2369. else
  2370. int_pix_fmt = PIX_FMT_YUV444P;
  2371. } else {
  2372. /* the two formats are rgb or gray8 or yuv[j]444p */
  2373. if (src_pix->is_alpha && dst_pix->is_alpha)
  2374. int_pix_fmt = PIX_FMT_RGB32;
  2375. else
  2376. int_pix_fmt = PIX_FMT_RGB24;
  2377. }
  2378. if (src_pix_fmt == int_pix_fmt)
  2379. return -1;
  2380. if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
  2381. return -1;
  2382. ret = -1;
  2383. if (img_convert(tmp, int_pix_fmt,
  2384. src, src_pix_fmt, src_width, src_height) < 0)
  2385. goto fail1;
  2386. if (img_convert(dst, dst_pix_fmt,
  2387. tmp, int_pix_fmt, dst_width, dst_height) < 0)
  2388. goto fail1;
  2389. ret = 0;
  2390. fail1:
  2391. avpicture_free(tmp);
  2392. return ret;
  2393. }
  2394. #endif
  2395. /* NOTE: we scan all the pixels to have an exact information */
  2396. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  2397. {
  2398. const unsigned char *p;
  2399. int src_wrap, ret, x, y;
  2400. unsigned int a;
  2401. uint32_t *palette = (uint32_t *)src->data[1];
  2402. p = src->data[0];
  2403. src_wrap = src->linesize[0] - width;
  2404. ret = 0;
  2405. for(y=0;y<height;y++) {
  2406. for(x=0;x<width;x++) {
  2407. a = palette[p[0]] >> 24;
  2408. if (a == 0x00) {
  2409. ret |= FF_ALPHA_TRANSP;
  2410. } else if (a != 0xff) {
  2411. ret |= FF_ALPHA_SEMI_TRANSP;
  2412. }
  2413. p++;
  2414. }
  2415. p += src_wrap;
  2416. }
  2417. return ret;
  2418. }
  2419. int img_get_alpha_info(const AVPicture *src,
  2420. int pix_fmt, int width, int height)
  2421. {
  2422. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  2423. int ret;
  2424. pf = &pix_fmt_info[pix_fmt];
  2425. /* no alpha can be represented in format */
  2426. if (!pf->is_alpha)
  2427. return 0;
  2428. switch(pix_fmt) {
  2429. case PIX_FMT_RGB32:
  2430. ret = get_alpha_info_rgb32(src, width, height);
  2431. break;
  2432. case PIX_FMT_PAL8:
  2433. ret = get_alpha_info_pal8(src, width, height);
  2434. break;
  2435. default:
  2436. /* we do not know, so everything is indicated */
  2437. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  2438. break;
  2439. }
  2440. return ret;
  2441. }
  2442. #ifdef HAVE_MMX
  2443. #define DEINT_INPLACE_LINE_LUM \
  2444. movd_m2r(lum_m4[0],mm0);\
  2445. movd_m2r(lum_m3[0],mm1);\
  2446. movd_m2r(lum_m2[0],mm2);\
  2447. movd_m2r(lum_m1[0],mm3);\
  2448. movd_m2r(lum[0],mm4);\
  2449. punpcklbw_r2r(mm7,mm0);\
  2450. movd_r2m(mm2,lum_m4[0]);\
  2451. punpcklbw_r2r(mm7,mm1);\
  2452. punpcklbw_r2r(mm7,mm2);\
  2453. punpcklbw_r2r(mm7,mm3);\
  2454. punpcklbw_r2r(mm7,mm4);\
  2455. paddw_r2r(mm3,mm1);\
  2456. psllw_i2r(1,mm2);\
  2457. paddw_r2r(mm4,mm0);\
  2458. psllw_i2r(2,mm1);\
  2459. paddw_r2r(mm6,mm2);\
  2460. paddw_r2r(mm2,mm1);\
  2461. psubusw_r2r(mm0,mm1);\
  2462. psrlw_i2r(3,mm1);\
  2463. packuswb_r2r(mm7,mm1);\
  2464. movd_r2m(mm1,lum_m2[0]);
  2465. #define DEINT_LINE_LUM \
  2466. movd_m2r(lum_m4[0],mm0);\
  2467. movd_m2r(lum_m3[0],mm1);\
  2468. movd_m2r(lum_m2[0],mm2);\
  2469. movd_m2r(lum_m1[0],mm3);\
  2470. movd_m2r(lum[0],mm4);\
  2471. punpcklbw_r2r(mm7,mm0);\
  2472. punpcklbw_r2r(mm7,mm1);\
  2473. punpcklbw_r2r(mm7,mm2);\
  2474. punpcklbw_r2r(mm7,mm3);\
  2475. punpcklbw_r2r(mm7,mm4);\
  2476. paddw_r2r(mm3,mm1);\
  2477. psllw_i2r(1,mm2);\
  2478. paddw_r2r(mm4,mm0);\
  2479. psllw_i2r(2,mm1);\
  2480. paddw_r2r(mm6,mm2);\
  2481. paddw_r2r(mm2,mm1);\
  2482. psubusw_r2r(mm0,mm1);\
  2483. psrlw_i2r(3,mm1);\
  2484. packuswb_r2r(mm7,mm1);\
  2485. movd_r2m(mm1,dst[0]);
  2486. #endif
  2487. /* filter parameters: [-1 4 2 4 -1] // 8 */
  2488. static void deinterlace_line(uint8_t *dst,
  2489. const uint8_t *lum_m4, const uint8_t *lum_m3,
  2490. const uint8_t *lum_m2, const uint8_t *lum_m1,
  2491. const uint8_t *lum,
  2492. int size)
  2493. {
  2494. #ifndef HAVE_MMX
  2495. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2496. int sum;
  2497. for(;size > 0;size--) {
  2498. sum = -lum_m4[0];
  2499. sum += lum_m3[0] << 2;
  2500. sum += lum_m2[0] << 1;
  2501. sum += lum_m1[0] << 2;
  2502. sum += -lum[0];
  2503. dst[0] = cm[(sum + 4) >> 3];
  2504. lum_m4++;
  2505. lum_m3++;
  2506. lum_m2++;
  2507. lum_m1++;
  2508. lum++;
  2509. dst++;
  2510. }
  2511. #else
  2512. {
  2513. pxor_r2r(mm7,mm7);
  2514. movq_m2r(ff_pw_4,mm6);
  2515. }
  2516. for (;size > 3; size-=4) {
  2517. DEINT_LINE_LUM
  2518. lum_m4+=4;
  2519. lum_m3+=4;
  2520. lum_m2+=4;
  2521. lum_m1+=4;
  2522. lum+=4;
  2523. dst+=4;
  2524. }
  2525. #endif
  2526. }
  2527. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  2528. int size)
  2529. {
  2530. #ifndef HAVE_MMX
  2531. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2532. int sum;
  2533. for(;size > 0;size--) {
  2534. sum = -lum_m4[0];
  2535. sum += lum_m3[0] << 2;
  2536. sum += lum_m2[0] << 1;
  2537. lum_m4[0]=lum_m2[0];
  2538. sum += lum_m1[0] << 2;
  2539. sum += -lum[0];
  2540. lum_m2[0] = cm[(sum + 4) >> 3];
  2541. lum_m4++;
  2542. lum_m3++;
  2543. lum_m2++;
  2544. lum_m1++;
  2545. lum++;
  2546. }
  2547. #else
  2548. {
  2549. pxor_r2r(mm7,mm7);
  2550. movq_m2r(ff_pw_4,mm6);
  2551. }
  2552. for (;size > 3; size-=4) {
  2553. DEINT_INPLACE_LINE_LUM
  2554. lum_m4+=4;
  2555. lum_m3+=4;
  2556. lum_m2+=4;
  2557. lum_m1+=4;
  2558. lum+=4;
  2559. }
  2560. #endif
  2561. }
  2562. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  2563. top field is copied as is, but the bottom field is deinterlaced
  2564. against the top field. */
  2565. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  2566. const uint8_t *src1, int src_wrap,
  2567. int width, int height)
  2568. {
  2569. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  2570. int y;
  2571. src_m2 = src1;
  2572. src_m1 = src1;
  2573. src_0=&src_m1[src_wrap];
  2574. src_p1=&src_0[src_wrap];
  2575. src_p2=&src_p1[src_wrap];
  2576. for(y=0;y<(height-2);y+=2) {
  2577. memcpy(dst,src_m1,width);
  2578. dst += dst_wrap;
  2579. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  2580. src_m2 = src_0;
  2581. src_m1 = src_p1;
  2582. src_0 = src_p2;
  2583. src_p1 += 2*src_wrap;
  2584. src_p2 += 2*src_wrap;
  2585. dst += dst_wrap;
  2586. }
  2587. memcpy(dst,src_m1,width);
  2588. dst += dst_wrap;
  2589. /* do last line */
  2590. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  2591. }
  2592. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  2593. int width, int height)
  2594. {
  2595. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  2596. int y;
  2597. uint8_t *buf;
  2598. buf = (uint8_t*)av_malloc(width);
  2599. src_m1 = src1;
  2600. memcpy(buf,src_m1,width);
  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. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  2606. src_m1 = src_p1;
  2607. src_0 = src_p2;
  2608. src_p1 += 2*src_wrap;
  2609. src_p2 += 2*src_wrap;
  2610. }
  2611. /* do last line */
  2612. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  2613. av_free(buf);
  2614. }
  2615. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  2616. int pix_fmt, int width, int height)
  2617. {
  2618. int i;
  2619. if (pix_fmt != PIX_FMT_YUV420P &&
  2620. pix_fmt != PIX_FMT_YUV422P &&
  2621. pix_fmt != PIX_FMT_YUV444P &&
  2622. pix_fmt != PIX_FMT_YUV411P &&
  2623. pix_fmt != PIX_FMT_GRAY8)
  2624. return -1;
  2625. if ((width & 3) != 0 || (height & 3) != 0)
  2626. return -1;
  2627. for(i=0;i<3;i++) {
  2628. if (i == 1) {
  2629. switch(pix_fmt) {
  2630. case PIX_FMT_YUV420P:
  2631. width >>= 1;
  2632. height >>= 1;
  2633. break;
  2634. case PIX_FMT_YUV422P:
  2635. width >>= 1;
  2636. break;
  2637. case PIX_FMT_YUV411P:
  2638. width >>= 2;
  2639. break;
  2640. default:
  2641. break;
  2642. }
  2643. if (pix_fmt == PIX_FMT_GRAY8) {
  2644. break;
  2645. }
  2646. }
  2647. if (src == dst) {
  2648. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  2649. width, height);
  2650. } else {
  2651. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  2652. src->data[i], src->linesize[i],
  2653. width, height);
  2654. }
  2655. }
  2656. emms_c();
  2657. return 0;
  2658. }