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.

1584 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 (pix_fmt_info[i].name && !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. /* compute loss */
  815. loss = 0;
  816. pf = &pix_fmt_info[dst_pix_fmt];
  817. if (pf->depth < ps->depth ||
  818. (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
  819. loss |= FF_LOSS_DEPTH;
  820. if (pf->x_chroma_shift > ps->x_chroma_shift ||
  821. pf->y_chroma_shift > ps->y_chroma_shift)
  822. loss |= FF_LOSS_RESOLUTION;
  823. switch(pf->color_type) {
  824. case FF_COLOR_RGB:
  825. if (ps->color_type != FF_COLOR_RGB &&
  826. ps->color_type != FF_COLOR_GRAY)
  827. loss |= FF_LOSS_COLORSPACE;
  828. break;
  829. case FF_COLOR_GRAY:
  830. if (ps->color_type != FF_COLOR_GRAY)
  831. loss |= FF_LOSS_COLORSPACE;
  832. break;
  833. case FF_COLOR_YUV:
  834. if (ps->color_type != FF_COLOR_YUV)
  835. loss |= FF_LOSS_COLORSPACE;
  836. break;
  837. case FF_COLOR_YUV_JPEG:
  838. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  839. ps->color_type != FF_COLOR_YUV &&
  840. ps->color_type != FF_COLOR_GRAY)
  841. loss |= FF_LOSS_COLORSPACE;
  842. break;
  843. default:
  844. /* fail safe test */
  845. if (ps->color_type != pf->color_type)
  846. loss |= FF_LOSS_COLORSPACE;
  847. break;
  848. }
  849. if (pf->color_type == FF_COLOR_GRAY &&
  850. ps->color_type != FF_COLOR_GRAY)
  851. loss |= FF_LOSS_CHROMA;
  852. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  853. loss |= FF_LOSS_ALPHA;
  854. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  855. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  856. loss |= FF_LOSS_COLORQUANT;
  857. return loss;
  858. }
  859. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  860. {
  861. int bits;
  862. const PixFmtInfo *pf;
  863. pf = &pix_fmt_info[pix_fmt];
  864. switch(pf->pixel_type) {
  865. case FF_PIXEL_PACKED:
  866. switch(pix_fmt) {
  867. case PIX_FMT_YUYV422:
  868. case PIX_FMT_UYVY422:
  869. case PIX_FMT_RGB565:
  870. case PIX_FMT_RGB555:
  871. case PIX_FMT_BGR565:
  872. case PIX_FMT_BGR555:
  873. bits = 16;
  874. break;
  875. case PIX_FMT_UYYVYY411:
  876. bits = 12;
  877. break;
  878. default:
  879. bits = pf->depth * pf->nb_channels;
  880. break;
  881. }
  882. break;
  883. case FF_PIXEL_PLANAR:
  884. if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
  885. bits = pf->depth * pf->nb_channels;
  886. } else {
  887. bits = pf->depth + ((2 * pf->depth) >>
  888. (pf->x_chroma_shift + pf->y_chroma_shift));
  889. }
  890. break;
  891. case FF_PIXEL_PALETTE:
  892. bits = 8;
  893. break;
  894. default:
  895. bits = -1;
  896. break;
  897. }
  898. return bits;
  899. }
  900. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  901. enum PixelFormat src_pix_fmt,
  902. int has_alpha,
  903. int loss_mask)
  904. {
  905. int dist, i, loss, min_dist;
  906. enum PixelFormat dst_pix_fmt;
  907. /* find exact color match with smallest size */
  908. dst_pix_fmt = -1;
  909. min_dist = 0x7fffffff;
  910. for(i = 0;i < PIX_FMT_NB; i++) {
  911. if (pix_fmt_mask & (1ULL << i)) {
  912. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  913. if (loss == 0) {
  914. dist = avg_bits_per_pixel(i);
  915. if (dist < min_dist) {
  916. min_dist = dist;
  917. dst_pix_fmt = i;
  918. }
  919. }
  920. }
  921. }
  922. return dst_pix_fmt;
  923. }
  924. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  925. int has_alpha, int *loss_ptr)
  926. {
  927. enum PixelFormat dst_pix_fmt;
  928. int loss_mask, i;
  929. static const int loss_mask_order[] = {
  930. ~0, /* no loss first */
  931. ~FF_LOSS_ALPHA,
  932. ~FF_LOSS_RESOLUTION,
  933. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  934. ~FF_LOSS_COLORQUANT,
  935. ~FF_LOSS_DEPTH,
  936. 0,
  937. };
  938. /* try with successive loss */
  939. i = 0;
  940. for(;;) {
  941. loss_mask = loss_mask_order[i++];
  942. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  943. has_alpha, loss_mask);
  944. if (dst_pix_fmt >= 0)
  945. goto found;
  946. if (loss_mask == 0)
  947. break;
  948. }
  949. return -1;
  950. found:
  951. if (loss_ptr)
  952. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  953. return dst_pix_fmt;
  954. }
  955. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  956. const uint8_t *src, int src_wrap,
  957. int width, int height)
  958. {
  959. if((!dst) || (!src))
  960. return;
  961. for(;height > 0; height--) {
  962. memcpy(dst, src, width);
  963. dst += dst_wrap;
  964. src += src_wrap;
  965. }
  966. }
  967. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  968. {
  969. int bits;
  970. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  971. pf = &pix_fmt_info[pix_fmt];
  972. switch(pf->pixel_type) {
  973. case FF_PIXEL_PACKED:
  974. switch(pix_fmt) {
  975. case PIX_FMT_YUYV422:
  976. case PIX_FMT_UYVY422:
  977. case PIX_FMT_RGB565:
  978. case PIX_FMT_RGB555:
  979. case PIX_FMT_BGR565:
  980. case PIX_FMT_BGR555:
  981. bits = 16;
  982. break;
  983. case PIX_FMT_UYYVYY411:
  984. bits = 12;
  985. break;
  986. default:
  987. bits = pf->depth * pf->nb_channels;
  988. break;
  989. }
  990. return (width * bits + 7) >> 3;
  991. break;
  992. case FF_PIXEL_PLANAR:
  993. if (plane == 1 || plane == 2)
  994. width= -((-width)>>pf->x_chroma_shift);
  995. return (width * pf->depth + 7) >> 3;
  996. break;
  997. case FF_PIXEL_PALETTE:
  998. if (plane == 0)
  999. return width;
  1000. break;
  1001. }
  1002. return -1;
  1003. }
  1004. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  1005. enum PixelFormat pix_fmt, int width, int height)
  1006. {
  1007. int i;
  1008. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1009. switch(pf->pixel_type) {
  1010. case FF_PIXEL_PACKED:
  1011. case FF_PIXEL_PLANAR:
  1012. for(i = 0; i < pf->nb_channels; i++) {
  1013. int h;
  1014. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  1015. h = height;
  1016. if (i == 1 || i == 2) {
  1017. h= -((-height)>>pf->y_chroma_shift);
  1018. }
  1019. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  1020. src->data[i], src->linesize[i],
  1021. bwidth, h);
  1022. }
  1023. break;
  1024. case FF_PIXEL_PALETTE:
  1025. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  1026. src->data[0], src->linesize[0],
  1027. width, height);
  1028. /* copy the palette */
  1029. ff_img_copy_plane(dst->data[1], dst->linesize[1],
  1030. src->data[1], src->linesize[1],
  1031. 4, 256);
  1032. break;
  1033. }
  1034. }
  1035. /* 2x2 -> 1x1 */
  1036. void ff_shrink22(uint8_t *dst, int dst_wrap,
  1037. const uint8_t *src, int src_wrap,
  1038. int width, int height)
  1039. {
  1040. int w;
  1041. const uint8_t *s1, *s2;
  1042. uint8_t *d;
  1043. for(;height > 0; height--) {
  1044. s1 = src;
  1045. s2 = s1 + src_wrap;
  1046. d = dst;
  1047. for(w = width;w >= 4; w-=4) {
  1048. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1049. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  1050. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  1051. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1052. s1 += 8;
  1053. s2 += 8;
  1054. d += 4;
  1055. }
  1056. for(;w > 0; w--) {
  1057. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1058. s1 += 2;
  1059. s2 += 2;
  1060. d++;
  1061. }
  1062. src += 2 * src_wrap;
  1063. dst += dst_wrap;
  1064. }
  1065. }
  1066. /* 4x4 -> 1x1 */
  1067. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1068. const uint8_t *src, int src_wrap,
  1069. int width, int height)
  1070. {
  1071. int w;
  1072. const uint8_t *s1, *s2, *s3, *s4;
  1073. uint8_t *d;
  1074. for(;height > 0; height--) {
  1075. s1 = src;
  1076. s2 = s1 + src_wrap;
  1077. s3 = s2 + src_wrap;
  1078. s4 = s3 + src_wrap;
  1079. d = dst;
  1080. for(w = width;w > 0; w--) {
  1081. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1082. s2[0] + s2[1] + s2[2] + s2[3] +
  1083. s3[0] + s3[1] + s3[2] + s3[3] +
  1084. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1085. s1 += 4;
  1086. s2 += 4;
  1087. s3 += 4;
  1088. s4 += 4;
  1089. d++;
  1090. }
  1091. src += 4 * src_wrap;
  1092. dst += dst_wrap;
  1093. }
  1094. }
  1095. /* 8x8 -> 1x1 */
  1096. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1097. const uint8_t *src, int src_wrap,
  1098. int width, int height)
  1099. {
  1100. int w, i;
  1101. for(;height > 0; height--) {
  1102. for(w = width;w > 0; w--) {
  1103. int tmp=0;
  1104. for(i=0; i<8; i++){
  1105. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1106. src += src_wrap;
  1107. }
  1108. *(dst++) = (tmp + 32)>>6;
  1109. src += 8 - 8*src_wrap;
  1110. }
  1111. src += 8*src_wrap - 8*width;
  1112. dst += dst_wrap - width;
  1113. }
  1114. }
  1115. int avpicture_alloc(AVPicture *picture,
  1116. enum PixelFormat pix_fmt, int width, int height)
  1117. {
  1118. int size;
  1119. void *ptr;
  1120. size = avpicture_fill(picture, NULL, pix_fmt, width, height);
  1121. if(size<0)
  1122. goto fail;
  1123. ptr = av_malloc(size);
  1124. if (!ptr)
  1125. goto fail;
  1126. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1127. if(picture->data[1] && !picture->data[2])
  1128. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1129. return 0;
  1130. fail:
  1131. memset(picture, 0, sizeof(AVPicture));
  1132. return -1;
  1133. }
  1134. void avpicture_free(AVPicture *picture)
  1135. {
  1136. av_free(picture->data[0]);
  1137. }
  1138. /* return true if yuv planar */
  1139. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1140. {
  1141. return (ps->color_type == FF_COLOR_YUV ||
  1142. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1143. ps->pixel_type == FF_PIXEL_PLANAR;
  1144. }
  1145. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1146. int pix_fmt, int top_band, int left_band)
  1147. {
  1148. int y_shift;
  1149. int x_shift;
  1150. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1151. return -1;
  1152. y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
  1153. x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
  1154. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1155. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1156. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1157. dst->linesize[0] = src->linesize[0];
  1158. dst->linesize[1] = src->linesize[1];
  1159. dst->linesize[2] = src->linesize[2];
  1160. return 0;
  1161. }
  1162. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1163. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1164. int *color)
  1165. {
  1166. uint8_t *optr;
  1167. int y_shift;
  1168. int x_shift;
  1169. int yheight;
  1170. int i, y;
  1171. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1172. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1173. for (i = 0; i < 3; i++) {
  1174. x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
  1175. y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
  1176. if (padtop || padleft) {
  1177. memset(dst->data[i], color[i],
  1178. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1179. }
  1180. if (padleft || padright) {
  1181. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1182. (dst->linesize[i] - (padright >> x_shift));
  1183. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1184. for (y = 0; y < yheight; y++) {
  1185. memset(optr, color[i], (padleft + padright) >> x_shift);
  1186. optr += dst->linesize[i];
  1187. }
  1188. }
  1189. if (src) { /* first line */
  1190. uint8_t *iptr = src->data[i];
  1191. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1192. (padleft >> x_shift);
  1193. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1194. iptr += src->linesize[i];
  1195. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1196. (dst->linesize[i] - (padright >> x_shift));
  1197. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1198. for (y = 0; y < yheight; y++) {
  1199. memset(optr, color[i], (padleft + padright) >> x_shift);
  1200. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1201. (width - padleft - padright) >> x_shift);
  1202. iptr += src->linesize[i];
  1203. optr += dst->linesize[i];
  1204. }
  1205. }
  1206. if (padbottom || padright) {
  1207. optr = dst->data[i] + dst->linesize[i] *
  1208. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1209. memset(optr, color[i],dst->linesize[i] *
  1210. (padbottom >> y_shift) + (padright >> x_shift));
  1211. }
  1212. }
  1213. return 0;
  1214. }
  1215. /* NOTE: we scan all the pixels to have an exact information */
  1216. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  1217. {
  1218. const unsigned char *p;
  1219. int src_wrap, ret, x, y;
  1220. unsigned int a;
  1221. uint32_t *palette = (uint32_t *)src->data[1];
  1222. p = src->data[0];
  1223. src_wrap = src->linesize[0] - width;
  1224. ret = 0;
  1225. for(y=0;y<height;y++) {
  1226. for(x=0;x<width;x++) {
  1227. a = palette[p[0]] >> 24;
  1228. if (a == 0x00) {
  1229. ret |= FF_ALPHA_TRANSP;
  1230. } else if (a != 0xff) {
  1231. ret |= FF_ALPHA_SEMI_TRANSP;
  1232. }
  1233. p++;
  1234. }
  1235. p += src_wrap;
  1236. }
  1237. return ret;
  1238. }
  1239. int img_get_alpha_info(const AVPicture *src,
  1240. enum PixelFormat pix_fmt, int width, int height)
  1241. {
  1242. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1243. int ret;
  1244. /* no alpha can be represented in format */
  1245. if (!pf->is_alpha)
  1246. return 0;
  1247. switch(pix_fmt) {
  1248. case PIX_FMT_PAL8:
  1249. ret = get_alpha_info_pal8(src, width, height);
  1250. break;
  1251. default:
  1252. /* we do not know, so everything is indicated */
  1253. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  1254. break;
  1255. }
  1256. return ret;
  1257. }
  1258. #if HAVE_MMX
  1259. #define DEINT_INPLACE_LINE_LUM \
  1260. movd_m2r(lum_m4[0],mm0);\
  1261. movd_m2r(lum_m3[0],mm1);\
  1262. movd_m2r(lum_m2[0],mm2);\
  1263. movd_m2r(lum_m1[0],mm3);\
  1264. movd_m2r(lum[0],mm4);\
  1265. punpcklbw_r2r(mm7,mm0);\
  1266. movd_r2m(mm2,lum_m4[0]);\
  1267. punpcklbw_r2r(mm7,mm1);\
  1268. punpcklbw_r2r(mm7,mm2);\
  1269. punpcklbw_r2r(mm7,mm3);\
  1270. punpcklbw_r2r(mm7,mm4);\
  1271. paddw_r2r(mm3,mm1);\
  1272. psllw_i2r(1,mm2);\
  1273. paddw_r2r(mm4,mm0);\
  1274. psllw_i2r(2,mm1);\
  1275. paddw_r2r(mm6,mm2);\
  1276. paddw_r2r(mm2,mm1);\
  1277. psubusw_r2r(mm0,mm1);\
  1278. psrlw_i2r(3,mm1);\
  1279. packuswb_r2r(mm7,mm1);\
  1280. movd_r2m(mm1,lum_m2[0]);
  1281. #define DEINT_LINE_LUM \
  1282. movd_m2r(lum_m4[0],mm0);\
  1283. movd_m2r(lum_m3[0],mm1);\
  1284. movd_m2r(lum_m2[0],mm2);\
  1285. movd_m2r(lum_m1[0],mm3);\
  1286. movd_m2r(lum[0],mm4);\
  1287. punpcklbw_r2r(mm7,mm0);\
  1288. punpcklbw_r2r(mm7,mm1);\
  1289. punpcklbw_r2r(mm7,mm2);\
  1290. punpcklbw_r2r(mm7,mm3);\
  1291. punpcklbw_r2r(mm7,mm4);\
  1292. paddw_r2r(mm3,mm1);\
  1293. psllw_i2r(1,mm2);\
  1294. paddw_r2r(mm4,mm0);\
  1295. psllw_i2r(2,mm1);\
  1296. paddw_r2r(mm6,mm2);\
  1297. paddw_r2r(mm2,mm1);\
  1298. psubusw_r2r(mm0,mm1);\
  1299. psrlw_i2r(3,mm1);\
  1300. packuswb_r2r(mm7,mm1);\
  1301. movd_r2m(mm1,dst[0]);
  1302. #endif
  1303. /* filter parameters: [-1 4 2 4 -1] // 8 */
  1304. static void deinterlace_line(uint8_t *dst,
  1305. const uint8_t *lum_m4, const uint8_t *lum_m3,
  1306. const uint8_t *lum_m2, const uint8_t *lum_m1,
  1307. const uint8_t *lum,
  1308. int size)
  1309. {
  1310. #if !HAVE_MMX
  1311. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1312. int sum;
  1313. for(;size > 0;size--) {
  1314. sum = -lum_m4[0];
  1315. sum += lum_m3[0] << 2;
  1316. sum += lum_m2[0] << 1;
  1317. sum += lum_m1[0] << 2;
  1318. sum += -lum[0];
  1319. dst[0] = cm[(sum + 4) >> 3];
  1320. lum_m4++;
  1321. lum_m3++;
  1322. lum_m2++;
  1323. lum_m1++;
  1324. lum++;
  1325. dst++;
  1326. }
  1327. #else
  1328. {
  1329. pxor_r2r(mm7,mm7);
  1330. movq_m2r(ff_pw_4,mm6);
  1331. }
  1332. for (;size > 3; size-=4) {
  1333. DEINT_LINE_LUM
  1334. lum_m4+=4;
  1335. lum_m3+=4;
  1336. lum_m2+=4;
  1337. lum_m1+=4;
  1338. lum+=4;
  1339. dst+=4;
  1340. }
  1341. #endif
  1342. }
  1343. static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
  1344. int size)
  1345. {
  1346. #if !HAVE_MMX
  1347. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  1348. int sum;
  1349. for(;size > 0;size--) {
  1350. sum = -lum_m4[0];
  1351. sum += lum_m3[0] << 2;
  1352. sum += lum_m2[0] << 1;
  1353. lum_m4[0]=lum_m2[0];
  1354. sum += lum_m1[0] << 2;
  1355. sum += -lum[0];
  1356. lum_m2[0] = cm[(sum + 4) >> 3];
  1357. lum_m4++;
  1358. lum_m3++;
  1359. lum_m2++;
  1360. lum_m1++;
  1361. lum++;
  1362. }
  1363. #else
  1364. {
  1365. pxor_r2r(mm7,mm7);
  1366. movq_m2r(ff_pw_4,mm6);
  1367. }
  1368. for (;size > 3; size-=4) {
  1369. DEINT_INPLACE_LINE_LUM
  1370. lum_m4+=4;
  1371. lum_m3+=4;
  1372. lum_m2+=4;
  1373. lum_m1+=4;
  1374. lum+=4;
  1375. }
  1376. #endif
  1377. }
  1378. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  1379. top field is copied as is, but the bottom field is deinterlaced
  1380. against the top field. */
  1381. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  1382. const uint8_t *src1, int src_wrap,
  1383. int width, int height)
  1384. {
  1385. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  1386. int y;
  1387. src_m2 = src1;
  1388. src_m1 = src1;
  1389. src_0=&src_m1[src_wrap];
  1390. src_p1=&src_0[src_wrap];
  1391. src_p2=&src_p1[src_wrap];
  1392. for(y=0;y<(height-2);y+=2) {
  1393. memcpy(dst,src_m1,width);
  1394. dst += dst_wrap;
  1395. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  1396. src_m2 = src_0;
  1397. src_m1 = src_p1;
  1398. src_0 = src_p2;
  1399. src_p1 += 2*src_wrap;
  1400. src_p2 += 2*src_wrap;
  1401. dst += dst_wrap;
  1402. }
  1403. memcpy(dst,src_m1,width);
  1404. dst += dst_wrap;
  1405. /* do last line */
  1406. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  1407. }
  1408. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  1409. int width, int height)
  1410. {
  1411. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  1412. int y;
  1413. uint8_t *buf;
  1414. buf = (uint8_t*)av_malloc(width);
  1415. src_m1 = src1;
  1416. memcpy(buf,src_m1,width);
  1417. src_0=&src_m1[src_wrap];
  1418. src_p1=&src_0[src_wrap];
  1419. src_p2=&src_p1[src_wrap];
  1420. for(y=0;y<(height-2);y+=2) {
  1421. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  1422. src_m1 = src_p1;
  1423. src_0 = src_p2;
  1424. src_p1 += 2*src_wrap;
  1425. src_p2 += 2*src_wrap;
  1426. }
  1427. /* do last line */
  1428. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  1429. av_free(buf);
  1430. }
  1431. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  1432. enum PixelFormat pix_fmt, int width, int height)
  1433. {
  1434. int i;
  1435. if (pix_fmt != PIX_FMT_YUV420P &&
  1436. pix_fmt != PIX_FMT_YUV422P &&
  1437. pix_fmt != PIX_FMT_YUV444P &&
  1438. pix_fmt != PIX_FMT_YUV411P &&
  1439. pix_fmt != PIX_FMT_GRAY8)
  1440. return -1;
  1441. if ((width & 3) != 0 || (height & 3) != 0)
  1442. return -1;
  1443. for(i=0;i<3;i++) {
  1444. if (i == 1) {
  1445. switch(pix_fmt) {
  1446. case PIX_FMT_YUV420P:
  1447. width >>= 1;
  1448. height >>= 1;
  1449. break;
  1450. case PIX_FMT_YUV422P:
  1451. width >>= 1;
  1452. break;
  1453. case PIX_FMT_YUV411P:
  1454. width >>= 2;
  1455. break;
  1456. default:
  1457. break;
  1458. }
  1459. if (pix_fmt == PIX_FMT_GRAY8) {
  1460. break;
  1461. }
  1462. }
  1463. if (src == dst) {
  1464. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  1465. width, height);
  1466. } else {
  1467. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  1468. src->data[i], src->linesize[i],
  1469. width, height);
  1470. }
  1471. }
  1472. emms_c();
  1473. return 0;
  1474. }