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.

350 lines
11KB

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