drawutils.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. memset(draw, 0, sizeof(*draw));
  187. draw->desc = desc;
  188. draw->format = format;
  189. draw->nb_planes = nb_planes;
  190. memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
  191. draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
  192. draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
  193. for (i = 0; i < (desc->nb_components - !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)); i++)
  194. draw->comp_mask[desc->comp[i].plane] |=
  195. 1 << desc->comp[i].offset;
  196. return 0;
  197. }
  198. void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
  199. {
  200. unsigned i;
  201. uint8_t rgba_map[4];
  202. if (rgba != color->rgba)
  203. memcpy(color->rgba, rgba, sizeof(color->rgba));
  204. if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  205. ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
  206. if (draw->nb_planes == 1) {
  207. for (i = 0; i < 4; i++) {
  208. color->comp[0].u8[rgba_map[i]] = rgba[i];
  209. if (draw->desc->comp[rgba_map[i]].depth > 8) {
  210. color->comp[0].u16[rgba_map[i]] = color->comp[0].u8[rgba_map[i]] << 8;
  211. }
  212. }
  213. } else {
  214. for (i = 0; i < 4; i++) {
  215. color->comp[rgba_map[i]].u8[0] = rgba[i];
  216. if (draw->desc->comp[rgba_map[i]].depth > 8)
  217. color->comp[rgba_map[i]].u16[0] = color->comp[rgba_map[i]].u8[0] << (draw->desc->comp[rgba_map[i]].depth - 8);
  218. }
  219. }
  220. } else if (draw->nb_planes >= 2) {
  221. /* assume YUV */
  222. const AVPixFmtDescriptor *desc = draw->desc;
  223. color->comp[desc->comp[0].plane].u8[desc->comp[0].offset] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  224. color->comp[desc->comp[1].plane].u8[desc->comp[1].offset] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
  225. color->comp[desc->comp[2].plane].u8[desc->comp[2].offset] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
  226. color->comp[3].u8[0] = rgba[3];
  227. #define EXPAND(compn) \
  228. if (desc->comp[compn].depth > 8) \
  229. color->comp[desc->comp[compn].plane].u16[desc->comp[compn].offset] = \
  230. color->comp[desc->comp[compn].plane].u8[desc->comp[compn].offset] << (draw->desc->comp[compn].depth - 8)
  231. EXPAND(3);
  232. EXPAND(2);
  233. EXPAND(1);
  234. EXPAND(0);
  235. } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
  236. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  237. color->comp[1].u8[0] = rgba[3];
  238. } else if (draw->format == AV_PIX_FMT_GRAY16LE || draw->format == AV_PIX_FMT_YA16LE) {
  239. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  240. color->comp[0].u16[0] = color->comp[0].u8[0] << 8;
  241. color->comp[1].u8[0] = rgba[3];
  242. color->comp[1].u16[0] = color->comp[1].u8[0] << 8;
  243. } else {
  244. av_log(NULL, AV_LOG_WARNING,
  245. "Color conversion not implemented for %s\n", draw->desc->name);
  246. memset(color, 128, sizeof(*color));
  247. }
  248. }
  249. static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
  250. int plane, int x, int y)
  251. {
  252. return data[plane] +
  253. (y >> draw->vsub[plane]) * linesize[plane] +
  254. (x >> draw->hsub[plane]) * draw->pixelstep[plane];
  255. }
  256. void ff_copy_rectangle2(FFDrawContext *draw,
  257. uint8_t *dst[], int dst_linesize[],
  258. uint8_t *src[], int src_linesize[],
  259. int dst_x, int dst_y, int src_x, int src_y,
  260. int w, int h)
  261. {
  262. int plane, y, wp, hp;
  263. uint8_t *p, *q;
  264. for (plane = 0; plane < draw->nb_planes; plane++) {
  265. p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
  266. q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  267. wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
  268. hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
  269. for (y = 0; y < hp; y++) {
  270. memcpy(q, p, wp);
  271. p += src_linesize[plane];
  272. q += dst_linesize[plane];
  273. }
  274. }
  275. }
  276. void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
  277. uint8_t *dst[], int dst_linesize[],
  278. int dst_x, int dst_y, int w, int h)
  279. {
  280. int plane, x, y, wp, hp;
  281. uint8_t *p0, *p;
  282. FFDrawColor color_tmp = *color;
  283. for (plane = 0; plane < draw->nb_planes; plane++) {
  284. p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  285. wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
  286. hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
  287. if (!hp)
  288. return;
  289. p = p0;
  290. if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) {
  291. for (x = 0; 2*x < draw->pixelstep[plane]; x++)
  292. color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]);
  293. }
  294. /* copy first line from color */
  295. for (x = 0; x < wp; x++) {
  296. memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]);
  297. p += draw->pixelstep[plane];
  298. }
  299. wp *= draw->pixelstep[plane];
  300. /* copy next lines from first line */
  301. p = p0 + dst_linesize[plane];
  302. for (y = 1; y < hp; y++) {
  303. memcpy(p, p0, wp);
  304. p += dst_linesize[plane];
  305. }
  306. }
  307. }
  308. /**
  309. * Clip interval [x; x+w[ within [0; wmax[.
  310. * The resulting w may be negative if the final interval is empty.
  311. * dx, if not null, return the difference between in and out value of x.
  312. */
  313. static void clip_interval(int wmax, int *x, int *w, int *dx)
  314. {
  315. if (dx)
  316. *dx = 0;
  317. if (*x < 0) {
  318. if (dx)
  319. *dx = -*x;
  320. *w += *x;
  321. *x = 0;
  322. }
  323. if (*x + *w > wmax)
  324. *w = wmax - *x;
  325. }
  326. /**
  327. * Decompose w pixels starting at x
  328. * into start + (w starting at x) + end
  329. * with x and w aligned on multiples of 1<<sub.
  330. */
  331. static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
  332. {
  333. int mask = (1 << sub) - 1;
  334. *start = (-*x) & mask;
  335. *x += *start;
  336. *start = FFMIN(*start, *w);
  337. *w -= *start;
  338. *end = *w & mask;
  339. *w >>= sub;
  340. }
  341. static int component_used(FFDrawContext *draw, int plane, int comp)
  342. {
  343. return (draw->comp_mask[plane] >> comp) & 1;
  344. }
  345. /* If alpha is in the [ 0 ; 0x1010101 ] range,
  346. then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
  347. and >> 24 gives a correct rounding. */
  348. static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
  349. int dx, int w, unsigned hsub, int left, int right)
  350. {
  351. unsigned asrc = alpha * src;
  352. unsigned tau = 0x1010101 - alpha;
  353. int x;
  354. if (left) {
  355. unsigned suba = (left * alpha) >> hsub;
  356. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  357. dst += dx;
  358. }
  359. for (x = 0; x < w; x++) {
  360. *dst = (*dst * tau + asrc) >> 24;
  361. dst += dx;
  362. }
  363. if (right) {
  364. unsigned suba = (right * alpha) >> hsub;
  365. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  366. }
  367. }
  368. static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha,
  369. int dx, int w, unsigned hsub, int left, int right)
  370. {
  371. unsigned asrc = alpha * src;
  372. unsigned tau = 0x10001 - alpha;
  373. int x;
  374. if (left) {
  375. unsigned suba = (left * alpha) >> hsub;
  376. uint16_t value = AV_RL16(dst);
  377. AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
  378. dst += dx;
  379. }
  380. for (x = 0; x < w; x++) {
  381. uint16_t value = AV_RL16(dst);
  382. AV_WL16(dst, (value * tau + asrc) >> 16);
  383. dst += dx;
  384. }
  385. if (right) {
  386. unsigned suba = (right * alpha) >> hsub;
  387. uint16_t value = AV_RL16(dst);
  388. AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16);
  389. }
  390. }
  391. void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
  392. uint8_t *dst[], int dst_linesize[],
  393. int dst_w, int dst_h,
  394. int x0, int y0, int w, int h)
  395. {
  396. unsigned alpha, nb_planes, nb_comp, plane, comp;
  397. int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
  398. uint8_t *p0, *p;
  399. /* TODO optimize if alpha = 0xFF */
  400. clip_interval(dst_w, &x0, &w, NULL);
  401. clip_interval(dst_h, &y0, &h, NULL);
  402. if (w <= 0 || h <= 0 || !color->rgba[3])
  403. return;
  404. if (draw->desc->comp[0].depth <= 8) {
  405. /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
  406. alpha = 0x10203 * color->rgba[3] + 0x2;
  407. } else {
  408. /* 0x101 * alpha is in the [ 2 ; 0x1001] range */
  409. alpha = 0x101 * color->rgba[3] + 0x2;
  410. }
  411. nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA);
  412. nb_planes += !nb_planes;
  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 - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA);
  579. nb_planes += !nb_planes;
  580. for (plane = 0; plane < nb_planes; plane++) {
  581. nb_comp = draw->pixelstep[plane];
  582. p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
  583. w_sub = mask_w;
  584. h_sub = mask_h;
  585. x_sub = x0;
  586. y_sub = y0;
  587. subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
  588. subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
  589. for (comp = 0; comp < nb_comp; comp++) {
  590. const int depth = draw->desc->comp[comp].depth;
  591. if (!component_used(draw, plane, comp))
  592. continue;
  593. p = p0 + comp;
  594. m = mask;
  595. if (top) {
  596. if (depth <= 8) {
  597. blend_line_hv(p, draw->pixelstep[plane],
  598. color->comp[plane].u8[comp], alpha,
  599. m, mask_linesize, l2depth, w_sub,
  600. draw->hsub[plane], draw->vsub[plane],
  601. xm0, left, right, top);
  602. } else {
  603. blend_line_hv16(p, draw->pixelstep[plane],
  604. color->comp[plane].u16[comp], alpha,
  605. m, mask_linesize, l2depth, w_sub,
  606. draw->hsub[plane], draw->vsub[plane],
  607. xm0, left, right, top);
  608. }
  609. p += dst_linesize[plane];
  610. m += top * mask_linesize;
  611. }
  612. if (depth <= 8) {
  613. for (y = 0; y < h_sub; y++) {
  614. blend_line_hv(p, draw->pixelstep[plane],
  615. color->comp[plane].u8[comp], alpha,
  616. m, mask_linesize, l2depth, w_sub,
  617. draw->hsub[plane], draw->vsub[plane],
  618. xm0, left, right, 1 << draw->vsub[plane]);
  619. p += dst_linesize[plane];
  620. m += mask_linesize << draw->vsub[plane];
  621. }
  622. } else {
  623. for (y = 0; y < h_sub; y++) {
  624. blend_line_hv16(p, draw->pixelstep[plane],
  625. color->comp[plane].u16[comp], alpha,
  626. m, mask_linesize, l2depth, w_sub,
  627. draw->hsub[plane], draw->vsub[plane],
  628. xm0, left, right, 1 << draw->vsub[plane]);
  629. p += dst_linesize[plane];
  630. m += mask_linesize << draw->vsub[plane];
  631. }
  632. }
  633. if (bottom) {
  634. if (depth <= 8) {
  635. blend_line_hv(p, draw->pixelstep[plane],
  636. color->comp[plane].u8[comp], alpha,
  637. m, mask_linesize, l2depth, w_sub,
  638. draw->hsub[plane], draw->vsub[plane],
  639. xm0, left, right, bottom);
  640. } else {
  641. blend_line_hv16(p, draw->pixelstep[plane],
  642. color->comp[plane].u16[comp], alpha,
  643. m, mask_linesize, l2depth, w_sub,
  644. draw->hsub[plane], draw->vsub[plane],
  645. xm0, left, right, bottom);
  646. }
  647. }
  648. }
  649. }
  650. }
  651. int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
  652. int value)
  653. {
  654. unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
  655. if (!shift)
  656. return value;
  657. if (round_dir >= 0)
  658. value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
  659. return (value >> shift) << shift;
  660. }
  661. AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
  662. {
  663. enum AVPixelFormat i;
  664. FFDrawContext draw;
  665. AVFilterFormats *fmts = NULL;
  666. int ret;
  667. for (i = 0; av_pix_fmt_desc_get(i); i++)
  668. if (ff_draw_init(&draw, i, flags) >= 0 &&
  669. (ret = ff_add_format(&fmts, i)) < 0)
  670. return NULL;
  671. return fmts;
  672. }