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.

431 lines
14KB

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