imgutils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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_minus1+1) > max_pixsteps[comp->plane]) {
  41. max_pixsteps[comp->plane] = comp->step_minus1+1;
  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 ((unsigned)pix_fmt >= AV_PIX_FMT_NB || 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. size[0] = (size[0] + 3) & ~3;
  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. return ret;
  189. }
  190. typedef struct ImgUtils {
  191. const AVClass *class;
  192. int log_offset;
  193. void *log_ctx;
  194. } ImgUtils;
  195. static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
  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 <= 0 || sar.num < 0)
  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. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  220. const uint8_t *src, int src_linesize,
  221. int bytewidth, int height)
  222. {
  223. if (!dst || !src)
  224. return;
  225. av_assert0(abs(src_linesize) >= bytewidth);
  226. av_assert0(abs(dst_linesize) >= bytewidth);
  227. for (;height > 0; height--) {
  228. memcpy(dst, src, bytewidth);
  229. dst += dst_linesize;
  230. src += src_linesize;
  231. }
  232. }
  233. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  234. const uint8_t *src_data[4], const int src_linesizes[4],
  235. enum AVPixelFormat pix_fmt, int width, int height)
  236. {
  237. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  238. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  239. return;
  240. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  241. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  242. av_image_copy_plane(dst_data[0], dst_linesizes[0],
  243. src_data[0], src_linesizes[0],
  244. width, height);
  245. /* copy the palette */
  246. memcpy(dst_data[1], src_data[1], 4*256);
  247. } else {
  248. int i, planes_nb = 0;
  249. for (i = 0; i < desc->nb_components; i++)
  250. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  251. for (i = 0; i < planes_nb; i++) {
  252. int h = height;
  253. int bwidth = av_image_get_linesize(pix_fmt, width, i);
  254. if (bwidth < 0) {
  255. av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
  256. return;
  257. }
  258. if (i == 1 || i == 2) {
  259. h = FF_CEIL_RSHIFT(height, desc->log2_chroma_h);
  260. }
  261. av_image_copy_plane(dst_data[i], dst_linesizes[i],
  262. src_data[i], src_linesizes[i],
  263. bwidth, h);
  264. }
  265. }
  266. }
  267. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  268. const uint8_t *src,
  269. enum AVPixelFormat pix_fmt, int width, int height, int align)
  270. {
  271. int ret, i;
  272. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  273. return ret;
  274. if ((ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width)) < 0)
  275. return ret;
  276. for (i = 0; i < 4; i++)
  277. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  278. if ((ret = av_image_fill_pointers(dst_data, pix_fmt, width, NULL, dst_linesize)) < 0)
  279. return ret;
  280. return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
  281. }
  282. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
  283. {
  284. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  285. uint8_t *data[4];
  286. int linesize[4];
  287. if (!desc)
  288. return AVERROR(EINVAL);
  289. if (av_image_check_size(width, height, 0, NULL) < 0)
  290. return AVERROR(EINVAL);
  291. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  292. // do not include palette for these pseudo-paletted formats
  293. return width * height;
  294. return av_image_fill_arrays(data, linesize, NULL, pix_fmt, width, height, align);
  295. }
  296. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  297. const uint8_t * const src_data[4], const int src_linesize[4],
  298. enum AVPixelFormat pix_fmt, int width, int height, int align)
  299. {
  300. int i, j, nb_planes = 0, linesize[4];
  301. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  302. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  303. if (size > dst_size || size < 0)
  304. return AVERROR(EINVAL);
  305. for (i = 0; i < desc->nb_components; i++)
  306. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  307. nb_planes++;
  308. av_image_fill_linesizes(linesize, pix_fmt, width);
  309. for (i = 0; i < nb_planes; i++) {
  310. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  311. const uint8_t *src = src_data[i];
  312. h = (height + (1 << shift) - 1) >> shift;
  313. for (j = 0; j < h; j++) {
  314. memcpy(dst, src, linesize[i]);
  315. dst += FFALIGN(linesize[i], align);
  316. src += src_linesize[i];
  317. }
  318. }
  319. if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
  320. uint32_t *d32 = (uint32_t *)(((size_t)dst + 3) & ~3);
  321. for (i = 0; i<256; i++)
  322. AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
  323. }
  324. return size;
  325. }