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.

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