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.

345 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/internal.h"
  38. #include "libavutil/imgutils.h"
  39. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  40. {
  41. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  42. *h_shift = desc->log2_chroma_w;
  43. *v_shift = desc->log2_chroma_h;
  44. }
  45. static int is_gray(const AVPixFmtDescriptor *desc)
  46. {
  47. return desc->nb_components - (desc->flags & AV_PIX_FMT_FLAG_ALPHA) == 1;
  48. }
  49. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
  50. enum AVPixelFormat src_pix_fmt,
  51. int has_alpha)
  52. {
  53. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  54. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  55. int loss, i, nb_components = FFMIN(src_desc->nb_components,
  56. dst_desc->nb_components);
  57. /* compute loss */
  58. loss = 0;
  59. if (dst_pix_fmt == src_pix_fmt)
  60. return 0;
  61. for (i = 0; i < nb_components; i++)
  62. if (src_desc->comp[i].depth > dst_desc->comp[i].depth)
  63. loss |= FF_LOSS_DEPTH;
  64. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  65. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  66. loss |= FF_LOSS_RESOLUTION;
  67. if ((src_desc->flags & AV_PIX_FMT_FLAG_RGB) != (dst_desc->flags & AV_PIX_FMT_FLAG_RGB))
  68. loss |= FF_LOSS_COLORSPACE;
  69. if (has_alpha && !(dst_desc->flags & AV_PIX_FMT_FLAG_ALPHA) &&
  70. (src_desc->flags & AV_PIX_FMT_FLAG_ALPHA))
  71. loss |= FF_LOSS_ALPHA;
  72. if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
  73. return loss | FF_LOSS_COLORQUANT;
  74. if (src_desc->nb_components > dst_desc->nb_components)
  75. if (is_gray(dst_desc))
  76. loss |= FF_LOSS_CHROMA;
  77. return loss;
  78. }
  79. static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
  80. enum AVPixelFormat src_pix_fmt,
  81. int has_alpha,
  82. int loss_mask)
  83. {
  84. int dist, i, loss, min_dist;
  85. enum AVPixelFormat dst_pix_fmt;
  86. /* find exact color match with smallest size */
  87. dst_pix_fmt = AV_PIX_FMT_NONE;
  88. min_dist = 0x7fffffff;
  89. i = 0;
  90. while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
  91. enum AVPixelFormat pix_fmt = pix_fmt_list[i];
  92. if (i > AV_PIX_FMT_NB) {
  93. av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
  94. "it is either not properly terminated or contains duplicates\n");
  95. return AV_PIX_FMT_NONE;
  96. }
  97. loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
  98. if (loss == 0) {
  99. dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
  100. if (dist < min_dist) {
  101. min_dist = dist;
  102. dst_pix_fmt = pix_fmt;
  103. }
  104. }
  105. i++;
  106. }
  107. return dst_pix_fmt;
  108. }
  109. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  110. enum AVPixelFormat src_pix_fmt,
  111. int has_alpha, int *loss_ptr)
  112. {
  113. enum AVPixelFormat dst_pix_fmt;
  114. int loss_mask, i;
  115. static const int loss_mask_order[] = {
  116. ~0, /* no loss first */
  117. ~FF_LOSS_ALPHA,
  118. ~FF_LOSS_RESOLUTION,
  119. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  120. ~FF_LOSS_COLORQUANT,
  121. ~FF_LOSS_DEPTH,
  122. 0,
  123. };
  124. /* try with successive loss */
  125. i = 0;
  126. for(;;) {
  127. loss_mask = loss_mask_order[i++];
  128. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
  129. has_alpha, loss_mask);
  130. if (dst_pix_fmt >= 0)
  131. goto found;
  132. if (loss_mask == 0)
  133. break;
  134. }
  135. return AV_PIX_FMT_NONE;
  136. found:
  137. if (loss_ptr)
  138. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  139. return dst_pix_fmt;
  140. }
  141. /* 2x2 -> 1x1 */
  142. void ff_shrink22(uint8_t *dst, int dst_wrap,
  143. const uint8_t *src, int src_wrap,
  144. int width, int height)
  145. {
  146. int w;
  147. const uint8_t *s1, *s2;
  148. uint8_t *d;
  149. for(;height > 0; height--) {
  150. s1 = src;
  151. s2 = s1 + src_wrap;
  152. d = dst;
  153. for(w = width;w >= 4; w-=4) {
  154. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  155. d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
  156. d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
  157. d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
  158. s1 += 8;
  159. s2 += 8;
  160. d += 4;
  161. }
  162. for(;w > 0; w--) {
  163. d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
  164. s1 += 2;
  165. s2 += 2;
  166. d++;
  167. }
  168. src += 2 * src_wrap;
  169. dst += dst_wrap;
  170. }
  171. }
  172. /* 4x4 -> 1x1 */
  173. void ff_shrink44(uint8_t *dst, int dst_wrap,
  174. const uint8_t *src, int src_wrap,
  175. int width, int height)
  176. {
  177. int w;
  178. const uint8_t *s1, *s2, *s3, *s4;
  179. uint8_t *d;
  180. for(;height > 0; height--) {
  181. s1 = src;
  182. s2 = s1 + src_wrap;
  183. s3 = s2 + src_wrap;
  184. s4 = s3 + src_wrap;
  185. d = dst;
  186. for(w = width;w > 0; w--) {
  187. d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
  188. s2[0] + s2[1] + s2[2] + s2[3] +
  189. s3[0] + s3[1] + s3[2] + s3[3] +
  190. s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
  191. s1 += 4;
  192. s2 += 4;
  193. s3 += 4;
  194. s4 += 4;
  195. d++;
  196. }
  197. src += 4 * src_wrap;
  198. dst += dst_wrap;
  199. }
  200. }
  201. /* 8x8 -> 1x1 */
  202. void ff_shrink88(uint8_t *dst, int dst_wrap,
  203. const uint8_t *src, int src_wrap,
  204. int width, int height)
  205. {
  206. int w, i;
  207. for(;height > 0; height--) {
  208. for(w = width;w > 0; w--) {
  209. int tmp=0;
  210. for(i=0; i<8; i++){
  211. tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
  212. src += src_wrap;
  213. }
  214. *(dst++) = (tmp + 32)>>6;
  215. src += 8 - 8*src_wrap;
  216. }
  217. src += 8*src_wrap - 8*width;
  218. dst += dst_wrap - width;
  219. }
  220. }
  221. /* return true if yuv planar */
  222. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  223. {
  224. return (!(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  225. (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  226. }
  227. #if FF_API_AVPICTURE
  228. FF_DISABLE_DEPRECATION_WARNINGS
  229. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  230. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  231. {
  232. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  233. int y_shift;
  234. int x_shift;
  235. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
  236. return -1;
  237. y_shift = desc->log2_chroma_h;
  238. x_shift = desc->log2_chroma_w;
  239. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  240. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  241. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  242. dst->linesize[0] = src->linesize[0];
  243. dst->linesize[1] = src->linesize[1];
  244. dst->linesize[2] = src->linesize[2];
  245. return 0;
  246. }
  247. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  248. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  249. int *color)
  250. {
  251. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  252. uint8_t *optr;
  253. int y_shift;
  254. int x_shift;
  255. int yheight;
  256. int i, y;
  257. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  258. !is_yuv_planar(desc)) return -1;
  259. for (i = 0; i < 3; i++) {
  260. x_shift = i ? desc->log2_chroma_w : 0;
  261. y_shift = i ? desc->log2_chroma_h : 0;
  262. if (padtop || padleft) {
  263. memset(dst->data[i], color[i],
  264. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  265. }
  266. if (padleft || padright) {
  267. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  268. (dst->linesize[i] - (padright >> x_shift));
  269. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  270. for (y = 0; y < yheight; y++) {
  271. memset(optr, color[i], (padleft + padright) >> x_shift);
  272. optr += dst->linesize[i];
  273. }
  274. }
  275. if (src) { /* first line */
  276. uint8_t *iptr = src->data[i];
  277. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  278. (padleft >> x_shift);
  279. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  280. iptr += src->linesize[i];
  281. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  282. (dst->linesize[i] - (padright >> x_shift));
  283. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  284. for (y = 0; y < yheight; y++) {
  285. memset(optr, color[i], (padleft + padright) >> x_shift);
  286. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  287. (width - padleft - padright) >> x_shift);
  288. iptr += src->linesize[i];
  289. optr += dst->linesize[i];
  290. }
  291. }
  292. if (padbottom || padright) {
  293. optr = dst->data[i] + dst->linesize[i] *
  294. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  295. memset(optr, color[i],dst->linesize[i] *
  296. (padbottom >> y_shift) + (padright >> x_shift));
  297. }
  298. }
  299. return 0;
  300. }
  301. FF_ENABLE_DEPRECATION_WARNINGS
  302. #endif /* FF_API_AVPICTURE */