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.

253 lines
8.4KB

  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. #include "avcodec.h"
  26. #include "internal.h"
  27. #include "mathops.h"
  28. #include "libavutil/colorspace.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "libavutil/internal.h"
  32. #include "libavutil/imgutils.h"
  33. #if FF_API_GETCHROMA
  34. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  35. {
  36. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  37. *h_shift = desc->log2_chroma_w;
  38. *v_shift = desc->log2_chroma_h;
  39. }
  40. #endif
  41. static int is_gray(const AVPixFmtDescriptor *desc)
  42. {
  43. return desc->nb_components - (desc->flags & AV_PIX_FMT_FLAG_ALPHA) == 1;
  44. }
  45. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
  46. enum AVPixelFormat src_pix_fmt,
  47. int has_alpha)
  48. {
  49. const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
  50. const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
  51. int loss, i, nb_components = FFMIN(src_desc->nb_components,
  52. dst_desc->nb_components);
  53. /* compute loss */
  54. loss = 0;
  55. if (dst_pix_fmt == src_pix_fmt)
  56. return 0;
  57. for (i = 0; i < nb_components; i++)
  58. if (src_desc->comp[i].depth > dst_desc->comp[i].depth)
  59. loss |= FF_LOSS_DEPTH;
  60. if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
  61. dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
  62. loss |= FF_LOSS_RESOLUTION;
  63. if ((src_desc->flags & AV_PIX_FMT_FLAG_RGB) != (dst_desc->flags & AV_PIX_FMT_FLAG_RGB))
  64. loss |= FF_LOSS_COLORSPACE;
  65. if (has_alpha && !(dst_desc->flags & AV_PIX_FMT_FLAG_ALPHA) &&
  66. (src_desc->flags & AV_PIX_FMT_FLAG_ALPHA))
  67. loss |= FF_LOSS_ALPHA;
  68. if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
  69. return loss | FF_LOSS_COLORQUANT;
  70. if (src_desc->nb_components > dst_desc->nb_components)
  71. if (is_gray(dst_desc))
  72. loss |= FF_LOSS_CHROMA;
  73. return loss;
  74. }
  75. static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
  76. enum AVPixelFormat src_pix_fmt,
  77. int has_alpha,
  78. int loss_mask)
  79. {
  80. int dist, i, loss, min_dist;
  81. enum AVPixelFormat dst_pix_fmt;
  82. /* find exact color match with smallest size */
  83. dst_pix_fmt = AV_PIX_FMT_NONE;
  84. min_dist = 0x7fffffff;
  85. i = 0;
  86. while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
  87. enum AVPixelFormat pix_fmt = pix_fmt_list[i];
  88. if (i > AV_PIX_FMT_NB) {
  89. av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
  90. "it is either not properly terminated or contains duplicates\n");
  91. return AV_PIX_FMT_NONE;
  92. }
  93. loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
  94. if (loss == 0) {
  95. dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
  96. if (dist < min_dist) {
  97. min_dist = dist;
  98. dst_pix_fmt = pix_fmt;
  99. }
  100. }
  101. i++;
  102. }
  103. return dst_pix_fmt;
  104. }
  105. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
  106. enum AVPixelFormat src_pix_fmt,
  107. int has_alpha, int *loss_ptr)
  108. {
  109. enum AVPixelFormat dst_pix_fmt;
  110. int loss_mask, i;
  111. static const int loss_mask_order[] = {
  112. ~0, /* no loss first */
  113. ~FF_LOSS_ALPHA,
  114. ~FF_LOSS_RESOLUTION,
  115. ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
  116. ~FF_LOSS_COLORQUANT,
  117. ~FF_LOSS_DEPTH,
  118. 0,
  119. };
  120. /* try with successive loss */
  121. i = 0;
  122. for(;;) {
  123. loss_mask = loss_mask_order[i++];
  124. dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
  125. has_alpha, loss_mask);
  126. if (dst_pix_fmt >= 0)
  127. goto found;
  128. if (loss_mask == 0)
  129. break;
  130. }
  131. return AV_PIX_FMT_NONE;
  132. found:
  133. if (loss_ptr)
  134. *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  135. return dst_pix_fmt;
  136. }
  137. #if FF_API_AVPICTURE
  138. FF_DISABLE_DEPRECATION_WARNINGS
  139. /* return true if yuv planar */
  140. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  141. {
  142. return (!(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  143. (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  144. }
  145. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  146. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  147. {
  148. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  149. int y_shift;
  150. int x_shift;
  151. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
  152. return -1;
  153. y_shift = desc->log2_chroma_h;
  154. x_shift = desc->log2_chroma_w;
  155. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  156. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  157. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  158. dst->linesize[0] = src->linesize[0];
  159. dst->linesize[1] = src->linesize[1];
  160. dst->linesize[2] = src->linesize[2];
  161. return 0;
  162. }
  163. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  164. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  165. int *color)
  166. {
  167. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  168. uint8_t *optr;
  169. int y_shift;
  170. int x_shift;
  171. int yheight;
  172. int i, y;
  173. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
  174. !is_yuv_planar(desc)) return -1;
  175. for (i = 0; i < 3; i++) {
  176. x_shift = i ? desc->log2_chroma_w : 0;
  177. y_shift = i ? desc->log2_chroma_h : 0;
  178. if (padtop || padleft) {
  179. memset(dst->data[i], color[i],
  180. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  181. }
  182. if (padleft || padright) {
  183. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  184. (dst->linesize[i] - (padright >> x_shift));
  185. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  186. for (y = 0; y < yheight; y++) {
  187. memset(optr, color[i], (padleft + padright) >> x_shift);
  188. optr += dst->linesize[i];
  189. }
  190. }
  191. if (src) { /* first line */
  192. uint8_t *iptr = src->data[i];
  193. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  194. (padleft >> x_shift);
  195. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  196. iptr += src->linesize[i];
  197. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  198. (dst->linesize[i] - (padright >> x_shift));
  199. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  200. for (y = 0; y < yheight; y++) {
  201. memset(optr, color[i], (padleft + padright) >> x_shift);
  202. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  203. (width - padleft - padright) >> x_shift);
  204. iptr += src->linesize[i];
  205. optr += dst->linesize[i];
  206. }
  207. }
  208. if (padbottom || padright) {
  209. optr = dst->data[i] + dst->linesize[i] *
  210. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  211. memset(optr, color[i],dst->linesize[i] *
  212. (padbottom >> y_shift) + (padright >> x_shift));
  213. }
  214. }
  215. return 0;
  216. }
  217. FF_ENABLE_DEPRECATION_WARNINGS
  218. #endif /* FF_API_AVPICTURE */