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.

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