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.

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