imgutils.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "pixdesc.h"
  29. void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
  30. const AVPixFmtDescriptor *pixdesc)
  31. {
  32. int i;
  33. memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
  34. if (max_pixstep_comps)
  35. memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
  36. for (i = 0; i < 4; i++) {
  37. const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
  38. if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
  39. max_pixsteps[comp->plane] = comp->step_minus1+1;
  40. if (max_pixstep_comps)
  41. max_pixstep_comps[comp->plane] = i;
  42. }
  43. }
  44. }
  45. static inline
  46. int image_get_linesize(int width, int plane,
  47. int max_step, int max_step_comp,
  48. const AVPixFmtDescriptor *desc)
  49. {
  50. int s, shifted_w, linesize;
  51. if (!desc)
  52. return AVERROR(EINVAL);
  53. if (width < 0)
  54. return AVERROR(EINVAL);
  55. s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
  56. shifted_w = ((width + (1 << s) - 1)) >> s;
  57. if (shifted_w && max_step > INT_MAX / shifted_w)
  58. return AVERROR(EINVAL);
  59. linesize = max_step * shifted_w;
  60. if (desc->flags & PIX_FMT_BITSTREAM)
  61. linesize = (linesize + 7) >> 3;
  62. return linesize;
  63. }
  64. int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
  65. {
  66. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  67. int max_step [4]; /* max pixel step for each plane */
  68. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  69. if ((unsigned)pix_fmt >= AV_PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
  70. return AVERROR(EINVAL);
  71. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  72. return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
  73. }
  74. int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
  75. {
  76. int i, ret;
  77. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  78. int max_step [4]; /* max pixel step for each plane */
  79. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  80. memset(linesizes, 0, 4*sizeof(linesizes[0]));
  81. if (!desc || desc->flags & PIX_FMT_HWACCEL)
  82. return AVERROR(EINVAL);
  83. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  84. for (i = 0; i < 4; i++) {
  85. if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
  86. return ret;
  87. linesizes[i] = ret;
  88. }
  89. return 0;
  90. }
  91. int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
  92. uint8_t *ptr, const int linesizes[4])
  93. {
  94. int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
  95. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  96. memset(data , 0, sizeof(data[0])*4);
  97. if (!desc || desc->flags & PIX_FMT_HWACCEL)
  98. return AVERROR(EINVAL);
  99. data[0] = ptr;
  100. if (linesizes[0] > (INT_MAX - 1024) / height)
  101. return AVERROR(EINVAL);
  102. size[0] = linesizes[0] * height;
  103. if (desc->flags & PIX_FMT_PAL ||
  104. desc->flags & PIX_FMT_PSEUDOPAL) {
  105. size[0] = (size[0] + 3) & ~3;
  106. data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
  107. return size[0] + 256 * 4;
  108. }
  109. for (i = 0; i < 4; i++)
  110. has_plane[desc->comp[i].plane] = 1;
  111. total_size = size[0];
  112. for (i = 1; i < 4 && has_plane[i]; i++) {
  113. int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  114. data[i] = data[i-1] + size[i-1];
  115. h = (height + (1 << s) - 1) >> s;
  116. if (linesizes[i] > INT_MAX / h)
  117. return AVERROR(EINVAL);
  118. size[i] = h * linesizes[i];
  119. if (total_size > INT_MAX - size[i])
  120. return AVERROR(EINVAL);
  121. total_size += size[i];
  122. }
  123. return total_size;
  124. }
  125. int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
  126. {
  127. int i;
  128. for (i = 0; i < 256; i++) {
  129. int r, g, b;
  130. switch (pix_fmt) {
  131. case AV_PIX_FMT_RGB8:
  132. r = (i>>5 )*36;
  133. g = ((i>>2)&7)*36;
  134. b = (i&3 )*85;
  135. break;
  136. case AV_PIX_FMT_BGR8:
  137. b = (i>>6 )*85;
  138. g = ((i>>3)&7)*36;
  139. r = (i&7 )*36;
  140. break;
  141. case AV_PIX_FMT_RGB4_BYTE:
  142. r = (i>>3 )*255;
  143. g = ((i>>1)&3)*85;
  144. b = (i&1 )*255;
  145. break;
  146. case AV_PIX_FMT_BGR4_BYTE:
  147. b = (i>>3 )*255;
  148. g = ((i>>1)&3)*85;
  149. r = (i&1 )*255;
  150. break;
  151. case AV_PIX_FMT_GRAY8:
  152. r = b = g = i;
  153. break;
  154. default:
  155. return AVERROR(EINVAL);
  156. }
  157. pal[i] = b + (g<<8) + (r<<16) + (0xFFU<<24);
  158. }
  159. return 0;
  160. }
  161. int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
  162. int w, int h, enum AVPixelFormat pix_fmt, int align)
  163. {
  164. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  165. int i, ret;
  166. uint8_t *buf;
  167. if (!desc)
  168. return AVERROR(EINVAL);
  169. if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
  170. return ret;
  171. if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
  172. return ret;
  173. for (i = 0; i < 4; i++)
  174. linesizes[i] = FFALIGN(linesizes[i], align);
  175. if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
  176. return ret;
  177. buf = av_malloc(ret + align);
  178. if (!buf)
  179. return AVERROR(ENOMEM);
  180. if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
  181. av_free(buf);
  182. return ret;
  183. }
  184. if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)
  185. avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
  186. return ret;
  187. }
  188. typedef struct ImgUtils {
  189. const AVClass *class;
  190. int log_offset;
  191. void *log_ctx;
  192. } ImgUtils;
  193. static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
  194. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  195. {
  196. ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
  197. if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  198. return 0;
  199. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  200. return AVERROR(EINVAL);
  201. }
  202. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  203. const uint8_t *src, int src_linesize,
  204. int bytewidth, int height)
  205. {
  206. if (!dst || !src)
  207. return;
  208. av_assert0(abs(src_linesize) >= bytewidth);
  209. av_assert0(abs(dst_linesize) >= bytewidth);
  210. for (;height > 0; height--) {
  211. memcpy(dst, src, bytewidth);
  212. dst += dst_linesize;
  213. src += src_linesize;
  214. }
  215. }
  216. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  217. const uint8_t *src_data[4], const int src_linesizes[4],
  218. enum AVPixelFormat pix_fmt, int width, int height)
  219. {
  220. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  221. if (!desc || desc->flags & PIX_FMT_HWACCEL)
  222. return;
  223. if (desc->flags & PIX_FMT_PAL ||
  224. desc->flags & PIX_FMT_PSEUDOPAL) {
  225. av_image_copy_plane(dst_data[0], dst_linesizes[0],
  226. src_data[0], src_linesizes[0],
  227. width, height);
  228. /* copy the palette */
  229. memcpy(dst_data[1], src_data[1], 4*256);
  230. } else {
  231. int i, planes_nb = 0;
  232. for (i = 0; i < desc->nb_components; i++)
  233. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  234. for (i = 0; i < planes_nb; i++) {
  235. int h = height;
  236. int bwidth = av_image_get_linesize(pix_fmt, width, i);
  237. if (bwidth < 0) {
  238. av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
  239. return;
  240. }
  241. if (i == 1 || i == 2) {
  242. h= -((-height)>>desc->log2_chroma_h);
  243. }
  244. av_image_copy_plane(dst_data[i], dst_linesizes[i],
  245. src_data[i], src_linesizes[i],
  246. bwidth, h);
  247. }
  248. }
  249. }
  250. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  251. const uint8_t *src,
  252. enum AVPixelFormat pix_fmt, int width, int height, int align)
  253. {
  254. int ret, i;
  255. if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
  256. return ret;
  257. if ((ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width)) < 0)
  258. return ret;
  259. for (i = 0; i < 4; i++)
  260. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  261. if ((ret = av_image_fill_pointers(dst_data, pix_fmt, width, NULL, dst_linesize)) < 0)
  262. return ret;
  263. return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
  264. }
  265. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
  266. {
  267. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  268. uint8_t *data[4];
  269. int linesize[4];
  270. if (!desc)
  271. return AVERROR(EINVAL);
  272. if (av_image_check_size(width, height, 0, NULL) < 0)
  273. return AVERROR(EINVAL);
  274. if (desc->flags & PIX_FMT_PSEUDOPAL)
  275. // do not include palette for these pseudo-paletted formats
  276. return width * height;
  277. return av_image_fill_arrays(data, linesize, NULL, pix_fmt, width, height, align);
  278. }
  279. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  280. const uint8_t * const src_data[4], const int src_linesize[4],
  281. enum AVPixelFormat pix_fmt, int width, int height, int align)
  282. {
  283. int i, j, nb_planes = 0, linesize[4];
  284. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  285. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  286. if (size > dst_size || size < 0)
  287. return AVERROR(EINVAL);
  288. for (i = 0; i < desc->nb_components; i++)
  289. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  290. nb_planes++;
  291. av_image_fill_linesizes(linesize, pix_fmt, width);
  292. for (i = 0; i < nb_planes; i++) {
  293. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  294. const uint8_t *src = src_data[i];
  295. h = (height + (1 << shift) - 1) >> shift;
  296. for (j = 0; j < h; j++) {
  297. memcpy(dst, src, linesize[i]);
  298. dst += FFALIGN(linesize[i], align);
  299. src += src_linesize[i];
  300. }
  301. }
  302. if (desc->flags & PIX_FMT_PAL) {
  303. uint32_t *d32 = (uint32_t *)(((size_t)dst + 3) & ~3);
  304. for (i = 0; i<256; i++)
  305. AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
  306. }
  307. return size;
  308. }