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.

1491 lines
42KB

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