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.

513 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_x86.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 & AV_PIX_FMT_FLAG_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 & AV_PIX_FMT_FLAG_RGB) != (dst_desc->flags & AV_PIX_FMT_FLAG_RGB))
  77. loss |= FF_LOSS_COLORSPACE;
  78. if (has_alpha && !(dst_desc->flags & AV_PIX_FMT_FLAG_ALPHA) &&
  79. (dst_desc->flags & AV_PIX_FMT_FLAG_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. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  119. enum AVPixelFormat src_pix_fmt,
  120. int has_alpha, int *loss_ptr)
  121. {
  122. enum AVPixelFormat dst_pix_fmt;
  123. int loss_mask, i;
  124. static const int loss_mask_order[] = {
  125. ~0, /* no loss first */
  126. ~FF_LOSS_ALPHA,
  127. ~FF_LOSS_RESOLUTION,
  128. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  129. ~FF_LOSS_COLORQUANT,
  130. ~FF_LOSS_DEPTH,
  131. 0,
  132. };
  133. /* try with successive loss */
  134. i = 0;
  135. for(;;) {
  136. loss_mask = loss_mask_order[i++];
  137. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
  138. has_alpha, loss_mask);
  139. if (dst_pix_fmt >= 0)
  140. goto found;
  141. if (loss_mask == 0)
  142. break;
  143. }
  144. return AV_PIX_FMT_NONE;
  145. found:
  146. if (loss_ptr)
  147. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  148. return dst_pix_fmt;
  149. }
  150. /* 2x2 -> 1x1 */
  151. void ff_shrink22(uint8_t *dst, int dst_wrap,
  152. const uint8_t *src, int src_wrap,
  153. int width, int height)
  154. {
  155. int w;
  156. const uint8_t *s1, *s2;
  157. uint8_t *d;
  158. for(;height > 0; height--) {
  159. s1 = src;
  160. s2 = s1 + src_wrap;
  161. d = dst;
  162. for(w = width;w >= 4; w-=4) {
  163. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  164. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  165. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  166. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  167. s1 += 8;
  168. s2 += 8;
  169. d += 4;
  170. }
  171. for(;w > 0; w--) {
  172. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  173. s1 += 2;
  174. s2 += 2;
  175. d++;
  176. }
  177. src += 2 * src_wrap;
  178. dst += dst_wrap;
  179. }
  180. }
  181. /* 4x4 -> 1x1 */
  182. void ff_shrink44(uint8_t *dst, int dst_wrap,
  183. const uint8_t *src, int src_wrap,
  184. int width, int height)
  185. {
  186. int w;
  187. const uint8_t *s1, *s2, *s3, *s4;
  188. uint8_t *d;
  189. for(;height > 0; height--) {
  190. s1 = src;
  191. s2 = s1 + src_wrap;
  192. s3 = s2 + src_wrap;
  193. s4 = s3 + src_wrap;
  194. d = dst;
  195. for(w = width;w > 0; w--) {
  196. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  197. s2[0] + s2[1] + s2[2] + s2[3] +
  198. s3[0] + s3[1] + s3[2] + s3[3] +
  199. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  200. s1 += 4;
  201. s2 += 4;
  202. s3 += 4;
  203. s4 += 4;
  204. d++;
  205. }
  206. src += 4 * src_wrap;
  207. dst += dst_wrap;
  208. }
  209. }
  210. /* 8x8 -> 1x1 */
  211. void ff_shrink88(uint8_t *dst, int dst_wrap,
  212. const uint8_t *src, int src_wrap,
  213. int width, int height)
  214. {
  215. int w, i;
  216. for(;height > 0; height--) {
  217. for(w = width;w > 0; w--) {
  218. int tmp=0;
  219. for(i=0; i<8; i++){
  220. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  221. src += src_wrap;
  222. }
  223. *(dst++) = (tmp + 32)>>6;
  224. src += 8 - 8*src_wrap;
  225. }
  226. src += 8*src_wrap - 8*width;
  227. dst += dst_wrap - width;
  228. }
  229. }
  230. /* return true if yuv planar */
  231. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  232. {
  233. return (!(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  234. (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  235. }
  236. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  237. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  238. {
  239. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  240. int y_shift;
  241. int x_shift;
  242. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
  243. return -1;
  244. y_shift = desc->log2_chroma_h;
  245. x_shift = desc->log2_chroma_w;
  246. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  247. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  248. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  249. dst->linesize[0] = src->linesize[0];
  250. dst->linesize[1] = src->linesize[1];
  251. dst->linesize[2] = src->linesize[2];
  252. return 0;
  253. }
  254. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  255. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  256. int *color)
  257. {
  258. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  259. uint8_t *optr;
  260. int y_shift;
  261. int x_shift;
  262. int yheight;
  263. int i, y;
  264. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  265. !is_yuv_planar(desc)) return -1;
  266. for (i = 0; i < 3; i++) {
  267. x_shift = i ? desc->log2_chroma_w : 0;
  268. y_shift = i ? desc->log2_chroma_h : 0;
  269. if (padtop || padleft) {
  270. memset(dst->data[i], color[i],
  271. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  272. }
  273. if (padleft || padright) {
  274. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  275. (dst->linesize[i] - (padright >> x_shift));
  276. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  277. for (y = 0; y < yheight; y++) {
  278. memset(optr, color[i], (padleft + padright) >> x_shift);
  279. optr += dst->linesize[i];
  280. }
  281. }
  282. if (src) { /* first line */
  283. uint8_t *iptr = src->data[i];
  284. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  285. (padleft >> x_shift);
  286. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  287. iptr += src->linesize[i];
  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. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  294. (width - padleft - padright) >> x_shift);
  295. iptr += src->linesize[i];
  296. optr += dst->linesize[i];
  297. }
  298. }
  299. if (padbottom || padright) {
  300. optr = dst->data[i] + dst->linesize[i] *
  301. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  302. memset(optr, color[i],dst->linesize[i] *
  303. (padbottom >> y_shift) + (padright >> x_shift));
  304. }
  305. }
  306. return 0;
  307. }
  308. #if FF_API_DEINTERLACE
  309. #if !HAVE_MMX_EXTERNAL
  310. /* filter parameters: [-1 4 2 4 -1] // 8 */
  311. static void deinterlace_line_c(uint8_t *dst,
  312. const uint8_t *lum_m4, const uint8_t *lum_m3,
  313. const uint8_t *lum_m2, const uint8_t *lum_m1,
  314. const uint8_t *lum,
  315. int size)
  316. {
  317. const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  318. int sum;
  319. for(;size > 0;size--) {
  320. sum = -lum_m4[0];
  321. sum += lum_m3[0] << 2;
  322. sum += lum_m2[0] << 1;
  323. sum += lum_m1[0] << 2;
  324. sum += -lum[0];
  325. dst[0] = cm[(sum + 4) >> 3];
  326. lum_m4++;
  327. lum_m3++;
  328. lum_m2++;
  329. lum_m1++;
  330. lum++;
  331. dst++;
  332. }
  333. }
  334. static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
  335. uint8_t *lum_m2, uint8_t *lum_m1,
  336. uint8_t *lum, int size)
  337. {
  338. const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
  339. int sum;
  340. for(;size > 0;size--) {
  341. sum = -lum_m4[0];
  342. sum += lum_m3[0] << 2;
  343. sum += lum_m2[0] << 1;
  344. lum_m4[0]=lum_m2[0];
  345. sum += lum_m1[0] << 2;
  346. sum += -lum[0];
  347. lum_m2[0] = cm[(sum + 4) >> 3];
  348. lum_m4++;
  349. lum_m3++;
  350. lum_m2++;
  351. lum_m1++;
  352. lum++;
  353. }
  354. }
  355. #endif /* !HAVE_MMX_EXTERNAL */
  356. /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
  357. top field is copied as is, but the bottom field is deinterlaced
  358. against the top field. */
  359. static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
  360. const uint8_t *src1, int src_wrap,
  361. int width, int height)
  362. {
  363. const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
  364. int y;
  365. src_m2 = src1;
  366. src_m1 = src1;
  367. src_0=&src_m1[src_wrap];
  368. src_p1=&src_0[src_wrap];
  369. src_p2=&src_p1[src_wrap];
  370. for(y=0;y<(height-2);y+=2) {
  371. memcpy(dst,src_m1,width);
  372. dst += dst_wrap;
  373. deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
  374. src_m2 = src_0;
  375. src_m1 = src_p1;
  376. src_0 = src_p2;
  377. src_p1 += 2*src_wrap;
  378. src_p2 += 2*src_wrap;
  379. dst += dst_wrap;
  380. }
  381. memcpy(dst,src_m1,width);
  382. dst += dst_wrap;
  383. /* do last line */
  384. deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
  385. }
  386. static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
  387. int width, int height)
  388. {
  389. uint8_t *src_m1, *src_0, *src_p1, *src_p2;
  390. int y;
  391. uint8_t *buf;
  392. buf = av_malloc(width);
  393. src_m1 = src1;
  394. memcpy(buf,src_m1,width);
  395. src_0=&src_m1[src_wrap];
  396. src_p1=&src_0[src_wrap];
  397. src_p2=&src_p1[src_wrap];
  398. for(y=0;y<(height-2);y+=2) {
  399. deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
  400. src_m1 = src_p1;
  401. src_0 = src_p2;
  402. src_p1 += 2*src_wrap;
  403. src_p2 += 2*src_wrap;
  404. }
  405. /* do last line */
  406. deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
  407. av_free(buf);
  408. }
  409. int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
  410. enum AVPixelFormat pix_fmt, int width, int height)
  411. {
  412. int i;
  413. if (pix_fmt != AV_PIX_FMT_YUV420P &&
  414. pix_fmt != AV_PIX_FMT_YUVJ420P &&
  415. pix_fmt != AV_PIX_FMT_YUV422P &&
  416. pix_fmt != AV_PIX_FMT_YUVJ422P &&
  417. pix_fmt != AV_PIX_FMT_YUV444P &&
  418. pix_fmt != AV_PIX_FMT_YUV411P &&
  419. pix_fmt != AV_PIX_FMT_GRAY8)
  420. return -1;
  421. if ((width & 3) != 0 || (height & 3) != 0)
  422. return -1;
  423. for(i=0;i<3;i++) {
  424. if (i == 1) {
  425. switch(pix_fmt) {
  426. case AV_PIX_FMT_YUVJ420P:
  427. case AV_PIX_FMT_YUV420P:
  428. width >>= 1;
  429. height >>= 1;
  430. break;
  431. case AV_PIX_FMT_YUV422P:
  432. case AV_PIX_FMT_YUVJ422P:
  433. width >>= 1;
  434. break;
  435. case AV_PIX_FMT_YUV411P:
  436. width >>= 2;
  437. break;
  438. default:
  439. break;
  440. }
  441. if (pix_fmt == AV_PIX_FMT_GRAY8) {
  442. break;
  443. }
  444. }
  445. if (src == dst) {
  446. deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
  447. width, height);
  448. } else {
  449. deinterlace_bottom_field(dst->data[i],dst->linesize[i],
  450. src->data[i], src->linesize[i],
  451. width, height);
  452. }
  453. }
  454. emms_c();
  455. return 0;
  456. }
  457. #endif /* FF_API_DEINTERLACE */