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.

605 lines
19KB

  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. }
  364. // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
  365. // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
  366. // dst_size%clear_size!=0), the remaining data will be filled with the beginning
  367. // of the clear data only.
  368. static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
  369. size_t clear_size)
  370. {
  371. size_t pos = 0;
  372. int same = 1;
  373. int i;
  374. if (!clear_size)
  375. return;
  376. // Reduce to memset() if possible.
  377. for (i = 0; i < clear_size; i++) {
  378. if (clear[i] != clear[0]) {
  379. same = 0;
  380. break;
  381. }
  382. }
  383. if (same)
  384. clear_size = 1;
  385. if (clear_size == 1) {
  386. memset(dst, clear[0], dst_size);
  387. dst_size = 0;
  388. } else if (clear_size == 2) {
  389. uint16_t val = AV_RN16(clear);
  390. for (; dst_size >= 2; dst_size -= 2) {
  391. AV_WN16(dst, val);
  392. dst += 2;
  393. }
  394. } else if (clear_size == 4) {
  395. uint32_t val = AV_RN32(clear);
  396. for (; dst_size >= 4; dst_size -= 4) {
  397. AV_WN32(dst, val);
  398. dst += 4;
  399. }
  400. } else if (clear_size == 8) {
  401. uint32_t val = AV_RN64(clear);
  402. for (; dst_size >= 8; dst_size -= 8) {
  403. AV_WN64(dst, val);
  404. dst += 8;
  405. }
  406. }
  407. for (; dst_size; dst_size--)
  408. *dst++ = clear[pos++ % clear_size];
  409. }
  410. // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
  411. // if it's a subsampled packed format).
  412. #define MAX_BLOCK_SIZE 32
  413. int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
  414. enum AVPixelFormat pix_fmt, enum AVColorRange range,
  415. int width, int height)
  416. {
  417. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  418. int nb_planes = av_pix_fmt_count_planes(pix_fmt);
  419. // A pixel or a group of pixels on each plane, with a value that represents black.
  420. // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
  421. uint8_t clear_block[4][MAX_BLOCK_SIZE] = {0}; // clear padding with 0
  422. int clear_block_size[4] = {0};
  423. ptrdiff_t plane_line_bytes[4] = {0};
  424. int rgb, limited;
  425. int plane, c;
  426. if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  427. return AVERROR(EINVAL);
  428. rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
  429. limited = !rgb && range != AVCOL_RANGE_JPEG;
  430. if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
  431. ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
  432. uint8_t *data;
  433. int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
  434. int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
  435. if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
  436. return AVERROR(EINVAL);
  437. if (!dst_data)
  438. return 0;
  439. data = dst_data[0];
  440. // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
  441. for (;height > 0; height--) {
  442. memset(data, fill, bytewidth);
  443. data += dst_linesize[0];
  444. }
  445. return 0;
  446. }
  447. for (c = 0; c < desc->nb_components; c++) {
  448. const AVComponentDescriptor comp = desc->comp[c];
  449. // We try to operate on entire non-subsampled pixel groups (for
  450. // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
  451. clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
  452. if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
  453. return AVERROR(EINVAL);
  454. }
  455. // Create a byte array for clearing 1 pixel (sometimes several pixels).
  456. for (c = 0; c < desc->nb_components; c++) {
  457. const AVComponentDescriptor comp = desc->comp[c];
  458. // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
  459. int w = clear_block_size[comp.plane] / comp.step;
  460. uint8_t *c_data[4];
  461. const int c_linesize[4] = {0};
  462. uint16_t src_array[MAX_BLOCK_SIZE];
  463. uint16_t src = 0;
  464. int x;
  465. if (comp.depth > 16)
  466. return AVERROR(EINVAL);
  467. if (!rgb && comp.depth < 8)
  468. return AVERROR(EINVAL);
  469. if (w < 1)
  470. return AVERROR(EINVAL);
  471. if (c == 0 && limited) {
  472. src = 16 << (comp.depth - 8);
  473. } else if ((c == 1 || c == 2) && !rgb) {
  474. src = 128 << (comp.depth - 8);
  475. } else if (c == 3) {
  476. // (Assume even limited YUV uses full range alpha.)
  477. src = (1 << comp.depth) - 1;
  478. }
  479. for (x = 0; x < w; x++)
  480. src_array[x] = src;
  481. for (x = 0; x < 4; x++)
  482. c_data[x] = &clear_block[x][0];
  483. av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
  484. }
  485. for (plane = 0; plane < nb_planes; plane++) {
  486. plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
  487. if (plane_line_bytes[plane] < 0)
  488. return AVERROR(EINVAL);
  489. }
  490. if (!dst_data)
  491. return 0;
  492. for (plane = 0; plane < nb_planes; plane++) {
  493. size_t bytewidth = plane_line_bytes[plane];
  494. uint8_t *data = dst_data[plane];
  495. int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
  496. int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
  497. for (; plane_h > 0; plane_h--) {
  498. memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
  499. data += dst_linesize[plane];
  500. }
  501. }
  502. return 0;
  503. }