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.

494 lines
16KB

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