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.

1552 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. #include "libavutil/pixdesc.h"
  34. #if HAVE_MMX
  35. #include "x86/mmx.h"
  36. #include "x86/dsputil_mmx.h"
  37. #endif
  38. #define xglue(x, y) x ## y
  39. #define glue(x, y) xglue(x, y)
  40. #define FF_COLOR_RGB 0 /**< RGB color space */
  41. #define FF_COLOR_GRAY 1 /**< gray color space */
  42. #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
  43. #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
  44. #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
  45. #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
  46. #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
  47. typedef struct PixFmtInfo {
  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 depth; /**< bit depth of the color components */
  53. } PixFmtInfo;
  54. /* this table gives more information about formats */
  55. static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
  56. /* YUV formats */
  57. [PIX_FMT_YUV420P] = {
  58. .nb_channels = 3,
  59. .color_type = FF_COLOR_YUV,
  60. .pixel_type = FF_PIXEL_PLANAR,
  61. .depth = 8,
  62. },
  63. [PIX_FMT_YUV422P] = {
  64. .nb_channels = 3,
  65. .color_type = FF_COLOR_YUV,
  66. .pixel_type = FF_PIXEL_PLANAR,
  67. .depth = 8,
  68. },
  69. [PIX_FMT_YUV444P] = {
  70. .nb_channels = 3,
  71. .color_type = FF_COLOR_YUV,
  72. .pixel_type = FF_PIXEL_PLANAR,
  73. .depth = 8,
  74. },
  75. [PIX_FMT_YUYV422] = {
  76. .nb_channels = 1,
  77. .color_type = FF_COLOR_YUV,
  78. .pixel_type = FF_PIXEL_PACKED,
  79. .depth = 8,
  80. },
  81. [PIX_FMT_UYVY422] = {
  82. .nb_channels = 1,
  83. .color_type = FF_COLOR_YUV,
  84. .pixel_type = FF_PIXEL_PACKED,
  85. .depth = 8,
  86. },
  87. [PIX_FMT_YUV410P] = {
  88. .nb_channels = 3,
  89. .color_type = FF_COLOR_YUV,
  90. .pixel_type = FF_PIXEL_PLANAR,
  91. .depth = 8,
  92. },
  93. [PIX_FMT_YUV411P] = {
  94. .nb_channels = 3,
  95. .color_type = FF_COLOR_YUV,
  96. .pixel_type = FF_PIXEL_PLANAR,
  97. .depth = 8,
  98. },
  99. [PIX_FMT_YUV440P] = {
  100. .nb_channels = 3,
  101. .color_type = FF_COLOR_YUV,
  102. .pixel_type = FF_PIXEL_PLANAR,
  103. .depth = 8,
  104. },
  105. [PIX_FMT_YUV420P16LE] = {
  106. .nb_channels = 3,
  107. .color_type = FF_COLOR_YUV,
  108. .pixel_type = FF_PIXEL_PLANAR,
  109. .depth = 16,
  110. },
  111. [PIX_FMT_YUV422P16LE] = {
  112. .nb_channels = 3,
  113. .color_type = FF_COLOR_YUV,
  114. .pixel_type = FF_PIXEL_PLANAR,
  115. .depth = 16,
  116. },
  117. [PIX_FMT_YUV444P16LE] = {
  118. .nb_channels = 3,
  119. .color_type = FF_COLOR_YUV,
  120. .pixel_type = FF_PIXEL_PLANAR,
  121. .depth = 16,
  122. },
  123. [PIX_FMT_YUV420P16BE] = {
  124. .nb_channels = 3,
  125. .color_type = FF_COLOR_YUV,
  126. .pixel_type = FF_PIXEL_PLANAR,
  127. .depth = 16,
  128. },
  129. [PIX_FMT_YUV422P16BE] = {
  130. .nb_channels = 3,
  131. .color_type = FF_COLOR_YUV,
  132. .pixel_type = FF_PIXEL_PLANAR,
  133. .depth = 16,
  134. },
  135. [PIX_FMT_YUV444P16BE] = {
  136. .nb_channels = 3,
  137. .color_type = FF_COLOR_YUV,
  138. .pixel_type = FF_PIXEL_PLANAR,
  139. .depth = 16,
  140. },
  141. /* YUV formats with alpha plane */
  142. [PIX_FMT_YUVA420P] = {
  143. .nb_channels = 4,
  144. .color_type = FF_COLOR_YUV,
  145. .pixel_type = FF_PIXEL_PLANAR,
  146. .depth = 8,
  147. },
  148. /* JPEG YUV */
  149. [PIX_FMT_YUVJ420P] = {
  150. .nb_channels = 3,
  151. .color_type = FF_COLOR_YUV_JPEG,
  152. .pixel_type = FF_PIXEL_PLANAR,
  153. .depth = 8,
  154. },
  155. [PIX_FMT_YUVJ422P] = {
  156. .nb_channels = 3,
  157. .color_type = FF_COLOR_YUV_JPEG,
  158. .pixel_type = FF_PIXEL_PLANAR,
  159. .depth = 8,
  160. },
  161. [PIX_FMT_YUVJ444P] = {
  162. .nb_channels = 3,
  163. .color_type = FF_COLOR_YUV_JPEG,
  164. .pixel_type = FF_PIXEL_PLANAR,
  165. .depth = 8,
  166. },
  167. [PIX_FMT_YUVJ440P] = {
  168. .nb_channels = 3,
  169. .color_type = FF_COLOR_YUV_JPEG,
  170. .pixel_type = FF_PIXEL_PLANAR,
  171. .depth = 8,
  172. },
  173. /* RGB formats */
  174. [PIX_FMT_RGB24] = {
  175. .nb_channels = 3,
  176. .color_type = FF_COLOR_RGB,
  177. .pixel_type = FF_PIXEL_PACKED,
  178. .depth = 8,
  179. },
  180. [PIX_FMT_BGR24] = {
  181. .nb_channels = 3,
  182. .color_type = FF_COLOR_RGB,
  183. .pixel_type = FF_PIXEL_PACKED,
  184. .depth = 8,
  185. },
  186. [PIX_FMT_ARGB] = {
  187. .nb_channels = 4, .is_alpha = 1,
  188. .color_type = FF_COLOR_RGB,
  189. .pixel_type = FF_PIXEL_PACKED,
  190. .depth = 8,
  191. },
  192. [PIX_FMT_RGB48BE] = {
  193. .nb_channels = 3,
  194. .color_type = FF_COLOR_RGB,
  195. .pixel_type = FF_PIXEL_PACKED,
  196. .depth = 16,
  197. },
  198. [PIX_FMT_RGB48LE] = {
  199. .nb_channels = 3,
  200. .color_type = FF_COLOR_RGB,
  201. .pixel_type = FF_PIXEL_PACKED,
  202. .depth = 16,
  203. },
  204. [PIX_FMT_RGB565BE] = {
  205. .nb_channels = 3,
  206. .color_type = FF_COLOR_RGB,
  207. .pixel_type = FF_PIXEL_PACKED,
  208. .depth = 5,
  209. },
  210. [PIX_FMT_RGB565LE] = {
  211. .nb_channels = 3,
  212. .color_type = FF_COLOR_RGB,
  213. .pixel_type = FF_PIXEL_PACKED,
  214. .depth = 5,
  215. },
  216. [PIX_FMT_RGB555BE] = {
  217. .nb_channels = 3,
  218. .color_type = FF_COLOR_RGB,
  219. .pixel_type = FF_PIXEL_PACKED,
  220. .depth = 5,
  221. },
  222. [PIX_FMT_RGB555LE] = {
  223. .nb_channels = 3,
  224. .color_type = FF_COLOR_RGB,
  225. .pixel_type = FF_PIXEL_PACKED,
  226. .depth = 5,
  227. },
  228. /* gray / mono formats */
  229. [PIX_FMT_GRAY16BE] = {
  230. .nb_channels = 1,
  231. .color_type = FF_COLOR_GRAY,
  232. .pixel_type = FF_PIXEL_PLANAR,
  233. .depth = 16,
  234. },
  235. [PIX_FMT_GRAY16LE] = {
  236. .nb_channels = 1,
  237. .color_type = FF_COLOR_GRAY,
  238. .pixel_type = FF_PIXEL_PLANAR,
  239. .depth = 16,
  240. },
  241. [PIX_FMT_GRAY8] = {
  242. .nb_channels = 1,
  243. .color_type = FF_COLOR_GRAY,
  244. .pixel_type = FF_PIXEL_PLANAR,
  245. .depth = 8,
  246. },
  247. [PIX_FMT_MONOWHITE] = {
  248. .nb_channels = 1,
  249. .color_type = FF_COLOR_GRAY,
  250. .pixel_type = FF_PIXEL_PLANAR,
  251. .depth = 1,
  252. },
  253. [PIX_FMT_MONOBLACK] = {
  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. .nb_channels = 4, .is_alpha = 1,
  262. .color_type = FF_COLOR_RGB,
  263. .pixel_type = FF_PIXEL_PALETTE,
  264. .depth = 8,
  265. },
  266. [PIX_FMT_XVMC_MPEG2_MC] = {
  267. },
  268. [PIX_FMT_XVMC_MPEG2_IDCT] = {
  269. },
  270. [PIX_FMT_VDPAU_MPEG1] = {
  271. },
  272. [PIX_FMT_VDPAU_MPEG2] = {
  273. },
  274. [PIX_FMT_VDPAU_H264] = {
  275. },
  276. [PIX_FMT_VDPAU_WMV3] = {
  277. },
  278. [PIX_FMT_VDPAU_VC1] = {
  279. },
  280. [PIX_FMT_VDPAU_MPEG4] = {
  281. },
  282. [PIX_FMT_UYYVYY411] = {
  283. .nb_channels = 1,
  284. .color_type = FF_COLOR_YUV,
  285. .pixel_type = FF_PIXEL_PACKED,
  286. .depth = 8,
  287. },
  288. [PIX_FMT_ABGR] = {
  289. .nb_channels = 4, .is_alpha = 1,
  290. .color_type = FF_COLOR_RGB,
  291. .pixel_type = FF_PIXEL_PACKED,
  292. .depth = 8,
  293. },
  294. [PIX_FMT_BGR565BE] = {
  295. .nb_channels = 3,
  296. .color_type = FF_COLOR_RGB,
  297. .pixel_type = FF_PIXEL_PACKED,
  298. .depth = 5,
  299. },
  300. [PIX_FMT_BGR565LE] = {
  301. .nb_channels = 3,
  302. .color_type = FF_COLOR_RGB,
  303. .pixel_type = FF_PIXEL_PACKED,
  304. .depth = 5,
  305. },
  306. [PIX_FMT_BGR555BE] = {
  307. .nb_channels = 3,
  308. .color_type = FF_COLOR_RGB,
  309. .pixel_type = FF_PIXEL_PACKED,
  310. .depth = 5,
  311. },
  312. [PIX_FMT_BGR555LE] = {
  313. .nb_channels = 3,
  314. .color_type = FF_COLOR_RGB,
  315. .pixel_type = FF_PIXEL_PACKED,
  316. .depth = 5,
  317. },
  318. [PIX_FMT_RGB8] = {
  319. .nb_channels = 1,
  320. .color_type = FF_COLOR_RGB,
  321. .pixel_type = FF_PIXEL_PACKED,
  322. .depth = 8,
  323. },
  324. [PIX_FMT_RGB4] = {
  325. .nb_channels = 1,
  326. .color_type = FF_COLOR_RGB,
  327. .pixel_type = FF_PIXEL_PACKED,
  328. .depth = 4,
  329. },
  330. [PIX_FMT_RGB4_BYTE] = {
  331. .nb_channels = 1,
  332. .color_type = FF_COLOR_RGB,
  333. .pixel_type = FF_PIXEL_PACKED,
  334. .depth = 8,
  335. },
  336. [PIX_FMT_BGR8] = {
  337. .nb_channels = 1,
  338. .color_type = FF_COLOR_RGB,
  339. .pixel_type = FF_PIXEL_PACKED,
  340. .depth = 8,
  341. },
  342. [PIX_FMT_BGR4] = {
  343. .nb_channels = 1,
  344. .color_type = FF_COLOR_RGB,
  345. .pixel_type = FF_PIXEL_PACKED,
  346. .depth = 4,
  347. },
  348. [PIX_FMT_BGR4_BYTE] = {
  349. .nb_channels = 1,
  350. .color_type = FF_COLOR_RGB,
  351. .pixel_type = FF_PIXEL_PACKED,
  352. .depth = 8,
  353. },
  354. [PIX_FMT_NV12] = {
  355. .nb_channels = 2,
  356. .color_type = FF_COLOR_YUV,
  357. .pixel_type = FF_PIXEL_PLANAR,
  358. .depth = 8,
  359. },
  360. [PIX_FMT_NV21] = {
  361. .nb_channels = 2,
  362. .color_type = FF_COLOR_YUV,
  363. .pixel_type = FF_PIXEL_PLANAR,
  364. .depth = 8,
  365. },
  366. [PIX_FMT_BGRA] = {
  367. .nb_channels = 4, .is_alpha = 1,
  368. .color_type = FF_COLOR_RGB,
  369. .pixel_type = FF_PIXEL_PACKED,
  370. .depth = 8,
  371. },
  372. [PIX_FMT_RGBA] = {
  373. .nb_channels = 4, .is_alpha = 1,
  374. .color_type = FF_COLOR_RGB,
  375. .pixel_type = FF_PIXEL_PACKED,
  376. .depth = 8,
  377. },
  378. /* VA API formats */
  379. [PIX_FMT_VAAPI_MOCO] = {
  380. },
  381. [PIX_FMT_VAAPI_IDCT] = {
  382. },
  383. [PIX_FMT_VAAPI_VLD] = {
  384. },
  385. };
  386. void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
  387. {
  388. *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  389. *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  390. }
  391. const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
  392. {
  393. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  394. return NULL;
  395. else
  396. return av_pix_fmt_descriptors[pix_fmt].name;
  397. }
  398. static enum PixelFormat avcodec_get_pix_fmt_internal(const char *name)
  399. {
  400. int i;
  401. for (i=0; i < PIX_FMT_NB; i++)
  402. if (av_pix_fmt_descriptors[i].name && !strcmp(av_pix_fmt_descriptors[i].name, name))
  403. return i;
  404. return PIX_FMT_NONE;
  405. }
  406. #if HAVE_BIGENDIAN
  407. # define X_NE(be, le) be
  408. #else
  409. # define X_NE(be, le) le
  410. #endif
  411. enum PixelFormat avcodec_get_pix_fmt(const char *name)
  412. {
  413. enum PixelFormat pix_fmt;
  414. if (!strcmp(name, "rgb32"))
  415. name = X_NE("argb", "bgra");
  416. else if (!strcmp(name, "bgr32"))
  417. name = X_NE("abgr", "rgba");
  418. pix_fmt = avcodec_get_pix_fmt_internal(name);
  419. if (pix_fmt == PIX_FMT_NONE) {
  420. char name2[32];
  421. snprintf(name2, sizeof(name2), "%s%s", name, X_NE("be", "le"));
  422. pix_fmt = avcodec_get_pix_fmt_internal(name2);
  423. }
  424. return pix_fmt;
  425. }
  426. void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
  427. {
  428. /* print header */
  429. if (pix_fmt < 0)
  430. snprintf (buf, buf_size,
  431. "name " " nb_channels" " depth" " is_alpha"
  432. );
  433. else{
  434. PixFmtInfo info= pix_fmt_info[pix_fmt];
  435. char is_alpha_char= info.is_alpha ? 'y' : 'n';
  436. snprintf (buf, buf_size,
  437. "%-11s %5d %9d %6c",
  438. av_pix_fmt_descriptors[pix_fmt].name,
  439. info.nb_channels,
  440. info.depth,
  441. is_alpha_char
  442. );
  443. }
  444. }
  445. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  446. {
  447. return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
  448. }
  449. int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
  450. int i;
  451. for(i=0; i<256; i++){
  452. int r,g,b;
  453. switch(pix_fmt) {
  454. case PIX_FMT_RGB8:
  455. r= (i>>5 )*36;
  456. g= ((i>>2)&7)*36;
  457. b= (i&3 )*85;
  458. break;
  459. case PIX_FMT_BGR8:
  460. b= (i>>6 )*85;
  461. g= ((i>>3)&7)*36;
  462. r= (i&7 )*36;
  463. break;
  464. case PIX_FMT_RGB4_BYTE:
  465. r= (i>>3 )*255;
  466. g= ((i>>1)&3)*85;
  467. b= (i&1 )*255;
  468. break;
  469. case PIX_FMT_BGR4_BYTE:
  470. b= (i>>3 )*255;
  471. g= ((i>>1)&3)*85;
  472. r= (i&1 )*255;
  473. break;
  474. case PIX_FMT_GRAY8:
  475. r=b=g= i;
  476. break;
  477. default:
  478. return -1;
  479. }
  480. pal[i] = b + (g<<8) + (r<<16);
  481. }
  482. return 0;
  483. }
  484. int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
  485. {
  486. int w2;
  487. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  488. memset(picture->linesize, 0, sizeof(picture->linesize));
  489. switch(pix_fmt) {
  490. case PIX_FMT_YUV420P:
  491. case PIX_FMT_YUV422P:
  492. case PIX_FMT_YUV444P:
  493. case PIX_FMT_YUV410P:
  494. case PIX_FMT_YUV411P:
  495. case PIX_FMT_YUV440P:
  496. case PIX_FMT_YUVJ420P:
  497. case PIX_FMT_YUVJ422P:
  498. case PIX_FMT_YUVJ444P:
  499. case PIX_FMT_YUVJ440P:
  500. w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
  501. picture->linesize[0] = width;
  502. picture->linesize[1] = w2;
  503. picture->linesize[2] = w2;
  504. break;
  505. case PIX_FMT_YUV420P16LE:
  506. case PIX_FMT_YUV422P16LE:
  507. case PIX_FMT_YUV444P16LE:
  508. case PIX_FMT_YUV420P16BE:
  509. case PIX_FMT_YUV422P16BE:
  510. case PIX_FMT_YUV444P16BE:
  511. w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
  512. picture->linesize[0] = 2*width;
  513. picture->linesize[1] = 2*w2;
  514. picture->linesize[2] = 2*w2;
  515. break;
  516. case PIX_FMT_YUVA420P:
  517. w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
  518. picture->linesize[0] = width;
  519. picture->linesize[1] = w2;
  520. picture->linesize[2] = w2;
  521. picture->linesize[3] = width;
  522. break;
  523. case PIX_FMT_NV12:
  524. case PIX_FMT_NV21:
  525. w2 = (width + (1 << desc->log2_chroma_w) - 1) >> desc->log2_chroma_w;
  526. picture->linesize[0] = width;
  527. picture->linesize[1] = 2 * w2;
  528. break;
  529. case PIX_FMT_RGB24:
  530. case PIX_FMT_BGR24:
  531. picture->linesize[0] = width * 3;
  532. break;
  533. case PIX_FMT_ARGB:
  534. case PIX_FMT_ABGR:
  535. case PIX_FMT_RGBA:
  536. case PIX_FMT_BGRA:
  537. picture->linesize[0] = width * 4;
  538. break;
  539. case PIX_FMT_RGB48BE:
  540. case PIX_FMT_RGB48LE:
  541. picture->linesize[0] = width * 6;
  542. break;
  543. case PIX_FMT_GRAY16BE:
  544. case PIX_FMT_GRAY16LE:
  545. case PIX_FMT_BGR555BE:
  546. case PIX_FMT_BGR555LE:
  547. case PIX_FMT_BGR565BE:
  548. case PIX_FMT_BGR565LE:
  549. case PIX_FMT_RGB555BE:
  550. case PIX_FMT_RGB555LE:
  551. case PIX_FMT_RGB565BE:
  552. case PIX_FMT_RGB565LE:
  553. case PIX_FMT_YUYV422:
  554. picture->linesize[0] = width * 2;
  555. break;
  556. case PIX_FMT_UYVY422:
  557. picture->linesize[0] = width * 2;
  558. break;
  559. case PIX_FMT_UYYVYY411:
  560. picture->linesize[0] = width + width/2;
  561. break;
  562. case PIX_FMT_RGB4:
  563. case PIX_FMT_BGR4:
  564. picture->linesize[0] = width / 2;
  565. break;
  566. case PIX_FMT_MONOWHITE:
  567. case PIX_FMT_MONOBLACK:
  568. picture->linesize[0] = (width + 7) >> 3;
  569. break;
  570. case PIX_FMT_PAL8:
  571. case PIX_FMT_RGB8:
  572. case PIX_FMT_BGR8:
  573. case PIX_FMT_RGB4_BYTE:
  574. case PIX_FMT_BGR4_BYTE:
  575. case PIX_FMT_GRAY8:
  576. picture->linesize[0] = width;
  577. break;
  578. default:
  579. return -1;
  580. }
  581. return 0;
  582. }
  583. int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
  584. int height)
  585. {
  586. int size, h2, size2;
  587. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  588. size = picture->linesize[0] * height;
  589. switch(pix_fmt) {
  590. case PIX_FMT_YUV420P:
  591. case PIX_FMT_YUV422P:
  592. case PIX_FMT_YUV444P:
  593. case PIX_FMT_YUV410P:
  594. case PIX_FMT_YUV411P:
  595. case PIX_FMT_YUV440P:
  596. case PIX_FMT_YUVJ420P:
  597. case PIX_FMT_YUVJ422P:
  598. case PIX_FMT_YUVJ444P:
  599. case PIX_FMT_YUVJ440P:
  600. case PIX_FMT_YUV420P16LE:
  601. case PIX_FMT_YUV422P16LE:
  602. case PIX_FMT_YUV444P16LE:
  603. case PIX_FMT_YUV420P16BE:
  604. case PIX_FMT_YUV422P16BE:
  605. case PIX_FMT_YUV444P16BE:
  606. h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
  607. size2 = picture->linesize[1] * h2;
  608. picture->data[0] = ptr;
  609. picture->data[1] = picture->data[0] + size;
  610. picture->data[2] = picture->data[1] + size2;
  611. picture->data[3] = NULL;
  612. return size + 2 * size2;
  613. case PIX_FMT_YUVA420P:
  614. h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
  615. size2 = picture->linesize[1] * h2;
  616. picture->data[0] = ptr;
  617. picture->data[1] = picture->data[0] + size;
  618. picture->data[2] = picture->data[1] + size2;
  619. picture->data[3] = picture->data[1] + size2 + size2;
  620. return 2 * size + 2 * size2;
  621. case PIX_FMT_NV12:
  622. case PIX_FMT_NV21:
  623. h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
  624. size2 = picture->linesize[1] * h2;
  625. picture->data[0] = ptr;
  626. picture->data[1] = picture->data[0] + size;
  627. picture->data[2] = NULL;
  628. picture->data[3] = NULL;
  629. return size + size2;
  630. case PIX_FMT_RGB24:
  631. case PIX_FMT_BGR24:
  632. case PIX_FMT_ARGB:
  633. case PIX_FMT_ABGR:
  634. case PIX_FMT_RGBA:
  635. case PIX_FMT_BGRA:
  636. case PIX_FMT_RGB48BE:
  637. case PIX_FMT_RGB48LE:
  638. case PIX_FMT_GRAY16BE:
  639. case PIX_FMT_GRAY16LE:
  640. case PIX_FMT_BGR555BE:
  641. case PIX_FMT_BGR555LE:
  642. case PIX_FMT_BGR565BE:
  643. case PIX_FMT_BGR565LE:
  644. case PIX_FMT_RGB555BE:
  645. case PIX_FMT_RGB555LE:
  646. case PIX_FMT_RGB565BE:
  647. case PIX_FMT_RGB565LE:
  648. case PIX_FMT_YUYV422:
  649. case PIX_FMT_UYVY422:
  650. case PIX_FMT_UYYVYY411:
  651. case PIX_FMT_RGB4:
  652. case PIX_FMT_BGR4:
  653. case PIX_FMT_MONOWHITE:
  654. case PIX_FMT_MONOBLACK:
  655. picture->data[0] = ptr;
  656. picture->data[1] = NULL;
  657. picture->data[2] = NULL;
  658. picture->data[3] = NULL;
  659. return size;
  660. case PIX_FMT_PAL8:
  661. case PIX_FMT_RGB8:
  662. case PIX_FMT_BGR8:
  663. case PIX_FMT_RGB4_BYTE:
  664. case PIX_FMT_BGR4_BYTE:
  665. case PIX_FMT_GRAY8:
  666. size2 = (size + 3) & ~3;
  667. picture->data[0] = ptr;
  668. picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
  669. picture->data[2] = NULL;
  670. picture->data[3] = NULL;
  671. return size2 + 256 * 4;
  672. default:
  673. picture->data[0] = NULL;
  674. picture->data[1] = NULL;
  675. picture->data[2] = NULL;
  676. picture->data[3] = NULL;
  677. return -1;
  678. }
  679. }
  680. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  681. enum PixelFormat pix_fmt, int width, int height)
  682. {
  683. if(avcodec_check_dimensions(NULL, width, height))
  684. return -1;
  685. if (ff_fill_linesize(picture, pix_fmt, width))
  686. return -1;
  687. return ff_fill_pointer(picture, ptr, pix_fmt, height);
  688. }
  689. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  690. unsigned char *dest, int dest_size)
  691. {
  692. const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
  693. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  694. int i, j, w, ow, h, oh, data_planes;
  695. const unsigned char* s;
  696. int size = avpicture_get_size(pix_fmt, width, height);
  697. if (size > dest_size || size < 0)
  698. return -1;
  699. if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
  700. if (pix_fmt == PIX_FMT_YUYV422 ||
  701. pix_fmt == PIX_FMT_UYVY422 ||
  702. pix_fmt == PIX_FMT_BGR565BE ||
  703. pix_fmt == PIX_FMT_BGR565LE ||
  704. pix_fmt == PIX_FMT_BGR555BE ||
  705. pix_fmt == PIX_FMT_BGR555LE ||
  706. pix_fmt == PIX_FMT_RGB565BE ||
  707. pix_fmt == PIX_FMT_RGB565LE ||
  708. pix_fmt == PIX_FMT_RGB555BE ||
  709. pix_fmt == PIX_FMT_RGB555LE)
  710. w = width * 2;
  711. else if (pix_fmt == PIX_FMT_UYYVYY411)
  712. w = width + width/2;
  713. else if (pix_fmt == PIX_FMT_PAL8)
  714. w = width;
  715. else
  716. w = width * (pf->depth * pf->nb_channels / 8);
  717. data_planes = 1;
  718. h = height;
  719. } else {
  720. data_planes = pf->nb_channels;
  721. w = (width*pf->depth + 7)/8;
  722. h = height;
  723. }
  724. ow = w;
  725. oh = h;
  726. for (i=0; i<data_planes; i++) {
  727. if (i == 1) {
  728. w = ((width >> desc->log2_chroma_w) * pf->depth + 7) / 8;
  729. h = height >> desc->log2_chroma_h;
  730. if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
  731. w <<= 1;
  732. } else if (i == 3) {
  733. w = ow;
  734. h = oh;
  735. }
  736. s = src->data[i];
  737. for(j=0; j<h; j++) {
  738. memcpy(dest, s, w);
  739. dest += w;
  740. s += src->linesize[i];
  741. }
  742. }
  743. if (pf->pixel_type == FF_PIXEL_PALETTE)
  744. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  745. return size;
  746. }
  747. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  748. {
  749. AVPicture dummy_pict;
  750. if(avcodec_check_dimensions(NULL, width, height))
  751. return -1;
  752. switch (pix_fmt) {
  753. case PIX_FMT_RGB8:
  754. case PIX_FMT_BGR8:
  755. case PIX_FMT_RGB4_BYTE:
  756. case PIX_FMT_BGR4_BYTE:
  757. case PIX_FMT_GRAY8:
  758. // do not include palette for these pseudo-paletted formats
  759. return width * height;
  760. }
  761. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  762. }
  763. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  764. int has_alpha)
  765. {
  766. const PixFmtInfo *pf, *ps;
  767. const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  768. const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  769. int loss;
  770. ps = &pix_fmt_info[src_pix_fmt];
  771. /* compute loss */
  772. loss = 0;
  773. pf = &pix_fmt_info[dst_pix_fmt];
  774. if (pf->depth < ps->depth ||
  775. ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE) &&
  776. (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE)))
  777. loss |= FF_LOSS_DEPTH;
  778. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  779. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  780. loss |= FF_LOSS_RESOLUTION;
  781. switch(pf->color_type) {
  782. case FF_COLOR_RGB:
  783. if (ps->color_type != FF_COLOR_RGB &&
  784. ps->color_type != FF_COLOR_GRAY)
  785. loss |= FF_LOSS_COLORSPACE;
  786. break;
  787. case FF_COLOR_GRAY:
  788. if (ps->color_type != FF_COLOR_GRAY)
  789. loss |= FF_LOSS_COLORSPACE;
  790. break;
  791. case FF_COLOR_YUV:
  792. if (ps->color_type != FF_COLOR_YUV)
  793. loss |= FF_LOSS_COLORSPACE;
  794. break;
  795. case FF_COLOR_YUV_JPEG:
  796. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  797. ps->color_type != FF_COLOR_YUV &&
  798. ps->color_type != FF_COLOR_GRAY)
  799. loss |= FF_LOSS_COLORSPACE;
  800. break;
  801. default:
  802. /* fail safe test */
  803. if (ps->color_type != pf->color_type)
  804. loss |= FF_LOSS_COLORSPACE;
  805. break;
  806. }
  807. if (pf->color_type == FF_COLOR_GRAY &&
  808. ps->color_type != FF_COLOR_GRAY)
  809. loss |= FF_LOSS_CHROMA;
  810. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  811. loss |= FF_LOSS_ALPHA;
  812. if (pf->pixel_type == FF_PIXEL_PALETTE &&
  813. (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
  814. loss |= FF_LOSS_COLORQUANT;
  815. return loss;
  816. }
  817. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  818. {
  819. int bits;
  820. const PixFmtInfo *pf;
  821. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  822. pf = &pix_fmt_info[pix_fmt];
  823. switch(pf->pixel_type) {
  824. case FF_PIXEL_PACKED:
  825. switch(pix_fmt) {
  826. case PIX_FMT_YUYV422:
  827. case PIX_FMT_UYVY422:
  828. case PIX_FMT_RGB565BE:
  829. case PIX_FMT_RGB565LE:
  830. case PIX_FMT_RGB555BE:
  831. case PIX_FMT_RGB555LE:
  832. case PIX_FMT_BGR565BE:
  833. case PIX_FMT_BGR565LE:
  834. case PIX_FMT_BGR555BE:
  835. case PIX_FMT_BGR555LE:
  836. bits = 16;
  837. break;
  838. case PIX_FMT_UYYVYY411:
  839. bits = 12;
  840. break;
  841. default:
  842. bits = pf->depth * pf->nb_channels;
  843. break;
  844. }
  845. break;
  846. case FF_PIXEL_PLANAR:
  847. if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
  848. bits = pf->depth * pf->nb_channels;
  849. } else {
  850. bits = pf->depth + ((2 * pf->depth) >>
  851. (desc->log2_chroma_w + desc->log2_chroma_h));
  852. }
  853. break;
  854. case FF_PIXEL_PALETTE:
  855. bits = 8;
  856. break;
  857. default:
  858. bits = -1;
  859. break;
  860. }
  861. return bits;
  862. }
  863. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  864. enum PixelFormat src_pix_fmt,
  865. int has_alpha,
  866. int loss_mask)
  867. {
  868. int dist, i, loss, min_dist;
  869. enum PixelFormat dst_pix_fmt;
  870. /* find exact color match with smallest size */
  871. dst_pix_fmt = -1;
  872. min_dist = 0x7fffffff;
  873. for(i = 0;i < PIX_FMT_NB; i++) {
  874. if (pix_fmt_mask & (1ULL << i)) {
  875. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  876. if (loss == 0) {
  877. dist = avg_bits_per_pixel(i);
  878. if (dist < min_dist) {
  879. min_dist = dist;
  880. dst_pix_fmt = i;
  881. }
  882. }
  883. }
  884. }
  885. return dst_pix_fmt;
  886. }
  887. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  888. int has_alpha, int *loss_ptr)
  889. {
  890. enum PixelFormat dst_pix_fmt;
  891. int loss_mask, i;
  892. static const int loss_mask_order[] = {
  893. ~0, /* no loss first */
  894. ~FF_LOSS_ALPHA,
  895. ~FF_LOSS_RESOLUTION,
  896. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  897. ~FF_LOSS_COLORQUANT,
  898. ~FF_LOSS_DEPTH,
  899. 0,
  900. };
  901. /* try with successive loss */
  902. i = 0;
  903. for(;;) {
  904. loss_mask = loss_mask_order[i++];
  905. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  906. has_alpha, loss_mask);
  907. if (dst_pix_fmt >= 0)
  908. goto found;
  909. if (loss_mask == 0)
  910. break;
  911. }
  912. return -1;
  913. found:
  914. if (loss_ptr)
  915. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  916. return dst_pix_fmt;
  917. }
  918. void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
  919. const uint8_t *src, int src_wrap,
  920. int width, int height)
  921. {
  922. if((!dst) || (!src))
  923. return;
  924. for(;height > 0; height--) {
  925. memcpy(dst, src, width);
  926. dst += dst_wrap;
  927. src += src_wrap;
  928. }
  929. }
  930. int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
  931. {
  932. int bits;
  933. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  934. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  935. pf = &pix_fmt_info[pix_fmt];
  936. switch(pf->pixel_type) {
  937. case FF_PIXEL_PACKED:
  938. switch(pix_fmt) {
  939. case PIX_FMT_YUYV422:
  940. case PIX_FMT_UYVY422:
  941. case PIX_FMT_RGB565BE:
  942. case PIX_FMT_RGB565LE:
  943. case PIX_FMT_RGB555BE:
  944. case PIX_FMT_RGB555LE:
  945. case PIX_FMT_BGR565BE:
  946. case PIX_FMT_BGR565LE:
  947. case PIX_FMT_BGR555BE:
  948. case PIX_FMT_BGR555LE:
  949. bits = 16;
  950. break;
  951. case PIX_FMT_UYYVYY411:
  952. bits = 12;
  953. break;
  954. default:
  955. bits = pf->depth * pf->nb_channels;
  956. break;
  957. }
  958. return (width * bits + 7) >> 3;
  959. break;
  960. case FF_PIXEL_PLANAR:
  961. if (plane == 1 || plane == 2)
  962. width= -((-width)>>desc->log2_chroma_w);
  963. return (width * pf->depth + 7) >> 3;
  964. break;
  965. case FF_PIXEL_PALETTE:
  966. if (plane == 0)
  967. return width;
  968. break;
  969. }
  970. return -1;
  971. }
  972. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  973. enum PixelFormat pix_fmt, int width, int height)
  974. {
  975. int i;
  976. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  977. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  978. switch(pf->pixel_type) {
  979. case FF_PIXEL_PACKED:
  980. case FF_PIXEL_PLANAR:
  981. for(i = 0; i < pf->nb_channels; i++) {
  982. int h;
  983. int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
  984. h = height;
  985. if (i == 1 || i == 2) {
  986. h= -((-height)>>desc->log2_chroma_h);
  987. }
  988. ff_img_copy_plane(dst->data[i], dst->linesize[i],
  989. src->data[i], src->linesize[i],
  990. bwidth, h);
  991. }
  992. break;
  993. case FF_PIXEL_PALETTE:
  994. ff_img_copy_plane(dst->data[0], dst->linesize[0],
  995. src->data[0], src->linesize[0],
  996. width, height);
  997. /* copy the palette */
  998. memcpy(dst->data[1], src->data[1], 4*256);
  999. break;
  1000. }
  1001. }
  1002. /* 2x2 -> 1x1 */
  1003. void ff_shrink22(uint8_t *dst, int dst_wrap,
  1004. const uint8_t *src, int src_wrap,
  1005. int width, int height)
  1006. {
  1007. int w;
  1008. const uint8_t *s1, *s2;
  1009. uint8_t *d;
  1010. for(;height > 0; height--) {
  1011. s1 = src;
  1012. s2 = s1 + src_wrap;
  1013. d = dst;
  1014. for(w = width;w >= 4; w-=4) {
  1015. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1016. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  1017. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  1018. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  1019. s1 += 8;
  1020. s2 += 8;
  1021. d += 4;
  1022. }
  1023. for(;w > 0; w--) {
  1024. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  1025. s1 += 2;
  1026. s2 += 2;
  1027. d++;
  1028. }
  1029. src += 2 * src_wrap;
  1030. dst += dst_wrap;
  1031. }
  1032. }
  1033. /* 4x4 -> 1x1 */
  1034. void ff_shrink44(uint8_t *dst, int dst_wrap,
  1035. const uint8_t *src, int src_wrap,
  1036. int width, int height)
  1037. {
  1038. int w;
  1039. const uint8_t *s1, *s2, *s3, *s4;
  1040. uint8_t *d;
  1041. for(;height > 0; height--) {
  1042. s1 = src;
  1043. s2 = s1 + src_wrap;
  1044. s3 = s2 + src_wrap;
  1045. s4 = s3 + src_wrap;
  1046. d = dst;
  1047. for(w = width;w > 0; w--) {
  1048. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  1049. s2[0] + s2[1] + s2[2] + s2[3] +
  1050. s3[0] + s3[1] + s3[2] + s3[3] +
  1051. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  1052. s1 += 4;
  1053. s2 += 4;
  1054. s3 += 4;
  1055. s4 += 4;
  1056. d++;
  1057. }
  1058. src += 4 * src_wrap;
  1059. dst += dst_wrap;
  1060. }
  1061. }
  1062. /* 8x8 -> 1x1 */
  1063. void ff_shrink88(uint8_t *dst, int dst_wrap,
  1064. const uint8_t *src, int src_wrap,
  1065. int width, int height)
  1066. {
  1067. int w, i;
  1068. for(;height > 0; height--) {
  1069. for(w = width;w > 0; w--) {
  1070. int tmp=0;
  1071. for(i=0; i<8; i++){
  1072. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  1073. src += src_wrap;
  1074. }
  1075. *(dst++) = (tmp + 32)>>6;
  1076. src += 8 - 8*src_wrap;
  1077. }
  1078. src += 8*src_wrap - 8*width;
  1079. dst += dst_wrap - width;
  1080. }
  1081. }
  1082. int avpicture_alloc(AVPicture *picture,
  1083. enum PixelFormat pix_fmt, int width, int height)
  1084. {
  1085. int size;
  1086. void *ptr;
  1087. size = avpicture_fill(picture, NULL, pix_fmt, width, height);
  1088. if(size<0)
  1089. goto fail;
  1090. ptr = av_malloc(size);
  1091. if (!ptr)
  1092. goto fail;
  1093. avpicture_fill(picture, ptr, pix_fmt, width, height);
  1094. if(picture->data[1] && !picture->data[2])
  1095. ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
  1096. return 0;
  1097. fail:
  1098. memset(picture, 0, sizeof(AVPicture));
  1099. return -1;
  1100. }
  1101. void avpicture_free(AVPicture *picture)
  1102. {
  1103. av_free(picture->data[0]);
  1104. }
  1105. /* return true if yuv planar */
  1106. static inline int is_yuv_planar(const PixFmtInfo *ps)
  1107. {
  1108. return (ps->color_type == FF_COLOR_YUV ||
  1109. ps->color_type == FF_COLOR_YUV_JPEG) &&
  1110. ps->pixel_type == FF_PIXEL_PLANAR;
  1111. }
  1112. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  1113. enum PixelFormat pix_fmt, int top_band, int left_band)
  1114. {
  1115. int y_shift;
  1116. int x_shift;
  1117. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
  1118. return -1;
  1119. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  1120. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  1121. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  1122. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  1123. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  1124. dst->linesize[0] = src->linesize[0];
  1125. dst->linesize[1] = src->linesize[1];
  1126. dst->linesize[2] = src->linesize[2];
  1127. return 0;
  1128. }
  1129. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  1130. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  1131. int *color)
  1132. {
  1133. uint8_t *optr;
  1134. int y_shift;
  1135. int x_shift;
  1136. int yheight;
  1137. int i, y;
  1138. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  1139. !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
  1140. for (i = 0; i < 3; i++) {
  1141. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  1142. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  1143. if (padtop || padleft) {
  1144. memset(dst->data[i], color[i],
  1145. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  1146. }
  1147. if (padleft || padright) {
  1148. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1149. (dst->linesize[i] - (padright >> x_shift));
  1150. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1151. for (y = 0; y < yheight; y++) {
  1152. memset(optr, color[i], (padleft + padright) >> x_shift);
  1153. optr += dst->linesize[i];
  1154. }
  1155. }
  1156. if (src) { /* first line */
  1157. uint8_t *iptr = src->data[i];
  1158. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1159. (padleft >> x_shift);
  1160. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  1161. iptr += src->linesize[i];
  1162. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  1163. (dst->linesize[i] - (padright >> x_shift));
  1164. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  1165. for (y = 0; y < yheight; y++) {
  1166. memset(optr, color[i], (padleft + padright) >> x_shift);
  1167. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  1168. (width - padleft - padright) >> x_shift);
  1169. iptr += src->linesize[i];
  1170. optr += dst->linesize[i];
  1171. }
  1172. }
  1173. if (padbottom || padright) {
  1174. optr = dst->data[i] + dst->linesize[i] *
  1175. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  1176. memset(optr, color[i],dst->linesize[i] *
  1177. (padbottom >> y_shift) + (padright >> x_shift));
  1178. }
  1179. }
  1180. return 0;
  1181. }
  1182. /* NOTE: we scan all the pixels to have an exact information */
  1183. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  1184. {
  1185. const unsigned char *p;
  1186. int src_wrap, ret, x, y;
  1187. unsigned int a;
  1188. uint32_t *palette = (uint32_t *)src->data[1];
  1189. p = src->data[0];
  1190. src_wrap = src->linesize[0] - width;
  1191. ret = 0;
  1192. for(y=0;y<height;y++) {
  1193. for(x=0;x<width;x++) {
  1194. a = palette[p[0]] >> 24;
  1195. if (a == 0x00) {
  1196. ret |= FF_ALPHA_TRANSP;
  1197. } else if (a != 0xff) {
  1198. ret |= FF_ALPHA_SEMI_TRANSP;
  1199. }
  1200. p++;
  1201. }
  1202. p += src_wrap;
  1203. }
  1204. return ret;
  1205. }
  1206. int img_get_alpha_info(const AVPicture *src,
  1207. enum PixelFormat pix_fmt, int width, int height)
  1208. {
  1209. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  1210. int ret;
  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. }