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.

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