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.

301 lines
9.3KB

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