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.

1650 lines
47KB

  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. [PIX_FMT_YUV420PLE] = {
  125. .name = "yuv420ple",
  126. .nb_channels = 3,
  127. .color_type = FF_COLOR_YUV,
  128. .pixel_type = FF_PIXEL_PLANAR,
  129. .depth = 16,
  130. .x_chroma_shift = 1, .y_chroma_shift = 1,
  131. },
  132. [PIX_FMT_YUV422PLE] = {
  133. .name = "yuv422ple",
  134. .nb_channels = 3,
  135. .color_type = FF_COLOR_YUV,
  136. .pixel_type = FF_PIXEL_PLANAR,
  137. .depth = 16,
  138. .x_chroma_shift = 1, .y_chroma_shift = 0,
  139. },
  140. [PIX_FMT_YUV444PLE] = {
  141. .name = "yuv444ple",
  142. .nb_channels = 3,
  143. .color_type = FF_COLOR_YUV,
  144. .pixel_type = FF_PIXEL_PLANAR,
  145. .depth = 16,
  146. .x_chroma_shift = 0, .y_chroma_shift = 0,
  147. },
  148. [PIX_FMT_YUV420PBE] = {
  149. .name = "yuv420pbe",
  150. .nb_channels = 3,
  151. .color_type = FF_COLOR_YUV,
  152. .pixel_type = FF_PIXEL_PLANAR,
  153. .depth = 16,
  154. .x_chroma_shift = 1, .y_chroma_shift = 1,
  155. },
  156. [PIX_FMT_YUV422PBE] = {
  157. .name = "yuv422pbe",
  158. .nb_channels = 3,
  159. .color_type = FF_COLOR_YUV,
  160. .pixel_type = FF_PIXEL_PLANAR,
  161. .depth = 16,
  162. .x_chroma_shift = 1, .y_chroma_shift = 0,
  163. },
  164. [PIX_FMT_YUV444PBE] = {
  165. .name = "yuv444pbe",
  166. .nb_channels = 3,
  167. .color_type = FF_COLOR_YUV,
  168. .pixel_type = FF_PIXEL_PLANAR,
  169. .depth = 16,
  170. .x_chroma_shift = 0, .y_chroma_shift = 0,
  171. },
  172. /* YUV formats with alpha plane */
  173. [PIX_FMT_YUVA420P] = {
  174. .name = "yuva420p",
  175. .nb_channels = 4,
  176. .color_type = FF_COLOR_YUV,
  177. .pixel_type = FF_PIXEL_PLANAR,
  178. .depth = 8,
  179. .x_chroma_shift = 1, .y_chroma_shift = 1,
  180. },
  181. /* JPEG YUV */
  182. [PIX_FMT_YUVJ420P] = {
  183. .name = "yuvj420p",
  184. .nb_channels = 3,
  185. .color_type = FF_COLOR_YUV_JPEG,
  186. .pixel_type = FF_PIXEL_PLANAR,
  187. .depth = 8,
  188. .x_chroma_shift = 1, .y_chroma_shift = 1,
  189. },
  190. [PIX_FMT_YUVJ422P] = {
  191. .name = "yuvj422p",
  192. .nb_channels = 3,
  193. .color_type = FF_COLOR_YUV_JPEG,
  194. .pixel_type = FF_PIXEL_PLANAR,
  195. .depth = 8,
  196. .x_chroma_shift = 1, .y_chroma_shift = 0,
  197. },
  198. [PIX_FMT_YUVJ444P] = {
  199. .name = "yuvj444p",
  200. .nb_channels = 3,
  201. .color_type = FF_COLOR_YUV_JPEG,
  202. .pixel_type = FF_PIXEL_PLANAR,
  203. .depth = 8,
  204. .x_chroma_shift = 0, .y_chroma_shift = 0,
  205. },
  206. [PIX_FMT_YUVJ440P] = {
  207. .name = "yuvj440p",
  208. .nb_channels = 3,
  209. .color_type = FF_COLOR_YUV_JPEG,
  210. .pixel_type = FF_PIXEL_PLANAR,
  211. .depth = 8,
  212. .x_chroma_shift = 0, .y_chroma_shift = 1,
  213. },
  214. /* RGB formats */
  215. [PIX_FMT_RGB24] = {
  216. .name = "rgb24",
  217. .nb_channels = 3,
  218. .color_type = FF_COLOR_RGB,
  219. .pixel_type = FF_PIXEL_PACKED,
  220. .depth = 8,
  221. .x_chroma_shift = 0, .y_chroma_shift = 0,
  222. },
  223. [PIX_FMT_BGR24] = {
  224. .name = "bgr24",
  225. .nb_channels = 3,
  226. .color_type = FF_COLOR_RGB,
  227. .pixel_type = FF_PIXEL_PACKED,
  228. .depth = 8,
  229. .x_chroma_shift = 0, .y_chroma_shift = 0,
  230. },
  231. [PIX_FMT_ARGB] = {
  232. .name = "argb",
  233. .nb_channels = 4, .is_alpha = 1,
  234. .color_type = FF_COLOR_RGB,
  235. .pixel_type = FF_PIXEL_PACKED,
  236. .depth = 8,
  237. .x_chroma_shift = 0, .y_chroma_shift = 0,
  238. },
  239. [PIX_FMT_RGB48BE] = {
  240. .name = "rgb48be",
  241. .nb_channels = 3,
  242. .color_type = FF_COLOR_RGB,
  243. .pixel_type = FF_PIXEL_PACKED,
  244. .depth = 16,
  245. .x_chroma_shift = 0, .y_chroma_shift = 0,
  246. },
  247. [PIX_FMT_RGB48LE] = {
  248. .name = "rgb48le",
  249. .nb_channels = 3,
  250. .color_type = FF_COLOR_RGB,
  251. .pixel_type = FF_PIXEL_PACKED,
  252. .depth = 16,
  253. .x_chroma_shift = 0, .y_chroma_shift = 0,
  254. },
  255. [PIX_FMT_RGB565BE] = {
  256. .name = "rgb565be",
  257. .nb_channels = 3,
  258. .color_type = FF_COLOR_RGB,
  259. .pixel_type = FF_PIXEL_PACKED,
  260. .depth = 5,
  261. .x_chroma_shift = 0, .y_chroma_shift = 0,
  262. },
  263. [PIX_FMT_RGB565LE] = {
  264. .name = "rgb565le",
  265. .nb_channels = 3,
  266. .color_type = FF_COLOR_RGB,
  267. .pixel_type = FF_PIXEL_PACKED,
  268. .depth = 5,
  269. .x_chroma_shift = 0, .y_chroma_shift = 0,
  270. },
  271. [PIX_FMT_RGB555BE] = {
  272. .name = "rgb555be",
  273. .nb_channels = 3,
  274. .color_type = FF_COLOR_RGB,
  275. .pixel_type = FF_PIXEL_PACKED,
  276. .depth = 5,
  277. .x_chroma_shift = 0, .y_chroma_shift = 0,
  278. },
  279. [PIX_FMT_RGB555LE] = {
  280. .name = "rgb555le",
  281. .nb_channels = 3,
  282. .color_type = FF_COLOR_RGB,
  283. .pixel_type = FF_PIXEL_PACKED,
  284. .depth = 5,
  285. .x_chroma_shift = 0, .y_chroma_shift = 0,
  286. },
  287. /* gray / mono formats */
  288. [PIX_FMT_GRAY16BE] = {
  289. .name = "gray16be",
  290. .nb_channels = 1,
  291. .color_type = FF_COLOR_GRAY,
  292. .pixel_type = FF_PIXEL_PLANAR,
  293. .depth = 16,
  294. },
  295. [PIX_FMT_GRAY16LE] = {
  296. .name = "gray16le",
  297. .nb_channels = 1,
  298. .color_type = FF_COLOR_GRAY,
  299. .pixel_type = FF_PIXEL_PLANAR,
  300. .depth = 16,
  301. },
  302. [PIX_FMT_GRAY8] = {
  303. .name = "gray",
  304. .nb_channels = 1,
  305. .color_type = FF_COLOR_GRAY,
  306. .pixel_type = FF_PIXEL_PLANAR,
  307. .depth = 8,
  308. },
  309. [PIX_FMT_MONOWHITE] = {
  310. .name = "monow",
  311. .nb_channels = 1,
  312. .color_type = FF_COLOR_GRAY,
  313. .pixel_type = FF_PIXEL_PLANAR,
  314. .depth = 1,
  315. },
  316. [PIX_FMT_MONOBLACK] = {
  317. .name = "monob",
  318. .nb_channels = 1,
  319. .color_type = FF_COLOR_GRAY,
  320. .pixel_type = FF_PIXEL_PLANAR,
  321. .depth = 1,
  322. },
  323. /* paletted formats */
  324. [PIX_FMT_PAL8] = {
  325. .name = "pal8",
  326. .nb_channels = 4, .is_alpha = 1,
  327. .color_type = FF_COLOR_RGB,
  328. .pixel_type = FF_PIXEL_PALETTE,
  329. .depth = 8,
  330. },
  331. [PIX_FMT_XVMC_MPEG2_MC] = {
  332. .name = "xvmcmc",
  333. .is_hwaccel = 1,
  334. },
  335. [PIX_FMT_XVMC_MPEG2_IDCT] = {
  336. .name = "xvmcidct",
  337. .is_hwaccel = 1,
  338. },
  339. [PIX_FMT_VDPAU_MPEG1] = {
  340. .name = "vdpau_mpeg1",
  341. .is_hwaccel = 1,
  342. .x_chroma_shift = 1, .y_chroma_shift = 1,
  343. },
  344. [PIX_FMT_VDPAU_MPEG2] = {
  345. .name = "vdpau_mpeg2",
  346. .is_hwaccel = 1,
  347. .x_chroma_shift = 1, .y_chroma_shift = 1,
  348. },
  349. [PIX_FMT_VDPAU_H264] = {
  350. .name = "vdpau_h264",
  351. .is_hwaccel = 1,
  352. .x_chroma_shift = 1, .y_chroma_shift = 1,
  353. },
  354. [PIX_FMT_VDPAU_WMV3] = {
  355. .name = "vdpau_wmv3",
  356. .is_hwaccel = 1,
  357. .x_chroma_shift = 1, .y_chroma_shift = 1,
  358. },
  359. [PIX_FMT_VDPAU_VC1] = {
  360. .name = "vdpau_vc1",
  361. .is_hwaccel = 1,
  362. .x_chroma_shift = 1, .y_chroma_shift = 1,
  363. },
  364. [PIX_FMT_UYYVYY411] = {
  365. .name = "uyyvyy411",
  366. .nb_channels = 1,
  367. .color_type = FF_COLOR_YUV,
  368. .pixel_type = FF_PIXEL_PACKED,
  369. .depth = 8,
  370. .x_chroma_shift = 2, .y_chroma_shift = 0,
  371. },
  372. [PIX_FMT_ABGR] = {
  373. .name = "abgr",
  374. .nb_channels = 4, .is_alpha = 1,
  375. .color_type = FF_COLOR_RGB,
  376. .pixel_type = FF_PIXEL_PACKED,
  377. .depth = 8,
  378. .x_chroma_shift = 0, .y_chroma_shift = 0,
  379. },
  380. [PIX_FMT_BGR565BE] = {
  381. .name = "bgr565be",
  382. .nb_channels = 3,
  383. .color_type = FF_COLOR_RGB,
  384. .pixel_type = FF_PIXEL_PACKED,
  385. .depth = 5,
  386. .x_chroma_shift = 0, .y_chroma_shift = 0,
  387. },
  388. [PIX_FMT_BGR565LE] = {
  389. .name = "bgr565le",
  390. .nb_channels = 3,
  391. .color_type = FF_COLOR_RGB,
  392. .pixel_type = FF_PIXEL_PACKED,
  393. .depth = 5,
  394. .x_chroma_shift = 0, .y_chroma_shift = 0,
  395. },
  396. [PIX_FMT_BGR555BE] = {
  397. .name = "bgr555be",
  398. .nb_channels = 3,
  399. .color_type = FF_COLOR_RGB,
  400. .pixel_type = FF_PIXEL_PACKED,
  401. .depth = 5,
  402. .x_chroma_shift = 0, .y_chroma_shift = 0,
  403. },
  404. [PIX_FMT_BGR555LE] = {
  405. .name = "bgr555le",
  406. .nb_channels = 3,
  407. .color_type = FF_COLOR_RGB,
  408. .pixel_type = FF_PIXEL_PACKED,
  409. .depth = 5,
  410. .x_chroma_shift = 0, .y_chroma_shift = 0,
  411. },
  412. [PIX_FMT_RGB8] = {
  413. .name = "rgb8",
  414. .nb_channels = 1,
  415. .color_type = FF_COLOR_RGB,
  416. .pixel_type = FF_PIXEL_PACKED,
  417. .depth = 8,
  418. .x_chroma_shift = 0, .y_chroma_shift = 0,
  419. },
  420. [PIX_FMT_RGB4] = {
  421. .name = "rgb4",
  422. .nb_channels = 1,
  423. .color_type = FF_COLOR_RGB,
  424. .pixel_type = FF_PIXEL_PACKED,
  425. .depth = 4,
  426. .x_chroma_shift = 0, .y_chroma_shift = 0,
  427. },
  428. [PIX_FMT_RGB4_BYTE] = {
  429. .name = "rgb4_byte",
  430. .nb_channels = 1,
  431. .color_type = FF_COLOR_RGB,
  432. .pixel_type = FF_PIXEL_PACKED,
  433. .depth = 8,
  434. .x_chroma_shift = 0, .y_chroma_shift = 0,
  435. },
  436. [PIX_FMT_BGR8] = {
  437. .name = "bgr8",
  438. .nb_channels = 1,
  439. .color_type = FF_COLOR_RGB,
  440. .pixel_type = FF_PIXEL_PACKED,
  441. .depth = 8,
  442. .x_chroma_shift = 0, .y_chroma_shift = 0,
  443. },
  444. [PIX_FMT_BGR4] = {
  445. .name = "bgr4",
  446. .nb_channels = 1,
  447. .color_type = FF_COLOR_RGB,
  448. .pixel_type = FF_PIXEL_PACKED,
  449. .depth = 4,
  450. .x_chroma_shift = 0, .y_chroma_shift = 0,
  451. },
  452. [PIX_FMT_BGR4_BYTE] = {
  453. .name = "bgr4_byte",
  454. .nb_channels = 1,
  455. .color_type = FF_COLOR_RGB,
  456. .pixel_type = FF_PIXEL_PACKED,
  457. .depth = 8,
  458. .x_chroma_shift = 0, .y_chroma_shift = 0,
  459. },
  460. [PIX_FMT_NV12] = {
  461. .name = "nv12",
  462. .nb_channels = 2,
  463. .color_type = FF_COLOR_YUV,
  464. .pixel_type = FF_PIXEL_PLANAR,
  465. .depth = 8,
  466. .x_chroma_shift = 1, .y_chroma_shift = 1,
  467. },
  468. [PIX_FMT_NV21] = {
  469. .name = "nv12",
  470. .nb_channels = 2,
  471. .color_type = FF_COLOR_YUV,
  472. .pixel_type = FF_PIXEL_PLANAR,
  473. .depth = 8,
  474. .x_chroma_shift = 1, .y_chroma_shift = 1,
  475. },
  476. [PIX_FMT_BGRA] = {
  477. .name = "bgra",
  478. .nb_channels = 4, .is_alpha = 1,
  479. .color_type = FF_COLOR_RGB,
  480. .pixel_type = FF_PIXEL_PACKED,
  481. .depth = 8,
  482. .x_chroma_shift = 0, .y_chroma_shift = 0,
  483. },
  484. [PIX_FMT_RGBA] = {
  485. .name = "rgba",
  486. .nb_channels = 4, .is_alpha = 1,
  487. .color_type = FF_COLOR_RGB,
  488. .pixel_type = FF_PIXEL_PACKED,
  489. .depth = 8,
  490. .x_chroma_shift = 0, .y_chroma_shift = 0,
  491. },
  492. /* VA API formats */
  493. [PIX_FMT_VAAPI_MOCO] = {
  494. .name = "vaapi_moco",
  495. .is_hwaccel = 1,
  496. .x_chroma_shift = 1, .y_chroma_shift = 1,
  497. },
  498. [PIX_FMT_VAAPI_IDCT] = {
  499. .name = "vaapi_idct",
  500. .is_hwaccel = 1,
  501. .x_chroma_shift = 1, .y_chroma_shift = 1,
  502. },
  503. [PIX_FMT_VAAPI_VLD] = {
  504. .name = "vaapi_vld",
  505. .is_hwaccel = 1,
  506. .x_chroma_shift = 1, .y_chroma_shift = 1,
  507. },
  508. };
  509. void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
  510. {
  511. *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  512. *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  513. }
  514. const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
  515. {
  516. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  517. return NULL;
  518. else
  519. return pix_fmt_info[pix_fmt].name;
  520. }
  521. static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
  522. {
  523. int i;
  524. for (i=0; i < PIX_FMT_NB; i++)
  525. if (pix_fmt_info[i].name && !strcmp(pix_fmt_info[i].name, name))
  526. return i;
  527. return PIX_FMT_NONE;
  528. }
  529. #ifdef WORDS_BIGENDIAN
  530. # define X_NE(be, le) be
  531. #else
  532. # define X_NE(be, le) le
  533. #endif
  534. enum PixelFormat avcodec_get_pix_fmt(const char *name)
  535. {
  536. enum PixelFormat pix_fmt;
  537. if (!strcmp(name, "rgb32"))
  538. name = X_NE("argb", "bgra");
  539. else if (!strcmp(name, "bgr32"))
  540. name = X_NE("abgr", "rgba");
  541. pix_fmt = avcodec_get_pix_fmt_internal(name);
  542. if (pix_fmt == PIX_FMT_NONE) {
  543. char name2[32];
  544. snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
  545. pix_fmt = avcodec_get_pix_fmt_internal(name2);
  546. }
  547. return pix_fmt;
  548. }
  549. void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
  550. {
  551. /* print header */
  552. if (pix_fmt < 0)
  553. snprintf (buf, buf_size,
  554. "name " " nb_channels" " depth" " is_alpha"
  555. );
  556. else{
  557. PixFmtInfo info= pix_fmt_info[pix_fmt];
  558. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  559. snprintf (buf, buf_size,
  560. "%-10s" " %1d " " %2d " " %c ",
  561. info.name,
  562. info.nb_channels,
  563. info.depth,
  564. is_alpha_char
  565. );
  566. }
  567. }
  568. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  569. {
  570. return pix_fmt_info[pix_fmt].is_hwaccel;
  571. }
  572. int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
  573. int i;
  574. for(i=0; i<256; i++){
  575. int r,g,b;
  576. switch(pix_fmt) {
  577. case PIX_FMT_RGB8:
  578. r= (i>>5 )*36;
  579. g= ((i>>2)&7)*36;
  580. b= (i&3 )*85;
  581. break;
  582. case PIX_FMT_BGR8:
  583. b= (i>>6 )*85;
  584. g= ((i>>3)&7)*36;
  585. r= (i&7 )*36;
  586. break;
  587. case PIX_FMT_RGB4_BYTE:
  588. r= (i>>3 )*255;
  589. g= ((i>>1)&3)*85;
  590. b= (i&1 )*255;
  591. break;
  592. case PIX_FMT_BGR4_BYTE:
  593. b= (i>>3 )*255;
  594. g= ((i>>1)&3)*85;
  595. r= (i&1 )*255;
  596. break;
  597. case PIX_FMT_GRAY8:
  598. r=b=g= i;
  599. break;
  600. default:
  601. return -1;
  602. }
  603. pal[i] = b + (g<<8) + (r<<16);
  604. }
  605. return 0;
  606. }
  607. int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
  608. {
  609. int w2;
  610. const PixFmtInfo *pinfo;
  611. memset(picture->linesize, 0, sizeof(picture->linesize));
  612. pinfo = &pix_fmt_info[pix_fmt];
  613. switch(pix_fmt) {
  614. case PIX_FMT_YUV420P:
  615. case PIX_FMT_YUV422P:
  616. case PIX_FMT_YUV444P:
  617. case PIX_FMT_YUV410P:
  618. case PIX_FMT_YUV411P:
  619. case PIX_FMT_YUV440P:
  620. case PIX_FMT_YUVJ420P:
  621. case PIX_FMT_YUVJ422P:
  622. case PIX_FMT_YUVJ444P:
  623. case PIX_FMT_YUVJ440P:
  624. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  625. picture->linesize[0] = width;
  626. picture->linesize[1] = w2;
  627. picture->linesize[2] = w2;
  628. break;
  629. case PIX_FMT_YUV420PLE:
  630. case PIX_FMT_YUV422PLE:
  631. case PIX_FMT_YUV444PLE:
  632. case PIX_FMT_YUV420PBE:
  633. case PIX_FMT_YUV422PBE:
  634. case PIX_FMT_YUV444PBE:
  635. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  636. picture->linesize[0] = 2*width;
  637. picture->linesize[1] = 2*w2;
  638. picture->linesize[2] = 2*w2;
  639. break;
  640. case PIX_FMT_YUVA420P:
  641. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  642. picture->linesize[0] = width;
  643. picture->linesize[1] = w2;
  644. picture->linesize[2] = w2;
  645. picture->linesize[3] = width;
  646. break;
  647. case PIX_FMT_NV12:
  648. case PIX_FMT_NV21:
  649. w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
  650. picture->linesize[0] = width;
  651. picture->linesize[1] = w2;
  652. break;
  653. case PIX_FMT_RGB24:
  654. case PIX_FMT_BGR24:
  655. picture->linesize[0] = width * 3;
  656. break;
  657. case PIX_FMT_ARGB:
  658. case PIX_FMT_ABGR:
  659. case PIX_FMT_RGBA:
  660. case PIX_FMT_BGRA:
  661. picture->linesize[0] = width * 4;
  662. break;
  663. case PIX_FMT_RGB48BE:
  664. case PIX_FMT_RGB48LE:
  665. picture->linesize[0] = width * 6;
  666. break;
  667. case PIX_FMT_GRAY16BE:
  668. case PIX_FMT_GRAY16LE:
  669. case PIX_FMT_BGR555:
  670. case PIX_FMT_BGR565:
  671. case PIX_FMT_RGB555:
  672. case PIX_FMT_RGB565:
  673. case PIX_FMT_YUYV422:
  674. picture->linesize[0] = width * 2;
  675. break;
  676. case PIX_FMT_UYVY422:
  677. picture->linesize[0] = width * 2;
  678. break;
  679. case PIX_FMT_UYYVYY411:
  680. picture->linesize[0] = width + width/2;
  681. break;
  682. case PIX_FMT_RGB4:
  683. case PIX_FMT_BGR4:
  684. picture->linesize[0] = width / 2;
  685. break;
  686. case PIX_FMT_MONOWHITE:
  687. case PIX_FMT_MONOBLACK:
  688. picture->linesize[0] = (width + 7) >> 3;
  689. break;
  690. case PIX_FMT_PAL8:
  691. case PIX_FMT_RGB8:
  692. case PIX_FMT_BGR8:
  693. case PIX_FMT_RGB4_BYTE:
  694. case PIX_FMT_BGR4_BYTE:
  695. case PIX_FMT_GRAY8:
  696. picture->linesize[0] = width;
  697. break;
  698. default:
  699. return -1;
  700. }
  701. return 0;
  702. }
  703. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
  704. int height)
  705. {
  706. int size, h2, size2;
  707. const PixFmtInfo *pinfo;
  708. pinfo = &pix_fmt_info[pix_fmt];
  709. size = picture->linesize[0] * height;
  710. switch(pix_fmt) {
  711. case PIX_FMT_YUV420P:
  712. case PIX_FMT_YUV422P:
  713. case PIX_FMT_YUV444P:
  714. case PIX_FMT_YUV410P:
  715. case PIX_FMT_YUV411P:
  716. case PIX_FMT_YUV440P:
  717. case PIX_FMT_YUVJ420P:
  718. case PIX_FMT_YUVJ422P:
  719. case PIX_FMT_YUVJ444P:
  720. case PIX_FMT_YUVJ440P:
  721. case PIX_FMT_YUV420PLE:
  722. case PIX_FMT_YUV422PLE:
  723. case PIX_FMT_YUV444PLE:
  724. case PIX_FMT_YUV420PBE:
  725. case PIX_FMT_YUV422PBE:
  726. case PIX_FMT_YUV444PBE:
  727. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  728. size2 = picture->linesize[1] * h2;
  729. picture->data[0] = ptr;
  730. picture->data[1] = picture->data[0] + size;
  731. picture->data[2] = picture->data[1] + size2;
  732. picture->data[3] = NULL;
  733. return size + 2 * size2;
  734. case PIX_FMT_YUVA420P:
  735. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  736. size2 = picture->linesize[1] * h2;
  737. picture->data[0] = ptr;
  738. picture->data[1] = picture->data[0] + size;
  739. picture->data[2] = picture->data[1] + size2;
  740. picture->data[3] = picture->data[1] + size2 + size2;
  741. return 2 * size + 2 * size2;
  742. case PIX_FMT_NV12:
  743. case PIX_FMT_NV21:
  744. h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
  745. size2 = picture->linesize[1] * h2 * 2;
  746. picture->data[0] = ptr;
  747. picture->data[1] = picture->data[0] + size;
  748. picture->data[2] = NULL;
  749. picture->data[3] = NULL;
  750. return size + 2 * size2;
  751. case PIX_FMT_RGB24:
  752. case PIX_FMT_BGR24:
  753. case PIX_FMT_ARGB:
  754. case PIX_FMT_ABGR:
  755. case PIX_FMT_RGBA:
  756. case PIX_FMT_BGRA:
  757. case PIX_FMT_RGB48BE:
  758. case PIX_FMT_RGB48LE:
  759. case PIX_FMT_GRAY16BE:
  760. case PIX_FMT_GRAY16LE:
  761. case PIX_FMT_BGR555:
  762. case PIX_FMT_BGR565:
  763. case PIX_FMT_RGB555:
  764. case PIX_FMT_RGB565:
  765. case PIX_FMT_YUYV422:
  766. case PIX_FMT_UYVY422:
  767. case PIX_FMT_UYYVYY411:
  768. case PIX_FMT_RGB4:
  769. case PIX_FMT_BGR4:
  770. case PIX_FMT_MONOWHITE:
  771. case PIX_FMT_MONOBLACK:
  772. picture->data[0] = ptr;
  773. picture->data[1] = NULL;
  774. picture->data[2] = NULL;
  775. picture->data[3] = NULL;
  776. return size;
  777. case PIX_FMT_PAL8:
  778. case PIX_FMT_RGB8:
  779. case PIX_FMT_BGR8:
  780. case PIX_FMT_RGB4_BYTE:
  781. case PIX_FMT_BGR4_BYTE:
  782. case PIX_FMT_GRAY8:
  783. size2 = (size + 3) & ~3;
  784. picture->data[0] = ptr;
  785. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  786. picture->data[2] = NULL;
  787. picture->data[3] = NULL;
  788. return size2 + 256 * 4;
  789. default:
  790. picture->data[0] = NULL;
  791. picture->data[1] = NULL;
  792. picture->data[2] = NULL;
  793. picture->data[3] = NULL;
  794. return -1;
  795. }
  796. }
  797. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  798. enum PixelFormat pix_fmt, int width, int height)
  799. {
  800. if(avcodec_check_dimensions(NULL, width, height))
  801. return -1;
  802. if (ff_fill_linesize(picture, pix_fmt, width))
  803. return -1;
  804. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  805. }
  806. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  807. unsigned char *dest, int dest_size)
  808. {
  809. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  810. int i, j, w, ow, h, oh, data_planes;
  811. const unsigned char* s;
  812. int size = avpicture_get_size(pix_fmt, width, height);
  813. if (size > dest_size || size < 0)
  814. return -1;
  815. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  816. if (pix_fmt == PIX_FMT_YUYV422 ||
  817. pix_fmt == PIX_FMT_UYVY422 ||
  818. pix_fmt == PIX_FMT_BGR565 ||
  819. pix_fmt == PIX_FMT_BGR555 ||
  820. pix_fmt == PIX_FMT_RGB565 ||
  821. pix_fmt == PIX_FMT_RGB555)
  822. w = width * 2;
  823. else if (pix_fmt == PIX_FMT_UYYVYY411)
  824. w = width + width/2;
  825. else if (pix_fmt == PIX_FMT_PAL8)
  826. w = width;
  827. else
  828. w = width * (pf->depth * pf->nb_channels / 8);
  829. data_planes = 1;
  830. h = height;
  831. } else {
  832. data_planes = pf->nb_channels;
  833. w = (width*pf->depth + 7)/8;
  834. h = height;
  835. }
  836. ow = w;
  837. oh = h;
  838. for (i=0; i<data_planes; i++) {
  839. if (i == 1) {
  840. w = width >> pf->x_chroma_shift;
  841. h = height >> pf->y_chroma_shift;
  842. } else if (i == 3) {
  843. w = ow;
  844. h = oh;
  845. }
  846. s = src->data[i];
  847. for(j=0; j<h; j++) {
  848. memcpy(dest, s, w);
  849. dest += w;
  850. s += src->linesize[i];
  851. }
  852. }
  853. if (pf->pixel_type == FF_PIXEL_PALETTE)
  854. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  855. return size;
  856. }
  857. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  858. {
  859. AVPicture dummy_pict;
  860. if(avcodec_check_dimensions(NULL, width, height))
  861. return -1;
  862. switch (pix_fmt) {
  863. case PIX_FMT_RGB8:
  864. case PIX_FMT_BGR8:
  865. case PIX_FMT_RGB4_BYTE:
  866. case PIX_FMT_BGR4_BYTE:
  867. case PIX_FMT_GRAY8:
  868. // do not include palette for these pseudo-paletted formats
  869. return width * height;
  870. }
  871. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  872. }
  873. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  874. int has_alpha)
  875. {
  876. const PixFmtInfo *pf, *ps;
  877. int loss;
  878. ps = &pix_fmt_info[src_pix_fmt];
  879. /* compute loss */
  880. loss = 0;
  881. pf = &pix_fmt_info[dst_pix_fmt];
  882. if (pf->depth < ps->depth ||
  883. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  884. loss |= FF_LOSS_DEPTH;
  885. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  886. pf->y_chroma_shift > ps->y_chroma_shift)
  887. loss |= FF_LOSS_RESOLUTION;
  888. switch(pf->color_type) {
  889. case FF_COLOR_RGB:
  890. if (ps->color_type != FF_COLOR_RGB &&
  891. ps->color_type != FF_COLOR_GRAY)
  892. loss |= FF_LOSS_COLORSPACE;
  893. break;
  894. case FF_COLOR_GRAY:
  895. if (ps->color_type != FF_COLOR_GRAY)
  896. loss |= FF_LOSS_COLORSPACE;
  897. break;
  898. case FF_COLOR_YUV:
  899. if (ps->color_type != FF_COLOR_YUV)
  900. loss |= FF_LOSS_COLORSPACE;
  901. break;
  902. case FF_COLOR_YUV_JPEG:
  903. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  904. ps->color_type != FF_COLOR_YUV &&
  905. ps->color_type != FF_COLOR_GRAY)
  906. loss |= FF_LOSS_COLORSPACE;
  907. break;
  908. default:
  909. /* fail safe test */
  910. if (ps->color_type != pf->color_type)
  911. loss |= FF_LOSS_COLORSPACE;
  912. break;
  913. }
  914. if (pf->color_type == FF_COLOR_GRAY &&
  915. ps->color_type != FF_COLOR_GRAY)
  916. loss |= FF_LOSS_CHROMA;
  917. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  918. loss |= FF_LOSS_ALPHA;
  919. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  920. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  921. loss |= FF_LOSS_COLORQUANT;
  922. return loss;
  923. }
  924. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  925. {
  926. int bits;
  927. const PixFmtInfo *pf;
  928. pf = &pix_fmt_info[pix_fmt];
  929. switch(pf->pixel_type) {
  930. case FF_PIXEL_PACKED:
  931. switch(pix_fmt) {
  932. case PIX_FMT_YUYV422:
  933. case PIX_FMT_UYVY422:
  934. case PIX_FMT_RGB565:
  935. case PIX_FMT_RGB555:
  936. case PIX_FMT_BGR565:
  937. case PIX_FMT_BGR555:
  938. bits = 16;
  939. break;
  940. case PIX_FMT_UYYVYY411:
  941. bits = 12;
  942. break;
  943. default:
  944. bits = pf->depth * pf->nb_channels;
  945. break;
  946. }
  947. break;
  948. case FF_PIXEL_PLANAR:
  949. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  950. bits = pf->depth * pf->nb_channels;
  951. } else {
  952. bits = pf->depth + ((2 * pf->depth) >>
  953. (pf->x_chroma_shift + pf->y_chroma_shift));
  954. }
  955. break;
  956. case FF_PIXEL_PALETTE:
  957. bits = 8;
  958. break;
  959. default:
  960. bits = -1;
  961. break;
  962. }
  963. return bits;
  964. }
  965. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  966. enum PixelFormat src_pix_fmt,
  967. int has_alpha,
  968. int loss_mask)
  969. {
  970. int dist, i, loss, min_dist;
  971. enum PixelFormat dst_pix_fmt;
  972. /* find exact color match with smallest size */
  973. dst_pix_fmt = -1;
  974. min_dist = 0x7fffffff;
  975. for(i = 0;i < PIX_FMT_NB; i++) {
  976. if (pix_fmt_mask & (1ULL << i)) {
  977. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  978. if (loss == 0) {
  979. dist = avg_bits_per_pixel(i);
  980. if (dist < min_dist) {
  981. min_dist = dist;
  982. dst_pix_fmt = i;
  983. }
  984. }
  985. }
  986. }
  987. return dst_pix_fmt;
  988. }
  989. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  990. int has_alpha, int *loss_ptr)
  991. {
  992. enum PixelFormat dst_pix_fmt;
  993. int loss_mask, i;
  994. static const int loss_mask_order[] = {
  995. ~0, /* no loss first */
  996. ~FF_LOSS_ALPHA,
  997. ~FF_LOSS_RESOLUTION,
  998. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  999. ~FF_LOSS_COLORQUANT,
  1000. ~FF_LOSS_DEPTH,
  1001. 0,
  1002. };
  1003. /* try with successive loss */
  1004. i = 0;
  1005. for(;;) {
  1006. loss_mask = loss_mask_order[i++];
  1007. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  1008. has_alpha, loss_mask);
  1009. if (dst_pix_fmt >= 0)
  1010. goto found;
  1011. if (loss_mask == 0)
  1012. break;
  1013. }
  1014. return -1;
  1015. found:
  1016. if (loss_ptr)
  1017. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  1018. return dst_pix_fmt;
  1019. }
  1020. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  1021. const uint8_t *src, int src_wrap,
  1022. int width, int height)
  1023. {
  1024. if((!dst) || (!src))
  1025. return;
  1026. for(;height > 0; height--) {
  1027. memcpy(dst, src, width);
  1028. dst += dst_wrap;
  1029. src += src_wrap;
  1030. }
  1031. }
  1032. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  1033. {
  1034. int bits;
  1035. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1036. pf = &pix_fmt_info[pix_fmt];
  1037. switch(pf->pixel_type) {
  1038. case FF_PIXEL_PACKED:
  1039. switch(pix_fmt) {
  1040. case PIX_FMT_YUYV422:
  1041. case PIX_FMT_UYVY422:
  1042. case PIX_FMT_RGB565:
  1043. case PIX_FMT_RGB555:
  1044. case PIX_FMT_BGR565:
  1045. case PIX_FMT_BGR555:
  1046. bits = 16;
  1047. break;
  1048. case PIX_FMT_UYYVYY411:
  1049. bits = 12;
  1050. break;
  1051. default:
  1052. bits = pf->depth * pf->nb_channels;
  1053. break;
  1054. }
  1055. return (width * bits + 7) >> 3;
  1056. break;
  1057. case FF_PIXEL_PLANAR:
  1058. if (plane == 1 || plane == 2)
  1059. width= -((-width)>>pf->x_chroma_shift);
  1060. return (width * pf->depth + 7) >> 3;
  1061. break;
  1062. case FF_PIXEL_PALETTE:
  1063. if (plane == 0)
  1064. return width;
  1065. break;
  1066. }
  1067. return -1;
  1068. }
  1069. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  1070. enum PixelFormat pix_fmt, int width, int height)
  1071. {
  1072. int i;
  1073. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1074. switch(pf->pixel_type) {
  1075. case FF_PIXEL_PACKED:
  1076. case FF_PIXEL_PLANAR:
  1077. for(i = 0; i < pf->nb_channels; i++) {
  1078. int h;
  1079. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  1080. h = height;
  1081. if (i == 1 || i == 2) {
  1082. h= -((-height)>>pf->y_chroma_shift);
  1083. }
  1084. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  1085. src->data[i], src->linesize[i],
  1086. bwidth, h);
  1087. }
  1088. break;
  1089. case FF_PIXEL_PALETTE:
  1090. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  1091. src->data[0], src->linesize[0],
  1092. width, height);
  1093. /* copy the palette */
  1094. ff_img_copy_plane(dst->data[1], dst->linesize[1],
  1095. src->data[1], src->linesize[1],
  1096. 4, 256);
  1097. break;
  1098. }
  1099. }
  1100. /* 2x2 -> 1x1 */
  1101. void ff_shrink22(uint8_t *dst, int dst_wrap,
  1102. const uint8_t *src, int src_wrap,
  1103. int width, int height)
  1104. {
  1105. int w;
  1106. const uint8_t *s1, *s2;
  1107. uint8_t *d;
  1108. for(;height > 0; height--) {
  1109. s1 = src;
  1110. s2 = s1 + src_wrap;
  1111. d = dst;
  1112. for(w = width;w >= 4; w-=4) {
  1113. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1114. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  1115. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  1116. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1117. s1 += 8;
  1118. s2 += 8;
  1119. d += 4;
  1120. }
  1121. for(;w > 0; w--) {
  1122. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1123. s1 += 2;
  1124. s2 += 2;
  1125. d++;
  1126. }
  1127. src += 2 * src_wrap;
  1128. dst += dst_wrap;
  1129. }
  1130. }
  1131. /* 4x4 -> 1x1 */
  1132. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1133. const uint8_t *src, int src_wrap,
  1134. int width, int height)
  1135. {
  1136. int w;
  1137. const uint8_t *s1, *s2, *s3, *s4;
  1138. uint8_t *d;
  1139. for(;height > 0; height--) {
  1140. s1 = src;
  1141. s2 = s1 + src_wrap;
  1142. s3 = s2 + src_wrap;
  1143. s4 = s3 + src_wrap;
  1144. d = dst;
  1145. for(w = width;w > 0; w--) {
  1146. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1147. s2[0] + s2[1] + s2[2] + s2[3] +
  1148. s3[0] + s3[1] + s3[2] + s3[3] +
  1149. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1150. s1 += 4;
  1151. s2 += 4;
  1152. s3 += 4;
  1153. s4 += 4;
  1154. d++;
  1155. }
  1156. src += 4 * src_wrap;
  1157. dst += dst_wrap;
  1158. }
  1159. }
  1160. /* 8x8 -> 1x1 */
  1161. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1162. const uint8_t *src, int src_wrap,
  1163. int width, int height)
  1164. {
  1165. int w, i;
  1166. for(;height > 0; height--) {
  1167. for(w = width;w > 0; w--) {
  1168. int tmp=0;
  1169. for(i=0; i<8; i++){
  1170. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1171. src += src_wrap;
  1172. }
  1173. *(dst++) = (tmp + 32)>>6;
  1174. src += 8 - 8*src_wrap;
  1175. }
  1176. src += 8*src_wrap - 8*width;
  1177. dst += dst_wrap - width;
  1178. }
  1179. }
  1180. int avpicture_alloc(AVPicture *picture,
  1181. enum PixelFormat pix_fmt, int width, int height)
  1182. {
  1183. int size;
  1184. void *ptr;
  1185. size = avpicture_fill(picture, NULL, pix_fmt, width, height);
  1186. if(size<0)
  1187. goto fail;
  1188. ptr = av_malloc(size);
  1189. if (!ptr)
  1190. goto fail;
  1191. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1192. if(picture->data[1] && !picture->data[2])
  1193. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1194. return 0;
  1195. fail:
  1196. memset(picture, 0, sizeof(AVPicture));
  1197. return -1;
  1198. }
  1199. void avpicture_free(AVPicture *picture)
  1200. {
  1201. av_free(picture->data[0]);
  1202. }
  1203. /* return true if yuv planar */
  1204. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1205. {
  1206. return (ps->color_type == FF_COLOR_YUV ||
  1207. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1208. ps->pixel_type == FF_PIXEL_PLANAR;
  1209. }
  1210. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1211. int pix_fmt, int top_band, int left_band)
  1212. {
  1213. int y_shift;
  1214. int x_shift;
  1215. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1216. return -1;
  1217. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1218. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1219. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1220. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1221. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1222. dst->linesize[0] = src->linesize[0];
  1223. dst->linesize[1] = src->linesize[1];
  1224. dst->linesize[2] = src->linesize[2];
  1225. return 0;
  1226. }
  1227. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1228. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1229. int *color)
  1230. {
  1231. uint8_t *optr;
  1232. int y_shift;
  1233. int x_shift;
  1234. int yheight;
  1235. int i, y;
  1236. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1237. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1238. for (i = 0; i < 3; i++) {
  1239. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1240. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1241. if (padtop || padleft) {
  1242. memset(dst->data[i], color[i],
  1243. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1244. }
  1245. if (padleft || padright) {
  1246. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1247. (dst->linesize[i] - (padright >> x_shift));
  1248. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1249. for (y = 0; y < yheight; y++) {
  1250. memset(optr, color[i], (padleft + padright) >> x_shift);
  1251. optr += dst->linesize[i];
  1252. }
  1253. }
  1254. if (src) { /* first line */
  1255. uint8_t *iptr = src->data[i];
  1256. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1257. (padleft >> x_shift);
  1258. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1259. iptr += src->linesize[i];
  1260. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1261. (dst->linesize[i] - (padright >> x_shift));
  1262. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1263. for (y = 0; y < yheight; y++) {
  1264. memset(optr, color[i], (padleft + padright) >> x_shift);
  1265. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1266. (width - padleft - padright) >> x_shift);
  1267. iptr += src->linesize[i];
  1268. optr += dst->linesize[i];
  1269. }
  1270. }
  1271. if (padbottom || padright) {
  1272. optr = dst->data[i] + dst->linesize[i] *
  1273. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1274. memset(optr, color[i],dst->linesize[i] *
  1275. (padbottom >> y_shift) + (padright >> x_shift));
  1276. }
  1277. }
  1278. return 0;
  1279. }
  1280. /* NOTE: we scan all the pixels to have an exact information */
  1281. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  1282. {
  1283. const unsigned char *p;
  1284. int src_wrap, ret, x, y;
  1285. unsigned int a;
  1286. uint32_t *palette = (uint32_t *)src->data[1];
  1287. p = src->data[0];
  1288. src_wrap = src->linesize[0] - width;
  1289. ret = 0;
  1290. for(y=0;y<height;y++) {
  1291. for(x=0;x<width;x++) {
  1292. a = palette[p[0]] >> 24;
  1293. if (a == 0x00) {
  1294. ret |= FF_ALPHA_TRANSP;
  1295. } else if (a != 0xff) {
  1296. ret |= FF_ALPHA_SEMI_TRANSP;
  1297. }
  1298. p++;
  1299. }
  1300. p += src_wrap;
  1301. }
  1302. return ret;
  1303. }
  1304. int img_get_alpha_info(const AVPicture *src,
  1305. enum PixelFormat pix_fmt, int width, int height)
  1306. {
  1307. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1308. int ret;
  1309. /* no alpha can be represented in format */
  1310. if (!pf->is_alpha)
  1311. return 0;
  1312. switch(pix_fmt) {
  1313. case PIX_FMT_PAL8:
  1314. ret = get_alpha_info_pal8(src, width, height);
  1315. break;
  1316. default:
  1317. /* we do not know, so everything is indicated */
  1318. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1319. break;
  1320. }
  1321. return ret;
  1322. }
  1323. #if HAVE_MMX
  1324. #define DEINT_INPLACE_LINE_LUM \
  1325. movd_m2r(lum_m4[0],mm0);\
  1326. movd_m2r(lum_m3[0],mm1);\
  1327. movd_m2r(lum_m2[0],mm2);\
  1328. movd_m2r(lum_m1[0],mm3);\
  1329. movd_m2r(lum[0],mm4);\
  1330. punpcklbw_r2r(mm7,mm0);\
  1331. movd_r2m(mm2,lum_m4[0]);\
  1332. punpcklbw_r2r(mm7,mm1);\
  1333. punpcklbw_r2r(mm7,mm2);\
  1334. punpcklbw_r2r(mm7,mm3);\
  1335. punpcklbw_r2r(mm7,mm4);\
  1336. paddw_r2r(mm3,mm1);\
  1337. psllw_i2r(1,mm2);\
  1338. paddw_r2r(mm4,mm0);\
  1339. psllw_i2r(2,mm1);\
  1340. paddw_r2r(mm6,mm2);\
  1341. paddw_r2r(mm2,mm1);\
  1342. psubusw_r2r(mm0,mm1);\
  1343. psrlw_i2r(3,mm1);\
  1344. packuswb_r2r(mm7,mm1);\
  1345. movd_r2m(mm1,lum_m2[0]);
  1346. #define DEINT_LINE_LUM \
  1347. movd_m2r(lum_m4[0],mm0);\
  1348. movd_m2r(lum_m3[0],mm1);\
  1349. movd_m2r(lum_m2[0],mm2);\
  1350. movd_m2r(lum_m1[0],mm3);\
  1351. movd_m2r(lum[0],mm4);\
  1352. punpcklbw_r2r(mm7,mm0);\
  1353. punpcklbw_r2r(mm7,mm1);\
  1354. punpcklbw_r2r(mm7,mm2);\
  1355. punpcklbw_r2r(mm7,mm3);\
  1356. punpcklbw_r2r(mm7,mm4);\
  1357. paddw_r2r(mm3,mm1);\
  1358. psllw_i2r(1,mm2);\
  1359. paddw_r2r(mm4,mm0);\
  1360. psllw_i2r(2,mm1);\
  1361. paddw_r2r(mm6,mm2);\
  1362. paddw_r2r(mm2,mm1);\
  1363. psubusw_r2r(mm0,mm1);\
  1364. psrlw_i2r(3,mm1);\
  1365. packuswb_r2r(mm7,mm1);\
  1366. movd_r2m(mm1,dst[0]);
  1367. #endif
  1368. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1369. static void deinterlace_line(uint8_t *dst,
  1370. const uint8_t *lum_m4, const uint8_t *lum_m3,
  1371. const uint8_t *lum_m2, const uint8_t *lum_m1,
  1372. const uint8_t *lum,
  1373. int size)
  1374. {
  1375. #if !HAVE_MMX
  1376. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1377. int sum;
  1378. for(;size > 0;size--) {
  1379. sum = -lum_m4[0];
  1380. sum += lum_m3[0] << 2;
  1381. sum += lum_m2[0] << 1;
  1382. sum += lum_m1[0] << 2;
  1383. sum += -lum[0];
  1384. dst[0] = cm[(sum + 4) >> 3];
  1385. lum_m4++;
  1386. lum_m3++;
  1387. lum_m2++;
  1388. lum_m1++;
  1389. lum++;
  1390. dst++;
  1391. }
  1392. #else
  1393. {
  1394. pxor_r2r(mm7,mm7);
  1395. movq_m2r(ff_pw_4,mm6);
  1396. }
  1397. for (;size > 3; size-=4) {
  1398. DEINT_LINE_LUM
  1399. lum_m4+=4;
  1400. lum_m3+=4;
  1401. lum_m2+=4;
  1402. lum_m1+=4;
  1403. lum+=4;
  1404. dst+=4;
  1405. }
  1406. #endif
  1407. }
  1408. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1409. int size)
  1410. {
  1411. #if !HAVE_MMX
  1412. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1413. int sum;
  1414. for(;size > 0;size--) {
  1415. sum = -lum_m4[0];
  1416. sum += lum_m3[0] << 2;
  1417. sum += lum_m2[0] << 1;
  1418. lum_m4[0]=lum_m2[0];
  1419. sum += lum_m1[0] << 2;
  1420. sum += -lum[0];
  1421. lum_m2[0] = cm[(sum + 4) >> 3];
  1422. lum_m4++;
  1423. lum_m3++;
  1424. lum_m2++;
  1425. lum_m1++;
  1426. lum++;
  1427. }
  1428. #else
  1429. {
  1430. pxor_r2r(mm7,mm7);
  1431. movq_m2r(ff_pw_4,mm6);
  1432. }
  1433. for (;size > 3; size-=4) {
  1434. DEINT_INPLACE_LINE_LUM
  1435. lum_m4+=4;
  1436. lum_m3+=4;
  1437. lum_m2+=4;
  1438. lum_m1+=4;
  1439. lum+=4;
  1440. }
  1441. #endif
  1442. }
  1443. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1444. top field is copied as is, but the bottom field is deinterlaced
  1445. against the top field. */
  1446. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1447. const uint8_t *src1, int src_wrap,
  1448. int width, int height)
  1449. {
  1450. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1451. int y;
  1452. src_m2 = src1;
  1453. src_m1 = src1;
  1454. src_0=&src_m1[src_wrap];
  1455. src_p1=&src_0[src_wrap];
  1456. src_p2=&src_p1[src_wrap];
  1457. for(y=0;y<(height-2);y+=2) {
  1458. memcpy(dst,src_m1,width);
  1459. dst += dst_wrap;
  1460. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1461. src_m2 = src_0;
  1462. src_m1 = src_p1;
  1463. src_0 = src_p2;
  1464. src_p1 += 2*src_wrap;
  1465. src_p2 += 2*src_wrap;
  1466. dst += dst_wrap;
  1467. }
  1468. memcpy(dst,src_m1,width);
  1469. dst += dst_wrap;
  1470. /* do last line */
  1471. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1472. }
  1473. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1474. int width, int height)
  1475. {
  1476. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1477. int y;
  1478. uint8_t *buf;
  1479. buf = (uint8_t*)av_malloc(width);
  1480. src_m1 = src1;
  1481. memcpy(buf,src_m1,width);
  1482. src_0=&src_m1[src_wrap];
  1483. src_p1=&src_0[src_wrap];
  1484. src_p2=&src_p1[src_wrap];
  1485. for(y=0;y<(height-2);y+=2) {
  1486. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1487. src_m1 = src_p1;
  1488. src_0 = src_p2;
  1489. src_p1 += 2*src_wrap;
  1490. src_p2 += 2*src_wrap;
  1491. }
  1492. /* do last line */
  1493. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1494. av_free(buf);
  1495. }
  1496. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1497. enum PixelFormat pix_fmt, int width, int height)
  1498. {
  1499. int i;
  1500. if (pix_fmt != PIX_FMT_YUV420P &&
  1501. pix_fmt != PIX_FMT_YUV422P &&
  1502. pix_fmt != PIX_FMT_YUV444P &&
  1503. pix_fmt != PIX_FMT_YUV411P &&
  1504. pix_fmt != PIX_FMT_GRAY8)
  1505. return -1;
  1506. if ((width & 3) != 0 || (height & 3) != 0)
  1507. return -1;
  1508. for(i=0;i<3;i++) {
  1509. if (i == 1) {
  1510. switch(pix_fmt) {
  1511. case PIX_FMT_YUV420P:
  1512. width >>= 1;
  1513. height >>= 1;
  1514. break;
  1515. case PIX_FMT_YUV422P:
  1516. width >>= 1;
  1517. break;
  1518. case PIX_FMT_YUV411P:
  1519. width >>= 2;
  1520. break;
  1521. default:
  1522. break;
  1523. }
  1524. if (pix_fmt == PIX_FMT_GRAY8) {
  1525. break;
  1526. }
  1527. }
  1528. if (src == dst) {
  1529. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1530. width, height);
  1531. } else {
  1532. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1533. src->data[i], src->linesize[i],
  1534. width, height);
  1535. }
  1536. }
  1537. emms_c();
  1538. return 0;
  1539. }