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.

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