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.

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