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