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.

366 lines
12KB

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