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.

278 lines
8.7KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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 "common.h"
  23. #include "imgutils.h"
  24. #include "internal.h"
  25. #include "log.h"
  26. #include "pixdesc.h"
  27. void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
  28. const AVPixFmtDescriptor *pixdesc)
  29. {
  30. int i;
  31. memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
  32. if (max_pixstep_comps)
  33. memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
  34. for (i = 0; i < 4; i++) {
  35. const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
  36. if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
  37. max_pixsteps[comp->plane] = comp->step_minus1+1;
  38. if (max_pixstep_comps)
  39. max_pixstep_comps[comp->plane] = i;
  40. }
  41. }
  42. }
  43. int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
  44. {
  45. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  46. int max_step [4]; /* max pixel step for each plane */
  47. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  48. int s;
  49. if (!desc)
  50. return AVERROR(EINVAL);
  51. if (desc->flags & PIX_FMT_BITSTREAM)
  52. return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
  53. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  54. s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
  55. return max_step[plane] * (((width + (1 << s) - 1)) >> s);
  56. }
  57. int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
  58. {
  59. int i;
  60. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  61. int max_step [4]; /* max pixel step for each plane */
  62. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  63. memset(linesizes, 0, 4*sizeof(linesizes[0]));
  64. if (!desc || desc->flags & PIX_FMT_HWACCEL)
  65. return AVERROR(EINVAL);
  66. if (desc->flags & PIX_FMT_BITSTREAM) {
  67. if (width > (INT_MAX -7) / (desc->comp[0].step_minus1+1))
  68. return AVERROR(EINVAL);
  69. linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
  70. return 0;
  71. }
  72. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  73. for (i = 0; i < 4; i++) {
  74. int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
  75. int shifted_w = ((width + (1 << s) - 1)) >> s;
  76. if (max_step[i] > INT_MAX / shifted_w)
  77. return AVERROR(EINVAL);
  78. linesizes[i] = max_step[i] * shifted_w;
  79. }
  80. return 0;
  81. }
  82. int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
  83. uint8_t *ptr, const int linesizes[4])
  84. {
  85. int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
  86. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  87. memset(data , 0, sizeof(data[0])*4);
  88. if (!desc || desc->flags & PIX_FMT_HWACCEL)
  89. return AVERROR(EINVAL);
  90. data[0] = ptr;
  91. if (linesizes[0] > (INT_MAX - 1024) / height)
  92. return AVERROR(EINVAL);
  93. size[0] = linesizes[0] * height;
  94. if (desc->flags & PIX_FMT_PAL ||
  95. desc->flags & PIX_FMT_PSEUDOPAL) {
  96. size[0] = (size[0] + 3) & ~3;
  97. data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
  98. return size[0] + 256 * 4;
  99. }
  100. for (i = 0; i < 4; i++)
  101. has_plane[desc->comp[i].plane] = 1;
  102. total_size = size[0];
  103. for (i = 1; i < 4 && has_plane[i]; i++) {
  104. int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  105. data[i] = data[i-1] + size[i-1];
  106. h = (height + (1 << s) - 1) >> s;
  107. if (linesizes[i] > INT_MAX / h)
  108. return AVERROR(EINVAL);
  109. size[i] = h * linesizes[i];
  110. if (total_size > INT_MAX - size[i])
  111. return AVERROR(EINVAL);
  112. total_size += size[i];
  113. }
  114. return total_size;
  115. }
  116. int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
  117. {
  118. int i;
  119. for (i = 0; i < 256; i++) {
  120. int r, g, b;
  121. switch (pix_fmt) {
  122. case AV_PIX_FMT_RGB8:
  123. r = (i>>5 )*36;
  124. g = ((i>>2)&7)*36;
  125. b = (i&3 )*85;
  126. break;
  127. case AV_PIX_FMT_BGR8:
  128. b = (i>>6 )*85;
  129. g = ((i>>3)&7)*36;
  130. r = (i&7 )*36;
  131. break;
  132. case AV_PIX_FMT_RGB4_BYTE:
  133. r = (i>>3 )*255;
  134. g = ((i>>1)&3)*85;
  135. b = (i&1 )*255;
  136. break;
  137. case AV_PIX_FMT_BGR4_BYTE:
  138. b = (i>>3 )*255;
  139. g = ((i>>1)&3)*85;
  140. r = (i&1 )*255;
  141. break;
  142. case AV_PIX_FMT_GRAY8:
  143. r = b = g = i;
  144. break;
  145. default:
  146. return AVERROR(EINVAL);
  147. }
  148. pal[i] = b + (g<<8) + (r<<16);
  149. }
  150. return 0;
  151. }
  152. int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
  153. int w, int h, enum AVPixelFormat pix_fmt, int align)
  154. {
  155. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  156. int i, ret;
  157. uint8_t *buf;
  158. if (!desc)
  159. return AVERROR(EINVAL);
  160. if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
  161. return ret;
  162. if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
  163. return ret;
  164. for (i = 0; i < 4; i++)
  165. linesizes[i] = FFALIGN(linesizes[i], align);
  166. if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
  167. return ret;
  168. buf = av_malloc(ret + align);
  169. if (!buf)
  170. return AVERROR(ENOMEM);
  171. if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
  172. av_free(buf);
  173. return ret;
  174. }
  175. if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)
  176. avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
  177. return ret;
  178. }
  179. typedef struct ImgUtils {
  180. const AVClass *class;
  181. int log_offset;
  182. void *log_ctx;
  183. } ImgUtils;
  184. static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
  185. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  186. {
  187. ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
  188. if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  189. return 0;
  190. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  191. return AVERROR(EINVAL);
  192. }
  193. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  194. const uint8_t *src, int src_linesize,
  195. int bytewidth, int height)
  196. {
  197. if (!dst || !src)
  198. return;
  199. for (;height > 0; height--) {
  200. memcpy(dst, src, bytewidth);
  201. dst += dst_linesize;
  202. src += src_linesize;
  203. }
  204. }
  205. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  206. const uint8_t *src_data[4], const int src_linesizes[4],
  207. enum AVPixelFormat pix_fmt, int width, int height)
  208. {
  209. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  210. if (!desc || desc->flags & PIX_FMT_HWACCEL)
  211. return;
  212. if (desc->flags & PIX_FMT_PAL ||
  213. desc->flags & PIX_FMT_PSEUDOPAL) {
  214. av_image_copy_plane(dst_data[0], dst_linesizes[0],
  215. src_data[0], src_linesizes[0],
  216. width, height);
  217. /* copy the palette */
  218. memcpy(dst_data[1], src_data[1], 4*256);
  219. } else {
  220. int i, planes_nb = 0;
  221. for (i = 0; i < desc->nb_components; i++)
  222. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  223. for (i = 0; i < planes_nb; i++) {
  224. int h = height;
  225. int bwidth = av_image_get_linesize(pix_fmt, width, i);
  226. if (i == 1 || i == 2) {
  227. h= -((-height)>>desc->log2_chroma_h);
  228. }
  229. av_image_copy_plane(dst_data[i], dst_linesizes[i],
  230. src_data[i], src_linesizes[i],
  231. bwidth, h);
  232. }
  233. }
  234. }