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.

2983 lines
79KB

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