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.

1587 lines
45KB

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