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.

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