imgutils.c 14 KB

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