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.

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