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.

382 lines
12KB

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