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.

438 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 = {
  189. .class_name = "IMGUTILS",
  190. .item_name = av_default_item_name,
  191. .option = NULL,
  192. .version = LIBAVUTIL_VERSION_INT,
  193. .log_level_offset_offset = offsetof(ImgUtils, log_offset),
  194. .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
  195. };
  196. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  197. {
  198. ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
  199. if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  200. return 0;
  201. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  202. return AVERROR(EINVAL);
  203. }
  204. int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
  205. {
  206. int64_t scaled_dim;
  207. if (!sar.den)
  208. return AVERROR(EINVAL);
  209. if (!sar.num || sar.num == sar.den)
  210. return 0;
  211. if (sar.num < sar.den)
  212. scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
  213. else
  214. scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
  215. if (scaled_dim > 0)
  216. return 0;
  217. return AVERROR(EINVAL);
  218. }
  219. static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
  220. const uint8_t *src, ptrdiff_t src_linesize,
  221. ptrdiff_t bytewidth, int height)
  222. {
  223. if (!dst || !src)
  224. return;
  225. for (;height > 0; height--) {
  226. memcpy(dst, src, bytewidth);
  227. dst += dst_linesize;
  228. src += src_linesize;
  229. }
  230. }
  231. static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
  232. const uint8_t *src, ptrdiff_t src_linesize,
  233. ptrdiff_t bytewidth, int height)
  234. {
  235. int ret = -1;
  236. #if ARCH_X86
  237. ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
  238. bytewidth, height);
  239. #endif
  240. if (ret < 0)
  241. image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
  242. }
  243. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  244. const uint8_t *src, int src_linesize,
  245. int bytewidth, int height)
  246. {
  247. image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
  248. }
  249. static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
  250. const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
  251. enum AVPixelFormat pix_fmt, int width, int height,
  252. void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
  253. ptrdiff_t, ptrdiff_t, int))
  254. {
  255. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  256. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  257. return;
  258. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  259. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  260. copy_plane(dst_data[0], dst_linesizes[0],
  261. src_data[0], src_linesizes[0],
  262. width, height);
  263. /* copy the palette */
  264. memcpy(dst_data[1], src_data[1], 4*256);
  265. } else {
  266. int i, planes_nb = 0;
  267. for (i = 0; i < desc->nb_components; i++)
  268. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  269. for (i = 0; i < planes_nb; i++) {
  270. int h = height;
  271. ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
  272. if (i == 1 || i == 2) {
  273. h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
  274. }
  275. copy_plane(dst_data[i], dst_linesizes[i],
  276. src_data[i], src_linesizes[i],
  277. bwidth, h);
  278. }
  279. }
  280. }
  281. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  282. const uint8_t *src_data[4], const int src_linesizes[4],
  283. enum AVPixelFormat pix_fmt, int width, int height)
  284. {
  285. ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
  286. int i;
  287. for (i = 0; i < 4; i++) {
  288. dst_linesizes1[i] = dst_linesizes[i];
  289. src_linesizes1[i] = src_linesizes[i];
  290. }
  291. image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
  292. width, height, image_copy_plane);
  293. }
  294. void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
  295. const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
  296. enum AVPixelFormat pix_fmt, int width, int height)
  297. {
  298. image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
  299. width, height, image_copy_plane_uc_from);
  300. }
  301. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  302. const uint8_t *src, enum AVPixelFormat pix_fmt,
  303. int width, int height, int align)
  304. {
  305. int ret, i;
  306. ret = av_image_check_size(width, height, 0, NULL);
  307. if (ret < 0)
  308. return ret;
  309. ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
  310. if (ret < 0)
  311. return ret;
  312. for (i = 0; i < 4; i++)
  313. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  314. return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
  315. }
  316. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
  317. int width, int height, int align)
  318. {
  319. uint8_t *data[4];
  320. int linesize[4];
  321. int ret;
  322. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  323. if (!desc)
  324. return AVERROR_BUG;
  325. ret = av_image_check_size(width, height, 0, NULL);
  326. if (ret < 0)
  327. return ret;
  328. // do not include palette for these pseudo-paletted formats
  329. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  330. return width * height;
  331. return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
  332. width, height, align);
  333. }
  334. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  335. const uint8_t * const src_data[4],
  336. const int src_linesize[4],
  337. enum AVPixelFormat pix_fmt,
  338. int width, int height, int align)
  339. {
  340. int i, j, nb_planes = 0, linesize[4];
  341. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  342. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  343. if (size > dst_size || size < 0 || !desc)
  344. return AVERROR(EINVAL);
  345. for (i = 0; i < desc->nb_components; i++)
  346. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  347. nb_planes++;
  348. av_image_fill_linesizes(linesize, pix_fmt, width);
  349. for (i = 0; i < nb_planes; i++) {
  350. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  351. const uint8_t *src = src_data[i];
  352. h = (height + (1 << shift) - 1) >> shift;
  353. for (j = 0; j < h; j++) {
  354. memcpy(dst, src, linesize[i]);
  355. dst += FFALIGN(linesize[i], align);
  356. src += src_linesize[i];
  357. }
  358. }
  359. if (desc->flags & AV_PIX_FMT_FLAG_PAL)
  360. memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
  361. src_data[1], 256 * 4);
  362. return size;
  363. }