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.

734 lines
27KB

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