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.

697 lines
22KB

  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 "imgutils_internal.h"
  26. #include "internal.h"
  27. #include "intreadwrite.h"
  28. #include "log.h"
  29. #include "mathematics.h"
  30. #include "pixdesc.h"
  31. #include "rational.h"
  32. void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
  33. const AVPixFmtDescriptor *pixdesc)
  34. {
  35. int i;
  36. memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
  37. if (max_pixstep_comps)
  38. memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
  39. for (i = 0; i < 4; i++) {
  40. const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
  41. if (comp->step > max_pixsteps[comp->plane]) {
  42. max_pixsteps[comp->plane] = comp->step;
  43. if (max_pixstep_comps)
  44. max_pixstep_comps[comp->plane] = i;
  45. }
  46. }
  47. }
  48. static inline
  49. int image_get_linesize(int width, int plane,
  50. int max_step, int max_step_comp,
  51. const AVPixFmtDescriptor *desc)
  52. {
  53. int s, shifted_w, linesize;
  54. if (!desc)
  55. return AVERROR(EINVAL);
  56. if (width < 0)
  57. return AVERROR(EINVAL);
  58. s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
  59. shifted_w = ((width + (1 << s) - 1)) >> s;
  60. if (shifted_w && max_step > INT_MAX / shifted_w)
  61. return AVERROR(EINVAL);
  62. linesize = max_step * shifted_w;
  63. if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
  64. linesize = (linesize + 7) >> 3;
  65. return linesize;
  66. }
  67. int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
  68. {
  69. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  70. int max_step [4]; /* max pixel step for each plane */
  71. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  72. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  73. return AVERROR(EINVAL);
  74. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  75. return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
  76. }
  77. int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
  78. {
  79. int i, ret;
  80. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  81. int max_step [4]; /* max pixel step for each plane */
  82. int max_step_comp[4]; /* the component for each plane which has the max pixel step */
  83. memset(linesizes, 0, 4*sizeof(linesizes[0]));
  84. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  85. return AVERROR(EINVAL);
  86. av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
  87. for (i = 0; i < 4; i++) {
  88. if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
  89. return ret;
  90. linesizes[i] = ret;
  91. }
  92. return 0;
  93. }
  94. int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt,
  95. int height, const ptrdiff_t linesizes[4])
  96. {
  97. int i, has_plane[4] = { 0 };
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  99. memset(sizes , 0, sizeof(sizes[0])*4);
  100. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  101. return AVERROR(EINVAL);
  102. if (linesizes[0] > SIZE_MAX / height)
  103. return AVERROR(EINVAL);
  104. sizes[0] = linesizes[0] * (size_t)height;
  105. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  106. desc->flags & FF_PSEUDOPAL) {
  107. sizes[1] = 256 * 4; /* palette is stored here as 256 32 bits words */
  108. return 0;
  109. }
  110. for (i = 0; i < 4; i++)
  111. has_plane[desc->comp[i].plane] = 1;
  112. for (i = 1; i < 4 && has_plane[i]; i++) {
  113. int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  114. h = (height + (1 << s) - 1) >> s;
  115. if (linesizes[i] > SIZE_MAX / h)
  116. return AVERROR(EINVAL);
  117. sizes[i] = (size_t)h * linesizes[i];
  118. }
  119. return 0;
  120. }
  121. int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
  122. uint8_t *ptr, const int linesizes[4])
  123. {
  124. int i, ret;
  125. ptrdiff_t linesizes1[4];
  126. size_t sizes[4];
  127. memset(data , 0, sizeof(data[0])*4);
  128. for (i = 0; i < 4; i++)
  129. linesizes1[i] = linesizes[i];
  130. ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, linesizes1);
  131. if (ret < 0)
  132. return ret;
  133. ret = 0;
  134. for (i = 0; i < 4; i++) {
  135. if (sizes[i] > INT_MAX - ret)
  136. return AVERROR(EINVAL);
  137. ret += sizes[i];
  138. }
  139. data[0] = ptr;
  140. for (i = 1; i < 4 && sizes[i]; i++)
  141. data[i] = data[i - 1] + sizes[i - 1];
  142. return ret;
  143. }
  144. int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
  145. {
  146. int i;
  147. for (i = 0; i < 256; i++) {
  148. int r, g, b;
  149. switch (pix_fmt) {
  150. case AV_PIX_FMT_RGB8:
  151. r = (i>>5 )*36;
  152. g = ((i>>2)&7)*36;
  153. b = (i&3 )*85;
  154. break;
  155. case AV_PIX_FMT_BGR8:
  156. b = (i>>6 )*85;
  157. g = ((i>>3)&7)*36;
  158. r = (i&7 )*36;
  159. break;
  160. case AV_PIX_FMT_RGB4_BYTE:
  161. r = (i>>3 )*255;
  162. g = ((i>>1)&3)*85;
  163. b = (i&1 )*255;
  164. break;
  165. case AV_PIX_FMT_BGR4_BYTE:
  166. b = (i>>3 )*255;
  167. g = ((i>>1)&3)*85;
  168. r = (i&1 )*255;
  169. break;
  170. case AV_PIX_FMT_GRAY8:
  171. r = b = g = i;
  172. break;
  173. default:
  174. return AVERROR(EINVAL);
  175. }
  176. pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
  177. }
  178. return 0;
  179. }
  180. int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
  181. int w, int h, enum AVPixelFormat pix_fmt, int align)
  182. {
  183. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  184. int i, ret;
  185. ptrdiff_t linesizes1[4];
  186. size_t total_size, sizes[4];
  187. uint8_t *buf;
  188. if (!desc)
  189. return AVERROR(EINVAL);
  190. if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
  191. return ret;
  192. if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
  193. return ret;
  194. for (i = 0; i < 4; i++) {
  195. linesizes[i] = FFALIGN(linesizes[i], align);
  196. linesizes1[i] = linesizes[i];
  197. }
  198. if ((ret = av_image_fill_plane_sizes(sizes, pix_fmt, h, linesizes1)) < 0)
  199. return ret;
  200. total_size = align;
  201. for (i = 0; i < 4; i++) {
  202. if (total_size > SIZE_MAX - sizes[i])
  203. return AVERROR(EINVAL);
  204. total_size += sizes[i];
  205. }
  206. buf = av_malloc(total_size);
  207. if (!buf)
  208. return AVERROR(ENOMEM);
  209. if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
  210. av_free(buf);
  211. return ret;
  212. }
  213. if (desc->flags & AV_PIX_FMT_FLAG_PAL || (desc->flags & FF_PSEUDOPAL && pointers[1])) {
  214. avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
  215. if (align < 4) {
  216. av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n");
  217. av_free(buf);
  218. return AVERROR(EINVAL);
  219. }
  220. }
  221. if ((desc->flags & AV_PIX_FMT_FLAG_PAL ||
  222. desc->flags & FF_PSEUDOPAL) && pointers[1] &&
  223. pointers[1] - pointers[0] > linesizes[0] * h) {
  224. /* zero-initialize the padding before the palette */
  225. memset(pointers[0] + linesizes[0] * h, 0,
  226. pointers[1] - pointers[0] - linesizes[0] * h);
  227. }
  228. return ret;
  229. }
  230. typedef struct ImgUtils {
  231. const AVClass *class;
  232. int log_offset;
  233. void *log_ctx;
  234. } ImgUtils;
  235. static const AVClass imgutils_class = {
  236. .class_name = "IMGUTILS",
  237. .item_name = av_default_item_name,
  238. .option = NULL,
  239. .version = LIBAVUTIL_VERSION_INT,
  240. .log_level_offset_offset = offsetof(ImgUtils, log_offset),
  241. .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
  242. };
  243. int av_image_check_size2(unsigned int w, unsigned int h, int64_t max_pixels, enum AVPixelFormat pix_fmt, int log_offset, void *log_ctx)
  244. {
  245. ImgUtils imgutils = {
  246. .class = &imgutils_class,
  247. .log_offset = log_offset,
  248. .log_ctx = log_ctx,
  249. };
  250. int64_t stride = av_image_get_linesize(pix_fmt, w, 0);
  251. if (stride <= 0)
  252. stride = 8LL*w;
  253. stride += 128*8;
  254. if ((int)w<=0 || (int)h<=0 || stride >= INT_MAX || stride*(uint64_t)(h+128) >= INT_MAX) {
  255. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  256. return AVERROR(EINVAL);
  257. }
  258. if (max_pixels < INT64_MAX) {
  259. if (w*(int64_t)h > max_pixels) {
  260. av_log(&imgutils, AV_LOG_ERROR,
  261. "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n",
  262. w, h, max_pixels);
  263. return AVERROR(EINVAL);
  264. }
  265. }
  266. return 0;
  267. }
  268. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  269. {
  270. return av_image_check_size2(w, h, INT64_MAX, AV_PIX_FMT_NONE, log_offset, log_ctx);
  271. }
  272. int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
  273. {
  274. int64_t scaled_dim;
  275. if (sar.den <= 0 || sar.num < 0)
  276. return AVERROR(EINVAL);
  277. if (!sar.num || sar.num == sar.den)
  278. return 0;
  279. if (sar.num < sar.den)
  280. scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
  281. else
  282. scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
  283. if (scaled_dim > 0)
  284. return 0;
  285. return AVERROR(EINVAL);
  286. }
  287. static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
  288. const uint8_t *src, ptrdiff_t src_linesize,
  289. ptrdiff_t bytewidth, int height)
  290. {
  291. if (!dst || !src)
  292. return;
  293. av_assert0(FFABS(src_linesize) >= bytewidth);
  294. av_assert0(FFABS(dst_linesize) >= bytewidth);
  295. for (;height > 0; height--) {
  296. memcpy(dst, src, bytewidth);
  297. dst += dst_linesize;
  298. src += src_linesize;
  299. }
  300. }
  301. static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
  302. const uint8_t *src, ptrdiff_t src_linesize,
  303. ptrdiff_t bytewidth, int height)
  304. {
  305. int ret = -1;
  306. #if ARCH_X86
  307. ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
  308. bytewidth, height);
  309. #endif
  310. if (ret < 0)
  311. image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
  312. }
  313. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  314. const uint8_t *src, int src_linesize,
  315. int bytewidth, int height)
  316. {
  317. image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
  318. }
  319. static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
  320. const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
  321. enum AVPixelFormat pix_fmt, int width, int height,
  322. void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
  323. ptrdiff_t, ptrdiff_t, int))
  324. {
  325. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  326. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  327. return;
  328. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  329. desc->flags & FF_PSEUDOPAL) {
  330. copy_plane(dst_data[0], dst_linesizes[0],
  331. src_data[0], src_linesizes[0],
  332. width, height);
  333. /* copy the palette */
  334. if ((desc->flags & AV_PIX_FMT_FLAG_PAL) || (dst_data[1] && src_data[1]))
  335. memcpy(dst_data[1], src_data[1], 4*256);
  336. } else {
  337. int i, planes_nb = 0;
  338. for (i = 0; i < desc->nb_components; i++)
  339. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  340. for (i = 0; i < planes_nb; i++) {
  341. int h = height;
  342. ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
  343. if (bwidth < 0) {
  344. av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
  345. return;
  346. }
  347. if (i == 1 || i == 2) {
  348. h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
  349. }
  350. copy_plane(dst_data[i], dst_linesizes[i],
  351. src_data[i], src_linesizes[i],
  352. bwidth, h);
  353. }
  354. }
  355. }
  356. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  357. const uint8_t *src_data[4], const int src_linesizes[4],
  358. enum AVPixelFormat pix_fmt, int width, int height)
  359. {
  360. ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
  361. int i;
  362. for (i = 0; i < 4; i++) {
  363. dst_linesizes1[i] = dst_linesizes[i];
  364. src_linesizes1[i] = src_linesizes[i];
  365. }
  366. image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
  367. width, height, image_copy_plane);
  368. }
  369. void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
  370. const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
  371. enum AVPixelFormat pix_fmt, int width, int height)
  372. {
  373. image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
  374. width, height, image_copy_plane_uc_from);
  375. }
  376. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  377. const uint8_t *src, enum AVPixelFormat pix_fmt,
  378. int width, int height, int align)
  379. {
  380. int ret, i;
  381. ret = av_image_check_size(width, height, 0, NULL);
  382. if (ret < 0)
  383. return ret;
  384. ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
  385. if (ret < 0)
  386. return ret;
  387. for (i = 0; i < 4; i++)
  388. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  389. return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
  390. }
  391. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
  392. int width, int height, int align)
  393. {
  394. int ret, i;
  395. int linesize[4];
  396. ptrdiff_t aligned_linesize[4];
  397. size_t sizes[4];
  398. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  399. if (!desc)
  400. return AVERROR(EINVAL);
  401. ret = av_image_check_size(width, height, 0, NULL);
  402. if (ret < 0)
  403. return ret;
  404. // do not include palette for these pseudo-paletted formats
  405. if (desc->flags & FF_PSEUDOPAL)
  406. return FFALIGN(width, align) * height;
  407. ret = av_image_fill_linesizes(linesize, pix_fmt, width);
  408. if (ret < 0)
  409. return ret;
  410. for (i = 0; i < 4; i++)
  411. aligned_linesize[i] = FFALIGN(linesize[i], align);
  412. ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, aligned_linesize);
  413. if (ret < 0)
  414. return ret;
  415. ret = 0;
  416. for (i = 0; i < 4; i++) {
  417. if (sizes[i] > INT_MAX - ret)
  418. return AVERROR(EINVAL);
  419. ret += sizes[i];
  420. }
  421. return ret;
  422. }
  423. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  424. const uint8_t * const src_data[4],
  425. const int src_linesize[4],
  426. enum AVPixelFormat pix_fmt,
  427. int width, int height, int align)
  428. {
  429. int i, j, nb_planes = 0, linesize[4];
  430. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  431. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  432. int ret;
  433. if (size > dst_size || size < 0 || !desc)
  434. return AVERROR(EINVAL);
  435. for (i = 0; i < desc->nb_components; i++)
  436. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  437. nb_planes++;
  438. ret = av_image_fill_linesizes(linesize, pix_fmt, width);
  439. av_assert0(ret >= 0); // was checked previously
  440. for (i = 0; i < nb_planes; i++) {
  441. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  442. const uint8_t *src = src_data[i];
  443. h = (height + (1 << shift) - 1) >> shift;
  444. for (j = 0; j < h; j++) {
  445. memcpy(dst, src, linesize[i]);
  446. dst += FFALIGN(linesize[i], align);
  447. src += src_linesize[i];
  448. }
  449. }
  450. if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
  451. uint32_t *d32 = (uint32_t *)dst;
  452. for (i = 0; i<256; i++)
  453. AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
  454. }
  455. return size;
  456. }
  457. // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
  458. // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
  459. // dst_size%clear_size!=0), the remaining data will be filled with the beginning
  460. // of the clear data only.
  461. static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
  462. size_t clear_size)
  463. {
  464. int same = 1;
  465. int i;
  466. if (!clear_size)
  467. return;
  468. // Reduce to memset() if possible.
  469. for (i = 0; i < clear_size; i++) {
  470. if (clear[i] != clear[0]) {
  471. same = 0;
  472. break;
  473. }
  474. }
  475. if (same)
  476. clear_size = 1;
  477. if (clear_size == 1) {
  478. memset(dst, clear[0], dst_size);
  479. } else {
  480. if (clear_size > dst_size)
  481. clear_size = dst_size;
  482. memcpy(dst, clear, clear_size);
  483. av_memcpy_backptr(dst + clear_size, clear_size, dst_size - clear_size);
  484. }
  485. }
  486. // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
  487. // if it's a subsampled packed format).
  488. #define MAX_BLOCK_SIZE 32
  489. int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
  490. enum AVPixelFormat pix_fmt, enum AVColorRange range,
  491. int width, int height)
  492. {
  493. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  494. int nb_planes = av_pix_fmt_count_planes(pix_fmt);
  495. // A pixel or a group of pixels on each plane, with a value that represents black.
  496. // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
  497. uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
  498. int clear_block_size[4] = {0};
  499. ptrdiff_t plane_line_bytes[4] = {0};
  500. int rgb, limited;
  501. int plane, c;
  502. if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  503. return AVERROR(EINVAL);
  504. rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
  505. limited = !rgb && range != AVCOL_RANGE_JPEG;
  506. if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
  507. ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
  508. uint8_t *data;
  509. int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
  510. int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
  511. if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
  512. return AVERROR(EINVAL);
  513. if (!dst_data)
  514. return 0;
  515. data = dst_data[0];
  516. // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
  517. for (;height > 0; height--) {
  518. memset(data, fill, bytewidth);
  519. data += dst_linesize[0];
  520. }
  521. return 0;
  522. }
  523. for (c = 0; c < desc->nb_components; c++) {
  524. const AVComponentDescriptor comp = desc->comp[c];
  525. // We try to operate on entire non-subsampled pixel groups (for
  526. // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
  527. clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
  528. if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
  529. return AVERROR(EINVAL);
  530. }
  531. // Create a byte array for clearing 1 pixel (sometimes several pixels).
  532. for (c = 0; c < desc->nb_components; c++) {
  533. const AVComponentDescriptor comp = desc->comp[c];
  534. // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
  535. int w = clear_block_size[comp.plane] / comp.step;
  536. uint8_t *c_data[4];
  537. const int c_linesize[4] = {0};
  538. uint16_t src_array[MAX_BLOCK_SIZE];
  539. uint16_t src = 0;
  540. int x;
  541. if (comp.depth > 16)
  542. return AVERROR(EINVAL);
  543. if (!rgb && comp.depth < 8)
  544. return AVERROR(EINVAL);
  545. if (w < 1)
  546. return AVERROR(EINVAL);
  547. if (c == 0 && limited) {
  548. src = 16 << (comp.depth - 8);
  549. } else if ((c == 1 || c == 2) && !rgb) {
  550. src = 128 << (comp.depth - 8);
  551. } else if (c == 3) {
  552. // (Assume even limited YUV uses full range alpha.)
  553. src = (1 << comp.depth) - 1;
  554. }
  555. for (x = 0; x < w; x++)
  556. src_array[x] = src;
  557. for (x = 0; x < 4; x++)
  558. c_data[x] = &clear_block[x][0];
  559. av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
  560. }
  561. for (plane = 0; plane < nb_planes; plane++) {
  562. plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
  563. if (plane_line_bytes[plane] < 0)
  564. return AVERROR(EINVAL);
  565. }
  566. if (!dst_data)
  567. return 0;
  568. for (plane = 0; plane < nb_planes; plane++) {
  569. size_t bytewidth = plane_line_bytes[plane];
  570. uint8_t *data = dst_data[plane];
  571. int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
  572. int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
  573. for (; plane_h > 0; plane_h--) {
  574. memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
  575. data += dst_linesize[plane];
  576. }
  577. }
  578. return 0;
  579. }