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.

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