You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2924 lines
78KB

  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file imgconvert.c
  23. * misc image conversion routines
  24. */
  25. /* TODO:
  26. * - write 'ffimg' program to test all the image related stuff
  27. * - move all api to slice based system
  28. * - integrate deinterlacing, postprocessing and scaling in the conversion process
  29. */
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "colorspace.h"
  33. #ifdef HAVE_MMX
  34. #include "i386/mmx.h"
  35. #endif
  36. #define xglue(x, y) x ## y
  37. #define glue(x, y) xglue(x, y)
  38. #define FF_COLOR_RGB 0 /**< RGB color space */
  39. #define FF_COLOR_GRAY 1 /**< gray color space */
  40. #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  41. #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  42. #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
  43. #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
  44. #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
  45. typedef struct PixFmtInfo {
  46. const char *name;
  47. uint8_t nb_channels; /**< number of channels (including alpha) */
  48. uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
  49. uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
  50. uint8_t is_alpha : 1; /**< true if alpha can be specified */
  51. uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
  52. uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
  53. uint8_t depth; /**< bit depth of the color components */
  54. } PixFmtInfo;
  55. /* this table gives more information about formats */
  56. static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  57. /* YUV formats */
  58. [PIX_FMT_YUV420P] = {
  59. .name = "yuv420p",
  60. .nb_channels = 3,
  61. .color_type = FF_COLOR_YUV,
  62. .pixel_type = FF_PIXEL_PLANAR,
  63. .depth = 8,
  64. .x_chroma_shift = 1, .y_chroma_shift = 1,
  65. },
  66. [PIX_FMT_YUV422P] = {
  67. .name = "yuv422p",
  68. .nb_channels = 3,
  69. .color_type = FF_COLOR_YUV,
  70. .pixel_type = FF_PIXEL_PLANAR,
  71. .depth = 8,
  72. .x_chroma_shift = 1, .y_chroma_shift = 0,
  73. },
  74. [PIX_FMT_YUV444P] = {
  75. .name = "yuv444p",
  76. .nb_channels = 3,
  77. .color_type = FF_COLOR_YUV,
  78. .pixel_type = FF_PIXEL_PLANAR,
  79. .depth = 8,
  80. .x_chroma_shift = 0, .y_chroma_shift = 0,
  81. },
  82. [PIX_FMT_YUYV422] = {
  83. .name = "yuyv422",
  84. .nb_channels = 1,
  85. .color_type = FF_COLOR_YUV,
  86. .pixel_type = FF_PIXEL_PACKED,
  87. .depth = 8,
  88. .x_chroma_shift = 1, .y_chroma_shift = 0,
  89. },
  90. [PIX_FMT_UYVY422] = {
  91. .name = "uyvy422",
  92. .nb_channels = 1,
  93. .color_type = FF_COLOR_YUV,
  94. .pixel_type = FF_PIXEL_PACKED,
  95. .depth = 8,
  96. .x_chroma_shift = 1, .y_chroma_shift = 0,
  97. },
  98. [PIX_FMT_YUV410P] = {
  99. .name = "yuv410p",
  100. .nb_channels = 3,
  101. .color_type = FF_COLOR_YUV,
  102. .pixel_type = FF_PIXEL_PLANAR,
  103. .depth = 8,
  104. .x_chroma_shift = 2, .y_chroma_shift = 2,
  105. },
  106. [PIX_FMT_YUV411P] = {
  107. .name = "yuv411p",
  108. .nb_channels = 3,
  109. .color_type = FF_COLOR_YUV,
  110. .pixel_type = FF_PIXEL_PLANAR,
  111. .depth = 8,
  112. .x_chroma_shift = 2, .y_chroma_shift = 0,
  113. },
  114. [PIX_FMT_YUV440P] = {
  115. .name = "yuv440p",
  116. .nb_channels = 3,
  117. .color_type = FF_COLOR_YUV,
  118. .pixel_type = FF_PIXEL_PLANAR,
  119. .depth = 8,
  120. .x_chroma_shift = 0, .y_chroma_shift = 1,
  121. },
  122. /* YUV formats with alpha plane */
  123. [PIX_FMT_YUVA420P] = {
  124. .name = "yuva420p",
  125. .nb_channels = 4,
  126. .color_type = FF_COLOR_YUV,
  127. .pixel_type = FF_PIXEL_PLANAR,
  128. .depth = 8,
  129. .x_chroma_shift = 1, .y_chroma_shift = 1,
  130. },
  131. /* JPEG YUV */
  132. [PIX_FMT_YUVJ420P] = {
  133. .name = "yuvj420p",
  134. .nb_channels = 3,
  135. .color_type = FF_COLOR_YUV_JPEG,
  136. .pixel_type = FF_PIXEL_PLANAR,
  137. .depth = 8,
  138. .x_chroma_shift = 1, .y_chroma_shift = 1,
  139. },
  140. [PIX_FMT_YUVJ422P] = {
  141. .name = "yuvj422p",
  142. .nb_channels = 3,
  143. .color_type = FF_COLOR_YUV_JPEG,
  144. .pixel_type = FF_PIXEL_PLANAR,
  145. .depth = 8,
  146. .x_chroma_shift = 1, .y_chroma_shift = 0,
  147. },
  148. [PIX_FMT_YUVJ444P] = {
  149. .name = "yuvj444p",
  150. .nb_channels = 3,
  151. .color_type = FF_COLOR_YUV_JPEG,
  152. .pixel_type = FF_PIXEL_PLANAR,
  153. .depth = 8,
  154. .x_chroma_shift = 0, .y_chroma_shift = 0,
  155. },
  156. [PIX_FMT_YUVJ440P] = {
  157. .name = "yuvj440p",
  158. .nb_channels = 3,
  159. .color_type = FF_COLOR_YUV_JPEG,
  160. .pixel_type = FF_PIXEL_PLANAR,
  161. .depth = 8,
  162. .x_chroma_shift = 0, .y_chroma_shift = 1,
  163. },
  164. /* RGB formats */
  165. [PIX_FMT_RGB24] = {
  166. .name = "rgb24",
  167. .nb_channels = 3,
  168. .color_type = FF_COLOR_RGB,
  169. .pixel_type = FF_PIXEL_PACKED,
  170. .depth = 8,
  171. .x_chroma_shift = 0, .y_chroma_shift = 0,
  172. },
  173. [PIX_FMT_BGR24] = {
  174. .name = "bgr24",
  175. .nb_channels = 3,
  176. .color_type = FF_COLOR_RGB,
  177. .pixel_type = FF_PIXEL_PACKED,
  178. .depth = 8,
  179. .x_chroma_shift = 0, .y_chroma_shift = 0,
  180. },
  181. [PIX_FMT_RGB32] = {
  182. .name = "rgb32",
  183. .nb_channels = 4, .is_alpha = 1,
  184. .color_type = FF_COLOR_RGB,
  185. .pixel_type = FF_PIXEL_PACKED,
  186. .depth = 8,
  187. .x_chroma_shift = 0, .y_chroma_shift = 0,
  188. },
  189. [PIX_FMT_RGB565] = {
  190. .name = "rgb565",
  191. .nb_channels = 3,
  192. .color_type = FF_COLOR_RGB,
  193. .pixel_type = FF_PIXEL_PACKED,
  194. .depth = 5,
  195. .x_chroma_shift = 0, .y_chroma_shift = 0,
  196. },
  197. [PIX_FMT_RGB555] = {
  198. .name = "rgb555",
  199. .nb_channels = 3,
  200. .color_type = FF_COLOR_RGB,
  201. .pixel_type = FF_PIXEL_PACKED,
  202. .depth = 5,
  203. .x_chroma_shift = 0, .y_chroma_shift = 0,
  204. },
  205. /* gray / mono formats */
  206. [PIX_FMT_GRAY16BE] = {
  207. .name = "gray16be",
  208. .nb_channels = 1,
  209. .color_type = FF_COLOR_GRAY,
  210. .pixel_type = FF_PIXEL_PLANAR,
  211. .depth = 16,
  212. },
  213. [PIX_FMT_GRAY16LE] = {
  214. .name = "gray16le",
  215. .nb_channels = 1,
  216. .color_type = FF_COLOR_GRAY,
  217. .pixel_type = FF_PIXEL_PLANAR,
  218. .depth = 16,
  219. },
  220. [PIX_FMT_GRAY8] = {
  221. .name = "gray",
  222. .nb_channels = 1,
  223. .color_type = FF_COLOR_GRAY,
  224. .pixel_type = FF_PIXEL_PLANAR,
  225. .depth = 8,
  226. },
  227. [PIX_FMT_MONOWHITE] = {
  228. .name = "monow",
  229. .nb_channels = 1,
  230. .color_type = FF_COLOR_GRAY,
  231. .pixel_type = FF_PIXEL_PLANAR,
  232. .depth = 1,
  233. },
  234. [PIX_FMT_MONOBLACK] = {
  235. .name = "monob",
  236. .nb_channels = 1,
  237. .color_type = FF_COLOR_GRAY,
  238. .pixel_type = FF_PIXEL_PLANAR,
  239. .depth = 1,
  240. },
  241. /* paletted formats */
  242. [PIX_FMT_PAL8] = {
  243. .name = "pal8",
  244. .nb_channels = 4, .is_alpha = 1,
  245. .color_type = FF_COLOR_RGB,
  246. .pixel_type = FF_PIXEL_PALETTE,
  247. .depth = 8,
  248. },
  249. [PIX_FMT_XVMC_MPEG2_MC] = {
  250. .name = "xvmcmc",
  251. },
  252. [PIX_FMT_XVMC_MPEG2_IDCT] = {
  253. .name = "xvmcidct",
  254. },
  255. [PIX_FMT_UYYVYY411] = {
  256. .name = "uyyvyy411",
  257. .nb_channels = 1,
  258. .color_type = FF_COLOR_YUV,
  259. .pixel_type = FF_PIXEL_PACKED,
  260. .depth = 8,
  261. .x_chroma_shift = 2, .y_chroma_shift = 0,
  262. },
  263. [PIX_FMT_BGR32] = {
  264. .name = "bgr32",
  265. .nb_channels = 4, .is_alpha = 1,
  266. .color_type = FF_COLOR_RGB,
  267. .pixel_type = FF_PIXEL_PACKED,
  268. .depth = 8,
  269. .x_chroma_shift = 0, .y_chroma_shift = 0,
  270. },
  271. [PIX_FMT_BGR565] = {
  272. .name = "bgr565",
  273. .nb_channels = 3,
  274. .color_type = FF_COLOR_RGB,
  275. .pixel_type = FF_PIXEL_PACKED,
  276. .depth = 5,
  277. .x_chroma_shift = 0, .y_chroma_shift = 0,
  278. },
  279. [PIX_FMT_BGR555] = {
  280. .name = "bgr555",
  281. .nb_channels = 3,
  282. .color_type = FF_COLOR_RGB,
  283. .pixel_type = FF_PIXEL_PACKED,
  284. .depth = 5,
  285. .x_chroma_shift = 0, .y_chroma_shift = 0,
  286. },
  287. [PIX_FMT_RGB8] = {
  288. .name = "rgb8",
  289. .nb_channels = 1,
  290. .color_type = FF_COLOR_RGB,
  291. .pixel_type = FF_PIXEL_PACKED,
  292. .depth = 8,
  293. .x_chroma_shift = 0, .y_chroma_shift = 0,
  294. },
  295. [PIX_FMT_RGB4] = {
  296. .name = "rgb4",
  297. .nb_channels = 1,
  298. .color_type = FF_COLOR_RGB,
  299. .pixel_type = FF_PIXEL_PACKED,
  300. .depth = 4,
  301. .x_chroma_shift = 0, .y_chroma_shift = 0,
  302. },
  303. [PIX_FMT_RGB4_BYTE] = {
  304. .name = "rgb4_byte",
  305. .nb_channels = 1,
  306. .color_type = FF_COLOR_RGB,
  307. .pixel_type = FF_PIXEL_PACKED,
  308. .depth = 8,
  309. .x_chroma_shift = 0, .y_chroma_shift = 0,
  310. },
  311. [PIX_FMT_BGR8] = {
  312. .name = "bgr8",
  313. .nb_channels = 1,
  314. .color_type = FF_COLOR_RGB,
  315. .pixel_type = FF_PIXEL_PACKED,
  316. .depth = 8,
  317. .x_chroma_shift = 0, .y_chroma_shift = 0,
  318. },
  319. [PIX_FMT_BGR4] = {
  320. .name = "bgr4",
  321. .nb_channels = 1,
  322. .color_type = FF_COLOR_RGB,
  323. .pixel_type = FF_PIXEL_PACKED,
  324. .depth = 4,
  325. .x_chroma_shift = 0, .y_chroma_shift = 0,
  326. },
  327. [PIX_FMT_BGR4_BYTE] = {
  328. .name = "bgr4_byte",
  329. .nb_channels = 1,
  330. .color_type = FF_COLOR_RGB,
  331. .pixel_type = FF_PIXEL_PACKED,
  332. .depth = 8,
  333. .x_chroma_shift = 0, .y_chroma_shift = 0,
  334. },
  335. [PIX_FMT_NV12] = {
  336. .name = "nv12",
  337. .nb_channels = 2,
  338. .color_type = FF_COLOR_YUV,
  339. .pixel_type = FF_PIXEL_PLANAR,
  340. .depth = 8,
  341. .x_chroma_shift = 1, .y_chroma_shift = 1,
  342. },
  343. [PIX_FMT_NV21] = {
  344. .name = "nv12",
  345. .nb_channels = 2,
  346. .color_type = FF_COLOR_YUV,
  347. .pixel_type = FF_PIXEL_PLANAR,
  348. .depth = 8,
  349. .x_chroma_shift = 1, .y_chroma_shift = 1,
  350. },
  351. [PIX_FMT_BGR32_1] = {
  352. .name = "bgr32_1",
  353. .nb_channels = 4, .is_alpha = 1,
  354. .color_type = FF_COLOR_RGB,
  355. .pixel_type = FF_PIXEL_PACKED,
  356. .depth = 8,
  357. .x_chroma_shift = 0, .y_chroma_shift = 0,
  358. },
  359. [PIX_FMT_RGB32_1] = {
  360. .name = "rgb32_1",
  361. .nb_channels = 4, .is_alpha = 1,
  362. .color_type = FF_COLOR_RGB,
  363. .pixel_type = FF_PIXEL_PACKED,
  364. .depth = 8,
  365. .x_chroma_shift = 0, .y_chroma_shift = 0,
  366. },
  367. };
  368. void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
  369. {
  370. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  371. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  372. }
  373. const char *avcodec_get_pix_fmt_name(int pix_fmt)
  374. {
  375. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  376. return "???";
  377. else
  378. return pix_fmt_info[pix_fmt].name;
  379. }
  380. enum PixelFormat avcodec_get_pix_fmt(const char* name)
  381. {
  382. int i;
  383. for (i=0; i < PIX_FMT_NB; i++)
  384. if (!strcmp(pix_fmt_info[i].name, name))
  385. break;
  386. return i;
  387. }
  388. void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
  389. {
  390. PixFmtInfo info= pix_fmt_info[pix_fmt];
  391. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  392. /* print header */
  393. if (pix_fmt < 0)
  394. snprintf (buf, buf_size,
  395. "name " " nb_channels" " depth" " is_alpha"
  396. );
  397. else
  398. snprintf (buf, buf_size,
  399. "%-10s" " %1d " " %2d " " %c ",
  400. info.name,
  401. info.nb_channels,
  402. info.depth,
  403. is_alpha_char
  404. );
  405. }
  406. int ff_fill_linesize(AVPicture *picture, int pix_fmt, int width)
  407. {
  408. int w2;
  409. const PixFmtInfo *pinfo;
  410. memset(picture->linesize, 0, sizeof(picture->linesize));
  411. pinfo = &pix_fmt_info[pix_fmt];
  412. switch(pix_fmt) {
  413. case PIX_FMT_YUV420P:
  414. case PIX_FMT_YUV422P:
  415. case PIX_FMT_YUV444P:
  416. case PIX_FMT_YUV410P:
  417. case PIX_FMT_YUV411P:
  418. case PIX_FMT_YUV440P:
  419. case PIX_FMT_YUVJ420P:
  420. case PIX_FMT_YUVJ422P:
  421. case PIX_FMT_YUVJ444P:
  422. case PIX_FMT_YUVJ440P:
  423. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  424. picture->linesize[0] = width;
  425. picture->linesize[1] = w2;
  426. picture->linesize[2] = w2;
  427. break;
  428. case PIX_FMT_YUVA420P:
  429. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  430. picture->linesize[0] = width;
  431. picture->linesize[1] = w2;
  432. picture->linesize[2] = w2;
  433. picture->linesize[3] = width;
  434. break;
  435. case PIX_FMT_NV12:
  436. case PIX_FMT_NV21:
  437. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  438. picture->linesize[0] = width;
  439. picture->linesize[1] = w2;
  440. break;
  441. case PIX_FMT_RGB24:
  442. case PIX_FMT_BGR24:
  443. picture->linesize[0] = width * 3;
  444. break;
  445. case PIX_FMT_RGB32:
  446. case PIX_FMT_BGR32:
  447. case PIX_FMT_RGB32_1:
  448. case PIX_FMT_BGR32_1:
  449. picture->linesize[0] = width * 4;
  450. break;
  451. case PIX_FMT_GRAY16BE:
  452. case PIX_FMT_GRAY16LE:
  453. case PIX_FMT_BGR555:
  454. case PIX_FMT_BGR565:
  455. case PIX_FMT_RGB555:
  456. case PIX_FMT_RGB565:
  457. case PIX_FMT_YUYV422:
  458. picture->linesize[0] = width * 2;
  459. break;
  460. case PIX_FMT_UYVY422:
  461. picture->linesize[0] = width * 2;
  462. break;
  463. case PIX_FMT_UYYVYY411:
  464. picture->linesize[0] = width + width/2;
  465. break;
  466. case PIX_FMT_RGB8:
  467. case PIX_FMT_BGR8:
  468. case PIX_FMT_RGB4_BYTE:
  469. case PIX_FMT_BGR4_BYTE:
  470. case PIX_FMT_GRAY8:
  471. picture->linesize[0] = width;
  472. break;
  473. case PIX_FMT_RGB4:
  474. case PIX_FMT_BGR4:
  475. picture->linesize[0] = width / 2;
  476. break;
  477. case PIX_FMT_MONOWHITE:
  478. case PIX_FMT_MONOBLACK:
  479. picture->linesize[0] = (width + 7) >> 3;
  480. break;
  481. case PIX_FMT_PAL8:
  482. picture->linesize[0] = width;
  483. picture->linesize[1] = 4;
  484. break;
  485. default:
  486. return -1;
  487. }
  488. return 0;
  489. }
  490. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, int pix_fmt,
  491. int height)
  492. {
  493. int size, h2, size2;
  494. const PixFmtInfo *pinfo;
  495. pinfo = &pix_fmt_info[pix_fmt];
  496. size = picture->linesize[0] * height;
  497. switch(pix_fmt) {
  498. case PIX_FMT_YUV420P:
  499. case PIX_FMT_YUV422P:
  500. case PIX_FMT_YUV444P:
  501. case PIX_FMT_YUV410P:
  502. case PIX_FMT_YUV411P:
  503. case PIX_FMT_YUV440P:
  504. case PIX_FMT_YUVJ420P:
  505. case PIX_FMT_YUVJ422P:
  506. case PIX_FMT_YUVJ444P:
  507. case PIX_FMT_YUVJ440P:
  508. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  509. size2 = picture->linesize[1] * h2;
  510. picture->data[0] = ptr;
  511. picture->data[1] = picture->data[0] + size;
  512. picture->data[2] = picture->data[1] + size2;
  513. picture->data[3] = NULL;
  514. return size + 2 * size2;
  515. case PIX_FMT_YUVA420P:
  516. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  517. size2 = picture->linesize[1] * h2;
  518. picture->data[0] = ptr;
  519. picture->data[1] = picture->data[0] + size;
  520. picture->data[2] = picture->data[1] + size2;
  521. picture->data[3] = picture->data[1] + size2 + size2;
  522. return 2 * size + 2 * size2;
  523. case PIX_FMT_NV12:
  524. case PIX_FMT_NV21:
  525. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  526. size2 = picture->linesize[1] * h2 * 2;
  527. picture->data[0] = ptr;
  528. picture->data[1] = picture->data[0] + size;
  529. picture->data[2] = NULL;
  530. picture->data[3] = NULL;
  531. return size + 2 * size2;
  532. case PIX_FMT_RGB24:
  533. case PIX_FMT_BGR24:
  534. case PIX_FMT_RGB32:
  535. case PIX_FMT_BGR32:
  536. case PIX_FMT_RGB32_1:
  537. case PIX_FMT_BGR32_1:
  538. case PIX_FMT_GRAY16BE:
  539. case PIX_FMT_GRAY16LE:
  540. case PIX_FMT_BGR555:
  541. case PIX_FMT_BGR565:
  542. case PIX_FMT_RGB555:
  543. case PIX_FMT_RGB565:
  544. case PIX_FMT_YUYV422:
  545. case PIX_FMT_UYVY422:
  546. case PIX_FMT_UYYVYY411:
  547. case PIX_FMT_RGB8:
  548. case PIX_FMT_BGR8:
  549. case PIX_FMT_RGB4_BYTE:
  550. case PIX_FMT_BGR4_BYTE:
  551. case PIX_FMT_GRAY8:
  552. case PIX_FMT_RGB4:
  553. case PIX_FMT_BGR4:
  554. case PIX_FMT_MONOWHITE:
  555. case PIX_FMT_MONOBLACK:
  556. picture->data[0] = ptr;
  557. picture->data[1] = NULL;
  558. picture->data[2] = NULL;
  559. picture->data[3] = NULL;
  560. return size;
  561. case PIX_FMT_PAL8:
  562. size2 = (size + 3) & ~3;
  563. picture->data[0] = ptr;
  564. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  565. picture->data[2] = NULL;
  566. picture->data[3] = NULL;
  567. return size2 + 256 * 4;
  568. default:
  569. picture->data[0] = NULL;
  570. picture->data[1] = NULL;
  571. picture->data[2] = NULL;
  572. picture->data[3] = NULL;
  573. return -1;
  574. }
  575. }
  576. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  577. int pix_fmt, int width, int height)
  578. {
  579. if(avcodec_check_dimensions(NULL, width, height))
  580. return -1;
  581. if (ff_fill_linesize(picture, pix_fmt, width))
  582. return -1;
  583. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  584. }
  585. int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
  586. unsigned char *dest, int dest_size)
  587. {
  588. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  589. int i, j, w, h, data_planes;
  590. const unsigned char* s;
  591. int size = avpicture_get_size(pix_fmt, width, height);
  592. if (size > dest_size || size < 0)
  593. return -1;
  594. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  595. if (pix_fmt == PIX_FMT_YUYV422 ||
  596. pix_fmt == PIX_FMT_UYVY422 ||
  597. pix_fmt == PIX_FMT_BGR565 ||
  598. pix_fmt == PIX_FMT_BGR555 ||
  599. pix_fmt == PIX_FMT_RGB565 ||
  600. pix_fmt == PIX_FMT_RGB555)
  601. w = width * 2;
  602. else if (pix_fmt == PIX_FMT_UYYVYY411)
  603. w = width + width/2;
  604. else if (pix_fmt == PIX_FMT_PAL8)
  605. w = width;
  606. else
  607. w = width * (pf->depth * pf->nb_channels / 8);
  608. data_planes = 1;
  609. h = height;
  610. } else {
  611. data_planes = pf->nb_channels;
  612. w = (width*pf->depth + 7)/8;
  613. h = height;
  614. }
  615. for (i=0; i<data_planes; i++) {
  616. if (i == 1) {
  617. w = width >> pf->x_chroma_shift;
  618. h = height >> pf->y_chroma_shift;
  619. }
  620. s = src->data[i];
  621. for(j=0; j<h; j++) {
  622. memcpy(dest, s, w);
  623. dest += w;
  624. s += src->linesize[i];
  625. }
  626. }
  627. if (pf->pixel_type == FF_PIXEL_PALETTE)
  628. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  629. return size;
  630. }
  631. int avpicture_get_size(int pix_fmt, int width, int height)
  632. {
  633. AVPicture dummy_pict;
  634. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  635. }
  636. int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
  637. int has_alpha)
  638. {
  639. const PixFmtInfo *pf, *ps;
  640. int loss;
  641. ps = &pix_fmt_info[src_pix_fmt];
  642. pf = &pix_fmt_info[dst_pix_fmt];
  643. /* compute loss */
  644. loss = 0;
  645. pf = &pix_fmt_info[dst_pix_fmt];
  646. if (pf->depth < ps->depth ||
  647. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  648. loss |= FF_LOSS_DEPTH;
  649. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  650. pf->y_chroma_shift > ps->y_chroma_shift)
  651. loss |= FF_LOSS_RESOLUTION;
  652. switch(pf->color_type) {
  653. case FF_COLOR_RGB:
  654. if (ps->color_type != FF_COLOR_RGB &&
  655. ps->color_type != FF_COLOR_GRAY)
  656. loss |= FF_LOSS_COLORSPACE;
  657. break;
  658. case FF_COLOR_GRAY:
  659. if (ps->color_type != FF_COLOR_GRAY)
  660. loss |= FF_LOSS_COLORSPACE;
  661. break;
  662. case FF_COLOR_YUV:
  663. if (ps->color_type != FF_COLOR_YUV)
  664. loss |= FF_LOSS_COLORSPACE;
  665. break;
  666. case FF_COLOR_YUV_JPEG:
  667. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  668. ps->color_type != FF_COLOR_YUV &&
  669. ps->color_type != FF_COLOR_GRAY)
  670. loss |= FF_LOSS_COLORSPACE;
  671. break;
  672. default:
  673. /* fail safe test */
  674. if (ps->color_type != pf->color_type)
  675. loss |= FF_LOSS_COLORSPACE;
  676. break;
  677. }
  678. if (pf->color_type == FF_COLOR_GRAY &&
  679. ps->color_type != FF_COLOR_GRAY)
  680. loss |= FF_LOSS_CHROMA;
  681. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  682. loss |= FF_LOSS_ALPHA;
  683. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  684. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  685. loss |= FF_LOSS_COLORQUANT;
  686. return loss;
  687. }
  688. static int avg_bits_per_pixel(int pix_fmt)
  689. {
  690. int bits;
  691. const PixFmtInfo *pf;
  692. pf = &pix_fmt_info[pix_fmt];
  693. switch(pf->pixel_type) {
  694. case FF_PIXEL_PACKED:
  695. switch(pix_fmt) {
  696. case PIX_FMT_YUYV422:
  697. case PIX_FMT_UYVY422:
  698. case PIX_FMT_RGB565:
  699. case PIX_FMT_RGB555:
  700. case PIX_FMT_BGR565:
  701. case PIX_FMT_BGR555:
  702. bits = 16;
  703. break;
  704. case PIX_FMT_UYYVYY411:
  705. bits = 12;
  706. break;
  707. default:
  708. bits = pf->depth * pf->nb_channels;
  709. break;
  710. }
  711. break;
  712. case FF_PIXEL_PLANAR:
  713. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  714. bits = pf->depth * pf->nb_channels;
  715. } else {
  716. bits = pf->depth + ((2 * pf->depth) >>
  717. (pf->x_chroma_shift + pf->y_chroma_shift));
  718. }
  719. break;
  720. case FF_PIXEL_PALETTE:
  721. bits = 8;
  722. break;
  723. default:
  724. bits = -1;
  725. break;
  726. }
  727. return bits;
  728. }
  729. static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
  730. int src_pix_fmt,
  731. int has_alpha,
  732. int loss_mask)
  733. {
  734. int dist, i, loss, min_dist, dst_pix_fmt;
  735. /* find exact color match with smallest size */
  736. dst_pix_fmt = -1;
  737. min_dist = 0x7fffffff;
  738. for(i = 0;i < PIX_FMT_NB; i++) {
  739. if (pix_fmt_mask & (1 << i)) {
  740. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  741. if (loss == 0) {
  742. dist = avg_bits_per_pixel(i);
  743. if (dist < min_dist) {
  744. min_dist = dist;
  745. dst_pix_fmt = i;
  746. }
  747. }
  748. }
  749. }
  750. return dst_pix_fmt;
  751. }
  752. int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
  753. int has_alpha, int *loss_ptr)
  754. {
  755. int dst_pix_fmt, loss_mask, i;
  756. static const int loss_mask_order[] = {
  757. ~0, /* no loss first */
  758. ~FF_LOSS_ALPHA,
  759. ~FF_LOSS_RESOLUTION,
  760. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  761. ~FF_LOSS_COLORQUANT,
  762. ~FF_LOSS_DEPTH,
  763. 0,
  764. };
  765. /* try with successive loss */
  766. i = 0;
  767. for(;;) {
  768. loss_mask = loss_mask_order[i++];
  769. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  770. has_alpha, loss_mask);
  771. if (dst_pix_fmt >= 0)
  772. goto found;
  773. if (loss_mask == 0)
  774. break;
  775. }
  776. return -1;
  777. found:
  778. if (loss_ptr)
  779. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  780. return dst_pix_fmt;
  781. }
  782. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  783. const uint8_t *src, int src_wrap,
  784. int width, int height)
  785. {
  786. if((!dst) || (!src))
  787. return;
  788. for(;height > 0; height--) {
  789. memcpy(dst, src, width);
  790. dst += dst_wrap;
  791. src += src_wrap;
  792. }
  793. }
  794. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  795. {
  796. int bits;
  797. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  798. pf = &pix_fmt_info[pix_fmt];
  799. switch(pf->pixel_type) {
  800. case FF_PIXEL_PACKED:
  801. switch(pix_fmt) {
  802. case PIX_FMT_YUYV422:
  803. case PIX_FMT_UYVY422:
  804. case PIX_FMT_RGB565:
  805. case PIX_FMT_RGB555:
  806. case PIX_FMT_BGR565:
  807. case PIX_FMT_BGR555:
  808. bits = 16;
  809. break;
  810. case PIX_FMT_UYYVYY411:
  811. bits = 12;
  812. break;
  813. default:
  814. bits = pf->depth * pf->nb_channels;
  815. break;
  816. }
  817. return (width * bits + 7) >> 3;
  818. break;
  819. case FF_PIXEL_PLANAR:
  820. if (plane == 1 || plane == 2)
  821. width >>= pf->x_chroma_shift;
  822. return (width * pf->depth + 7) >> 3;
  823. break;
  824. case FF_PIXEL_PALETTE:
  825. if (plane == 0)
  826. return width;
  827. break;
  828. }
  829. return -1;
  830. }
  831. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  832. int pix_fmt, int width, int height)
  833. {
  834. int i;
  835. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  836. pf = &pix_fmt_info[pix_fmt];
  837. switch(pf->pixel_type) {
  838. case FF_PIXEL_PACKED:
  839. case FF_PIXEL_PLANAR:
  840. for(i = 0; i < pf->nb_channels; i++) {
  841. int w, h;
  842. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  843. w = width;
  844. h = height;
  845. if (i == 1 || i == 2) {
  846. w >>= pf->x_chroma_shift;
  847. h >>= pf->y_chroma_shift;
  848. }
  849. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  850. src->data[i], src->linesize[i],
  851. bwidth, h);
  852. }
  853. break;
  854. case FF_PIXEL_PALETTE:
  855. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  856. src->data[0], src->linesize[0],
  857. width, height);
  858. /* copy the palette */
  859. ff_img_copy_plane(dst->data[1], dst->linesize[1],
  860. src->data[1], src->linesize[1],
  861. 4, 256);
  862. break;
  863. }
  864. }
  865. /* XXX: totally non optimized */
  866. static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
  867. int width, int height)
  868. {
  869. const uint8_t *p, *p1;
  870. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  871. int w;
  872. p1 = src->data[0];
  873. lum1 = dst->data[0];
  874. cb1 = dst->data[1];
  875. cr1 = dst->data[2];
  876. for(;height >= 1; height -= 2) {
  877. p = p1;
  878. lum = lum1;
  879. cb = cb1;
  880. cr = cr1;
  881. for(w = width; w >= 2; w -= 2) {
  882. lum[0] = p[0];
  883. cb[0] = p[1];
  884. lum[1] = p[2];
  885. cr[0] = p[3];
  886. p += 4;
  887. lum += 2;
  888. cb++;
  889. cr++;
  890. }
  891. if (w) {
  892. lum[0] = p[0];
  893. cb[0] = p[1];
  894. cr[0] = p[3];
  895. cb++;
  896. cr++;
  897. }
  898. p1 += src->linesize[0];
  899. lum1 += dst->linesize[0];
  900. if (height>1) {
  901. p = p1;
  902. lum = lum1;
  903. for(w = width; w >= 2; w -= 2) {
  904. lum[0] = p[0];
  905. lum[1] = p[2];
  906. p += 4;
  907. lum += 2;
  908. }
  909. if (w) {
  910. lum[0] = p[0];
  911. }
  912. p1 += src->linesize[0];
  913. lum1 += dst->linesize[0];
  914. }
  915. cb1 += dst->linesize[1];
  916. cr1 += dst->linesize[2];
  917. }
  918. }
  919. static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
  920. int width, int height)
  921. {
  922. const uint8_t *p, *p1;
  923. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  924. int w;
  925. p1 = src->data[0];
  926. lum1 = dst->data[0];
  927. cb1 = dst->data[1];
  928. cr1 = dst->data[2];
  929. for(;height >= 1; height -= 2) {
  930. p = p1;
  931. lum = lum1;
  932. cb = cb1;
  933. cr = cr1;
  934. for(w = width; w >= 2; w -= 2) {
  935. lum[0] = p[1];
  936. cb[0] = p[0];
  937. lum[1] = p[3];
  938. cr[0] = p[2];
  939. p += 4;
  940. lum += 2;
  941. cb++;
  942. cr++;
  943. }
  944. if (w) {
  945. lum[0] = p[1];
  946. cb[0] = p[0];
  947. cr[0] = p[2];
  948. cb++;
  949. cr++;
  950. }
  951. p1 += src->linesize[0];
  952. lum1 += dst->linesize[0];
  953. if (height>1) {
  954. p = p1;
  955. lum = lum1;
  956. for(w = width; w >= 2; w -= 2) {
  957. lum[0] = p[1];
  958. lum[1] = p[3];
  959. p += 4;
  960. lum += 2;
  961. }
  962. if (w) {
  963. lum[0] = p[1];
  964. }
  965. p1 += src->linesize[0];
  966. lum1 += dst->linesize[0];
  967. }
  968. cb1 += dst->linesize[1];
  969. cr1 += dst->linesize[2];
  970. }
  971. }
  972. static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
  973. int width, int height)
  974. {
  975. const uint8_t *p, *p1;
  976. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  977. int w;
  978. p1 = src->data[0];
  979. lum1 = dst->data[0];
  980. cb1 = dst->data[1];
  981. cr1 = dst->data[2];
  982. for(;height > 0; height--) {
  983. p = p1;
  984. lum = lum1;
  985. cb = cb1;
  986. cr = cr1;
  987. for(w = width; w >= 2; w -= 2) {
  988. lum[0] = p[1];
  989. cb[0] = p[0];
  990. lum[1] = p[3];
  991. cr[0] = p[2];
  992. p += 4;
  993. lum += 2;
  994. cb++;
  995. cr++;
  996. }
  997. p1 += src->linesize[0];
  998. lum1 += dst->linesize[0];
  999. cb1 += dst->linesize[1];
  1000. cr1 += dst->linesize[2];
  1001. }
  1002. }
  1003. static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
  1004. int width, int height)
  1005. {
  1006. const uint8_t *p, *p1;
  1007. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1008. int w;
  1009. p1 = src->data[0];
  1010. lum1 = dst->data[0];
  1011. cb1 = dst->data[1];
  1012. cr1 = dst->data[2];
  1013. for(;height > 0; height--) {
  1014. p = p1;
  1015. lum = lum1;
  1016. cb = cb1;
  1017. cr = cr1;
  1018. for(w = width; w >= 2; w -= 2) {
  1019. lum[0] = p[0];
  1020. cb[0] = p[1];
  1021. lum[1] = p[2];
  1022. cr[0] = p[3];
  1023. p += 4;
  1024. lum += 2;
  1025. cb++;
  1026. cr++;
  1027. }
  1028. p1 += src->linesize[0];
  1029. lum1 += dst->linesize[0];
  1030. cb1 += dst->linesize[1];
  1031. cr1 += dst->linesize[2];
  1032. }
  1033. }
  1034. static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
  1035. int width, int height)
  1036. {
  1037. uint8_t *p, *p1;
  1038. const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1039. int w;
  1040. p1 = dst->data[0];
  1041. lum1 = src->data[0];
  1042. cb1 = src->data[1];
  1043. cr1 = src->data[2];
  1044. for(;height > 0; height--) {
  1045. p = p1;
  1046. lum = lum1;
  1047. cb = cb1;
  1048. cr = cr1;
  1049. for(w = width; w >= 2; w -= 2) {
  1050. p[0] = lum[0];
  1051. p[1] = cb[0];
  1052. p[2] = lum[1];
  1053. p[3] = cr[0];
  1054. p += 4;
  1055. lum += 2;
  1056. cb++;
  1057. cr++;
  1058. }
  1059. p1 += dst->linesize[0];
  1060. lum1 += src->linesize[0];
  1061. cb1 += src->linesize[1];
  1062. cr1 += src->linesize[2];
  1063. }
  1064. }
  1065. static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
  1066. int width, int height)
  1067. {
  1068. uint8_t *p, *p1;
  1069. const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1070. int w;
  1071. p1 = dst->data[0];
  1072. lum1 = src->data[0];
  1073. cb1 = src->data[1];
  1074. cr1 = src->data[2];
  1075. for(;height > 0; height--) {
  1076. p = p1;
  1077. lum = lum1;
  1078. cb = cb1;
  1079. cr = cr1;
  1080. for(w = width; w >= 2; w -= 2) {
  1081. p[1] = lum[0];
  1082. p[0] = cb[0];
  1083. p[3] = lum[1];
  1084. p[2] = cr[0];
  1085. p += 4;
  1086. lum += 2;
  1087. cb++;
  1088. cr++;
  1089. }
  1090. p1 += dst->linesize[0];
  1091. lum1 += src->linesize[0];
  1092. cb1 += src->linesize[1];
  1093. cr1 += src->linesize[2];
  1094. }
  1095. }
  1096. static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
  1097. int width, int height)
  1098. {
  1099. const uint8_t *p, *p1;
  1100. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
  1101. int w;
  1102. p1 = src->data[0];
  1103. lum1 = dst->data[0];
  1104. cb1 = dst->data[1];
  1105. cr1 = dst->data[2];
  1106. for(;height > 0; height--) {
  1107. p = p1;
  1108. lum = lum1;
  1109. cb = cb1;
  1110. cr = cr1;
  1111. for(w = width; w >= 4; w -= 4) {
  1112. cb[0] = p[0];
  1113. lum[0] = p[1];
  1114. lum[1] = p[2];
  1115. cr[0] = p[3];
  1116. lum[2] = p[4];
  1117. lum[3] = p[5];
  1118. p += 6;
  1119. lum += 4;
  1120. cb++;
  1121. cr++;
  1122. }
  1123. p1 += src->linesize[0];
  1124. lum1 += dst->linesize[0];
  1125. cb1 += dst->linesize[1];
  1126. cr1 += dst->linesize[2];
  1127. }
  1128. }
  1129. static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
  1130. int width, int height)
  1131. {
  1132. int w, h;
  1133. uint8_t *line1, *line2, *linesrc = dst->data[0];
  1134. uint8_t *lum1, *lum2, *lumsrc = src->data[0];
  1135. uint8_t *cb1, *cb2 = src->data[1];
  1136. uint8_t *cr1, *cr2 = src->data[2];
  1137. for(h = height / 2; h--;) {
  1138. line1 = linesrc;
  1139. line2 = linesrc + dst->linesize[0];
  1140. lum1 = lumsrc;
  1141. lum2 = lumsrc + src->linesize[0];
  1142. cb1 = cb2;
  1143. cr1 = cr2;
  1144. for(w = width / 2; w--;) {
  1145. *line1++ = *lum1++; *line2++ = *lum2++;
  1146. *line1++ = *line2++ = *cb1++;
  1147. *line1++ = *lum1++; *line2++ = *lum2++;
  1148. *line1++ = *line2++ = *cr1++;
  1149. }
  1150. linesrc += dst->linesize[0] * 2;
  1151. lumsrc += src->linesize[0] * 2;
  1152. cb2 += src->linesize[1];
  1153. cr2 += src->linesize[2];
  1154. }
  1155. }
  1156. static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
  1157. int width, int height)
  1158. {
  1159. int w, h;
  1160. uint8_t *line1, *line2, *linesrc = dst->data[0];
  1161. uint8_t *lum1, *lum2, *lumsrc = src->data[0];
  1162. uint8_t *cb1, *cb2 = src->data[1];
  1163. uint8_t *cr1, *cr2 = src->data[2];
  1164. for(h = height / 2; h--;) {
  1165. line1 = linesrc;
  1166. line2 = linesrc + dst->linesize[0];
  1167. lum1 = lumsrc;
  1168. lum2 = lumsrc + src->linesize[0];
  1169. cb1 = cb2;
  1170. cr1 = cr2;
  1171. for(w = width / 2; w--;) {
  1172. *line1++ = *line2++ = *cb1++;
  1173. *line1++ = *lum1++; *line2++ = *lum2++;
  1174. *line1++ = *line2++ = *cr1++;
  1175. *line1++ = *lum1++; *line2++ = *lum2++;
  1176. }
  1177. linesrc += dst->linesize[0] * 2;
  1178. lumsrc += src->linesize[0] * 2;
  1179. cb2 += src->linesize[1];
  1180. cr2 += src->linesize[2];
  1181. }
  1182. }
  1183. /* 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.h"
  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.h"
  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.h"
  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.h"
  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.h"
  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, src->linesize[i]);
  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. src->linesize[i]);
  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. #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
  1914. void img_copy(AVPicture *dst, const AVPicture *src,
  1915. int pix_fmt, int width, int height)
  1916. {
  1917. av_picture_copy(dst, src, pix_fmt, width, height);
  1918. }
  1919. int img_crop(AVPicture *dst, const AVPicture *src,
  1920. int pix_fmt, int top_band, int left_band)
  1921. {
  1922. return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
  1923. }
  1924. int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1925. int pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1926. int *color)
  1927. {
  1928. return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
  1929. }
  1930. #endif
  1931. #ifndef CONFIG_SWSCALE
  1932. static uint8_t y_ccir_to_jpeg[256];
  1933. static uint8_t y_jpeg_to_ccir[256];
  1934. static uint8_t c_ccir_to_jpeg[256];
  1935. static uint8_t c_jpeg_to_ccir[256];
  1936. /* init various conversion tables */
  1937. static void img_convert_init(void)
  1938. {
  1939. int i;
  1940. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1941. for(i = 0;i < 256; i++) {
  1942. y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
  1943. y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
  1944. c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
  1945. c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
  1946. }
  1947. }
  1948. /* apply to each pixel the given table */
  1949. static void img_apply_table(uint8_t *dst, int dst_wrap,
  1950. const uint8_t *src, int src_wrap,
  1951. int width, int height, const uint8_t *table1)
  1952. {
  1953. int n;
  1954. const uint8_t *s;
  1955. uint8_t *d;
  1956. const uint8_t *table;
  1957. table = table1;
  1958. for(;height > 0; height--) {
  1959. s = src;
  1960. d = dst;
  1961. n = width;
  1962. while (n >= 4) {
  1963. d[0] = table[s[0]];
  1964. d[1] = table[s[1]];
  1965. d[2] = table[s[2]];
  1966. d[3] = table[s[3]];
  1967. d += 4;
  1968. s += 4;
  1969. n -= 4;
  1970. }
  1971. while (n > 0) {
  1972. d[0] = table[s[0]];
  1973. d++;
  1974. s++;
  1975. n--;
  1976. }
  1977. dst += dst_wrap;
  1978. src += src_wrap;
  1979. }
  1980. }
  1981. /* XXX: use generic filter ? */
  1982. /* XXX: in most cases, the sampling position is incorrect */
  1983. /* 4x1 -> 1x1 */
  1984. static void shrink41(uint8_t *dst, int dst_wrap,
  1985. const uint8_t *src, int src_wrap,
  1986. int width, int height)
  1987. {
  1988. int w;
  1989. const uint8_t *s;
  1990. uint8_t *d;
  1991. for(;height > 0; height--) {
  1992. s = src;
  1993. d = dst;
  1994. for(w = width;w > 0; w--) {
  1995. d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
  1996. s += 4;
  1997. d++;
  1998. }
  1999. src += src_wrap;
  2000. dst += dst_wrap;
  2001. }
  2002. }
  2003. /* 2x1 -> 1x1 */
  2004. static void shrink21(uint8_t *dst, int dst_wrap,
  2005. const uint8_t *src, int src_wrap,
  2006. int width, int height)
  2007. {
  2008. int w;
  2009. const uint8_t *s;
  2010. uint8_t *d;
  2011. for(;height > 0; height--) {
  2012. s = src;
  2013. d = dst;
  2014. for(w = width;w > 0; w--) {
  2015. d[0] = (s[0] + s[1]) >> 1;
  2016. s += 2;
  2017. d++;
  2018. }
  2019. src += src_wrap;
  2020. dst += dst_wrap;
  2021. }
  2022. }
  2023. /* 1x2 -> 1x1 */
  2024. static void shrink12(uint8_t *dst, int dst_wrap,
  2025. const uint8_t *src, int src_wrap,
  2026. int width, int height)
  2027. {
  2028. int w;
  2029. uint8_t *d;
  2030. const uint8_t *s1, *s2;
  2031. for(;height > 0; height--) {
  2032. s1 = src;
  2033. s2 = s1 + src_wrap;
  2034. d = dst;
  2035. for(w = width;w >= 4; w-=4) {
  2036. d[0] = (s1[0] + s2[0]) >> 1;
  2037. d[1] = (s1[1] + s2[1]) >> 1;
  2038. d[2] = (s1[2] + s2[2]) >> 1;
  2039. d[3] = (s1[3] + s2[3]) >> 1;
  2040. s1 += 4;
  2041. s2 += 4;
  2042. d += 4;
  2043. }
  2044. for(;w > 0; w--) {
  2045. d[0] = (s1[0] + s2[0]) >> 1;
  2046. s1++;
  2047. s2++;
  2048. d++;
  2049. }
  2050. src += 2 * src_wrap;
  2051. dst += dst_wrap;
  2052. }
  2053. }
  2054. static void grow21_line(uint8_t *dst, const uint8_t *src,
  2055. int width)
  2056. {
  2057. int w;
  2058. const uint8_t *s1;
  2059. uint8_t *d;
  2060. s1 = src;
  2061. d = dst;
  2062. for(w = width;w >= 4; w-=4) {
  2063. d[1] = d[0] = s1[0];
  2064. d[3] = d[2] = s1[1];
  2065. s1 += 2;
  2066. d += 4;
  2067. }
  2068. for(;w >= 2; w -= 2) {
  2069. d[1] = d[0] = s1[0];
  2070. s1 ++;
  2071. d += 2;
  2072. }
  2073. /* only needed if width is not a multiple of two */
  2074. /* XXX: veryfy that */
  2075. if (w) {
  2076. d[0] = s1[0];
  2077. }
  2078. }
  2079. static void grow41_line(uint8_t *dst, const uint8_t *src,
  2080. int width)
  2081. {
  2082. int w, v;
  2083. const uint8_t *s1;
  2084. uint8_t *d;
  2085. s1 = src;
  2086. d = dst;
  2087. for(w = width;w >= 4; w-=4) {
  2088. v = s1[0];
  2089. d[0] = v;
  2090. d[1] = v;
  2091. d[2] = v;
  2092. d[3] = v;
  2093. s1 ++;
  2094. d += 4;
  2095. }
  2096. }
  2097. /* 1x1 -> 2x1 */
  2098. static void grow21(uint8_t *dst, int dst_wrap,
  2099. const uint8_t *src, int src_wrap,
  2100. int width, int height)
  2101. {
  2102. for(;height > 0; height--) {
  2103. grow21_line(dst, src, width);
  2104. src += src_wrap;
  2105. dst += dst_wrap;
  2106. }
  2107. }
  2108. /* 1x1 -> 1x2 */
  2109. static void grow12(uint8_t *dst, int dst_wrap,
  2110. const uint8_t *src, int src_wrap,
  2111. int width, int height)
  2112. {
  2113. for(;height > 0; height-=2) {
  2114. memcpy(dst, src, width);
  2115. dst += dst_wrap;
  2116. memcpy(dst, src, width);
  2117. dst += dst_wrap;
  2118. src += src_wrap;
  2119. }
  2120. }
  2121. /* 1x1 -> 2x2 */
  2122. static void grow22(uint8_t *dst, int dst_wrap,
  2123. const uint8_t *src, int src_wrap,
  2124. int width, int height)
  2125. {
  2126. for(;height > 0; height--) {
  2127. grow21_line(dst, src, width);
  2128. if (height%2)
  2129. src += src_wrap;
  2130. dst += dst_wrap;
  2131. }
  2132. }
  2133. /* 1x1 -> 4x1 */
  2134. static void grow41(uint8_t *dst, int dst_wrap,
  2135. const uint8_t *src, int src_wrap,
  2136. int width, int height)
  2137. {
  2138. for(;height > 0; height--) {
  2139. grow41_line(dst, src, width);
  2140. src += src_wrap;
  2141. dst += dst_wrap;
  2142. }
  2143. }
  2144. /* 1x1 -> 4x4 */
  2145. static void grow44(uint8_t *dst, int dst_wrap,
  2146. const uint8_t *src, int src_wrap,
  2147. int width, int height)
  2148. {
  2149. for(;height > 0; height--) {
  2150. grow41_line(dst, src, width);
  2151. if ((height & 3) == 1)
  2152. src += src_wrap;
  2153. dst += dst_wrap;
  2154. }
  2155. }
  2156. /* 1x2 -> 2x1 */
  2157. static void conv411(uint8_t *dst, int dst_wrap,
  2158. const uint8_t *src, int src_wrap,
  2159. int width, int height)
  2160. {
  2161. int w, c;
  2162. const uint8_t *s1, *s2;
  2163. uint8_t *d;
  2164. width>>=1;
  2165. for(;height > 0; height--) {
  2166. s1 = src;
  2167. s2 = src + src_wrap;
  2168. d = dst;
  2169. for(w = width;w > 0; w--) {
  2170. c = (s1[0] + s2[0]) >> 1;
  2171. d[0] = c;
  2172. d[1] = c;
  2173. s1++;
  2174. s2++;
  2175. d += 2;
  2176. }
  2177. src += src_wrap * 2;
  2178. dst += dst_wrap;
  2179. }
  2180. }
  2181. /* XXX: always use linesize. Return -1 if not supported */
  2182. int img_convert(AVPicture *dst, int dst_pix_fmt,
  2183. const AVPicture *src, int src_pix_fmt,
  2184. int src_width, int src_height)
  2185. {
  2186. static int initialized;
  2187. int i, ret, dst_width, dst_height, int_pix_fmt;
  2188. const PixFmtInfo *src_pix, *dst_pix;
  2189. const ConvertEntry *ce;
  2190. AVPicture tmp1, *tmp = &tmp1;
  2191. if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
  2192. dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
  2193. return -1;
  2194. if (src_width <= 0 || src_height <= 0)
  2195. return 0;
  2196. if (!initialized) {
  2197. initialized = 1;
  2198. img_convert_init();
  2199. }
  2200. dst_width = src_width;
  2201. dst_height = src_height;
  2202. dst_pix = &pix_fmt_info[dst_pix_fmt];
  2203. src_pix = &pix_fmt_info[src_pix_fmt];
  2204. if (src_pix_fmt == dst_pix_fmt) {
  2205. /* no conversion needed: just copy */
  2206. av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
  2207. return 0;
  2208. }
  2209. ce = &convert_table[src_pix_fmt][dst_pix_fmt];
  2210. if (ce->convert) {
  2211. /* specific conversion routine */
  2212. ce->convert(dst, src, dst_width, dst_height);
  2213. return 0;
  2214. }
  2215. /* gray to YUV */
  2216. if (is_yuv_planar(dst_pix) &&
  2217. src_pix_fmt == PIX_FMT_GRAY8) {
  2218. int w, h, y;
  2219. uint8_t *d;
  2220. if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
  2221. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2222. src->data[0], src->linesize[0],
  2223. dst_width, dst_height);
  2224. } else {
  2225. img_apply_table(dst->data[0], dst->linesize[0],
  2226. src->data[0], src->linesize[0],
  2227. dst_width, dst_height,
  2228. y_jpeg_to_ccir);
  2229. }
  2230. /* fill U and V with 128 */
  2231. w = dst_width;
  2232. h = dst_height;
  2233. w >>= dst_pix->x_chroma_shift;
  2234. h >>= dst_pix->y_chroma_shift;
  2235. for(i = 1; i <= 2; i++) {
  2236. d = dst->data[i];
  2237. for(y = 0; y< h; y++) {
  2238. memset(d, 128, w);
  2239. d += dst->linesize[i];
  2240. }
  2241. }
  2242. return 0;
  2243. }
  2244. /* YUV to gray */
  2245. if (is_yuv_planar(src_pix) &&
  2246. dst_pix_fmt == PIX_FMT_GRAY8) {
  2247. if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
  2248. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2249. src->data[0], src->linesize[0],
  2250. dst_width, dst_height);
  2251. } else {
  2252. img_apply_table(dst->data[0], dst->linesize[0],
  2253. src->data[0], src->linesize[0],
  2254. dst_width, dst_height,
  2255. y_ccir_to_jpeg);
  2256. }
  2257. return 0;
  2258. }
  2259. /* YUV to YUV planar */
  2260. if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
  2261. int x_shift, y_shift, w, h, xy_shift;
  2262. void (*resize_func)(uint8_t *dst, int dst_wrap,
  2263. const uint8_t *src, int src_wrap,
  2264. int width, int height);
  2265. /* compute chroma size of the smallest dimensions */
  2266. w = dst_width;
  2267. h = dst_height;
  2268. if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
  2269. w >>= dst_pix->x_chroma_shift;
  2270. else
  2271. w >>= src_pix->x_chroma_shift;
  2272. if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
  2273. h >>= dst_pix->y_chroma_shift;
  2274. else
  2275. h >>= src_pix->y_chroma_shift;
  2276. x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
  2277. y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
  2278. xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
  2279. /* there must be filters for conversion at least from and to
  2280. YUV444 format */
  2281. switch(xy_shift) {
  2282. case 0x00:
  2283. resize_func = ff_img_copy_plane;
  2284. break;
  2285. case 0x10:
  2286. resize_func = shrink21;
  2287. break;
  2288. case 0x20:
  2289. resize_func = shrink41;
  2290. break;
  2291. case 0x01:
  2292. resize_func = shrink12;
  2293. break;
  2294. case 0x11:
  2295. resize_func = ff_shrink22;
  2296. break;
  2297. case 0x22:
  2298. resize_func = ff_shrink44;
  2299. break;
  2300. case 0xf0:
  2301. resize_func = grow21;
  2302. break;
  2303. case 0x0f:
  2304. resize_func = grow12;
  2305. break;
  2306. case 0xe0:
  2307. resize_func = grow41;
  2308. break;
  2309. case 0xff:
  2310. resize_func = grow22;
  2311. break;
  2312. case 0xee:
  2313. resize_func = grow44;
  2314. break;
  2315. case 0xf1:
  2316. resize_func = conv411;
  2317. break;
  2318. default:
  2319. /* currently not handled */
  2320. goto no_chroma_filter;
  2321. }
  2322. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  2323. src->data[0], src->linesize[0],
  2324. dst_width, dst_height);
  2325. for(i = 1;i <= 2; i++)
  2326. resize_func(dst->data[i], dst->linesize[i],
  2327. src->data[i], src->linesize[i],
  2328. dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
  2329. /* if yuv color space conversion is needed, we do it here on
  2330. the destination image */
  2331. if (dst_pix->color_type != src_pix->color_type) {
  2332. const uint8_t *y_table, *c_table;
  2333. if (dst_pix->color_type == FF_COLOR_YUV) {
  2334. y_table = y_jpeg_to_ccir;
  2335. c_table = c_jpeg_to_ccir;
  2336. } else {
  2337. y_table = y_ccir_to_jpeg;
  2338. c_table = c_ccir_to_jpeg;
  2339. }
  2340. img_apply_table(dst->data[0], dst->linesize[0],
  2341. dst->data[0], dst->linesize[0],
  2342. dst_width, dst_height,
  2343. y_table);
  2344. for(i = 1;i <= 2; i++)
  2345. img_apply_table(dst->data[i], dst->linesize[i],
  2346. dst->data[i], dst->linesize[i],
  2347. dst_width>>dst_pix->x_chroma_shift,
  2348. dst_height>>dst_pix->y_chroma_shift,
  2349. c_table);
  2350. }
  2351. return 0;
  2352. }
  2353. no_chroma_filter:
  2354. /* try to use an intermediate format */
  2355. if (src_pix_fmt == PIX_FMT_YUYV422 ||
  2356. dst_pix_fmt == PIX_FMT_YUYV422) {
  2357. /* specific case: convert to YUV422P first */
  2358. int_pix_fmt = PIX_FMT_YUV422P;
  2359. } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
  2360. dst_pix_fmt == PIX_FMT_UYVY422) {
  2361. /* specific case: convert to YUV422P first */
  2362. int_pix_fmt = PIX_FMT_YUV422P;
  2363. } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
  2364. dst_pix_fmt == PIX_FMT_UYYVYY411) {
  2365. /* specific case: convert to YUV411P first */
  2366. int_pix_fmt = PIX_FMT_YUV411P;
  2367. } else if ((src_pix->color_type == FF_COLOR_GRAY &&
  2368. src_pix_fmt != PIX_FMT_GRAY8) ||
  2369. (dst_pix->color_type == FF_COLOR_GRAY &&
  2370. dst_pix_fmt != PIX_FMT_GRAY8)) {
  2371. /* gray8 is the normalized format */
  2372. int_pix_fmt = PIX_FMT_GRAY8;
  2373. } else if ((is_yuv_planar(src_pix) &&
  2374. src_pix_fmt != PIX_FMT_YUV444P &&
  2375. src_pix_fmt != PIX_FMT_YUVJ444P)) {
  2376. /* yuv444 is the normalized format */
  2377. if (src_pix->color_type == FF_COLOR_YUV_JPEG)
  2378. int_pix_fmt = PIX_FMT_YUVJ444P;
  2379. else
  2380. int_pix_fmt = PIX_FMT_YUV444P;
  2381. } else if ((is_yuv_planar(dst_pix) &&
  2382. dst_pix_fmt != PIX_FMT_YUV444P &&
  2383. dst_pix_fmt != PIX_FMT_YUVJ444P)) {
  2384. /* yuv444 is the normalized format */
  2385. if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
  2386. int_pix_fmt = PIX_FMT_YUVJ444P;
  2387. else
  2388. int_pix_fmt = PIX_FMT_YUV444P;
  2389. } else {
  2390. /* the two formats are rgb or gray8 or yuv[j]444p */
  2391. if (src_pix->is_alpha && dst_pix->is_alpha)
  2392. int_pix_fmt = PIX_FMT_RGB32;
  2393. else
  2394. int_pix_fmt = PIX_FMT_RGB24;
  2395. }
  2396. if (src_pix_fmt == int_pix_fmt)
  2397. return -1;
  2398. if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
  2399. return -1;
  2400. ret = -1;
  2401. if (img_convert(tmp, int_pix_fmt,
  2402. src, src_pix_fmt, src_width, src_height) < 0)
  2403. goto fail1;
  2404. if (img_convert(dst, dst_pix_fmt,
  2405. tmp, int_pix_fmt, dst_width, dst_height) < 0)
  2406. goto fail1;
  2407. ret = 0;
  2408. fail1:
  2409. avpicture_free(tmp);
  2410. return ret;
  2411. }
  2412. #endif
  2413. /* NOTE: we scan all the pixels to have an exact information */
  2414. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  2415. {
  2416. const unsigned char *p;
  2417. int src_wrap, ret, x, y;
  2418. unsigned int a;
  2419. uint32_t *palette = (uint32_t *)src->data[1];
  2420. p = src->data[0];
  2421. src_wrap = src->linesize[0] - width;
  2422. ret = 0;
  2423. for(y=0;y<height;y++) {
  2424. for(x=0;x<width;x++) {
  2425. a = palette[p[0]] >> 24;
  2426. if (a == 0x00) {
  2427. ret |= FF_ALPHA_TRANSP;
  2428. } else if (a != 0xff) {
  2429. ret |= FF_ALPHA_SEMI_TRANSP;
  2430. }
  2431. p++;
  2432. }
  2433. p += src_wrap;
  2434. }
  2435. return ret;
  2436. }
  2437. int img_get_alpha_info(const AVPicture *src,
  2438. int pix_fmt, int width, int height)
  2439. {
  2440. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  2441. int ret;
  2442. pf = &pix_fmt_info[pix_fmt];
  2443. /* no alpha can be represented in format */
  2444. if (!pf->is_alpha)
  2445. return 0;
  2446. switch(pix_fmt) {
  2447. case PIX_FMT_RGB32:
  2448. ret = get_alpha_info_rgb32(src, width, height);
  2449. break;
  2450. case PIX_FMT_PAL8:
  2451. ret = get_alpha_info_pal8(src, width, height);
  2452. break;
  2453. default:
  2454. /* we do not know, so everything is indicated */
  2455. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  2456. break;
  2457. }
  2458. return ret;
  2459. }
  2460. #ifdef HAVE_MMX
  2461. #define DEINT_INPLACE_LINE_LUM \
  2462. movd_m2r(lum_m4[0],mm0);\
  2463. movd_m2r(lum_m3[0],mm1);\
  2464. movd_m2r(lum_m2[0],mm2);\
  2465. movd_m2r(lum_m1[0],mm3);\
  2466. movd_m2r(lum[0],mm4);\
  2467. punpcklbw_r2r(mm7,mm0);\
  2468. movd_r2m(mm2,lum_m4[0]);\
  2469. punpcklbw_r2r(mm7,mm1);\
  2470. punpcklbw_r2r(mm7,mm2);\
  2471. punpcklbw_r2r(mm7,mm3);\
  2472. punpcklbw_r2r(mm7,mm4);\
  2473. paddw_r2r(mm3,mm1);\
  2474. psllw_i2r(1,mm2);\
  2475. paddw_r2r(mm4,mm0);\
  2476. psllw_i2r(2,mm1);\
  2477. paddw_r2r(mm6,mm2);\
  2478. paddw_r2r(mm2,mm1);\
  2479. psubusw_r2r(mm0,mm1);\
  2480. psrlw_i2r(3,mm1);\
  2481. packuswb_r2r(mm7,mm1);\
  2482. movd_r2m(mm1,lum_m2[0]);
  2483. #define DEINT_LINE_LUM \
  2484. movd_m2r(lum_m4[0],mm0);\
  2485. movd_m2r(lum_m3[0],mm1);\
  2486. movd_m2r(lum_m2[0],mm2);\
  2487. movd_m2r(lum_m1[0],mm3);\
  2488. movd_m2r(lum[0],mm4);\
  2489. punpcklbw_r2r(mm7,mm0);\
  2490. punpcklbw_r2r(mm7,mm1);\
  2491. punpcklbw_r2r(mm7,mm2);\
  2492. punpcklbw_r2r(mm7,mm3);\
  2493. punpcklbw_r2r(mm7,mm4);\
  2494. paddw_r2r(mm3,mm1);\
  2495. psllw_i2r(1,mm2);\
  2496. paddw_r2r(mm4,mm0);\
  2497. psllw_i2r(2,mm1);\
  2498. paddw_r2r(mm6,mm2);\
  2499. paddw_r2r(mm2,mm1);\
  2500. psubusw_r2r(mm0,mm1);\
  2501. psrlw_i2r(3,mm1);\
  2502. packuswb_r2r(mm7,mm1);\
  2503. movd_r2m(mm1,dst[0]);
  2504. #endif
  2505. /* filter parameters: [-1 4 2 4 -1] // 8 */
  2506. static void deinterlace_line(uint8_t *dst,
  2507. const uint8_t *lum_m4, const uint8_t *lum_m3,
  2508. const uint8_t *lum_m2, const uint8_t *lum_m1,
  2509. const uint8_t *lum,
  2510. int size)
  2511. {
  2512. #ifndef HAVE_MMX
  2513. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2514. int sum;
  2515. for(;size > 0;size--) {
  2516. sum = -lum_m4[0];
  2517. sum += lum_m3[0] << 2;
  2518. sum += lum_m2[0] << 1;
  2519. sum += lum_m1[0] << 2;
  2520. sum += -lum[0];
  2521. dst[0] = cm[(sum + 4) >> 3];
  2522. lum_m4++;
  2523. lum_m3++;
  2524. lum_m2++;
  2525. lum_m1++;
  2526. lum++;
  2527. dst++;
  2528. }
  2529. #else
  2530. {
  2531. mmx_t rounder;
  2532. rounder.uw[0]=4;
  2533. rounder.uw[1]=4;
  2534. rounder.uw[2]=4;
  2535. rounder.uw[3]=4;
  2536. pxor_r2r(mm7,mm7);
  2537. movq_m2r(rounder,mm6);
  2538. }
  2539. for (;size > 3; size-=4) {
  2540. DEINT_LINE_LUM
  2541. lum_m4+=4;
  2542. lum_m3+=4;
  2543. lum_m2+=4;
  2544. lum_m1+=4;
  2545. lum+=4;
  2546. dst+=4;
  2547. }
  2548. #endif
  2549. }
  2550. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  2551. int size)
  2552. {
  2553. #ifndef HAVE_MMX
  2554. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  2555. int sum;
  2556. for(;size > 0;size--) {
  2557. sum = -lum_m4[0];
  2558. sum += lum_m3[0] << 2;
  2559. sum += lum_m2[0] << 1;
  2560. lum_m4[0]=lum_m2[0];
  2561. sum += lum_m1[0] << 2;
  2562. sum += -lum[0];
  2563. lum_m2[0] = cm[(sum + 4) >> 3];
  2564. lum_m4++;
  2565. lum_m3++;
  2566. lum_m2++;
  2567. lum_m1++;
  2568. lum++;
  2569. }
  2570. #else
  2571. {
  2572. mmx_t rounder;
  2573. rounder.uw[0]=4;
  2574. rounder.uw[1]=4;
  2575. rounder.uw[2]=4;
  2576. rounder.uw[3]=4;
  2577. pxor_r2r(mm7,mm7);
  2578. movq_m2r(rounder,mm6);
  2579. }
  2580. for (;size > 3; size-=4) {
  2581. DEINT_INPLACE_LINE_LUM
  2582. lum_m4+=4;
  2583. lum_m3+=4;
  2584. lum_m2+=4;
  2585. lum_m1+=4;
  2586. lum+=4;
  2587. }
  2588. #endif
  2589. }
  2590. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  2591. top field is copied as is, but the bottom field is deinterlaced
  2592. against the top field. */
  2593. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  2594. const uint8_t *src1, int src_wrap,
  2595. int width, int height)
  2596. {
  2597. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  2598. int y;
  2599. src_m2 = src1;
  2600. src_m1 = src1;
  2601. src_0=&src_m1[src_wrap];
  2602. src_p1=&src_0[src_wrap];
  2603. src_p2=&src_p1[src_wrap];
  2604. for(y=0;y<(height-2);y+=2) {
  2605. memcpy(dst,src_m1,width);
  2606. dst += dst_wrap;
  2607. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  2608. src_m2 = src_0;
  2609. src_m1 = src_p1;
  2610. src_0 = src_p2;
  2611. src_p1 += 2*src_wrap;
  2612. src_p2 += 2*src_wrap;
  2613. dst += dst_wrap;
  2614. }
  2615. memcpy(dst,src_m1,width);
  2616. dst += dst_wrap;
  2617. /* do last line */
  2618. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  2619. }
  2620. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  2621. int width, int height)
  2622. {
  2623. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  2624. int y;
  2625. uint8_t *buf;
  2626. buf = (uint8_t*)av_malloc(width);
  2627. src_m1 = src1;
  2628. memcpy(buf,src_m1,width);
  2629. src_0=&src_m1[src_wrap];
  2630. src_p1=&src_0[src_wrap];
  2631. src_p2=&src_p1[src_wrap];
  2632. for(y=0;y<(height-2);y+=2) {
  2633. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  2634. src_m1 = src_p1;
  2635. src_0 = src_p2;
  2636. src_p1 += 2*src_wrap;
  2637. src_p2 += 2*src_wrap;
  2638. }
  2639. /* do last line */
  2640. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  2641. av_free(buf);
  2642. }
  2643. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  2644. int pix_fmt, int width, int height)
  2645. {
  2646. int i;
  2647. if (pix_fmt != PIX_FMT_YUV420P &&
  2648. pix_fmt != PIX_FMT_YUV422P &&
  2649. pix_fmt != PIX_FMT_YUV444P &&
  2650. pix_fmt != PIX_FMT_YUV411P &&
  2651. pix_fmt != PIX_FMT_GRAY8)
  2652. return -1;
  2653. if ((width & 3) != 0 || (height & 3) != 0)
  2654. return -1;
  2655. for(i=0;i<3;i++) {
  2656. if (i == 1) {
  2657. switch(pix_fmt) {
  2658. case PIX_FMT_YUV420P:
  2659. width >>= 1;
  2660. height >>= 1;
  2661. break;
  2662. case PIX_FMT_YUV422P:
  2663. width >>= 1;
  2664. break;
  2665. case PIX_FMT_YUV411P:
  2666. width >>= 2;
  2667. break;
  2668. default:
  2669. break;
  2670. }
  2671. if (pix_fmt == PIX_FMT_GRAY8) {
  2672. break;
  2673. }
  2674. }
  2675. if (src == dst) {
  2676. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  2677. width, height);
  2678. } else {
  2679. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  2680. src->data[i], src->linesize[i],
  2681. width, height);
  2682. }
  2683. }
  2684. emms_c();
  2685. return 0;
  2686. }