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.

1674 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 = "nv21",
  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. memcpy(dst->data[1], src->data[1], 4*256);
  1121. break;
  1122. }
  1123. }
  1124. /* 2x2 -> 1x1 */
  1125. void ff_shrink22(uint8_t *dst, int dst_wrap,
  1126. const uint8_t *src, int src_wrap,
  1127. int width, int height)
  1128. {
  1129. int w;
  1130. const uint8_t *s1, *s2;
  1131. uint8_t *d;
  1132. for(;height > 0; height--) {
  1133. s1 = src;
  1134. s2 = s1 + src_wrap;
  1135. d = dst;
  1136. for(w = width;w >= 4; w-=4) {
  1137. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1138. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  1139. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  1140. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1141. s1 += 8;
  1142. s2 += 8;
  1143. d += 4;
  1144. }
  1145. for(;w > 0; w--) {
  1146. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1147. s1 += 2;
  1148. s2 += 2;
  1149. d++;
  1150. }
  1151. src += 2 * src_wrap;
  1152. dst += dst_wrap;
  1153. }
  1154. }
  1155. /* 4x4 -> 1x1 */
  1156. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1157. const uint8_t *src, int src_wrap,
  1158. int width, int height)
  1159. {
  1160. int w;
  1161. const uint8_t *s1, *s2, *s3, *s4;
  1162. uint8_t *d;
  1163. for(;height > 0; height--) {
  1164. s1 = src;
  1165. s2 = s1 + src_wrap;
  1166. s3 = s2 + src_wrap;
  1167. s4 = s3 + src_wrap;
  1168. d = dst;
  1169. for(w = width;w > 0; w--) {
  1170. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1171. s2[0] + s2[1] + s2[2] + s2[3] +
  1172. s3[0] + s3[1] + s3[2] + s3[3] +
  1173. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1174. s1 += 4;
  1175. s2 += 4;
  1176. s3 += 4;
  1177. s4 += 4;
  1178. d++;
  1179. }
  1180. src += 4 * src_wrap;
  1181. dst += dst_wrap;
  1182. }
  1183. }
  1184. /* 8x8 -> 1x1 */
  1185. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1186. const uint8_t *src, int src_wrap,
  1187. int width, int height)
  1188. {
  1189. int w, i;
  1190. for(;height > 0; height--) {
  1191. for(w = width;w > 0; w--) {
  1192. int tmp=0;
  1193. for(i=0; i<8; i++){
  1194. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1195. src += src_wrap;
  1196. }
  1197. *(dst++) = (tmp + 32)>>6;
  1198. src += 8 - 8*src_wrap;
  1199. }
  1200. src += 8*src_wrap - 8*width;
  1201. dst += dst_wrap - width;
  1202. }
  1203. }
  1204. int avpicture_alloc(AVPicture *picture,
  1205. enum PixelFormat pix_fmt, int width, int height)
  1206. {
  1207. int size;
  1208. void *ptr;
  1209. size = avpicture_fill(picture, NULL, pix_fmt, width, height);
  1210. if(size<0)
  1211. goto fail;
  1212. ptr = av_malloc(size);
  1213. if (!ptr)
  1214. goto fail;
  1215. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1216. if(picture->data[1] && !picture->data[2])
  1217. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1218. return 0;
  1219. fail:
  1220. memset(picture, 0, sizeof(AVPicture));
  1221. return -1;
  1222. }
  1223. void avpicture_free(AVPicture *picture)
  1224. {
  1225. av_free(picture->data[0]);
  1226. }
  1227. /* return true if yuv planar */
  1228. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1229. {
  1230. return (ps->color_type == FF_COLOR_YUV ||
  1231. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1232. ps->pixel_type == FF_PIXEL_PLANAR;
  1233. }
  1234. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1235. enum PixelFormat pix_fmt, int top_band, int left_band)
  1236. {
  1237. int y_shift;
  1238. int x_shift;
  1239. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1240. return -1;
  1241. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1242. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1243. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1244. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1245. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1246. dst->linesize[0] = src->linesize[0];
  1247. dst->linesize[1] = src->linesize[1];
  1248. dst->linesize[2] = src->linesize[2];
  1249. return 0;
  1250. }
  1251. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1252. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1253. int *color)
  1254. {
  1255. uint8_t *optr;
  1256. int y_shift;
  1257. int x_shift;
  1258. int yheight;
  1259. int i, y;
  1260. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1261. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1262. for (i = 0; i < 3; i++) {
  1263. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1264. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1265. if (padtop || padleft) {
  1266. memset(dst->data[i], color[i],
  1267. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1268. }
  1269. if (padleft || padright) {
  1270. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1271. (dst->linesize[i] - (padright >> x_shift));
  1272. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1273. for (y = 0; y < yheight; y++) {
  1274. memset(optr, color[i], (padleft + padright) >> x_shift);
  1275. optr += dst->linesize[i];
  1276. }
  1277. }
  1278. if (src) { /* first line */
  1279. uint8_t *iptr = src->data[i];
  1280. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1281. (padleft >> x_shift);
  1282. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1283. iptr += src->linesize[i];
  1284. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1285. (dst->linesize[i] - (padright >> x_shift));
  1286. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1287. for (y = 0; y < yheight; y++) {
  1288. memset(optr, color[i], (padleft + padright) >> x_shift);
  1289. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1290. (width - padleft - padright) >> x_shift);
  1291. iptr += src->linesize[i];
  1292. optr += dst->linesize[i];
  1293. }
  1294. }
  1295. if (padbottom || padright) {
  1296. optr = dst->data[i] + dst->linesize[i] *
  1297. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1298. memset(optr, color[i],dst->linesize[i] *
  1299. (padbottom >> y_shift) + (padright >> x_shift));
  1300. }
  1301. }
  1302. return 0;
  1303. }
  1304. /* NOTE: we scan all the pixels to have an exact information */
  1305. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  1306. {
  1307. const unsigned char *p;
  1308. int src_wrap, ret, x, y;
  1309. unsigned int a;
  1310. uint32_t *palette = (uint32_t *)src->data[1];
  1311. p = src->data[0];
  1312. src_wrap = src->linesize[0] - width;
  1313. ret = 0;
  1314. for(y=0;y<height;y++) {
  1315. for(x=0;x<width;x++) {
  1316. a = palette[p[0]] >> 24;
  1317. if (a == 0x00) {
  1318. ret |= FF_ALPHA_TRANSP;
  1319. } else if (a != 0xff) {
  1320. ret |= FF_ALPHA_SEMI_TRANSP;
  1321. }
  1322. p++;
  1323. }
  1324. p += src_wrap;
  1325. }
  1326. return ret;
  1327. }
  1328. int img_get_alpha_info(const AVPicture *src,
  1329. enum PixelFormat pix_fmt, int width, int height)
  1330. {
  1331. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1332. int ret;
  1333. /* no alpha can be represented in format */
  1334. if (!pf->is_alpha)
  1335. return 0;
  1336. switch(pix_fmt) {
  1337. case PIX_FMT_PAL8:
  1338. ret = get_alpha_info_pal8(src, width, height);
  1339. break;
  1340. default:
  1341. /* we do not know, so everything is indicated */
  1342. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1343. break;
  1344. }
  1345. return ret;
  1346. }
  1347. #if HAVE_MMX
  1348. #define DEINT_INPLACE_LINE_LUM \
  1349. movd_m2r(lum_m4[0],mm0);\
  1350. movd_m2r(lum_m3[0],mm1);\
  1351. movd_m2r(lum_m2[0],mm2);\
  1352. movd_m2r(lum_m1[0],mm3);\
  1353. movd_m2r(lum[0],mm4);\
  1354. punpcklbw_r2r(mm7,mm0);\
  1355. movd_r2m(mm2,lum_m4[0]);\
  1356. punpcklbw_r2r(mm7,mm1);\
  1357. punpcklbw_r2r(mm7,mm2);\
  1358. punpcklbw_r2r(mm7,mm3);\
  1359. punpcklbw_r2r(mm7,mm4);\
  1360. paddw_r2r(mm3,mm1);\
  1361. psllw_i2r(1,mm2);\
  1362. paddw_r2r(mm4,mm0);\
  1363. psllw_i2r(2,mm1);\
  1364. paddw_r2r(mm6,mm2);\
  1365. paddw_r2r(mm2,mm1);\
  1366. psubusw_r2r(mm0,mm1);\
  1367. psrlw_i2r(3,mm1);\
  1368. packuswb_r2r(mm7,mm1);\
  1369. movd_r2m(mm1,lum_m2[0]);
  1370. #define DEINT_LINE_LUM \
  1371. movd_m2r(lum_m4[0],mm0);\
  1372. movd_m2r(lum_m3[0],mm1);\
  1373. movd_m2r(lum_m2[0],mm2);\
  1374. movd_m2r(lum_m1[0],mm3);\
  1375. movd_m2r(lum[0],mm4);\
  1376. punpcklbw_r2r(mm7,mm0);\
  1377. punpcklbw_r2r(mm7,mm1);\
  1378. punpcklbw_r2r(mm7,mm2);\
  1379. punpcklbw_r2r(mm7,mm3);\
  1380. punpcklbw_r2r(mm7,mm4);\
  1381. paddw_r2r(mm3,mm1);\
  1382. psllw_i2r(1,mm2);\
  1383. paddw_r2r(mm4,mm0);\
  1384. psllw_i2r(2,mm1);\
  1385. paddw_r2r(mm6,mm2);\
  1386. paddw_r2r(mm2,mm1);\
  1387. psubusw_r2r(mm0,mm1);\
  1388. psrlw_i2r(3,mm1);\
  1389. packuswb_r2r(mm7,mm1);\
  1390. movd_r2m(mm1,dst[0]);
  1391. #endif
  1392. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1393. static void deinterlace_line(uint8_t *dst,
  1394. const uint8_t *lum_m4, const uint8_t *lum_m3,
  1395. const uint8_t *lum_m2, const uint8_t *lum_m1,
  1396. const uint8_t *lum,
  1397. int size)
  1398. {
  1399. #if !HAVE_MMX
  1400. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1401. int sum;
  1402. for(;size > 0;size--) {
  1403. sum = -lum_m4[0];
  1404. sum += lum_m3[0] << 2;
  1405. sum += lum_m2[0] << 1;
  1406. sum += lum_m1[0] << 2;
  1407. sum += -lum[0];
  1408. dst[0] = cm[(sum + 4) >> 3];
  1409. lum_m4++;
  1410. lum_m3++;
  1411. lum_m2++;
  1412. lum_m1++;
  1413. lum++;
  1414. dst++;
  1415. }
  1416. #else
  1417. {
  1418. pxor_r2r(mm7,mm7);
  1419. movq_m2r(ff_pw_4,mm6);
  1420. }
  1421. for (;size > 3; size-=4) {
  1422. DEINT_LINE_LUM
  1423. lum_m4+=4;
  1424. lum_m3+=4;
  1425. lum_m2+=4;
  1426. lum_m1+=4;
  1427. lum+=4;
  1428. dst+=4;
  1429. }
  1430. #endif
  1431. }
  1432. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1433. int size)
  1434. {
  1435. #if !HAVE_MMX
  1436. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1437. int sum;
  1438. for(;size > 0;size--) {
  1439. sum = -lum_m4[0];
  1440. sum += lum_m3[0] << 2;
  1441. sum += lum_m2[0] << 1;
  1442. lum_m4[0]=lum_m2[0];
  1443. sum += lum_m1[0] << 2;
  1444. sum += -lum[0];
  1445. lum_m2[0] = cm[(sum + 4) >> 3];
  1446. lum_m4++;
  1447. lum_m3++;
  1448. lum_m2++;
  1449. lum_m1++;
  1450. lum++;
  1451. }
  1452. #else
  1453. {
  1454. pxor_r2r(mm7,mm7);
  1455. movq_m2r(ff_pw_4,mm6);
  1456. }
  1457. for (;size > 3; size-=4) {
  1458. DEINT_INPLACE_LINE_LUM
  1459. lum_m4+=4;
  1460. lum_m3+=4;
  1461. lum_m2+=4;
  1462. lum_m1+=4;
  1463. lum+=4;
  1464. }
  1465. #endif
  1466. }
  1467. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1468. top field is copied as is, but the bottom field is deinterlaced
  1469. against the top field. */
  1470. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1471. const uint8_t *src1, int src_wrap,
  1472. int width, int height)
  1473. {
  1474. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1475. int y;
  1476. src_m2 = src1;
  1477. src_m1 = src1;
  1478. src_0=&src_m1[src_wrap];
  1479. src_p1=&src_0[src_wrap];
  1480. src_p2=&src_p1[src_wrap];
  1481. for(y=0;y<(height-2);y+=2) {
  1482. memcpy(dst,src_m1,width);
  1483. dst += dst_wrap;
  1484. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1485. src_m2 = src_0;
  1486. src_m1 = src_p1;
  1487. src_0 = src_p2;
  1488. src_p1 += 2*src_wrap;
  1489. src_p2 += 2*src_wrap;
  1490. dst += dst_wrap;
  1491. }
  1492. memcpy(dst,src_m1,width);
  1493. dst += dst_wrap;
  1494. /* do last line */
  1495. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1496. }
  1497. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1498. int width, int height)
  1499. {
  1500. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1501. int y;
  1502. uint8_t *buf;
  1503. buf = (uint8_t*)av_malloc(width);
  1504. src_m1 = src1;
  1505. memcpy(buf,src_m1,width);
  1506. src_0=&src_m1[src_wrap];
  1507. src_p1=&src_0[src_wrap];
  1508. src_p2=&src_p1[src_wrap];
  1509. for(y=0;y<(height-2);y+=2) {
  1510. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1511. src_m1 = src_p1;
  1512. src_0 = src_p2;
  1513. src_p1 += 2*src_wrap;
  1514. src_p2 += 2*src_wrap;
  1515. }
  1516. /* do last line */
  1517. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1518. av_free(buf);
  1519. }
  1520. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1521. enum PixelFormat pix_fmt, int width, int height)
  1522. {
  1523. int i;
  1524. if (pix_fmt != PIX_FMT_YUV420P &&
  1525. pix_fmt != PIX_FMT_YUV422P &&
  1526. pix_fmt != PIX_FMT_YUV444P &&
  1527. pix_fmt != PIX_FMT_YUV411P &&
  1528. pix_fmt != PIX_FMT_GRAY8)
  1529. return -1;
  1530. if ((width & 3) != 0 || (height & 3) != 0)
  1531. return -1;
  1532. for(i=0;i<3;i++) {
  1533. if (i == 1) {
  1534. switch(pix_fmt) {
  1535. case PIX_FMT_YUV420P:
  1536. width >>= 1;
  1537. height >>= 1;
  1538. break;
  1539. case PIX_FMT_YUV422P:
  1540. width >>= 1;
  1541. break;
  1542. case PIX_FMT_YUV411P:
  1543. width >>= 2;
  1544. break;
  1545. default:
  1546. break;
  1547. }
  1548. if (pix_fmt == PIX_FMT_GRAY8) {
  1549. break;
  1550. }
  1551. }
  1552. if (src == dst) {
  1553. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1554. width, height);
  1555. } else {
  1556. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1557. src->data[i], src->linesize[i],
  1558. width, height);
  1559. }
  1560. }
  1561. emms_c();
  1562. return 0;
  1563. }