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.

338 lines
11KB

  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 > dst_desc->comp[i].depth)
  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. (src_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. }