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.

646 lines
24KB

  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/avassert.h"
  23. #include "libavutil/avutil.h"
  24. #include "libavutil/colorspace.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "drawutils.h"
  29. #include "formats.h"
  30. enum { RED = 0, GREEN, BLUE, ALPHA };
  31. int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
  32. {
  33. switch (pix_fmt) {
  34. case AV_PIX_FMT_0RGB:
  35. case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
  36. case AV_PIX_FMT_0BGR:
  37. case AV_PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
  38. case AV_PIX_FMT_RGB48LE:
  39. case AV_PIX_FMT_RGB48BE:
  40. case AV_PIX_FMT_RGBA64BE:
  41. case AV_PIX_FMT_RGBA64LE:
  42. case AV_PIX_FMT_RGB0:
  43. case AV_PIX_FMT_RGBA:
  44. case AV_PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
  45. case AV_PIX_FMT_BGR48LE:
  46. case AV_PIX_FMT_BGR48BE:
  47. case AV_PIX_FMT_BGRA64BE:
  48. case AV_PIX_FMT_BGRA64LE:
  49. case AV_PIX_FMT_BGRA:
  50. case AV_PIX_FMT_BGR0:
  51. case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
  52. case AV_PIX_FMT_GBRP9LE:
  53. case AV_PIX_FMT_GBRP9BE:
  54. case AV_PIX_FMT_GBRP10LE:
  55. case AV_PIX_FMT_GBRP10BE:
  56. case AV_PIX_FMT_GBRP12LE:
  57. case AV_PIX_FMT_GBRP12BE:
  58. case AV_PIX_FMT_GBRP14LE:
  59. case AV_PIX_FMT_GBRP14BE:
  60. case AV_PIX_FMT_GBRP16LE:
  61. case AV_PIX_FMT_GBRP16BE:
  62. case AV_PIX_FMT_GBRAP:
  63. case AV_PIX_FMT_GBRAP10LE:
  64. case AV_PIX_FMT_GBRAP10BE:
  65. case AV_PIX_FMT_GBRAP12LE:
  66. case AV_PIX_FMT_GBRAP12BE:
  67. case AV_PIX_FMT_GBRAP16LE:
  68. case AV_PIX_FMT_GBRAP16BE:
  69. case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
  70. default: /* unsupported */
  71. return AVERROR(EINVAL);
  72. }
  73. return 0;
  74. }
  75. int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
  76. {
  77. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  78. const AVComponentDescriptor *c;
  79. unsigned i, nb_planes = 0;
  80. int pixelstep[MAX_PLANES] = { 0 };
  81. int full_range = 0;
  82. if (!desc || !desc->name)
  83. return AVERROR(EINVAL);
  84. if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | FF_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA))
  85. return AVERROR(ENOSYS);
  86. if (format == AV_PIX_FMT_P010LE || format == AV_PIX_FMT_P010BE || format == AV_PIX_FMT_P016LE || format == AV_PIX_FMT_P016BE)
  87. return AVERROR(ENOSYS);
  88. if (format == AV_PIX_FMT_YUVJ420P || format == AV_PIX_FMT_YUVJ422P || format == AV_PIX_FMT_YUVJ444P ||
  89. format == AV_PIX_FMT_YUVJ411P || format == AV_PIX_FMT_YUVJ440P)
  90. full_range = 1;
  91. for (i = 0; i < desc->nb_components; i++) {
  92. c = &desc->comp[i];
  93. /* for now, only 8-16 bits formats */
  94. if (c->depth < 8 || c->depth > 16)
  95. return AVERROR(ENOSYS);
  96. if (desc->flags & AV_PIX_FMT_FLAG_BE)
  97. return AVERROR(ENOSYS);
  98. if (c->plane >= MAX_PLANES)
  99. return AVERROR(ENOSYS);
  100. /* strange interleaving */
  101. if (pixelstep[c->plane] != 0 &&
  102. pixelstep[c->plane] != c->step)
  103. return AVERROR(ENOSYS);
  104. if (pixelstep[c->plane] == 6 &&
  105. c->depth == 16)
  106. return AVERROR(ENOSYS);
  107. pixelstep[c->plane] = c->step;
  108. if (pixelstep[c->plane] >= 8)
  109. return AVERROR(ENOSYS);
  110. nb_planes = FFMAX(nb_planes, c->plane + 1);
  111. }
  112. memset(draw, 0, sizeof(*draw));
  113. draw->desc = desc;
  114. draw->format = format;
  115. draw->nb_planes = nb_planes;
  116. draw->flags = flags;
  117. draw->full_range = full_range;
  118. memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
  119. draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
  120. draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
  121. for (i = 0; i < (desc->nb_components - !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(flags & FF_DRAW_PROCESS_ALPHA))); i++)
  122. draw->comp_mask[desc->comp[i].plane] |=
  123. 1 << desc->comp[i].offset;
  124. return 0;
  125. }
  126. void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
  127. {
  128. unsigned i;
  129. uint8_t rgba_map[4];
  130. if (rgba != color->rgba)
  131. memcpy(color->rgba, rgba, sizeof(color->rgba));
  132. if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  133. ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
  134. if (draw->nb_planes == 1) {
  135. for (i = 0; i < 4; i++) {
  136. color->comp[0].u8[rgba_map[i]] = rgba[i];
  137. if (draw->desc->comp[rgba_map[i]].depth > 8) {
  138. color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
  139. }
  140. }
  141. } else {
  142. for (i = 0; i < 4; i++) {
  143. color->comp[rgba_map[i]].u8[0] = rgba[i];
  144. if (draw->desc->comp[rgba_map[i]].depth > 8)
  145. color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
  146. }
  147. }
  148. } else if (draw->nb_planes >= 2) {
  149. /* assume YUV */
  150. const AVPixFmtDescriptor *desc = draw->desc;
  151. color->comp[desc->comp[0].plane].u8[desc->comp[0].offset] = draw->full_range ? RGB_TO_Y_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  152. color->comp[desc->comp[1].plane].u8[desc->comp[1].offset] = draw->full_range ? RGB_TO_U_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
  153. color->comp[desc->comp[2].plane].u8[desc->comp[2].offset] = draw->full_range ? RGB_TO_V_JPEG(rgba[0], rgba[1], rgba[2]) : RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
  154. color->comp[3].u8[0] = rgba[3];
  155. #define EXPAND(compn) \
  156. if (desc->comp[compn].depth > 8) \
  157. color->comp[desc->comp[compn].plane].u16[desc->comp[compn].offset] = \
  158. color->comp[desc->comp[compn].plane].u8[desc->comp[compn].offset] << \
  159. (draw->desc->comp[compn].depth + draw->desc->comp[compn].shift - 8)
  160. EXPAND(3);
  161. EXPAND(2);
  162. EXPAND(1);
  163. EXPAND(0);
  164. } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A ||
  165. draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE ||
  166. draw->format == AV_PIX_FMT_GRAY9LE ||
  167. draw->format == AV_PIX_FMT_GRAY10LE ||
  168. draw->format == AV_PIX_FMT_GRAY12LE ||
  169. draw->format == AV_PIX_FMT_GRAY14LE) {
  170. const AVPixFmtDescriptor *desc = draw->desc;
  171. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  172. EXPAND(0);
  173. color->comp[1].u8[0] = rgba[3];
  174. EXPAND(1);
  175. } else {
  176. av_log(NULL, AV_LOG_WARNING,
  177. "Color conversion not implemented for %s\n", draw->desc->name);
  178. memset(color, 128, sizeof(*color));
  179. }
  180. }
  181. static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
  182. int plane, int x, int y)
  183. {
  184. return data[plane] +
  185. (y >> draw->vsub[plane]) * linesize[plane] +
  186. (x >> draw->hsub[plane]) * draw->pixelstep[plane];
  187. }
  188. void ff_copy_rectangle2(FFDrawContext *draw,
  189. uint8_t *dst[], int dst_linesize[],
  190. uint8_t *src[], int src_linesize[],
  191. int dst_x, int dst_y, int src_x, int src_y,
  192. int w, int h)
  193. {
  194. int plane, y, wp, hp;
  195. uint8_t *p, *q;
  196. for (plane = 0; plane < draw->nb_planes; plane++) {
  197. p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
  198. q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  199. wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
  200. hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
  201. for (y = 0; y < hp; y++) {
  202. memcpy(q, p, wp);
  203. p += src_linesize[plane];
  204. q += dst_linesize[plane];
  205. }
  206. }
  207. }
  208. void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
  209. uint8_t *dst[], int dst_linesize[],
  210. int dst_x, int dst_y, int w, int h)
  211. {
  212. int plane, x, y, wp, hp;
  213. uint8_t *p0, *p;
  214. FFDrawColor color_tmp = *color;
  215. for (plane = 0; plane < draw->nb_planes; plane++) {
  216. p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  217. wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
  218. hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
  219. if (!hp)
  220. return;
  221. p = p0;
  222. if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
  223. for (x = 0; 2*x < draw->pixelstep[plane]; x++)
  224. color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
  225. }
  226. /* copy first line from color */
  227. for (x = 0; x < wp; x++) {
  228. memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
  229. p += draw->pixelstep[plane];
  230. }
  231. wp *= draw->pixelstep[plane];
  232. /* copy next lines from first line */
  233. p = p0 + dst_linesize[plane];
  234. for (y = 1; y < hp; y++) {
  235. memcpy(p, p0, wp);
  236. p += dst_linesize[plane];
  237. }
  238. }
  239. }
  240. /**
  241. * Clip interval [x; x+w[ within [0; wmax[.
  242. * The resulting w may be negative if the final interval is empty.
  243. * dx, if not null, return the difference between in and out value of x.
  244. */
  245. static void clip_interval(int wmax, int *x, int *w, int *dx)
  246. {
  247. if (dx)
  248. *dx = 0;
  249. if (*x < 0) {
  250. if (dx)
  251. *dx = -*x;
  252. *w += *x;
  253. *x = 0;
  254. }
  255. if (*x + *w > wmax)
  256. *w = wmax - *x;
  257. }
  258. /**
  259. * Decompose w pixels starting at x
  260. * into start + (w starting at x) + end
  261. * with x and w aligned on multiples of 1<<sub.
  262. */
  263. static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
  264. {
  265. int mask = (1 << sub) - 1;
  266. *start = (-*x) & mask;
  267. *x += *start;
  268. *start = FFMIN(*start, *w);
  269. *w -= *start;
  270. *end = *w & mask;
  271. *w >>= sub;
  272. }
  273. static int component_used(FFDrawContext *draw, int plane, int comp)
  274. {
  275. return (draw->comp_mask[plane] >> comp) & 1;
  276. }
  277. /* If alpha is in the [ 0 ; 0x1010101 ] range,
  278. then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
  279. and >> 24 gives a correct rounding. */
  280. static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
  281. int dx, int w, unsigned hsub, int left, int right)
  282. {
  283. unsigned asrc = alpha * src;
  284. unsigned tau = 0x1010101 - alpha;
  285. int x;
  286. if (left) {
  287. unsigned suba = (left * alpha) >> hsub;
  288. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  289. dst += dx;
  290. }
  291. for (x = 0; x < w; x++) {
  292. *dst = (*dst * tau + asrc) >> 24;
  293. dst += dx;
  294. }
  295. if (right) {
  296. unsigned suba = (right * alpha) >> hsub;
  297. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  298. }
  299. }
  300. static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
  301. int dx, int w, unsigned hsub, int left, int right)
  302. {
  303. unsigned asrc = alpha * src;
  304. unsigned tau = 0x10001 - alpha;
  305. int x;
  306. if (left) {
  307. unsigned suba = (left * alpha) >> hsub;
  308. uint16_t value = AV_RL16(dst);
  309. AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
  310. dst += dx;
  311. }
  312. for (x = 0; x < w; x++) {
  313. uint16_t value = AV_RL16(dst);
  314. AV_WL16(dst, (value * tau + asrc) >> 16);
  315. dst += dx;
  316. }
  317. if (right) {
  318. unsigned suba = (right * alpha) >> hsub;
  319. uint16_t value = AV_RL16(dst);
  320. AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
  321. }
  322. }
  323. void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
  324. uint8_t *dst[], int dst_linesize[],
  325. int dst_w, int dst_h,
  326. int x0, int y0, int w, int h)
  327. {
  328. unsigned alpha, nb_planes, nb_comp, plane, comp;
  329. int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
  330. uint8_t *p0, *p;
  331. /* TODO optimize if alpha = 0xFF */
  332. clip_interval(dst_w, &x0, &w, NULL);
  333. clip_interval(dst_h, &y0, &h, NULL);
  334. if (w <= 0 || h <= 0 || !color->rgba[3])
  335. return;
  336. if (draw->desc->comp[0].depth <= 8) {
  337. /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
  338. alpha = 0x10203 * color->rgba[3] + 0x2;
  339. } else {
  340. /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
  341. alpha = 0x101 * color->rgba[3] + 0x2;
  342. }
  343. nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
  344. nb_planes += !nb_planes;
  345. for (plane = 0; plane < nb_planes; plane++) {
  346. nb_comp = draw->pixelstep[plane];
  347. p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
  348. w_sub = w;
  349. h_sub = h;
  350. x_sub = x0;
  351. y_sub = y0;
  352. subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
  353. subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
  354. for (comp = 0; comp < nb_comp; comp++) {
  355. const int depth = draw->desc->comp[comp].depth;
  356. if (!component_used(draw, plane, comp))
  357. continue;
  358. p = p0 + comp;
  359. if (top) {
  360. if (depth <= 8) {
  361. blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
  362. draw->pixelstep[plane], w_sub,
  363. draw->hsub[plane], left, right);
  364. } else {
  365. blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
  366. draw->pixelstep[plane], w_sub,
  367. draw->hsub[plane], left, right);
  368. }
  369. p += dst_linesize[plane];
  370. }
  371. if (depth <= 8) {
  372. for (y = 0; y < h_sub; y++) {
  373. blend_line(p, color->comp[plane].u8[comp], alpha,
  374. draw->pixelstep[plane], w_sub,
  375. draw->hsub[plane], left, right);
  376. p += dst_linesize[plane];
  377. }
  378. } else {
  379. for (y = 0; y < h_sub; y++) {
  380. blend_line16(p, color->comp[plane].u16[comp], alpha,
  381. draw->pixelstep[plane], w_sub,
  382. draw->hsub[plane], left, right);
  383. p += dst_linesize[plane];
  384. }
  385. }
  386. if (bottom) {
  387. if (depth <= 8) {
  388. blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
  389. draw->pixelstep[plane], w_sub,
  390. draw->hsub[plane], left, right);
  391. } else {
  392. blend_line16(p, color->comp[plane].u16[comp], alpha >> 1,
  393. draw->pixelstep[plane], w_sub,
  394. draw->hsub[plane], left, right);
  395. }
  396. }
  397. }
  398. }
  399. }
  400. static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha,
  401. const uint8_t *mask, int mask_linesize, int l2depth,
  402. unsigned w, unsigned h, unsigned shift, unsigned xm0)
  403. {
  404. unsigned xm, x, y, t = 0;
  405. unsigned xmshf = 3 - l2depth;
  406. unsigned xmmod = 7 >> l2depth;
  407. unsigned mbits = (1 << (1 << l2depth)) - 1;
  408. unsigned mmult = 255 / mbits;
  409. uint16_t value = AV_RL16(dst);
  410. for (y = 0; y < h; y++) {
  411. xm = xm0;
  412. for (x = 0; x < w; x++) {
  413. t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
  414. * mmult;
  415. xm++;
  416. }
  417. mask += mask_linesize;
  418. }
  419. alpha = (t >> shift) * alpha;
  420. AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16);
  421. }
  422. static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
  423. const uint8_t *mask, int mask_linesize, int l2depth,
  424. unsigned w, unsigned h, unsigned shift, unsigned xm0)
  425. {
  426. unsigned xm, x, y, t = 0;
  427. unsigned xmshf = 3 - l2depth;
  428. unsigned xmmod = 7 >> l2depth;
  429. unsigned mbits = (1 << (1 << l2depth)) - 1;
  430. unsigned mmult = 255 / mbits;
  431. for (y = 0; y < h; y++) {
  432. xm = xm0;
  433. for (x = 0; x < w; x++) {
  434. t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
  435. * mmult;
  436. xm++;
  437. }
  438. mask += mask_linesize;
  439. }
  440. alpha = (t >> shift) * alpha;
  441. *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
  442. }
  443. static void blend_line_hv16(uint8_t *dst, int dst_delta,
  444. unsigned src, unsigned alpha,
  445. const uint8_t *mask, int mask_linesize, int l2depth, int w,
  446. unsigned hsub, unsigned vsub,
  447. int xm, int left, int right, int hband)
  448. {
  449. int x;
  450. if (left) {
  451. blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
  452. left, hband, hsub + vsub, xm);
  453. dst += dst_delta;
  454. xm += left;
  455. }
  456. for (x = 0; x < w; x++) {
  457. blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
  458. 1 << hsub, hband, hsub + vsub, xm);
  459. dst += dst_delta;
  460. xm += 1 << hsub;
  461. }
  462. if (right)
  463. blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth,
  464. right, hband, hsub + vsub, xm);
  465. }
  466. static void blend_line_hv(uint8_t *dst, int dst_delta,
  467. unsigned src, unsigned alpha,
  468. const uint8_t *mask, int mask_linesize, int l2depth, int w,
  469. unsigned hsub, unsigned vsub,
  470. int xm, int left, int right, int hband)
  471. {
  472. int x;
  473. if (left) {
  474. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  475. left, hband, hsub + vsub, xm);
  476. dst += dst_delta;
  477. xm += left;
  478. }
  479. for (x = 0; x < w; x++) {
  480. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  481. 1 << hsub, hband, hsub + vsub, xm);
  482. dst += dst_delta;
  483. xm += 1 << hsub;
  484. }
  485. if (right)
  486. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  487. right, hband, hsub + vsub, xm);
  488. }
  489. void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
  490. uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
  491. const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
  492. int l2depth, unsigned endianness, int x0, int y0)
  493. {
  494. unsigned alpha, nb_planes, nb_comp, plane, comp;
  495. int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
  496. uint8_t *p0, *p;
  497. const uint8_t *m;
  498. clip_interval(dst_w, &x0, &mask_w, &xm0);
  499. clip_interval(dst_h, &y0, &mask_h, &ym0);
  500. mask += ym0 * mask_linesize;
  501. if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
  502. return;
  503. if (draw->desc->comp[0].depth <= 8) {
  504. /* alpha is in the [ 0 ; 0x10203 ] range,
  505. alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
  506. alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
  507. } else {
  508. alpha = (0x101 * color->rgba[3] + 0x2) >> 8;
  509. }
  510. nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA));
  511. nb_planes += !nb_planes;
  512. for (plane = 0; plane < nb_planes; plane++) {
  513. nb_comp = draw->pixelstep[plane];
  514. p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
  515. w_sub = mask_w;
  516. h_sub = mask_h;
  517. x_sub = x0;
  518. y_sub = y0;
  519. subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
  520. subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
  521. for (comp = 0; comp < nb_comp; comp++) {
  522. const int depth = draw->desc->comp[comp].depth;
  523. if (!component_used(draw, plane, comp))
  524. continue;
  525. p = p0 + comp;
  526. m = mask;
  527. if (top) {
  528. if (depth <= 8) {
  529. blend_line_hv(p, draw->pixelstep[plane],
  530. color->comp[plane].u8[comp], alpha,
  531. m, mask_linesize, l2depth, w_sub,
  532. draw->hsub[plane], draw->vsub[plane],
  533. xm0, left, right, top);
  534. } else {
  535. blend_line_hv16(p, draw->pixelstep[plane],
  536. color->comp[plane].u16[comp], alpha,
  537. m, mask_linesize, l2depth, w_sub,
  538. draw->hsub[plane], draw->vsub[plane],
  539. xm0, left, right, top);
  540. }
  541. p += dst_linesize[plane];
  542. m += top * mask_linesize;
  543. }
  544. if (depth <= 8) {
  545. for (y = 0; y < h_sub; y++) {
  546. blend_line_hv(p, draw->pixelstep[plane],
  547. color->comp[plane].u8[comp], alpha,
  548. m, mask_linesize, l2depth, w_sub,
  549. draw->hsub[plane], draw->vsub[plane],
  550. xm0, left, right, 1 << draw->vsub[plane]);
  551. p += dst_linesize[plane];
  552. m += mask_linesize << draw->vsub[plane];
  553. }
  554. } else {
  555. for (y = 0; y < h_sub; y++) {
  556. blend_line_hv16(p, draw->pixelstep[plane],
  557. color->comp[plane].u16[comp], alpha,
  558. m, mask_linesize, l2depth, w_sub,
  559. draw->hsub[plane], draw->vsub[plane],
  560. xm0, left, right, 1 << draw->vsub[plane]);
  561. p += dst_linesize[plane];
  562. m += mask_linesize << draw->vsub[plane];
  563. }
  564. }
  565. if (bottom) {
  566. if (depth <= 8) {
  567. blend_line_hv(p, draw->pixelstep[plane],
  568. color->comp[plane].u8[comp], alpha,
  569. m, mask_linesize, l2depth, w_sub,
  570. draw->hsub[plane], draw->vsub[plane],
  571. xm0, left, right, bottom);
  572. } else {
  573. blend_line_hv16(p, draw->pixelstep[plane],
  574. color->comp[plane].u16[comp], alpha,
  575. m, mask_linesize, l2depth, w_sub,
  576. draw->hsub[plane], draw->vsub[plane],
  577. xm0, left, right, bottom);
  578. }
  579. }
  580. }
  581. }
  582. }
  583. int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
  584. int value)
  585. {
  586. unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
  587. if (!shift)
  588. return value;
  589. if (round_dir >= 0)
  590. value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
  591. return (value >> shift) << shift;
  592. }
  593. AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
  594. {
  595. enum AVPixelFormat i;
  596. FFDrawContext draw;
  597. AVFilterFormats *fmts = NULL;
  598. int ret;
  599. for (i = 0; av_pix_fmt_desc_get(i); i++)
  600. if (ff_draw_init(&draw, i, flags) >= 0 &&
  601. (ret = ff_add_format(&fmts, i)) < 0)
  602. return NULL;
  603. return fmts;
  604. }