imgutils.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 & FF_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 & FF_PSEUDOPAL && pointers[1])) {
  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 & FF_PSEUDOPAL) && pointers[1] &&
  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 & FF_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. if ((desc->flags & AV_PIX_FMT_FLAG_PAL) || (dst_data[1] && src_data[1]))
  307. memcpy(dst_data[1], src_data[1], 4*256);
  308. } else {
  309. int i, planes_nb = 0;
  310. for (i = 0; i < desc->nb_components; i++)
  311. planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
  312. for (i = 0; i < planes_nb; i++) {
  313. int h = height;
  314. ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
  315. if (bwidth < 0) {
  316. av_log(NULL, AV_LOG_ERROR, "av_image_get_linesize failed\n");
  317. return;
  318. }
  319. if (i == 1 || i == 2) {
  320. h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
  321. }
  322. copy_plane(dst_data[i], dst_linesizes[i],
  323. src_data[i], src_linesizes[i],
  324. bwidth, h);
  325. }
  326. }
  327. }
  328. void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
  329. const uint8_t *src_data[4], const int src_linesizes[4],
  330. enum AVPixelFormat pix_fmt, int width, int height)
  331. {
  332. ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
  333. int i;
  334. for (i = 0; i < 4; i++) {
  335. dst_linesizes1[i] = dst_linesizes[i];
  336. src_linesizes1[i] = src_linesizes[i];
  337. }
  338. image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
  339. width, height, image_copy_plane);
  340. }
  341. void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
  342. const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
  343. enum AVPixelFormat pix_fmt, int width, int height)
  344. {
  345. image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
  346. width, height, image_copy_plane_uc_from);
  347. }
  348. int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
  349. const uint8_t *src, enum AVPixelFormat pix_fmt,
  350. int width, int height, int align)
  351. {
  352. int ret, i;
  353. ret = av_image_check_size(width, height, 0, NULL);
  354. if (ret < 0)
  355. return ret;
  356. ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
  357. if (ret < 0)
  358. return ret;
  359. for (i = 0; i < 4; i++)
  360. dst_linesize[i] = FFALIGN(dst_linesize[i], align);
  361. return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
  362. }
  363. int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
  364. int width, int height, int align)
  365. {
  366. uint8_t *data[4];
  367. int linesize[4];
  368. int ret;
  369. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  370. if (!desc)
  371. return AVERROR(EINVAL);
  372. ret = av_image_check_size(width, height, 0, NULL);
  373. if (ret < 0)
  374. return ret;
  375. // do not include palette for these pseudo-paletted formats
  376. if (desc->flags & FF_PSEUDOPAL)
  377. return FFALIGN(width, align) * height;
  378. return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
  379. width, height, align);
  380. }
  381. int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
  382. const uint8_t * const src_data[4],
  383. const int src_linesize[4],
  384. enum AVPixelFormat pix_fmt,
  385. int width, int height, int align)
  386. {
  387. int i, j, nb_planes = 0, linesize[4];
  388. int size = av_image_get_buffer_size(pix_fmt, width, height, align);
  389. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  390. int ret;
  391. if (size > dst_size || size < 0 || !desc)
  392. return AVERROR(EINVAL);
  393. for (i = 0; i < desc->nb_components; i++)
  394. nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
  395. nb_planes++;
  396. ret = av_image_fill_linesizes(linesize, pix_fmt, width);
  397. av_assert0(ret >= 0); // was checked previously
  398. for (i = 0; i < nb_planes; i++) {
  399. int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
  400. const uint8_t *src = src_data[i];
  401. h = (height + (1 << shift) - 1) >> shift;
  402. for (j = 0; j < h; j++) {
  403. memcpy(dst, src, linesize[i]);
  404. dst += FFALIGN(linesize[i], align);
  405. src += src_linesize[i];
  406. }
  407. }
  408. if (desc->flags & AV_PIX_FMT_FLAG_PAL) {
  409. uint32_t *d32 = (uint32_t *)dst;
  410. for (i = 0; i<256; i++)
  411. AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
  412. }
  413. return size;
  414. }
  415. // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
  416. // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
  417. // dst_size%clear_size!=0), the remaining data will be filled with the beginning
  418. // of the clear data only.
  419. static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
  420. size_t clear_size)
  421. {
  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 {
  439. if (clear_size > dst_size)
  440. clear_size = dst_size;
  441. memcpy(dst, clear, clear_size);
  442. av_memcpy_backptr(dst + clear_size, clear_size, dst_size - clear_size);
  443. }
  444. }
  445. // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
  446. // if it's a subsampled packed format).
  447. #define MAX_BLOCK_SIZE 32
  448. int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
  449. enum AVPixelFormat pix_fmt, enum AVColorRange range,
  450. int width, int height)
  451. {
  452. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  453. int nb_planes = av_pix_fmt_count_planes(pix_fmt);
  454. // A pixel or a group of pixels on each plane, with a value that represents black.
  455. // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
  456. uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
  457. int clear_block_size[4] = {0};
  458. ptrdiff_t plane_line_bytes[4] = {0};
  459. int rgb, limited;
  460. int plane, c;
  461. if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
  462. return AVERROR(EINVAL);
  463. rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
  464. limited = !rgb && range != AVCOL_RANGE_JPEG;
  465. if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
  466. ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
  467. uint8_t *data;
  468. int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
  469. int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
  470. if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
  471. return AVERROR(EINVAL);
  472. if (!dst_data)
  473. return 0;
  474. data = dst_data[0];
  475. // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
  476. for (;height > 0; height--) {
  477. memset(data, fill, bytewidth);
  478. data += dst_linesize[0];
  479. }
  480. return 0;
  481. }
  482. for (c = 0; c < desc->nb_components; c++) {
  483. const AVComponentDescriptor comp = desc->comp[c];
  484. // We try to operate on entire non-subsampled pixel groups (for
  485. // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
  486. clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
  487. if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
  488. return AVERROR(EINVAL);
  489. }
  490. // Create a byte array for clearing 1 pixel (sometimes several pixels).
  491. for (c = 0; c < desc->nb_components; c++) {
  492. const AVComponentDescriptor comp = desc->comp[c];
  493. // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
  494. int w = clear_block_size[comp.plane] / comp.step;
  495. uint8_t *c_data[4];
  496. const int c_linesize[4] = {0};
  497. uint16_t src_array[MAX_BLOCK_SIZE];
  498. uint16_t src = 0;
  499. int x;
  500. if (comp.depth > 16)
  501. return AVERROR(EINVAL);
  502. if (!rgb && comp.depth < 8)
  503. return AVERROR(EINVAL);
  504. if (w < 1)
  505. return AVERROR(EINVAL);
  506. if (c == 0 && limited) {
  507. src = 16 << (comp.depth - 8);
  508. } else if ((c == 1 || c == 2) && !rgb) {
  509. src = 128 << (comp.depth - 8);
  510. } else if (c == 3) {
  511. // (Assume even limited YUV uses full range alpha.)
  512. src = (1 << comp.depth) - 1;
  513. }
  514. for (x = 0; x < w; x++)
  515. src_array[x] = src;
  516. for (x = 0; x < 4; x++)
  517. c_data[x] = &clear_block[x][0];
  518. av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
  519. }
  520. for (plane = 0; plane < nb_planes; plane++) {
  521. plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
  522. if (plane_line_bytes[plane] < 0)
  523. return AVERROR(EINVAL);
  524. }
  525. if (!dst_data)
  526. return 0;
  527. for (plane = 0; plane < nb_planes; plane++) {
  528. size_t bytewidth = plane_line_bytes[plane];
  529. uint8_t *data = dst_data[plane];
  530. int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
  531. int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
  532. for (; plane_h > 0; plane_h--) {
  533. memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
  534. data += dst_linesize[plane];
  535. }
  536. }
  537. return 0;
  538. }