drawutils.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright 2011 Stefano Sabatini <stefano.sabatini-lala poste it>
  3. * Copyright 2012 Nicolas George <nicolas.george normalesup org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <string.h>
  22. #include "libavutil/avutil.h"
  23. #include "libavutil/colorspace.h"
  24. #include "libavutil/mem.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "drawutils.h"
  27. #include "formats.h"
  28. enum { RED = 0, GREEN, BLUE, ALPHA };
  29. int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
  30. {
  31. switch (pix_fmt) {
  32. case AV_PIX_FMT_0RGB:
  33. case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
  34. case AV_PIX_FMT_0BGR:
  35. case AV_PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
  36. case AV_PIX_FMT_RGB48LE:
  37. case AV_PIX_FMT_RGB48BE:
  38. case AV_PIX_FMT_RGBA64BE:
  39. case AV_PIX_FMT_RGBA64LE:
  40. case AV_PIX_FMT_RGB0:
  41. case AV_PIX_FMT_RGBA:
  42. case AV_PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
  43. case AV_PIX_FMT_BGR48LE:
  44. case AV_PIX_FMT_BGR48BE:
  45. case AV_PIX_FMT_BGRA64BE:
  46. case AV_PIX_FMT_BGRA64LE:
  47. case AV_PIX_FMT_BGRA:
  48. case AV_PIX_FMT_BGR0:
  49. case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
  50. case AV_PIX_FMT_GBRAP:
  51. case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
  52. default: /* unsupported */
  53. return AVERROR(EINVAL);
  54. }
  55. return 0;
  56. }
  57. int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
  58. enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],
  59. int *is_packed_rgba, uint8_t rgba_map_ptr[4])
  60. {
  61. uint8_t rgba_map[4] = {0};
  62. int i;
  63. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);
  64. int hsub = pix_desc->log2_chroma_w;
  65. *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
  66. if (*is_packed_rgba) {
  67. pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
  68. for (i = 0; i < 4; i++)
  69. dst_color[rgba_map[i]] = rgba_color[i];
  70. line[0] = av_malloc(w * pixel_step[0]);
  71. for (i = 0; i < w; i++)
  72. memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
  73. if (rgba_map_ptr)
  74. memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
  75. } else {
  76. int plane;
  77. dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
  78. dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  79. dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  80. dst_color[3] = rgba_color[3];
  81. for (plane = 0; plane < 4; plane++) {
  82. int line_size;
  83. int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
  84. pixel_step[plane] = 1;
  85. line_size = FF_CEIL_RSHIFT(w, hsub1) * pixel_step[plane];
  86. line[plane] = av_malloc(line_size);
  87. memset(line[plane], dst_color[plane], line_size);
  88. }
  89. }
  90. return 0;
  91. }
  92. void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
  93. uint8_t *src[4], int pixelstep[4],
  94. int hsub, int vsub, int x, int y, int w, int h)
  95. {
  96. int i, plane;
  97. uint8_t *p;
  98. for (plane = 0; plane < 4 && dst[plane]; plane++) {
  99. int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
  100. int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
  101. int width = FF_CEIL_RSHIFT(w, hsub1);
  102. int height = FF_CEIL_RSHIFT(h, vsub1);
  103. p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
  104. for (i = 0; i < height; i++) {
  105. memcpy(p + (x >> hsub1) * pixelstep[plane],
  106. src[plane], width * pixelstep[plane]);
  107. p += dst_linesize[plane];
  108. }
  109. }
  110. }
  111. void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
  112. uint8_t *src[4], int src_linesize[4], int pixelstep[4],
  113. int hsub, int vsub, int x, int y, int y2, int w, int h)
  114. {
  115. int i, plane;
  116. uint8_t *p;
  117. for (plane = 0; plane < 4 && dst[plane]; plane++) {
  118. int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
  119. int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
  120. int width = FF_CEIL_RSHIFT(w, hsub1);
  121. int height = FF_CEIL_RSHIFT(h, vsub1);
  122. p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
  123. for (i = 0; i < height; i++) {
  124. memcpy(p + (x >> hsub1) * pixelstep[plane],
  125. src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), width * pixelstep[plane]);
  126. p += dst_linesize[plane];
  127. }
  128. }
  129. }
  130. int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
  131. {
  132. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  133. const AVComponentDescriptor *c;
  134. unsigned i, nb_planes = 0;
  135. int pixelstep[MAX_PLANES] = { 0 };
  136. if (!desc->name)
  137. return AVERROR(EINVAL);
  138. if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA))
  139. return AVERROR(ENOSYS);
  140. for (i = 0; i < desc->nb_components; i++) {
  141. c = &desc->comp[i];
  142. /* for now, only 8-bits formats */
  143. if (c->depth_minus1 != 8 - 1)
  144. return AVERROR(ENOSYS);
  145. if (c->plane >= MAX_PLANES)
  146. return AVERROR(ENOSYS);
  147. /* strange interleaving */
  148. if (pixelstep[c->plane] != 0 &&
  149. pixelstep[c->plane] != c->step_minus1 + 1)
  150. return AVERROR(ENOSYS);
  151. pixelstep[c->plane] = c->step_minus1 + 1;
  152. if (pixelstep[c->plane] >= 8)
  153. return AVERROR(ENOSYS);
  154. nb_planes = FFMAX(nb_planes, c->plane + 1);
  155. }
  156. if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
  157. return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
  158. memset(draw, 0, sizeof(*draw));
  159. draw->desc = desc;
  160. draw->format = format;
  161. draw->nb_planes = nb_planes;
  162. memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
  163. draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
  164. draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
  165. for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
  166. draw->comp_mask[desc->comp[i].plane] |=
  167. 1 << (desc->comp[i].offset_plus1 - 1);
  168. return 0;
  169. }
  170. void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
  171. {
  172. unsigned i;
  173. uint8_t rgba_map[4];
  174. if (rgba != color->rgba)
  175. memcpy(color->rgba, rgba, sizeof(color->rgba));
  176. if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  177. ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
  178. if (draw->nb_planes == 1) {
  179. for (i = 0; i < 4; i++)
  180. color->comp[0].u8[rgba_map[i]] = rgba[i];
  181. } else {
  182. for (i = 0; i < 4; i++)
  183. color->comp[rgba_map[i]].u8[0] = rgba[i];
  184. }
  185. } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
  186. /* assume YUV */
  187. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  188. color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
  189. color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
  190. color->comp[3].u8[0] = rgba[3];
  191. } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
  192. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  193. color->comp[1].u8[0] = rgba[3];
  194. } else {
  195. av_log(NULL, AV_LOG_WARNING,
  196. "Color conversion not implemented for %s\n", draw->desc->name);
  197. memset(color, 128, sizeof(*color));
  198. }
  199. }
  200. static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
  201. int plane, int x, int y)
  202. {
  203. return data[plane] +
  204. (y >> draw->vsub[plane]) * linesize[plane] +
  205. (x >> draw->hsub[plane]) * draw->pixelstep[plane];
  206. }
  207. void ff_copy_rectangle2(FFDrawContext *draw,
  208. uint8_t *dst[], int dst_linesize[],
  209. uint8_t *src[], int src_linesize[],
  210. int dst_x, int dst_y, int src_x, int src_y,
  211. int w, int h)
  212. {
  213. int plane, y, wp, hp;
  214. uint8_t *p, *q;
  215. for (plane = 0; plane < draw->nb_planes; plane++) {
  216. p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
  217. q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  218. wp = FF_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
  219. hp = FF_CEIL_RSHIFT(h, draw->vsub[plane]);
  220. for (y = 0; y < hp; y++) {
  221. memcpy(q, p, wp);
  222. p += src_linesize[plane];
  223. q += dst_linesize[plane];
  224. }
  225. }
  226. }
  227. void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
  228. uint8_t *dst[], int dst_linesize[],
  229. int dst_x, int dst_y, int w, int h)
  230. {
  231. int plane, x, y, wp, hp;
  232. uint8_t *p0, *p;
  233. for (plane = 0; plane < draw->nb_planes; plane++) {
  234. p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  235. wp = FF_CEIL_RSHIFT(w, draw->hsub[plane]);
  236. hp = FF_CEIL_RSHIFT(h, draw->vsub[plane]);
  237. if (!hp)
  238. return;
  239. p = p0;
  240. /* copy first line from color */
  241. for (x = 0; x < wp; x++) {
  242. memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
  243. p += draw->pixelstep[plane];
  244. }
  245. wp *= draw->pixelstep[plane];
  246. /* copy next lines from first line */
  247. p = p0 + dst_linesize[plane];
  248. for (y = 1; y < hp; y++) {
  249. memcpy(p, p0, wp);
  250. p += dst_linesize[plane];
  251. }
  252. }
  253. }
  254. /**
  255. * Clip interval [x; x+w[ within [0; wmax[.
  256. * The resulting w may be negative if the final interval is empty.
  257. * dx, if not null, return the difference between in and out value of x.
  258. */
  259. static void clip_interval(int wmax, int *x, int *w, int *dx)
  260. {
  261. if (dx)
  262. *dx = 0;
  263. if (*x < 0) {
  264. if (dx)
  265. *dx = -*x;
  266. *w += *x;
  267. *x = 0;
  268. }
  269. if (*x + *w > wmax)
  270. *w = wmax - *x;
  271. }
  272. /**
  273. * Decompose w pixels starting at x
  274. * into start + (w starting at x) + end
  275. * with x and w aligned on multiples of 1<<sub.
  276. */
  277. static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
  278. {
  279. int mask = (1 << sub) - 1;
  280. *start = (-*x) & mask;
  281. *x += *start;
  282. *start = FFMIN(*start, *w);
  283. *w -= *start;
  284. *end = *w & mask;
  285. *w >>= sub;
  286. }
  287. static int component_used(FFDrawContext *draw, int plane, int comp)
  288. {
  289. return (draw->comp_mask[plane] >> comp) & 1;
  290. }
  291. /* If alpha is in the [ 0 ; 0x1010101 ] range,
  292. then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
  293. and >> 24 gives a correct rounding. */
  294. static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
  295. int dx, int w, unsigned hsub, int left, int right)
  296. {
  297. unsigned asrc = alpha * src;
  298. unsigned tau = 0x1010101 - alpha;
  299. int x;
  300. if (left) {
  301. unsigned suba = (left * alpha) >> hsub;
  302. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  303. dst += dx;
  304. }
  305. for (x = 0; x < w; x++) {
  306. *dst = (*dst * tau + asrc) >> 24;
  307. dst += dx;
  308. }
  309. if (right) {
  310. unsigned suba = (right * alpha) >> hsub;
  311. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  312. }
  313. }
  314. void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
  315. uint8_t *dst[], int dst_linesize[],
  316. int dst_w, int dst_h,
  317. int x0, int y0, int w, int h)
  318. {
  319. unsigned alpha, nb_planes, nb_comp, plane, comp;
  320. int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
  321. uint8_t *p0, *p;
  322. /* TODO optimize if alpha = 0xFF */
  323. clip_interval(dst_w, &x0, &w, NULL);
  324. clip_interval(dst_h, &y0, &h, NULL);
  325. if (w <= 0 || h <= 0 || !color->rgba[3])
  326. return;
  327. /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
  328. alpha = 0x10203 * color->rgba[3] + 0x2;
  329. nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
  330. for (plane = 0; plane < nb_planes; plane++) {
  331. nb_comp = draw->pixelstep[plane];
  332. p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
  333. w_sub = w;
  334. h_sub = h;
  335. x_sub = x0;
  336. y_sub = y0;
  337. subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
  338. subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
  339. for (comp = 0; comp < nb_comp; comp++) {
  340. if (!component_used(draw, plane, comp))
  341. continue;
  342. p = p0 + comp;
  343. if (top) {
  344. blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
  345. draw->pixelstep[plane], w_sub,
  346. draw->hsub[plane], left, right);
  347. p += dst_linesize[plane];
  348. }
  349. for (y = 0; y < h_sub; y++) {
  350. blend_line(p, color->comp[plane].u8[comp], alpha,
  351. draw->pixelstep[plane], w_sub,
  352. draw->hsub[plane], left, right);
  353. p += dst_linesize[plane];
  354. }
  355. if (bottom)
  356. blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
  357. draw->pixelstep[plane], w_sub,
  358. draw->hsub[plane], left, right);
  359. }
  360. }
  361. }
  362. static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
  363. uint8_t *mask, int mask_linesize, int l2depth,
  364. unsigned w, unsigned h, unsigned shift, unsigned xm0)
  365. {
  366. unsigned xm, x, y, t = 0;
  367. unsigned xmshf = 3 - l2depth;
  368. unsigned xmmod = 7 >> l2depth;
  369. unsigned mbits = (1 << (1 << l2depth)) - 1;
  370. unsigned mmult = 255 / mbits;
  371. for (y = 0; y < h; y++) {
  372. xm = xm0;
  373. for (x = 0; x < w; x++) {
  374. t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
  375. * mmult;
  376. xm++;
  377. }
  378. mask += mask_linesize;
  379. }
  380. alpha = (t >> shift) * alpha;
  381. *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
  382. }
  383. static void blend_line_hv(uint8_t *dst, int dst_delta,
  384. unsigned src, unsigned alpha,
  385. uint8_t *mask, int mask_linesize, int l2depth, int w,
  386. unsigned hsub, unsigned vsub,
  387. int xm, int left, int right, int hband)
  388. {
  389. int x;
  390. if (left) {
  391. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  392. left, hband, hsub + vsub, xm);
  393. dst += dst_delta;
  394. xm += left;
  395. }
  396. for (x = 0; x < w; x++) {
  397. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  398. 1 << hsub, hband, hsub + vsub, xm);
  399. dst += dst_delta;
  400. xm += 1 << hsub;
  401. }
  402. if (right)
  403. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  404. right, hband, hsub + vsub, xm);
  405. }
  406. void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
  407. uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
  408. uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
  409. int l2depth, unsigned endianness, int x0, int y0)
  410. {
  411. unsigned alpha, nb_planes, nb_comp, plane, comp;
  412. int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
  413. uint8_t *p0, *p, *m;
  414. clip_interval(dst_w, &x0, &mask_w, &xm0);
  415. clip_interval(dst_h, &y0, &mask_h, &ym0);
  416. mask += ym0 * mask_linesize;
  417. if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
  418. return;
  419. /* alpha is in the [ 0 ; 0x10203 ] range,
  420. alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
  421. alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
  422. nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
  423. for (plane = 0; plane < nb_planes; plane++) {
  424. nb_comp = draw->pixelstep[plane];
  425. p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
  426. w_sub = mask_w;
  427. h_sub = mask_h;
  428. x_sub = x0;
  429. y_sub = y0;
  430. subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
  431. subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
  432. for (comp = 0; comp < nb_comp; comp++) {
  433. if (!component_used(draw, plane, comp))
  434. continue;
  435. p = p0 + comp;
  436. m = mask;
  437. if (top) {
  438. blend_line_hv(p, draw->pixelstep[plane],
  439. color->comp[plane].u8[comp], alpha,
  440. m, mask_linesize, l2depth, w_sub,
  441. draw->hsub[plane], draw->vsub[plane],
  442. xm0, left, right, top);
  443. p += dst_linesize[plane];
  444. m += top * mask_linesize;
  445. }
  446. for (y = 0; y < h_sub; y++) {
  447. blend_line_hv(p, draw->pixelstep[plane],
  448. color->comp[plane].u8[comp], alpha,
  449. m, mask_linesize, l2depth, w_sub,
  450. draw->hsub[plane], draw->vsub[plane],
  451. xm0, left, right, 1 << draw->vsub[plane]);
  452. p += dst_linesize[plane];
  453. m += mask_linesize << draw->vsub[plane];
  454. }
  455. if (bottom)
  456. blend_line_hv(p, draw->pixelstep[plane],
  457. color->comp[plane].u8[comp], alpha,
  458. m, mask_linesize, l2depth, w_sub,
  459. draw->hsub[plane], draw->vsub[plane],
  460. xm0, left, right, bottom);
  461. }
  462. }
  463. }
  464. int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
  465. int value)
  466. {
  467. unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
  468. if (!shift)
  469. return value;
  470. if (round_dir >= 0)
  471. value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
  472. return (value >> shift) << shift;
  473. }
  474. AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
  475. {
  476. enum AVPixelFormat i;
  477. FFDrawContext draw;
  478. AVFilterFormats *fmts = NULL;
  479. for (i = 0; av_pix_fmt_desc_get(i); i++)
  480. if (ff_draw_init(&draw, i, flags) >= 0)
  481. ff_add_format(&fmts, i);
  482. return fmts;
  483. }
  484. #ifdef TEST
  485. #undef printf
  486. int main(void)
  487. {
  488. enum AVPixelFormat f;
  489. const AVPixFmtDescriptor *desc;
  490. FFDrawContext draw;
  491. FFDrawColor color;
  492. int r, i;
  493. for (f = 0; av_pix_fmt_desc_get(f); f++) {
  494. desc = av_pix_fmt_desc_get(f);
  495. if (!desc->name)
  496. continue;
  497. printf("Testing %s...%*s", desc->name,
  498. (int)(16 - strlen(desc->name)), "");
  499. r = ff_draw_init(&draw, f, 0);
  500. if (r < 0) {
  501. char buf[128];
  502. av_strerror(r, buf, sizeof(buf));
  503. printf("no: %s\n", buf);
  504. continue;
  505. }
  506. ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
  507. for (i = 0; i < sizeof(color); i++)
  508. if (((uint8_t *)&color)[i] != 128)
  509. break;
  510. if (i == sizeof(color)) {
  511. printf("fallback color\n");
  512. continue;
  513. }
  514. printf("ok\n");
  515. }
  516. return 0;
  517. }
  518. #endif