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.

954 lines
27KB

  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. #include "libavutil/imgutils.h"
  37. #if HAVE_MMX && HAVE_YASM
  38. #include "x86/dsputil_mmx.h"
  39. #endif
  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. #if HAVE_MMX && HAVE_YASM
  45. #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
  46. #define deinterlace_line ff_deinterlace_line_mmx
  47. #else
  48. #define deinterlace_line_inplace deinterlace_line_inplace_c
  49. #define deinterlace_line deinterlace_line_c
  50. #endif
  51. typedef struct PixFmtInfo {
  52. uint8_t color_type; /**< color type (see FF_COLOR_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. uint8_t padded_size; /**< padded size in bits if different from the non-padded size */
  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. .color_type = FF_COLOR_YUV,
  62. .depth = 8,
  63. },
  64. [PIX_FMT_YUV422P] = {
  65. .color_type = FF_COLOR_YUV,
  66. .depth = 8,
  67. },
  68. [PIX_FMT_YUV444P] = {
  69. .color_type = FF_COLOR_YUV,
  70. .depth = 8,
  71. },
  72. [PIX_FMT_YUYV422] = {
  73. .color_type = FF_COLOR_YUV,
  74. .depth = 8,
  75. },
  76. [PIX_FMT_UYVY422] = {
  77. .color_type = FF_COLOR_YUV,
  78. .depth = 8,
  79. },
  80. [PIX_FMT_YUV410P] = {
  81. .color_type = FF_COLOR_YUV,
  82. .depth = 8,
  83. },
  84. [PIX_FMT_YUV411P] = {
  85. .color_type = FF_COLOR_YUV,
  86. .depth = 8,
  87. },
  88. [PIX_FMT_YUV440P] = {
  89. .color_type = FF_COLOR_YUV,
  90. .depth = 8,
  91. },
  92. [PIX_FMT_YUV420P16LE] = {
  93. .color_type = FF_COLOR_YUV,
  94. .depth = 16,
  95. },
  96. [PIX_FMT_YUV422P16LE] = {
  97. .color_type = FF_COLOR_YUV,
  98. .depth = 16,
  99. },
  100. [PIX_FMT_YUV444P16LE] = {
  101. .color_type = FF_COLOR_YUV,
  102. .depth = 16,
  103. },
  104. [PIX_FMT_YUV420P16BE] = {
  105. .color_type = FF_COLOR_YUV,
  106. .depth = 16,
  107. },
  108. [PIX_FMT_YUV422P16BE] = {
  109. .color_type = FF_COLOR_YUV,
  110. .depth = 16,
  111. },
  112. [PIX_FMT_YUV444P16BE] = {
  113. .color_type = FF_COLOR_YUV,
  114. .depth = 16,
  115. },
  116. /* YUV formats with alpha plane */
  117. [PIX_FMT_YUVA420P] = {
  118. .color_type = FF_COLOR_YUV,
  119. .depth = 8,
  120. },
  121. /* JPEG YUV */
  122. [PIX_FMT_YUVJ420P] = {
  123. .color_type = FF_COLOR_YUV_JPEG,
  124. .depth = 8,
  125. },
  126. [PIX_FMT_YUVJ422P] = {
  127. .color_type = FF_COLOR_YUV_JPEG,
  128. .depth = 8,
  129. },
  130. [PIX_FMT_YUVJ444P] = {
  131. .color_type = FF_COLOR_YUV_JPEG,
  132. .depth = 8,
  133. },
  134. [PIX_FMT_YUVJ440P] = {
  135. .color_type = FF_COLOR_YUV_JPEG,
  136. .depth = 8,
  137. },
  138. /* RGB formats */
  139. [PIX_FMT_RGB24] = {
  140. .color_type = FF_COLOR_RGB,
  141. .depth = 8,
  142. },
  143. [PIX_FMT_BGR24] = {
  144. .color_type = FF_COLOR_RGB,
  145. .depth = 8,
  146. },
  147. [PIX_FMT_ARGB] = {
  148. .is_alpha = 1,
  149. .color_type = FF_COLOR_RGB,
  150. .depth = 8,
  151. },
  152. [PIX_FMT_RGB48BE] = {
  153. .color_type = FF_COLOR_RGB,
  154. .depth = 16,
  155. },
  156. [PIX_FMT_RGB48LE] = {
  157. .color_type = FF_COLOR_RGB,
  158. .depth = 16,
  159. },
  160. [PIX_FMT_RGB565BE] = {
  161. .color_type = FF_COLOR_RGB,
  162. .depth = 5,
  163. },
  164. [PIX_FMT_RGB565LE] = {
  165. .color_type = FF_COLOR_RGB,
  166. .depth = 5,
  167. },
  168. [PIX_FMT_RGB555BE] = {
  169. .color_type = FF_COLOR_RGB,
  170. .depth = 5,
  171. .padded_size = 16,
  172. },
  173. [PIX_FMT_RGB555LE] = {
  174. .color_type = FF_COLOR_RGB,
  175. .depth = 5,
  176. .padded_size = 16,
  177. },
  178. [PIX_FMT_RGB444BE] = {
  179. .color_type = FF_COLOR_RGB,
  180. .depth = 4,
  181. .padded_size = 16,
  182. },
  183. [PIX_FMT_RGB444LE] = {
  184. .color_type = FF_COLOR_RGB,
  185. .depth = 4,
  186. .padded_size = 16,
  187. },
  188. /* gray / mono formats */
  189. [PIX_FMT_GRAY16BE] = {
  190. .color_type = FF_COLOR_GRAY,
  191. .depth = 16,
  192. },
  193. [PIX_FMT_GRAY16LE] = {
  194. .color_type = FF_COLOR_GRAY,
  195. .depth = 16,
  196. },
  197. [PIX_FMT_GRAY8] = {
  198. .color_type = FF_COLOR_GRAY,
  199. .depth = 8,
  200. },
  201. [PIX_FMT_MONOWHITE] = {
  202. .color_type = FF_COLOR_GRAY,
  203. .depth = 1,
  204. },
  205. [PIX_FMT_MONOBLACK] = {
  206. .color_type = FF_COLOR_GRAY,
  207. .depth = 1,
  208. },
  209. /* paletted formats */
  210. [PIX_FMT_PAL8] = {
  211. .is_alpha = 1,
  212. .color_type = FF_COLOR_RGB,
  213. .depth = 8,
  214. },
  215. [PIX_FMT_UYYVYY411] = {
  216. .color_type = FF_COLOR_YUV,
  217. .depth = 8,
  218. },
  219. [PIX_FMT_ABGR] = {
  220. .is_alpha = 1,
  221. .color_type = FF_COLOR_RGB,
  222. .depth = 8,
  223. },
  224. [PIX_FMT_BGR565BE] = {
  225. .color_type = FF_COLOR_RGB,
  226. .depth = 5,
  227. .padded_size = 16,
  228. },
  229. [PIX_FMT_BGR565LE] = {
  230. .color_type = FF_COLOR_RGB,
  231. .depth = 5,
  232. .padded_size = 16,
  233. },
  234. [PIX_FMT_BGR555BE] = {
  235. .color_type = FF_COLOR_RGB,
  236. .depth = 5,
  237. .padded_size = 16,
  238. },
  239. [PIX_FMT_BGR555LE] = {
  240. .color_type = FF_COLOR_RGB,
  241. .depth = 5,
  242. .padded_size = 16,
  243. },
  244. [PIX_FMT_BGR444BE] = {
  245. .color_type = FF_COLOR_RGB,
  246. .depth = 4,
  247. .padded_size = 16,
  248. },
  249. [PIX_FMT_BGR444LE] = {
  250. .color_type = FF_COLOR_RGB,
  251. .depth = 4,
  252. .padded_size = 16,
  253. },
  254. [PIX_FMT_RGB8] = {
  255. .color_type = FF_COLOR_RGB,
  256. .depth = 8,
  257. },
  258. [PIX_FMT_RGB4] = {
  259. .color_type = FF_COLOR_RGB,
  260. .depth = 4,
  261. },
  262. [PIX_FMT_RGB4_BYTE] = {
  263. .color_type = FF_COLOR_RGB,
  264. .depth = 8,
  265. .padded_size = 8,
  266. },
  267. [PIX_FMT_BGR8] = {
  268. .color_type = FF_COLOR_RGB,
  269. .depth = 8,
  270. },
  271. [PIX_FMT_BGR4] = {
  272. .color_type = FF_COLOR_RGB,
  273. .depth = 4,
  274. },
  275. [PIX_FMT_BGR4_BYTE] = {
  276. .color_type = FF_COLOR_RGB,
  277. .depth = 8,
  278. .padded_size = 8,
  279. },
  280. [PIX_FMT_NV12] = {
  281. .color_type = FF_COLOR_YUV,
  282. .depth = 8,
  283. },
  284. [PIX_FMT_NV21] = {
  285. .color_type = FF_COLOR_YUV,
  286. .depth = 8,
  287. },
  288. [PIX_FMT_BGRA] = {
  289. .is_alpha = 1,
  290. .color_type = FF_COLOR_RGB,
  291. .depth = 8,
  292. },
  293. [PIX_FMT_RGBA] = {
  294. .is_alpha = 1,
  295. .color_type = FF_COLOR_RGB,
  296. .depth = 8,
  297. },
  298. };
  299. void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
  300. {
  301. *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  302. *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  303. }
  304. #if FF_API_GET_PIX_FMT_NAME
  305. const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
  306. {
  307. return av_get_pix_fmt_name(pix_fmt);
  308. }
  309. #endif
  310. int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
  311. {
  312. return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
  313. }
  314. int avpicture_fill(AVPicture *picture, uint8_t *ptr,
  315. enum PixelFormat pix_fmt, int width, int height)
  316. {
  317. int ret;
  318. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  319. return ret;
  320. if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
  321. return ret;
  322. return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
  323. }
  324. int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
  325. unsigned char *dest, int dest_size)
  326. {
  327. int i, j, nb_planes = 0, linesizes[4];
  328. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  329. int size = avpicture_get_size(pix_fmt, width, height);
  330. if (size > dest_size || size < 0)
  331. return AVERROR(EINVAL);
  332. for (i = 0; i < desc->nb_components; i++)
  333. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  334. nb_planes++;
  335. av_image_fill_linesizes(linesizes, pix_fmt, width);
  336. for (i = 0; i < nb_planes; i++) {
  337. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  338. const unsigned char *s = src->data[i];
  339. h = (height + (1 << shift) - 1) >> shift;
  340. for (j = 0; j < h; j++) {
  341. memcpy(dest, s, linesizes[i]);
  342. dest += linesizes[i];
  343. s += src->linesize[i];
  344. }
  345. }
  346. if (desc->flags & PIX_FMT_PAL)
  347. memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
  348. return size;
  349. }
  350. int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
  351. {
  352. AVPicture dummy_pict;
  353. if(av_image_check_size(width, height, 0, NULL))
  354. return -1;
  355. switch (pix_fmt) {
  356. case PIX_FMT_RGB8:
  357. case PIX_FMT_BGR8:
  358. case PIX_FMT_RGB4_BYTE:
  359. case PIX_FMT_BGR4_BYTE:
  360. case PIX_FMT_GRAY8:
  361. // do not include palette for these pseudo-paletted formats
  362. return width * height;
  363. }
  364. return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
  365. }
  366. int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
  367. int has_alpha)
  368. {
  369. const PixFmtInfo *pf, *ps;
  370. const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
  371. const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
  372. int loss;
  373. ps = &pix_fmt_info[src_pix_fmt];
  374. /* compute loss */
  375. loss = 0;
  376. pf = &pix_fmt_info[dst_pix_fmt];
  377. if (pf->depth < ps->depth ||
  378. ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
  379. dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
  380. (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
  381. src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
  382. loss |= FF_LOSS_DEPTH;
  383. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  384. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  385. loss |= FF_LOSS_RESOLUTION;
  386. switch(pf->color_type) {
  387. case FF_COLOR_RGB:
  388. if (ps->color_type != FF_COLOR_RGB &&
  389. ps->color_type != FF_COLOR_GRAY)
  390. loss |= FF_LOSS_COLORSPACE;
  391. break;
  392. case FF_COLOR_GRAY:
  393. if (ps->color_type != FF_COLOR_GRAY)
  394. loss |= FF_LOSS_COLORSPACE;
  395. break;
  396. case FF_COLOR_YUV:
  397. if (ps->color_type != FF_COLOR_YUV)
  398. loss |= FF_LOSS_COLORSPACE;
  399. break;
  400. case FF_COLOR_YUV_JPEG:
  401. if (ps->color_type != FF_COLOR_YUV_JPEG &&
  402. ps->color_type != FF_COLOR_YUV &&
  403. ps->color_type != FF_COLOR_GRAY)
  404. loss |= FF_LOSS_COLORSPACE;
  405. break;
  406. default:
  407. /* fail safe test */
  408. if (ps->color_type != pf->color_type)
  409. loss |= FF_LOSS_COLORSPACE;
  410. break;
  411. }
  412. if (pf->color_type == FF_COLOR_GRAY &&
  413. ps->color_type != FF_COLOR_GRAY)
  414. loss |= FF_LOSS_CHROMA;
  415. if (!pf->is_alpha && (ps->is_alpha && has_alpha))
  416. loss |= FF_LOSS_ALPHA;
  417. if (dst_pix_fmt == PIX_FMT_PAL8 &&
  418. (src_pix_fmt != PIX_FMT_PAL8 && ps->color_type != FF_COLOR_GRAY))
  419. loss |= FF_LOSS_COLORQUANT;
  420. return loss;
  421. }
  422. static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
  423. {
  424. const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
  425. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  426. return info->padded_size ?
  427. info->padded_size : av_get_bits_per_pixel(desc);
  428. }
  429. static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
  430. enum PixelFormat src_pix_fmt,
  431. int has_alpha,
  432. int loss_mask)
  433. {
  434. int dist, i, loss, min_dist;
  435. enum PixelFormat dst_pix_fmt;
  436. /* find exact color match with smallest size */
  437. dst_pix_fmt = PIX_FMT_NONE;
  438. min_dist = 0x7fffffff;
  439. for(i = 0;i < PIX_FMT_NB; i++) {
  440. if (pix_fmt_mask & (1ULL << i)) {
  441. loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
  442. if (loss == 0) {
  443. dist = avg_bits_per_pixel(i);
  444. if (dist < min_dist) {
  445. min_dist = dist;
  446. dst_pix_fmt = i;
  447. }
  448. }
  449. }
  450. }
  451. return dst_pix_fmt;
  452. }
  453. enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
  454. int has_alpha, int *loss_ptr)
  455. {
  456. enum PixelFormat dst_pix_fmt;
  457. int loss_mask, i;
  458. static const int loss_mask_order[] = {
  459. ~0, /* no loss first */
  460. ~FF_LOSS_ALPHA,
  461. ~FF_LOSS_RESOLUTION,
  462. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  463. ~FF_LOSS_COLORQUANT,
  464. ~FF_LOSS_DEPTH,
  465. 0,
  466. };
  467. /* try with successive loss */
  468. i = 0;
  469. for(;;) {
  470. loss_mask = loss_mask_order[i++];
  471. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
  472. has_alpha, loss_mask);
  473. if (dst_pix_fmt >= 0)
  474. goto found;
  475. if (loss_mask == 0)
  476. break;
  477. }
  478. return PIX_FMT_NONE;
  479. found:
  480. if (loss_ptr)
  481. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  482. return dst_pix_fmt;
  483. }
  484. void av_picture_copy(AVPicture *dst, const AVPicture *src,
  485. enum PixelFormat pix_fmt, int width, int height)
  486. {
  487. av_image_copy(dst->data, dst->linesize, src->data,
  488. src->linesize, pix_fmt, width, height);
  489. }
  490. /* 2x2 -> 1x1 */
  491. void ff_shrink22(uint8_t *dst, int dst_wrap,
  492. const uint8_t *src, int src_wrap,
  493. int width, int height)
  494. {
  495. int w;
  496. const uint8_t *s1, *s2;
  497. uint8_t *d;
  498. for(;height > 0; height--) {
  499. s1 = src;
  500. s2 = s1 + src_wrap;
  501. d = dst;
  502. for(w = width;w >= 4; w-=4) {
  503. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  504. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  505. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  506. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  507. s1 += 8;
  508. s2 += 8;
  509. d += 4;
  510. }
  511. for(;w > 0; w--) {
  512. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  513. s1 += 2;
  514. s2 += 2;
  515. d++;
  516. }
  517. src += 2 * src_wrap;
  518. dst += dst_wrap;
  519. }
  520. }
  521. /* 4x4 -> 1x1 */
  522. void ff_shrink44(uint8_t *dst, int dst_wrap,
  523. const uint8_t *src, int src_wrap,
  524. int width, int height)
  525. {
  526. int w;
  527. const uint8_t *s1, *s2, *s3, *s4;
  528. uint8_t *d;
  529. for(;height > 0; height--) {
  530. s1 = src;
  531. s2 = s1 + src_wrap;
  532. s3 = s2 + src_wrap;
  533. s4 = s3 + src_wrap;
  534. d = dst;
  535. for(w = width;w > 0; w--) {
  536. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  537. s2[0] + s2[1] + s2[2] + s2[3] +
  538. s3[0] + s3[1] + s3[2] + s3[3] +
  539. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  540. s1 += 4;
  541. s2 += 4;
  542. s3 += 4;
  543. s4 += 4;
  544. d++;
  545. }
  546. src += 4 * src_wrap;
  547. dst += dst_wrap;
  548. }
  549. }
  550. /* 8x8 -> 1x1 */
  551. void ff_shrink88(uint8_t *dst, int dst_wrap,
  552. const uint8_t *src, int src_wrap,
  553. int width, int height)
  554. {
  555. int w, i;
  556. for(;height > 0; height--) {
  557. for(w = width;w > 0; w--) {
  558. int tmp=0;
  559. for(i=0; i<8; i++){
  560. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  561. src += src_wrap;
  562. }
  563. *(dst++) = (tmp + 32)>>6;
  564. src += 8 - 8*src_wrap;
  565. }
  566. src += 8*src_wrap - 8*width;
  567. dst += dst_wrap - width;
  568. }
  569. }
  570. int avpicture_alloc(AVPicture *picture,
  571. enum PixelFormat pix_fmt, int width, int height)
  572. {
  573. int ret;
  574. if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
  575. memset(picture, 0, sizeof(AVPicture));
  576. return ret;
  577. }
  578. return 0;
  579. }
  580. void avpicture_free(AVPicture *picture)
  581. {
  582. av_free(picture->data[0]);
  583. }
  584. /* return true if yuv planar */
  585. static inline int is_yuv_planar(enum PixelFormat fmt)
  586. {
  587. const PixFmtInfo *info = &pix_fmt_info[fmt];
  588. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
  589. int i;
  590. int planes[4] = { 0 };
  591. if (info->color_type != FF_COLOR_YUV &&
  592. info->color_type != FF_COLOR_YUV_JPEG)
  593. return 0;
  594. /* set the used planes */
  595. for (i = 0; i < desc->nb_components; i++)
  596. planes[desc->comp[i].plane] = 1;
  597. /* if there is an unused plane, the format is not planar */
  598. for (i = 0; i < desc->nb_components; i++)
  599. if (!planes[i])
  600. return 0;
  601. return 1;
  602. }
  603. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  604. enum PixelFormat pix_fmt, int top_band, int left_band)
  605. {
  606. int y_shift;
  607. int x_shift;
  608. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
  609. return -1;
  610. y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
  611. x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
  612. if (is_yuv_planar(pix_fmt)) {
  613. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  614. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  615. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  616. } else{
  617. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  618. return -1;
  619. if(left_band) //FIXME add support for this too
  620. return -1;
  621. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  622. }
  623. dst->linesize[0] = src->linesize[0];
  624. dst->linesize[1] = src->linesize[1];
  625. dst->linesize[2] = src->linesize[2];
  626. return 0;
  627. }
  628. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  629. enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  630. int *color)
  631. {
  632. uint8_t *optr;
  633. int y_shift;
  634. int x_shift;
  635. int yheight;
  636. int i, y;
  637. if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
  638. !is_yuv_planar(pix_fmt)) return -1;
  639. for (i = 0; i < 3; i++) {
  640. x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
  641. y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
  642. if (padtop || padleft) {
  643. memset(dst->data[i], color[i],
  644. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  645. }
  646. if (padleft || padright) {
  647. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  648. (dst->linesize[i] - (padright >> x_shift));
  649. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  650. for (y = 0; y < yheight; y++) {
  651. memset(optr, color[i], (padleft + padright) >> x_shift);
  652. optr += dst->linesize[i];
  653. }
  654. }
  655. if (src) { /* first line */
  656. uint8_t *iptr = src->data[i];
  657. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  658. (padleft >> x_shift);
  659. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  660. iptr += src->linesize[i];
  661. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  662. (dst->linesize[i] - (padright >> x_shift));
  663. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  664. for (y = 0; y < yheight; y++) {
  665. memset(optr, color[i], (padleft + padright) >> x_shift);
  666. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  667. (width - padleft - padright) >> x_shift);
  668. iptr += src->linesize[i];
  669. optr += dst->linesize[i];
  670. }
  671. }
  672. if (padbottom || padright) {
  673. optr = dst->data[i] + dst->linesize[i] *
  674. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  675. memset(optr, color[i],dst->linesize[i] *
  676. (padbottom >> y_shift) + (padright >> x_shift));
  677. }
  678. }
  679. return 0;
  680. }
  681. /* NOTE: we scan all the pixels to have an exact information */
  682. static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
  683. {
  684. const unsigned char *p;
  685. int src_wrap, ret, x, y;
  686. unsigned int a;
  687. uint32_t *palette = (uint32_t *)src->data[1];
  688. p = src->data[0];
  689. src_wrap = src->linesize[0] - width;
  690. ret = 0;
  691. for(y=0;y<height;y++) {
  692. for(x=0;x<width;x++) {
  693. a = palette[p[0]] >> 24;
  694. if (a == 0x00) {
  695. ret |= FF_ALPHA_TRANSP;
  696. } else if (a != 0xff) {
  697. ret |= FF_ALPHA_SEMI_TRANSP;
  698. }
  699. p++;
  700. }
  701. p += src_wrap;
  702. }
  703. return ret;
  704. }
  705. int img_get_alpha_info(const AVPicture *src,
  706. enum PixelFormat pix_fmt, int width, int height)
  707. {
  708. const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
  709. int ret;
  710. /* no alpha can be represented in format */
  711. if (!pf->is_alpha)
  712. return 0;
  713. switch(pix_fmt) {
  714. case PIX_FMT_PAL8:
  715. ret = get_alpha_info_pal8(src, width, height);
  716. break;
  717. default:
  718. /* we do not know, so everything is indicated */
  719. ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
  720. break;
  721. }
  722. return ret;
  723. }
  724. #if !(HAVE_MMX && HAVE_YASM)
  725. /* filter parameters: [-1 4 2 4 -1] // 8 */
  726. static void deinterlace_line_c(uint8_t *dst,
  727. const uint8_t *lum_m4, const uint8_t *lum_m3,
  728. const uint8_t *lum_m2, const uint8_t *lum_m1,
  729. const uint8_t *lum,
  730. int size)
  731. {
  732. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  733. int sum;
  734. for(;size > 0;size--) {
  735. sum = -lum_m4[0];
  736. sum += lum_m3[0] << 2;
  737. sum += lum_m2[0] << 1;
  738. sum += lum_m1[0] << 2;
  739. sum += -lum[0];
  740. dst[0] = cm[(sum + 4) >> 3];
  741. lum_m4++;
  742. lum_m3++;
  743. lum_m2++;
  744. lum_m1++;
  745. lum++;
  746. dst++;
  747. }
  748. }
  749. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  750. uint8_t *lum_m2, uint8_t *lum_m1,
  751. uint8_t *lum, int size)
  752. {
  753. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  754. int sum;
  755. for(;size > 0;size--) {
  756. sum = -lum_m4[0];
  757. sum += lum_m3[0] << 2;
  758. sum += lum_m2[0] << 1;
  759. lum_m4[0]=lum_m2[0];
  760. sum += lum_m1[0] << 2;
  761. sum += -lum[0];
  762. lum_m2[0] = cm[(sum + 4) >> 3];
  763. lum_m4++;
  764. lum_m3++;
  765. lum_m2++;
  766. lum_m1++;
  767. lum++;
  768. }
  769. }
  770. #endif
  771. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  772. top field is copied as is, but the bottom field is deinterlaced
  773. against the top field. */
  774. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  775. const uint8_t *src1, int src_wrap,
  776. int width, int height)
  777. {
  778. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  779. int y;
  780. src_m2 = src1;
  781. src_m1 = src1;
  782. src_0=&src_m1[src_wrap];
  783. src_p1=&src_0[src_wrap];
  784. src_p2=&src_p1[src_wrap];
  785. for(y=0;y<(height-2);y+=2) {
  786. memcpy(dst,src_m1,width);
  787. dst += dst_wrap;
  788. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  789. src_m2 = src_0;
  790. src_m1 = src_p1;
  791. src_0 = src_p2;
  792. src_p1 += 2*src_wrap;
  793. src_p2 += 2*src_wrap;
  794. dst += dst_wrap;
  795. }
  796. memcpy(dst,src_m1,width);
  797. dst += dst_wrap;
  798. /* do last line */
  799. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  800. }
  801. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  802. int width, int height)
  803. {
  804. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  805. int y;
  806. uint8_t *buf;
  807. buf = (uint8_t*)av_malloc(width);
  808. src_m1 = src1;
  809. memcpy(buf,src_m1,width);
  810. src_0=&src_m1[src_wrap];
  811. src_p1=&src_0[src_wrap];
  812. src_p2=&src_p1[src_wrap];
  813. for(y=0;y<(height-2);y+=2) {
  814. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  815. src_m1 = src_p1;
  816. src_0 = src_p2;
  817. src_p1 += 2*src_wrap;
  818. src_p2 += 2*src_wrap;
  819. }
  820. /* do last line */
  821. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  822. av_free(buf);
  823. }
  824. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  825. enum PixelFormat pix_fmt, int width, int height)
  826. {
  827. int i;
  828. if (pix_fmt != PIX_FMT_YUV420P &&
  829. pix_fmt != PIX_FMT_YUVJ420P &&
  830. pix_fmt != PIX_FMT_YUV422P &&
  831. pix_fmt != PIX_FMT_YUVJ422P &&
  832. pix_fmt != PIX_FMT_YUV444P &&
  833. pix_fmt != PIX_FMT_YUV411P &&
  834. pix_fmt != PIX_FMT_GRAY8)
  835. return -1;
  836. if ((width & 3) != 0 || (height & 3) != 0)
  837. return -1;
  838. for(i=0;i<3;i++) {
  839. if (i == 1) {
  840. switch(pix_fmt) {
  841. case PIX_FMT_YUVJ420P:
  842. case PIX_FMT_YUV420P:
  843. width >>= 1;
  844. height >>= 1;
  845. break;
  846. case PIX_FMT_YUV422P:
  847. case PIX_FMT_YUVJ422P:
  848. width >>= 1;
  849. break;
  850. case PIX_FMT_YUV411P:
  851. width >>= 2;
  852. break;
  853. default:
  854. break;
  855. }
  856. if (pix_fmt == PIX_FMT_GRAY8) {
  857. break;
  858. }
  859. }
  860. if (src == dst) {
  861. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  862. width, height);
  863. } else {
  864. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  865. src->data[i], src->linesize[i],
  866. width, height);
  867. }
  868. }
  869. emms_c();
  870. return 0;
  871. }