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.

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