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.

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