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.

408 lines
13KB

  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 (!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. 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. if ((desc->flags & AV_PIX_FMT_FLAG_PAL ||
  189. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) &&
  190. pointers[1] - pointers[0] > linesizes[0] * h) {
  191. /* zero-initialize the padding before the palette */
  192. memset(pointers[0] + linesizes[0] * h, 0,
  193. pointers[1] - pointers[0] - linesizes[0] * h);
  194. }
  195. return ret;
  196. }
  197. typedef struct ImgUtils {
  198. const AVClass *class;
  199. int log_offset;
  200. void *log_ctx;
  201. } ImgUtils;
  202. static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
  203. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  204. {
  205. ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
  206. if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  207. return 0;
  208. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  209. return AVERROR(EINVAL);
  210. }
  211. int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
  212. {
  213. int64_t scaled_dim;
  214. if (sar.den <= 0 || sar.num < 0)
  215. return AVERROR(EINVAL);
  216. if (!sar.num || sar.num == sar.den)
  217. return 0;
  218. if (sar.num < sar.den)
  219. scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
  220. else
  221. scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
  222. if (scaled_dim > 0)
  223. return 0;
  224. return AVERROR(EINVAL);
  225. }
  226. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  227. const uint8_t *src, int src_linesize,
  228. int bytewidth, int height)
  229. {
  230. if (!dst || !src)
  231. return;
  232. av_assert0(abs(src_linesize) >= bytewidth);
  233. av_assert0(abs(dst_linesize) >= bytewidth);
  234. for (;height > 0; height--) {
  235. memcpy(dst, src, bytewidth);
  236. dst += dst_linesize;
  237. src += src_linesize;
  238. }
  239. }
  240. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  241. const uint8_t *src_data[4], const int src_linesizes[4],
  242. enum AVPixelFormat pix_fmt, int width, int height)
  243. {
  244. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  245. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  246. return;
  247. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  248. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  249. av_image_copy_plane(dst_data[0], dst_linesizes[0],
  250. src_data[0], src_linesizes[0],
  251. width, height);
  252. /* copy the palette */
  253. memcpy(dst_data[1], src_data[1], 4*256);
  254. } else {
  255. int i, planes_nb = 0;
  256. for (i = 0; i < desc->nb_components; i++)
  257. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  258. for (i = 0; i < planes_nb; i++) {
  259. int h = height;
  260. int bwidth = av_image_get_linesize(pix_fmt, width, i);
  261. if (bwidth < 0) {
  262. av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
  263. return;
  264. }
  265. if (i == 1 || i == 2) {
  266. h = FF_CEIL_RSHIFT(height, desc->log2_chroma_h);
  267. }
  268. av_image_copy_plane(dst_data[i], dst_linesizes[i],
  269. src_data[i], src_linesizes[i],
  270. bwidth, h);
  271. }
  272. }
  273. }
  274. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  275. const uint8_t *src, enum AVPixelFormat pix_fmt,
  276. int width, int height, int align)
  277. {
  278. int ret, i;
  279. ret = av_image_check_size(width, height, 0, NULL);
  280. if (ret < 0)
  281. return ret;
  282. ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
  283. if (ret < 0)
  284. return ret;
  285. for (i = 0; i < 4; i++)
  286. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  287. return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
  288. }
  289. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
  290. int width, int height, int align)
  291. {
  292. uint8_t *data[4];
  293. int linesize[4];
  294. int ret;
  295. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  296. if (!desc)
  297. return AVERROR(EINVAL);
  298. ret = av_image_check_size(width, height, 0, NULL);
  299. if (ret < 0)
  300. return ret;
  301. // do not include palette for these pseudo-paletted formats
  302. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  303. return width * height;
  304. return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
  305. width, height, align);
  306. }
  307. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  308. const uint8_t * const src_data[4],
  309. const int src_linesize[4],
  310. enum AVPixelFormat pix_fmt,
  311. int width, int height, int align)
  312. {
  313. int i, j, nb_planes = 0, linesize[4];
  314. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  315. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  316. if (size > dst_size || size < 0 || !desc)
  317. return AVERROR(EINVAL);
  318. for (i = 0; i < desc->nb_components; i++)
  319. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  320. nb_planes++;
  321. av_image_fill_linesizes(linesize, pix_fmt, width);
  322. for (i = 0; i < nb_planes; i++) {
  323. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  324. const uint8_t *src = src_data[i];
  325. h = (height + (1 << shift) - 1) >> shift;
  326. for (j = 0; j < h; j++) {
  327. memcpy(dst, src, linesize[i]);
  328. dst += FFALIGN(linesize[i], align);
  329. src += src_linesize[i];
  330. }
  331. }
  332. if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
  333. uint32_t *d32 = (uint32_t *)(((size_t)dst + 3) & ~3);
  334. for (i = 0; i<256; i++)
  335. AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
  336. }
  337. return size;
  338. }