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.

728 lines
26KB

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