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.

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