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.

217 lines
7.2KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * misc image utilities
  21. */
  22. #include "imgutils.h"
  23. #include "libavutil/pixdesc.h"
  24. void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
  25. const AVPixFmtDescriptor *pixdesc)
  26. {
  27. int i;
  28. memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
  29. if (max_pixstep_comps)
  30. memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
  31. for (i = 0; i < 4; i++) {
  32. const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
  33. if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
  34. max_pixsteps[comp->plane] = comp->step_minus1+1;
  35. if (max_pixstep_comps)
  36. max_pixstep_comps[comp->plane] = i;
  37. }
  38. }
  39. }
  40. int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
  41. {
  42. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  43. int max_step [4]; /* max pixel step for each plane */
  44. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  45. int s;
  46. if (desc->flags & PIX_FMT_BITSTREAM)
  47. return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
  48. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  49. s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
  50. return max_step[plane] * (((width + (1 << s) - 1)) >> s);
  51. }
  52. int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
  53. {
  54. int i;
  55. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  56. int max_step [4]; /* max pixel step for each plane */
  57. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  58. memset(linesizes, 0, 4*sizeof(linesizes[0]));
  59. if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
  60. return AVERROR(EINVAL);
  61. if (desc->flags & PIX_FMT_BITSTREAM) {
  62. linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
  63. return 0;
  64. }
  65. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  66. for (i = 0; i < 4; i++) {
  67. int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
  68. linesizes[i] = max_step[i] * (((width + (1 << s) - 1)) >> s);
  69. }
  70. return 0;
  71. }
  72. int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
  73. uint8_t *ptr, const int linesizes[4])
  74. {
  75. int i, total_size, size[4], has_plane[4];
  76. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  77. memset(data , 0, sizeof(data[0])*4);
  78. memset(size , 0, sizeof(size));
  79. memset(has_plane, 0, sizeof(has_plane));
  80. if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
  81. return AVERROR(EINVAL);
  82. data[0] = ptr;
  83. size[0] = linesizes[0] * height;
  84. if (desc->flags & PIX_FMT_PAL) {
  85. size[0] = (size[0] + 3) & ~3;
  86. data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
  87. return size[0] + 256 * 4;
  88. }
  89. for (i = 0; i < 4; i++)
  90. has_plane[desc->comp[i].plane] = 1;
  91. total_size = size[0];
  92. for (i = 1; has_plane[i] && i < 4; i++) {
  93. int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  94. data[i] = data[i-1] + size[i-1];
  95. h = (height + (1 << s) - 1) >> s;
  96. size[i] = h * linesizes[i];
  97. total_size += size[i];
  98. }
  99. return total_size;
  100. }
  101. typedef struct ImgUtils {
  102. const AVClass *class;
  103. int log_offset;
  104. void *log_ctx;
  105. } ImgUtils;
  106. static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
  107. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  108. {
  109. ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
  110. if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  111. return 0;
  112. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  113. return AVERROR(EINVAL);
  114. }
  115. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  116. const uint8_t *src, int src_linesize,
  117. int bytewidth, int height)
  118. {
  119. if (!dst || !src)
  120. return;
  121. for (;height > 0; height--) {
  122. memcpy(dst, src, bytewidth);
  123. dst += dst_linesize;
  124. src += src_linesize;
  125. }
  126. }
  127. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  128. const uint8_t *src_data[4], const int src_linesizes[4],
  129. enum PixelFormat pix_fmt, int width, int height)
  130. {
  131. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
  132. if (desc->flags & PIX_FMT_HWACCEL)
  133. return;
  134. if (desc->flags & PIX_FMT_PAL) {
  135. av_image_copy_plane(dst_data[0], dst_linesizes[0],
  136. src_data[0], src_linesizes[0],
  137. width, height);
  138. /* copy the palette */
  139. memcpy(dst_data[1], src_data[1], 4*256);
  140. } else {
  141. int i, planes_nb = 0;
  142. for (i = 0; i < desc->nb_components; i++)
  143. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  144. for (i = 0; i < planes_nb; i++) {
  145. int h = height;
  146. int bwidth = av_image_get_linesize(pix_fmt, width, i);
  147. if (i == 1 || i == 2) {
  148. h= -((-height)>>desc->log2_chroma_h);
  149. }
  150. av_image_copy_plane(dst_data[i], dst_linesizes[i],
  151. src_data[i], src_linesizes[i],
  152. bwidth, h);
  153. }
  154. }
  155. }
  156. #if FF_API_OLD_IMAGE_NAMES
  157. void av_fill_image_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
  158. const AVPixFmtDescriptor *pixdesc)
  159. {
  160. av_image_fill_max_pixsteps(max_pixsteps, max_pixstep_comps, pixdesc);
  161. }
  162. int av_get_image_linesize(enum PixelFormat pix_fmt, int width, int plane)
  163. {
  164. return av_image_get_linesize(pix_fmt, width, plane);
  165. }
  166. int av_fill_image_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
  167. {
  168. return av_image_fill_linesizes(linesizes, pix_fmt, width);
  169. }
  170. int av_fill_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
  171. uint8_t *ptr, const int linesizes[4])
  172. {
  173. return av_image_fill_pointers(data, pix_fmt, height, ptr, linesizes);
  174. }
  175. int av_check_image_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  176. {
  177. return av_image_check_size(w, h, log_offset, log_ctx);
  178. }
  179. #endif