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.

1676 lines
48KB

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