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.

1549 lines
44KB

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