imgconvert.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Misc image conversion routines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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/avassert.h"
  29. #include "libavutil/colorspace.h"
  30. #include "libavutil/common.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/imgutils.h"
  34. #if FF_API_GETCHROMA
  35. void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
  36. {
  37. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  38. av_assert0(desc);
  39. *h_shift = desc->log2_chroma_w;
  40. *v_shift = desc->log2_chroma_h;
  41. }
  42. #endif
  43. int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
  44. enum AVPixelFormat src_pix_fmt,
  45. int has_alpha)
  46. {
  47. return av_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
  48. }
  49. enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  50. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  51. {
  52. return av_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  53. }
  54. enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
  55. enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
  56. {
  57. return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
  58. }
  59. enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
  60. enum AVPixelFormat src_pix_fmt,
  61. int has_alpha, int *loss_ptr){
  62. int i;
  63. enum AVPixelFormat best = AV_PIX_FMT_NONE;
  64. int loss;
  65. for (i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++) {
  66. loss = loss_ptr ? *loss_ptr : 0;
  67. best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, &loss);
  68. }
  69. if (loss_ptr)
  70. *loss_ptr = loss;
  71. return best;
  72. }
  73. #if FF_API_AVPICTURE
  74. FF_DISABLE_DEPRECATION_WARNINGS
  75. /* return true if yuv planar */
  76. static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
  77. {
  78. int i;
  79. int planes[4] = { 0 };
  80. if ( desc->flags & AV_PIX_FMT_FLAG_RGB
  81. || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
  82. return 0;
  83. /* set the used planes */
  84. for (i = 0; i < desc->nb_components; i++)
  85. planes[desc->comp[i].plane] = 1;
  86. /* if there is an unused plane, the format is not planar */
  87. for (i = 0; i < desc->nb_components; i++)
  88. if (!planes[i])
  89. return 0;
  90. return 1;
  91. }
  92. int av_picture_crop(AVPicture *dst, const AVPicture *src,
  93. enum AVPixelFormat pix_fmt, int top_band, int left_band)
  94. {
  95. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  96. int y_shift;
  97. int x_shift;
  98. int max_step[4];
  99. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  100. return -1;
  101. y_shift = desc->log2_chroma_h;
  102. x_shift = desc->log2_chroma_w;
  103. av_image_fill_max_pixsteps(max_step, NULL, desc);
  104. if (is_yuv_planar(desc)) {
  105. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
  106. dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
  107. dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
  108. } else{
  109. if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
  110. return -1;
  111. dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + (left_band * max_step[0]);
  112. }
  113. dst->linesize[0] = src->linesize[0];
  114. dst->linesize[1] = src->linesize[1];
  115. dst->linesize[2] = src->linesize[2];
  116. return 0;
  117. }
  118. int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
  119. enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
  120. int *color)
  121. {
  122. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  123. uint8_t *optr;
  124. int y_shift;
  125. int x_shift;
  126. int yheight;
  127. int i, y;
  128. int max_step[4];
  129. if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
  130. return -1;
  131. if (!is_yuv_planar(desc)) {
  132. if (src)
  133. return -1; //TODO: Not yet implemented
  134. av_image_fill_max_pixsteps(max_step, NULL, desc);
  135. if (padtop || padleft) {
  136. memset(dst->data[0], color[0],
  137. dst->linesize[0] * padtop + (padleft * max_step[0]));
  138. }
  139. if (padleft || padright) {
  140. optr = dst->data[0] + dst->linesize[0] * padtop +
  141. (dst->linesize[0] - (padright * max_step[0]));
  142. yheight = height - 1 - (padtop + padbottom);
  143. for (y = 0; y < yheight; y++) {
  144. memset(optr, color[0], (padleft + padright) * max_step[0]);
  145. optr += dst->linesize[0];
  146. }
  147. }
  148. if (padbottom || padright) {
  149. optr = dst->data[0] + dst->linesize[0] * (height - padbottom) -
  150. (padright * max_step[0]);
  151. memset(optr, color[0], dst->linesize[0] * padbottom +
  152. (padright * max_step[0]));
  153. }
  154. return 0;
  155. }
  156. for (i = 0; i < 3; i++) {
  157. x_shift = i ? desc->log2_chroma_w : 0;
  158. y_shift = i ? desc->log2_chroma_h : 0;
  159. if (padtop || padleft) {
  160. memset(dst->data[i], color[i],
  161. dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
  162. }
  163. if (padleft || padright) {
  164. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  165. (dst->linesize[i] - (padright >> x_shift));
  166. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  167. for (y = 0; y < yheight; y++) {
  168. memset(optr, color[i], (padleft + padright) >> x_shift);
  169. optr += dst->linesize[i];
  170. }
  171. }
  172. if (src) { /* first line */
  173. uint8_t *iptr = src->data[i];
  174. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  175. (padleft >> x_shift);
  176. memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
  177. iptr += src->linesize[i];
  178. optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
  179. (dst->linesize[i] - (padright >> x_shift));
  180. yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
  181. for (y = 0; y < yheight; y++) {
  182. memset(optr, color[i], (padleft + padright) >> x_shift);
  183. memcpy(optr + ((padleft + padright) >> x_shift), iptr,
  184. (width - padleft - padright) >> x_shift);
  185. iptr += src->linesize[i];
  186. optr += dst->linesize[i];
  187. }
  188. }
  189. if (padbottom || padright) {
  190. optr = dst->data[i] + dst->linesize[i] *
  191. ((height - padbottom) >> y_shift) - (padright >> x_shift);
  192. memset(optr, color[i],dst->linesize[i] *
  193. (padbottom >> y_shift) + (padright >> x_shift));
  194. }
  195. }
  196. return 0;
  197. }
  198. FF_ENABLE_DEPRECATION_WARNINGS
  199. #endif /* FF_API_AVPICTURE */