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.

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