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.

426 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. data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
  108. return size[0] + 256 * 4;
  109. }
  110. for (i = 0; i < 4; i++)
  111. has_plane[desc->comp[i].plane] = 1;
  112. total_size = size[0];
  113. for (i = 1; i < 4 && has_plane[i]; i++) {
  114. int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  115. data[i] = data[i-1] + size[i-1];
  116. h = (height + (1 << s) - 1) >> s;
  117. if (linesizes[i] > INT_MAX / h)
  118. return AVERROR(EINVAL);
  119. size[i] = h * linesizes[i];
  120. if (total_size > INT_MAX - size[i])
  121. return AVERROR(EINVAL);
  122. total_size += size[i];
  123. }
  124. return total_size;
  125. }
  126. int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
  127. {
  128. int i;
  129. for (i = 0; i < 256; i++) {
  130. int r, g, b;
  131. switch (pix_fmt) {
  132. case AV_PIX_FMT_RGB8:
  133. r = (i>>5 )*36;
  134. g = ((i>>2)&7)*36;
  135. b = (i&3 )*85;
  136. break;
  137. case AV_PIX_FMT_BGR8:
  138. b = (i>>6 )*85;
  139. g = ((i>>3)&7)*36;
  140. r = (i&7 )*36;
  141. break;
  142. case AV_PIX_FMT_RGB4_BYTE:
  143. r = (i>>3 )*255;
  144. g = ((i>>1)&3)*85;
  145. b = (i&1 )*255;
  146. break;
  147. case AV_PIX_FMT_BGR4_BYTE:
  148. b = (i>>3 )*255;
  149. g = ((i>>1)&3)*85;
  150. r = (i&1 )*255;
  151. break;
  152. case AV_PIX_FMT_GRAY8:
  153. r = b = g = i;
  154. break;
  155. default:
  156. return AVERROR(EINVAL);
  157. }
  158. pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
  159. }
  160. return 0;
  161. }
  162. int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
  163. int w, int h, enum AVPixelFormat pix_fmt, int align)
  164. {
  165. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  166. int i, ret;
  167. uint8_t *buf;
  168. if (!desc)
  169. return AVERROR(EINVAL);
  170. if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
  171. return ret;
  172. if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
  173. return ret;
  174. for (i = 0; i < 4; i++)
  175. linesizes[i] = FFALIGN(linesizes[i], align);
  176. if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
  177. return ret;
  178. buf = av_malloc(ret + align);
  179. if (!buf)
  180. return AVERROR(ENOMEM);
  181. if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
  182. av_free(buf);
  183. return ret;
  184. }
  185. if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  186. avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
  187. if (align < 4) {
  188. av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n");
  189. return AVERROR(EINVAL);
  190. }
  191. }
  192. if ((desc->flags & AV_PIX_FMT_FLAG_PAL ||
  193. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) &&
  194. pointers[1] - pointers[0] > linesizes[0] * h) {
  195. /* zero-initialize the padding before the palette */
  196. memset(pointers[0] + linesizes[0] * h, 0,
  197. pointers[1] - pointers[0] - linesizes[0] * h);
  198. }
  199. return ret;
  200. }
  201. typedef struct ImgUtils {
  202. const AVClass *class;
  203. int log_offset;
  204. void *log_ctx;
  205. } ImgUtils;
  206. static const AVClass imgutils_class = {
  207. .class_name = "IMGUTILS",
  208. .item_name = av_default_item_name,
  209. .version = LIBAVUTIL_VERSION_INT,
  210. .log_level_offset_offset = offsetof(ImgUtils, log_offset),
  211. .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
  212. };
  213. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  214. {
  215. ImgUtils imgutils = {
  216. .class = &imgutils_class,
  217. .log_offset = log_offset,
  218. .log_ctx = log_ctx,
  219. };
  220. if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
  221. return 0;
  222. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  223. return AVERROR(EINVAL);
  224. }
  225. int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
  226. {
  227. int64_t scaled_dim;
  228. if (sar.den <= 0 || sar.num < 0)
  229. return AVERROR(EINVAL);
  230. if (!sar.num || sar.num == sar.den)
  231. return 0;
  232. if (sar.num < sar.den)
  233. scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
  234. else
  235. scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
  236. if (scaled_dim > 0)
  237. return 0;
  238. return AVERROR(EINVAL);
  239. }
  240. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  241. const uint8_t *src, int src_linesize,
  242. int bytewidth, int height)
  243. {
  244. if (!dst || !src)
  245. return;
  246. av_assert0(abs(src_linesize) >= bytewidth);
  247. av_assert0(abs(dst_linesize) >= bytewidth);
  248. for (;height > 0; height--) {
  249. memcpy(dst, src, bytewidth);
  250. dst += dst_linesize;
  251. src += src_linesize;
  252. }
  253. }
  254. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  255. const uint8_t *src_data[4], const int src_linesizes[4],
  256. enum AVPixelFormat pix_fmt, int width, int height)
  257. {
  258. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  259. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  260. return;
  261. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  262. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  263. av_image_copy_plane(dst_data[0], dst_linesizes[0],
  264. src_data[0], src_linesizes[0],
  265. width, height);
  266. /* copy the palette */
  267. memcpy(dst_data[1], src_data[1], 4*256);
  268. } else {
  269. int i, planes_nb = 0;
  270. for (i = 0; i < desc->nb_components; i++)
  271. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  272. for (i = 0; i < planes_nb; i++) {
  273. int h = height;
  274. int bwidth = av_image_get_linesize(pix_fmt, width, i);
  275. if (bwidth < 0) {
  276. av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
  277. return;
  278. }
  279. if (i == 1 || i == 2) {
  280. h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
  281. }
  282. av_image_copy_plane(dst_data[i], dst_linesizes[i],
  283. src_data[i], src_linesizes[i],
  284. bwidth, h);
  285. }
  286. }
  287. }
  288. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  289. const uint8_t *src, enum AVPixelFormat pix_fmt,
  290. int width, int height, int align)
  291. {
  292. int ret, i;
  293. ret = av_image_check_size(width, height, 0, NULL);
  294. if (ret < 0)
  295. return ret;
  296. ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
  297. if (ret < 0)
  298. return ret;
  299. for (i = 0; i < 4; i++)
  300. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  301. return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
  302. }
  303. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
  304. int width, int height, int align)
  305. {
  306. uint8_t *data[4];
  307. int linesize[4];
  308. int ret;
  309. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  310. if (!desc)
  311. return AVERROR(EINVAL);
  312. ret = av_image_check_size(width, height, 0, NULL);
  313. if (ret < 0)
  314. return ret;
  315. // do not include palette for these pseudo-paletted formats
  316. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  317. return FFALIGN(width, align) * height;
  318. return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
  319. width, height, align);
  320. }
  321. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  322. const uint8_t * const src_data[4],
  323. const int src_linesize[4],
  324. enum AVPixelFormat pix_fmt,
  325. int width, int height, int align)
  326. {
  327. int i, j, nb_planes = 0, linesize[4];
  328. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  329. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  330. int ret;
  331. if (size > dst_size || size < 0 || !desc)
  332. return AVERROR(EINVAL);
  333. for (i = 0; i < desc->nb_components; i++)
  334. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  335. nb_planes++;
  336. ret = av_image_fill_linesizes(linesize, pix_fmt, width);
  337. av_assert0(ret >= 0); // was checked previously
  338. for (i = 0; i < nb_planes; i++) {
  339. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  340. const uint8_t *src = src_data[i];
  341. h = (height + (1 << shift) - 1) >> shift;
  342. for (j = 0; j < h; j++) {
  343. memcpy(dst, src, linesize[i]);
  344. dst += FFALIGN(linesize[i], align);
  345. src += src_linesize[i];
  346. }
  347. }
  348. if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
  349. uint32_t *d32 = (uint32_t *)dst;
  350. for (i = 0; i<256; i++)
  351. AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
  352. }
  353. return size;
  354. }