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.

526 lines
16KB

  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavutil/colorspace.h"
  34. #include "libavutil/common.h"
  35. #include "libavutil/pixdesc.h"
  36. #include "libavutil/imgutils.h"
  37. #if HAVE_MMX_EXTERNAL
  38. #include "x86/dsputil_mmx.h"
  39. #endif
  40. #if HAVE_MMX_EXTERNAL
  41. #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
  42. #define deinterlace_line ff_deinterlace_line_mmx
  43. #else
  44. #define deinterlace_line_inplace deinterlace_line_inplace_c
  45. #define deinterlace_line deinterlace_line_c
  46. #endif
  47. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  48. {
  49. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  50. *h_shift = desc->log2_chroma_w;
  51. *v_shift = desc->log2_chroma_h;
  52. }
  53. static int is_gray(const AVPixFmtDescriptor *desc)
  54. {
  55. return desc->nb_components - (desc->flags & PIX_FMT_ALPHA) == 1;
  56. }
  57. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
  58. enum AVPixelFormat src_pix_fmt,
  59. int has_alpha)
  60. {
  61. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  62. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  63. int loss, i, nb_components = FFMIN(src_desc->nb_components,
  64. dst_desc->nb_components);
  65. /* compute loss */
  66. loss = 0;
  67. if (dst_pix_fmt == src_pix_fmt)
  68. return 0;
  69. for (i = 0; i < nb_components; i++)
  70. if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1)
  71. loss |= FF_LOSS_DEPTH;
  72. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  73. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  74. loss |= FF_LOSS_RESOLUTION;
  75. if ((src_desc->flags & PIX_FMT_RGB) != (dst_desc->flags & PIX_FMT_RGB))
  76. loss |= FF_LOSS_COLORSPACE;
  77. if (has_alpha && !(dst_desc->flags & PIX_FMT_ALPHA) &&
  78. (dst_desc->flags & PIX_FMT_ALPHA))
  79. loss |= FF_LOSS_ALPHA;
  80. if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
  81. return loss | FF_LOSS_COLORQUANT;
  82. if (src_desc->nb_components > dst_desc->nb_components)
  83. if (is_gray(dst_desc))
  84. loss |= FF_LOSS_CHROMA;
  85. return loss;
  86. }
  87. static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
  88. enum AVPixelFormat src_pix_fmt,
  89. int has_alpha,
  90. int loss_mask)
  91. {
  92. int dist, i, loss, min_dist;
  93. enum AVPixelFormat dst_pix_fmt;
  94. /* find exact color match with smallest size */
  95. dst_pix_fmt = AV_PIX_FMT_NONE;
  96. min_dist = 0x7fffffff;
  97. i = 0;
  98. while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
  99. enum AVPixelFormat pix_fmt = pix_fmt_list[i];
  100. if (i > AV_PIX_FMT_NB) {
  101. av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
  102. "it is either not properly terminated or contains duplicates\n");
  103. return AV_PIX_FMT_NONE;
  104. }
  105. loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
  106. if (loss == 0) {
  107. dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
  108. if (dist < min_dist) {
  109. min_dist = dist;
  110. dst_pix_fmt = pix_fmt;
  111. }
  112. }
  113. i++;
  114. }
  115. return dst_pix_fmt;
  116. }
  117. #if FF_API_FIND_BEST_PIX_FMT
  118. enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
  119. int has_alpha, int *loss_ptr)
  120. {
  121. enum AVPixelFormat list[64];
  122. int i, j = 0;
  123. // test only the first 64 pixel formats to avoid undefined behaviour
  124. for (i = 0; i < 64; i++) {
  125. if (pix_fmt_mask & (1ULL << i))
  126. list[j++] = i;
  127. }
  128. list[j] = AV_PIX_FMT_NONE;
  129. return avcodec_find_best_pix_fmt2(list, src_pix_fmt, has_alpha, loss_ptr);
  130. }
  131. #endif /* FF_API_FIND_BEST_PIX_FMT */
  132. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  133. enum AVPixelFormat src_pix_fmt,
  134. int has_alpha, int *loss_ptr)
  135. {
  136. enum AVPixelFormat dst_pix_fmt;
  137. int loss_mask, i;
  138. static const int loss_mask_order[] = {
  139. ~0, /* no loss first */
  140. ~FF_LOSS_ALPHA,
  141. ~FF_LOSS_RESOLUTION,
  142. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  143. ~FF_LOSS_COLORQUANT,
  144. ~FF_LOSS_DEPTH,
  145. 0,
  146. };
  147. /* try with successive loss */
  148. i = 0;
  149. for(;;) {
  150. loss_mask = loss_mask_order[i++];
  151. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
  152. has_alpha, loss_mask);
  153. if (dst_pix_fmt >= 0)
  154. goto found;
  155. if (loss_mask == 0)
  156. break;
  157. }
  158. return AV_PIX_FMT_NONE;
  159. found:
  160. if (loss_ptr)
  161. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  162. return dst_pix_fmt;
  163. }
  164. /* 2x2 -> 1x1 */
  165. void ff_shrink22(uint8_t *dst, int dst_wrap,
  166. const uint8_t *src, int src_wrap,
  167. int width, int height)
  168. {
  169. int w;
  170. const uint8_t *s1, *s2;
  171. uint8_t *d;
  172. for(;height > 0; height--) {
  173. s1 = src;
  174. s2 = s1 + src_wrap;
  175. d = dst;
  176. for(w = width;w >= 4; w-=4) {
  177. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  178. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  179. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  180. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  181. s1 += 8;
  182. s2 += 8;
  183. d += 4;
  184. }
  185. for(;w > 0; w--) {
  186. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  187. s1 += 2;
  188. s2 += 2;
  189. d++;
  190. }
  191. src += 2 * src_wrap;
  192. dst += dst_wrap;
  193. }
  194. }
  195. /* 4x4 -> 1x1 */
  196. void ff_shrink44(uint8_t *dst, int dst_wrap,
  197. const uint8_t *src, int src_wrap,
  198. int width, int height)
  199. {
  200. int w;
  201. const uint8_t *s1, *s2, *s3, *s4;
  202. uint8_t *d;
  203. for(;height > 0; height--) {
  204. s1 = src;
  205. s2 = s1 + src_wrap;
  206. s3 = s2 + src_wrap;
  207. s4 = s3 + src_wrap;
  208. d = dst;
  209. for(w = width;w > 0; w--) {
  210. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  211. s2[0] + s2[1] + s2[2] + s2[3] +
  212. s3[0] + s3[1] + s3[2] + s3[3] +
  213. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  214. s1 += 4;
  215. s2 += 4;
  216. s3 += 4;
  217. s4 += 4;
  218. d++;
  219. }
  220. src += 4 * src_wrap;
  221. dst += dst_wrap;
  222. }
  223. }
  224. /* 8x8 -> 1x1 */
  225. void ff_shrink88(uint8_t *dst, int dst_wrap,
  226. const uint8_t *src, int src_wrap,
  227. int width, int height)
  228. {
  229. int w, i;
  230. for(;height > 0; height--) {
  231. for(w = width;w > 0; w--) {
  232. int tmp=0;
  233. for(i=0; i<8; i++){
  234. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  235. src += src_wrap;
  236. }
  237. *(dst++) = (tmp + 32)>>6;
  238. src += 8 - 8*src_wrap;
  239. }
  240. src += 8*src_wrap - 8*width;
  241. dst += dst_wrap - width;
  242. }
  243. }
  244. /* return true if yuv planar */
  245. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  246. {
  247. return (!(desc->flags & PIX_FMT_RGB) &&
  248. (desc->flags & PIX_FMT_PLANAR));
  249. }
  250. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  251. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  252. {
  253. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  254. int y_shift;
  255. int x_shift;
  256. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
  257. return -1;
  258. y_shift = desc->log2_chroma_h;
  259. x_shift = desc->log2_chroma_w;
  260. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  261. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  262. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  263. dst->linesize[0] = src->linesize[0];
  264. dst->linesize[1] = src->linesize[1];
  265. dst->linesize[2] = src->linesize[2];
  266. return 0;
  267. }
  268. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  269. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  270. int *color)
  271. {
  272. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  273. uint8_t *optr;
  274. int y_shift;
  275. int x_shift;
  276. int yheight;
  277. int i, y;
  278. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  279. !is_yuv_planar(desc)) return -1;
  280. for (i = 0; i < 3; i++) {
  281. x_shift = i ? desc->log2_chroma_w : 0;
  282. y_shift = i ? desc->log2_chroma_h : 0;
  283. if (padtop || padleft) {
  284. memset(dst->data[i], color[i],
  285. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  286. }
  287. if (padleft || padright) {
  288. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  289. (dst->linesize[i] - (padright >> x_shift));
  290. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  291. for (y = 0; y < yheight; y++) {
  292. memset(optr, color[i], (padleft + padright) >> x_shift);
  293. optr += dst->linesize[i];
  294. }
  295. }
  296. if (src) { /* first line */
  297. uint8_t *iptr = src->data[i];
  298. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  299. (padleft >> x_shift);
  300. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  301. iptr += src->linesize[i];
  302. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  303. (dst->linesize[i] - (padright >> x_shift));
  304. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  305. for (y = 0; y < yheight; y++) {
  306. memset(optr, color[i], (padleft + padright) >> x_shift);
  307. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  308. (width - padleft - padright) >> x_shift);
  309. iptr += src->linesize[i];
  310. optr += dst->linesize[i];
  311. }
  312. }
  313. if (padbottom || padright) {
  314. optr = dst->data[i] + dst->linesize[i] *
  315. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  316. memset(optr, color[i],dst->linesize[i] *
  317. (padbottom >> y_shift) + (padright >> x_shift));
  318. }
  319. }
  320. return 0;
  321. }
  322. #if !HAVE_MMX_EXTERNAL
  323. /* filter parameters: [-1 4 2 4 -1] // 8 */
  324. static void deinterlace_line_c(uint8_t *dst,
  325. const uint8_t *lum_m4, const uint8_t *lum_m3,
  326. const uint8_t *lum_m2, const uint8_t *lum_m1,
  327. const uint8_t *lum,
  328. int size)
  329. {
  330. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  331. int sum;
  332. for(;size > 0;size--) {
  333. sum = -lum_m4[0];
  334. sum += lum_m3[0] << 2;
  335. sum += lum_m2[0] << 1;
  336. sum += lum_m1[0] << 2;
  337. sum += -lum[0];
  338. dst[0] = cm[(sum + 4) >> 3];
  339. lum_m4++;
  340. lum_m3++;
  341. lum_m2++;
  342. lum_m1++;
  343. lum++;
  344. dst++;
  345. }
  346. }
  347. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  348. uint8_t *lum_m2, uint8_t *lum_m1,
  349. uint8_t *lum, int size)
  350. {
  351. uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  352. int sum;
  353. for(;size > 0;size--) {
  354. sum = -lum_m4[0];
  355. sum += lum_m3[0] << 2;
  356. sum += lum_m2[0] << 1;
  357. lum_m4[0]=lum_m2[0];
  358. sum += lum_m1[0] << 2;
  359. sum += -lum[0];
  360. lum_m2[0] = cm[(sum + 4) >> 3];
  361. lum_m4++;
  362. lum_m3++;
  363. lum_m2++;
  364. lum_m1++;
  365. lum++;
  366. }
  367. }
  368. #endif /* !HAVE_MMX_EXTERNAL */
  369. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  370. top field is copied as is, but the bottom field is deinterlaced
  371. against the top field. */
  372. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  373. const uint8_t *src1, int src_wrap,
  374. int width, int height)
  375. {
  376. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  377. int y;
  378. src_m2 = src1;
  379. src_m1 = src1;
  380. src_0=&src_m1[src_wrap];
  381. src_p1=&src_0[src_wrap];
  382. src_p2=&src_p1[src_wrap];
  383. for(y=0;y<(height-2);y+=2) {
  384. memcpy(dst,src_m1,width);
  385. dst += dst_wrap;
  386. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  387. src_m2 = src_0;
  388. src_m1 = src_p1;
  389. src_0 = src_p2;
  390. src_p1 += 2*src_wrap;
  391. src_p2 += 2*src_wrap;
  392. dst += dst_wrap;
  393. }
  394. memcpy(dst,src_m1,width);
  395. dst += dst_wrap;
  396. /* do last line */
  397. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  398. }
  399. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  400. int width, int height)
  401. {
  402. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  403. int y;
  404. uint8_t *buf;
  405. buf = av_malloc(width);
  406. src_m1 = src1;
  407. memcpy(buf,src_m1,width);
  408. src_0=&src_m1[src_wrap];
  409. src_p1=&src_0[src_wrap];
  410. src_p2=&src_p1[src_wrap];
  411. for(y=0;y<(height-2);y+=2) {
  412. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  413. src_m1 = src_p1;
  414. src_0 = src_p2;
  415. src_p1 += 2*src_wrap;
  416. src_p2 += 2*src_wrap;
  417. }
  418. /* do last line */
  419. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  420. av_free(buf);
  421. }
  422. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  423. enum AVPixelFormat pix_fmt, int width, int height)
  424. {
  425. int i;
  426. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  427. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  428. pix_fmt != AV_PIX_FMT_YUV422P &&
  429. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  430. pix_fmt != AV_PIX_FMT_YUV444P &&
  431. pix_fmt != AV_PIX_FMT_YUV411P &&
  432. pix_fmt != AV_PIX_FMT_GRAY8)
  433. return -1;
  434. if ((width & 3) != 0 || (height & 3) != 0)
  435. return -1;
  436. for(i=0;i<3;i++) {
  437. if (i == 1) {
  438. switch(pix_fmt) {
  439. case AV_PIX_FMT_YUVJ420P:
  440. case AV_PIX_FMT_YUV420P:
  441. width >>= 1;
  442. height >>= 1;
  443. break;
  444. case AV_PIX_FMT_YUV422P:
  445. case AV_PIX_FMT_YUVJ422P:
  446. width >>= 1;
  447. break;
  448. case AV_PIX_FMT_YUV411P:
  449. width >>= 2;
  450. break;
  451. default:
  452. break;
  453. }
  454. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  455. break;
  456. }
  457. }
  458. if (src == dst) {
  459. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  460. width, height);
  461. } else {
  462. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  463. src->data[i], src->linesize[i],
  464. width, height);
  465. }
  466. }
  467. emms_c();
  468. return 0;
  469. }