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.

662 lines
21KB

  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_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
  95. uint8_t *ptr, const int linesizes[4])
  96. {
  97. int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  99. memset(data , 0, sizeof(data[0])*4);
  100. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  101. return AVERROR(EINVAL);
  102. data[0] = ptr;
  103. if (linesizes[0] > (INT_MAX - 1024) / height)
  104. return AVERROR(EINVAL);
  105. size[0] = linesizes[0] * height;
  106. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  107. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  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 (align < 4) {
  189. av_log(NULL, AV_LOG_ERROR, "Formats with a palette require a minimum alignment of 4\n");
  190. return AVERROR(EINVAL);
  191. }
  192. }
  193. if ((desc->flags & AV_PIX_FMT_FLAG_PAL ||
  194. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) &&
  195. pointers[1] - pointers[0] > linesizes[0] * h) {
  196. /* zero-initialize the padding before the palette */
  197. memset(pointers[0] + linesizes[0] * h, 0,
  198. pointers[1] - pointers[0] - linesizes[0] * h);
  199. }
  200. return ret;
  201. }
  202. typedef struct ImgUtils {
  203. const AVClass *class;
  204. int log_offset;
  205. void *log_ctx;
  206. } ImgUtils;
  207. static const AVClass imgutils_class = {
  208. .class_name = "IMGUTILS",
  209. .item_name = av_default_item_name,
  210. .option = NULL,
  211. .version = LIBAVUTIL_VERSION_INT,
  212. .log_level_offset_offset = offsetof(ImgUtils, log_offset),
  213. .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
  214. };
  215. 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)
  216. {
  217. ImgUtils imgutils = {
  218. .class = &imgutils_class,
  219. .log_offset = log_offset,
  220. .log_ctx = log_ctx,
  221. };
  222. int64_t stride = av_image_get_linesize(pix_fmt, w, 0);
  223. if (stride <= 0)
  224. stride = 8LL*w;
  225. stride += 128*8;
  226. if ((int)w<=0 || (int)h<=0 || stride >= INT_MAX || stride*(uint64_t)(h+128) >= INT_MAX) {
  227. av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
  228. return AVERROR(EINVAL);
  229. }
  230. if (max_pixels < INT64_MAX) {
  231. if (w*(int64_t)h > max_pixels) {
  232. av_log(&imgutils, AV_LOG_ERROR,
  233. "Picture size %ux%u exceeds specified max pixel count %"PRId64", see the documentation if you wish to increase it\n",
  234. w, h, max_pixels);
  235. return AVERROR(EINVAL);
  236. }
  237. }
  238. return 0;
  239. }
  240. int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
  241. {
  242. return av_image_check_size2(w, h, INT64_MAX, AV_PIX_FMT_NONE, log_offset, log_ctx);
  243. }
  244. int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
  245. {
  246. int64_t scaled_dim;
  247. if (sar.den <= 0 || sar.num < 0)
  248. return AVERROR(EINVAL);
  249. if (!sar.num || sar.num == sar.den)
  250. return 0;
  251. if (sar.num < sar.den)
  252. scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
  253. else
  254. scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
  255. if (scaled_dim > 0)
  256. return 0;
  257. return AVERROR(EINVAL);
  258. }
  259. static void image_copy_plane(uint8_t *dst, ptrdiff_t dst_linesize,
  260. const uint8_t *src, ptrdiff_t src_linesize,
  261. ptrdiff_t bytewidth, int height)
  262. {
  263. if (!dst || !src)
  264. return;
  265. av_assert0(abs(src_linesize) >= bytewidth);
  266. av_assert0(abs(dst_linesize) >= bytewidth);
  267. for (;height > 0; height--) {
  268. memcpy(dst, src, bytewidth);
  269. dst += dst_linesize;
  270. src += src_linesize;
  271. }
  272. }
  273. static void image_copy_plane_uc_from(uint8_t *dst, ptrdiff_t dst_linesize,
  274. const uint8_t *src, ptrdiff_t src_linesize,
  275. ptrdiff_t bytewidth, int height)
  276. {
  277. int ret = -1;
  278. #if ARCH_X86
  279. ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
  280. bytewidth, height);
  281. #endif
  282. if (ret < 0)
  283. image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
  284. }
  285. void av_image_copy_plane(uint8_t *dst, int dst_linesize,
  286. const uint8_t *src, int src_linesize,
  287. int bytewidth, int height)
  288. {
  289. image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
  290. }
  291. static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
  292. const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
  293. enum AVPixelFormat pix_fmt, int width, int height,
  294. void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
  295. ptrdiff_t, ptrdiff_t, int))
  296. {
  297. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  298. if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  299. return;
  300. if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
  301. desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
  302. copy_plane(dst_data[0], dst_linesizes[0],
  303. src_data[0], src_linesizes[0],
  304. width, height);
  305. /* copy the palette */
  306. memcpy(dst_data[1], src_data[1], 4*256);
  307. } else {
  308. int i, planes_nb = 0;
  309. for (i = 0; i < desc->nb_components; i++)
  310. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  311. for (i = 0; i < planes_nb; i++) {
  312. int h = height;
  313. ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
  314. if (bwidth < 0) {
  315. av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
  316. return;
  317. }
  318. if (i == 1 || i == 2) {
  319. h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
  320. }
  321. copy_plane(dst_data[i], dst_linesizes[i],
  322. src_data[i], src_linesizes[i],
  323. bwidth, h);
  324. }
  325. }
  326. }
  327. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  328. const uint8_t *src_data[4], const int src_linesizes[4],
  329. enum AVPixelFormat pix_fmt, int width, int height)
  330. {
  331. ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
  332. int i;
  333. for (i = 0; i < 4; i++) {
  334. dst_linesizes1[i] = dst_linesizes[i];
  335. src_linesizes1[i] = src_linesizes[i];
  336. }
  337. image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
  338. width, height, image_copy_plane);
  339. }
  340. void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
  341. const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
  342. enum AVPixelFormat pix_fmt, int width, int height)
  343. {
  344. image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
  345. width, height, image_copy_plane_uc_from);
  346. }
  347. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  348. const uint8_t *src, enum AVPixelFormat pix_fmt,
  349. int width, int height, int align)
  350. {
  351. int ret, i;
  352. ret = av_image_check_size(width, height, 0, NULL);
  353. if (ret < 0)
  354. return ret;
  355. ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
  356. if (ret < 0)
  357. return ret;
  358. for (i = 0; i < 4; i++)
  359. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  360. return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
  361. }
  362. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
  363. int width, int height, int align)
  364. {
  365. uint8_t *data[4];
  366. int linesize[4];
  367. int ret;
  368. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  369. if (!desc)
  370. return AVERROR(EINVAL);
  371. ret = av_image_check_size(width, height, 0, NULL);
  372. if (ret < 0)
  373. return ret;
  374. // do not include palette for these pseudo-paletted formats
  375. if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
  376. return FFALIGN(width, align) * height;
  377. return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
  378. width, height, align);
  379. }
  380. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  381. const uint8_t * const src_data[4],
  382. const int src_linesize[4],
  383. enum AVPixelFormat pix_fmt,
  384. int width, int height, int align)
  385. {
  386. int i, j, nb_planes = 0, linesize[4];
  387. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  388. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  389. int ret;
  390. if (size > dst_size || size < 0 || !desc)
  391. return AVERROR(EINVAL);
  392. for (i = 0; i < desc->nb_components; i++)
  393. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  394. nb_planes++;
  395. ret = av_image_fill_linesizes(linesize, pix_fmt, width);
  396. av_assert0(ret >= 0); // was checked previously
  397. for (i = 0; i < nb_planes; i++) {
  398. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  399. const uint8_t *src = src_data[i];
  400. h = (height + (1 << shift) - 1) >> shift;
  401. for (j = 0; j < h; j++) {
  402. memcpy(dst, src, linesize[i]);
  403. dst += FFALIGN(linesize[i], align);
  404. src += src_linesize[i];
  405. }
  406. }
  407. if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
  408. uint32_t *d32 = (uint32_t *)dst;
  409. for (i = 0; i<256; i++)
  410. AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
  411. }
  412. return size;
  413. }
  414. // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
  415. // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
  416. // dst_size%clear_size!=0), the remaining data will be filled with the beginning
  417. // of the clear data only.
  418. static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
  419. size_t clear_size)
  420. {
  421. size_t pos = 0;
  422. int same = 1;
  423. int i;
  424. if (!clear_size)
  425. return;
  426. // Reduce to memset() if possible.
  427. for (i = 0; i < clear_size; i++) {
  428. if (clear[i] != clear[0]) {
  429. same = 0;
  430. break;
  431. }
  432. }
  433. if (same)
  434. clear_size = 1;
  435. if (clear_size == 1) {
  436. memset(dst, clear[0], dst_size);
  437. dst_size = 0;
  438. } else if (clear_size == 2) {
  439. uint16_t val = AV_RN16(clear);
  440. for (; dst_size >= 2; dst_size -= 2) {
  441. AV_WN16(dst, val);
  442. dst += 2;
  443. }
  444. } else if (clear_size == 4) {
  445. uint32_t val = AV_RN32(clear);
  446. for (; dst_size >= 4; dst_size -= 4) {
  447. AV_WN32(dst, val);
  448. dst += 4;
  449. }
  450. } else if (clear_size == 8) {
  451. uint32_t val = AV_RN64(clear);
  452. for (; dst_size >= 8; dst_size -= 8) {
  453. AV_WN64(dst, val);
  454. dst += 8;
  455. }
  456. }
  457. for (; dst_size; dst_size--)
  458. *dst++ = clear[pos++ % clear_size];
  459. }
  460. // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
  461. // if it's a subsampled packed format).
  462. #define MAX_BLOCK_SIZE 32
  463. int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
  464. enum AVPixelFormat pix_fmt, enum AVColorRange range,
  465. int width, int height)
  466. {
  467. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  468. int nb_planes = av_pix_fmt_count_planes(pix_fmt);
  469. // A pixel or a group of pixels on each plane, with a value that represents black.
  470. // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
  471. uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
  472. int clear_block_size[4] = {0};
  473. ptrdiff_t plane_line_bytes[4] = {0};
  474. int rgb, limited;
  475. int plane, c;
  476. if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  477. return AVERROR(EINVAL);
  478. rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
  479. limited = !rgb && range != AVCOL_RANGE_JPEG;
  480. if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
  481. ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
  482. uint8_t *data;
  483. int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
  484. int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
  485. if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
  486. return AVERROR(EINVAL);
  487. if (!dst_data)
  488. return 0;
  489. data = dst_data[0];
  490. // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
  491. for (;height > 0; height--) {
  492. memset(data, fill, bytewidth);
  493. data += dst_linesize[0];
  494. }
  495. return 0;
  496. }
  497. for (c = 0; c < desc->nb_components; c++) {
  498. const AVComponentDescriptor comp = desc->comp[c];
  499. // We try to operate on entire non-subsampled pixel groups (for
  500. // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
  501. clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
  502. if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
  503. return AVERROR(EINVAL);
  504. }
  505. // Create a byte array for clearing 1 pixel (sometimes several pixels).
  506. for (c = 0; c < desc->nb_components; c++) {
  507. const AVComponentDescriptor comp = desc->comp[c];
  508. // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
  509. int w = clear_block_size[comp.plane] / comp.step;
  510. uint8_t *c_data[4];
  511. const int c_linesize[4] = {0};
  512. uint16_t src_array[MAX_BLOCK_SIZE];
  513. uint16_t src = 0;
  514. int x;
  515. if (comp.depth > 16)
  516. return AVERROR(EINVAL);
  517. if (!rgb && comp.depth < 8)
  518. return AVERROR(EINVAL);
  519. if (w < 1)
  520. return AVERROR(EINVAL);
  521. if (c == 0 && limited) {
  522. src = 16 << (comp.depth - 8);
  523. } else if ((c == 1 || c == 2) && !rgb) {
  524. src = 128 << (comp.depth - 8);
  525. } else if (c == 3) {
  526. // (Assume even limited YUV uses full range alpha.)
  527. src = (1 << comp.depth) - 1;
  528. }
  529. for (x = 0; x < w; x++)
  530. src_array[x] = src;
  531. for (x = 0; x < 4; x++)
  532. c_data[x] = &clear_block[x][0];
  533. av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
  534. }
  535. for (plane = 0; plane < nb_planes; plane++) {
  536. plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
  537. if (plane_line_bytes[plane] < 0)
  538. return AVERROR(EINVAL);
  539. }
  540. if (!dst_data)
  541. return 0;
  542. for (plane = 0; plane < nb_planes; plane++) {
  543. size_t bytewidth = plane_line_bytes[plane];
  544. uint8_t *data = dst_data[plane];
  545. int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
  546. int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
  547. for (; plane_h > 0; plane_h--) {
  548. memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
  549. data += dst_linesize[plane];
  550. }
  551. }
  552. return 0;
  553. }