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.

769 lines
28KB

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