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.

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