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.

244 lines
8.2KB

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