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.

588 lines
20KB

  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/mem.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "drawutils.h"
  28. #include "formats.h"
  29. enum { RED = 0, GREEN, BLUE, ALPHA };
  30. int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
  31. {
  32. switch (pix_fmt) {
  33. case AV_PIX_FMT_0RGB:
  34. case AV_PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
  35. case AV_PIX_FMT_0BGR:
  36. case AV_PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
  37. case AV_PIX_FMT_RGB48LE:
  38. case AV_PIX_FMT_RGB48BE:
  39. case AV_PIX_FMT_RGBA64BE:
  40. case AV_PIX_FMT_RGBA64LE:
  41. case AV_PIX_FMT_RGB0:
  42. case AV_PIX_FMT_RGBA:
  43. case AV_PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
  44. case AV_PIX_FMT_BGR48LE:
  45. case AV_PIX_FMT_BGR48BE:
  46. case AV_PIX_FMT_BGRA64BE:
  47. case AV_PIX_FMT_BGRA64LE:
  48. case AV_PIX_FMT_BGRA:
  49. case AV_PIX_FMT_BGR0:
  50. case AV_PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
  51. case AV_PIX_FMT_GBRP9:
  52. case AV_PIX_FMT_GBRP10:
  53. case AV_PIX_FMT_GBRP12:
  54. case AV_PIX_FMT_GBRP14:
  55. case AV_PIX_FMT_GBRAP:
  56. case AV_PIX_FMT_GBRP: rgba_map[GREEN] = 0; rgba_map[BLUE ] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
  57. default: /* unsupported */
  58. return AVERROR(EINVAL);
  59. }
  60. return 0;
  61. }
  62. int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
  63. enum AVPixelFormat pix_fmt, uint8_t rgba_color[4],
  64. int *is_packed_rgba, uint8_t rgba_map_ptr[4])
  65. {
  66. uint8_t rgba_map[4] = {0};
  67. int i;
  68. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(pix_fmt);
  69. int hsub;
  70. av_assert0(pix_desc);
  71. hsub = pix_desc->log2_chroma_w;
  72. *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
  73. if (*is_packed_rgba) {
  74. pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
  75. for (i = 0; i < 4; i++)
  76. dst_color[rgba_map[i]] = rgba_color[i];
  77. line[0] = av_malloc_array(w, pixel_step[0]);
  78. if (!line[0])
  79. return AVERROR(ENOMEM);
  80. for (i = 0; i < w; i++)
  81. memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
  82. if (rgba_map_ptr)
  83. memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
  84. } else {
  85. int plane;
  86. dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
  87. dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  88. dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  89. dst_color[3] = rgba_color[3];
  90. for (plane = 0; plane < 4; plane++) {
  91. int line_size;
  92. int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
  93. pixel_step[plane] = 1;
  94. line_size = AV_CEIL_RSHIFT(w, hsub1) * pixel_step[plane];
  95. line[plane] = av_malloc(line_size);
  96. if (!line[plane]) {
  97. while(plane && line[plane-1])
  98. av_freep(&line[--plane]);
  99. return AVERROR(ENOMEM);
  100. }
  101. memset(line[plane], dst_color[plane], line_size);
  102. }
  103. }
  104. return 0;
  105. }
  106. void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
  107. uint8_t *src[4], int pixelstep[4],
  108. int hsub, int vsub, int x, int y, int w, int h)
  109. {
  110. int i, plane;
  111. uint8_t *p;
  112. for (plane = 0; plane < 4 && dst[plane]; plane++) {
  113. int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
  114. int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
  115. int width = AV_CEIL_RSHIFT(w, hsub1);
  116. int height = AV_CEIL_RSHIFT(h, vsub1);
  117. p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
  118. for (i = 0; i < height; i++) {
  119. memcpy(p + (x >> hsub1) * pixelstep[plane],
  120. src[plane], width * pixelstep[plane]);
  121. p += dst_linesize[plane];
  122. }
  123. }
  124. }
  125. void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
  126. uint8_t *src[4], int src_linesize[4], int pixelstep[4],
  127. int hsub, int vsub, int x, int y, int y2, int w, int h)
  128. {
  129. int i, plane;
  130. uint8_t *p;
  131. for (plane = 0; plane < 4 && dst[plane]; plane++) {
  132. int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
  133. int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
  134. int width = AV_CEIL_RSHIFT(w, hsub1);
  135. int height = AV_CEIL_RSHIFT(h, vsub1);
  136. p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
  137. for (i = 0; i < height; i++) {
  138. memcpy(p + (x >> hsub1) * pixelstep[plane],
  139. src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), width * pixelstep[plane]);
  140. p += dst_linesize[plane];
  141. }
  142. }
  143. }
  144. int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags)
  145. {
  146. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
  147. const AVComponentDescriptor *c;
  148. unsigned i, nb_planes = 0;
  149. int pixelstep[MAX_PLANES] = { 0 };
  150. if (!desc || !desc->name)
  151. return AVERROR(EINVAL);
  152. if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PSEUDOPAL | AV_PIX_FMT_FLAG_ALPHA))
  153. return AVERROR(ENOSYS);
  154. for (i = 0; i < desc->nb_components; i++) {
  155. c = &desc->comp[i];
  156. /* for now, only 8-bits formats */
  157. if (c->depth != 8)
  158. return AVERROR(ENOSYS);
  159. if (c->plane >= MAX_PLANES)
  160. return AVERROR(ENOSYS);
  161. /* strange interleaving */
  162. if (pixelstep[c->plane] != 0 &&
  163. pixelstep[c->plane] != c->step)
  164. return AVERROR(ENOSYS);
  165. pixelstep[c->plane] = c->step;
  166. if (pixelstep[c->plane] >= 8)
  167. return AVERROR(ENOSYS);
  168. nb_planes = FFMAX(nb_planes, c->plane + 1);
  169. }
  170. if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
  171. return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
  172. memset(draw, 0, sizeof(*draw));
  173. draw->desc = desc;
  174. draw->format = format;
  175. draw->nb_planes = nb_planes;
  176. memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
  177. draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
  178. draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
  179. for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
  180. draw->comp_mask[desc->comp[i].plane] |=
  181. 1 << desc->comp[i].offset;
  182. return 0;
  183. }
  184. void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
  185. {
  186. unsigned i;
  187. uint8_t rgba_map[4];
  188. if (rgba != color->rgba)
  189. memcpy(color->rgba, rgba, sizeof(color->rgba));
  190. if ((draw->desc->flags & AV_PIX_FMT_FLAG_RGB) &&
  191. ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
  192. if (draw->nb_planes == 1) {
  193. for (i = 0; i < 4; i++)
  194. color->comp[0].u8[rgba_map[i]] = rgba[i];
  195. } else {
  196. for (i = 0; i < 4; i++)
  197. color->comp[rgba_map[i]].u8[0] = rgba[i];
  198. }
  199. } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
  200. /* assume YUV */
  201. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  202. color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
  203. color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
  204. color->comp[3].u8[0] = rgba[3];
  205. } else if (draw->format == AV_PIX_FMT_GRAY8 || draw->format == AV_PIX_FMT_GRAY8A) {
  206. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  207. color->comp[1].u8[0] = rgba[3];
  208. } else {
  209. av_log(NULL, AV_LOG_WARNING,
  210. "Color conversion not implemented for %s\n", draw->desc->name);
  211. memset(color, 128, sizeof(*color));
  212. }
  213. }
  214. static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
  215. int plane, int x, int y)
  216. {
  217. return data[plane] +
  218. (y >> draw->vsub[plane]) * linesize[plane] +
  219. (x >> draw->hsub[plane]) * draw->pixelstep[plane];
  220. }
  221. void ff_copy_rectangle2(FFDrawContext *draw,
  222. uint8_t *dst[], int dst_linesize[],
  223. uint8_t *src[], int src_linesize[],
  224. int dst_x, int dst_y, int src_x, int src_y,
  225. int w, int h)
  226. {
  227. int plane, y, wp, hp;
  228. uint8_t *p, *q;
  229. for (plane = 0; plane < draw->nb_planes; plane++) {
  230. p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
  231. q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  232. wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane];
  233. hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
  234. for (y = 0; y < hp; y++) {
  235. memcpy(q, p, wp);
  236. p += src_linesize[plane];
  237. q += dst_linesize[plane];
  238. }
  239. }
  240. }
  241. void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
  242. uint8_t *dst[], int dst_linesize[],
  243. int dst_x, int dst_y, int w, int h)
  244. {
  245. int plane, x, y, wp, hp;
  246. uint8_t *p0, *p;
  247. for (plane = 0; plane < draw->nb_planes; plane++) {
  248. p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  249. wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]);
  250. hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]);
  251. if (!hp)
  252. return;
  253. p = p0;
  254. /* copy first line from color */
  255. for (x = 0; x < wp; x++) {
  256. memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
  257. p += draw->pixelstep[plane];
  258. }
  259. wp *= draw->pixelstep[plane];
  260. /* copy next lines from first line */
  261. p = p0 + dst_linesize[plane];
  262. for (y = 1; y < hp; y++) {
  263. memcpy(p, p0, wp);
  264. p += dst_linesize[plane];
  265. }
  266. }
  267. }
  268. /**
  269. * Clip interval [x; x+w[ within [0; wmax[.
  270. * The resulting w may be negative if the final interval is empty.
  271. * dx, if not null, return the difference between in and out value of x.
  272. */
  273. static void clip_interval(int wmax, int *x, int *w, int *dx)
  274. {
  275. if (dx)
  276. *dx = 0;
  277. if (*x < 0) {
  278. if (dx)
  279. *dx = -*x;
  280. *w += *x;
  281. *x = 0;
  282. }
  283. if (*x + *w > wmax)
  284. *w = wmax - *x;
  285. }
  286. /**
  287. * Decompose w pixels starting at x
  288. * into start + (w starting at x) + end
  289. * with x and w aligned on multiples of 1<<sub.
  290. */
  291. static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
  292. {
  293. int mask = (1 << sub) - 1;
  294. *start = (-*x) & mask;
  295. *x += *start;
  296. *start = FFMIN(*start, *w);
  297. *w -= *start;
  298. *end = *w & mask;
  299. *w >>= sub;
  300. }
  301. static int component_used(FFDrawContext *draw, int plane, int comp)
  302. {
  303. return (draw->comp_mask[plane] >> comp) & 1;
  304. }
  305. /* If alpha is in the [ 0 ; 0x1010101 ] range,
  306. then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range,
  307. and >> 24 gives a correct rounding. */
  308. static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
  309. int dx, int w, unsigned hsub, int left, int right)
  310. {
  311. unsigned asrc = alpha * src;
  312. unsigned tau = 0x1010101 - alpha;
  313. int x;
  314. if (left) {
  315. unsigned suba = (left * alpha) >> hsub;
  316. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  317. dst += dx;
  318. }
  319. for (x = 0; x < w; x++) {
  320. *dst = (*dst * tau + asrc) >> 24;
  321. dst += dx;
  322. }
  323. if (right) {
  324. unsigned suba = (right * alpha) >> hsub;
  325. *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
  326. }
  327. }
  328. void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
  329. uint8_t *dst[], int dst_linesize[],
  330. int dst_w, int dst_h,
  331. int x0, int y0, int w, int h)
  332. {
  333. unsigned alpha, nb_planes, nb_comp, plane, comp;
  334. int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
  335. uint8_t *p0, *p;
  336. /* TODO optimize if alpha = 0xFF */
  337. clip_interval(dst_w, &x0, &w, NULL);
  338. clip_interval(dst_h, &y0, &h, NULL);
  339. if (w <= 0 || h <= 0 || !color->rgba[3])
  340. return;
  341. /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */
  342. alpha = 0x10203 * color->rgba[3] + 0x2;
  343. nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
  344. for (plane = 0; plane < nb_planes; plane++) {
  345. nb_comp = draw->pixelstep[plane];
  346. p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
  347. w_sub = w;
  348. h_sub = h;
  349. x_sub = x0;
  350. y_sub = y0;
  351. subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
  352. subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
  353. for (comp = 0; comp < nb_comp; comp++) {
  354. if (!component_used(draw, plane, comp))
  355. continue;
  356. p = p0 + comp;
  357. if (top) {
  358. blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
  359. draw->pixelstep[plane], w_sub,
  360. draw->hsub[plane], left, right);
  361. p += dst_linesize[plane];
  362. }
  363. for (y = 0; y < h_sub; y++) {
  364. blend_line(p, color->comp[plane].u8[comp], alpha,
  365. draw->pixelstep[plane], w_sub,
  366. draw->hsub[plane], left, right);
  367. p += dst_linesize[plane];
  368. }
  369. if (bottom)
  370. blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
  371. draw->pixelstep[plane], w_sub,
  372. draw->hsub[plane], left, right);
  373. }
  374. }
  375. }
  376. static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
  377. const uint8_t *mask, int mask_linesize, int l2depth,
  378. unsigned w, unsigned h, unsigned shift, unsigned xm0)
  379. {
  380. unsigned xm, x, y, t = 0;
  381. unsigned xmshf = 3 - l2depth;
  382. unsigned xmmod = 7 >> l2depth;
  383. unsigned mbits = (1 << (1 << l2depth)) - 1;
  384. unsigned mmult = 255 / mbits;
  385. for (y = 0; y < h; y++) {
  386. xm = xm0;
  387. for (x = 0; x < w; x++) {
  388. t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
  389. * mmult;
  390. xm++;
  391. }
  392. mask += mask_linesize;
  393. }
  394. alpha = (t >> shift) * alpha;
  395. *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
  396. }
  397. static void blend_line_hv(uint8_t *dst, int dst_delta,
  398. unsigned src, unsigned alpha,
  399. const uint8_t *mask, int mask_linesize, int l2depth, int w,
  400. unsigned hsub, unsigned vsub,
  401. int xm, int left, int right, int hband)
  402. {
  403. int x;
  404. if (left) {
  405. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  406. left, hband, hsub + vsub, xm);
  407. dst += dst_delta;
  408. xm += left;
  409. }
  410. for (x = 0; x < w; x++) {
  411. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  412. 1 << hsub, hband, hsub + vsub, xm);
  413. dst += dst_delta;
  414. xm += 1 << hsub;
  415. }
  416. if (right)
  417. blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
  418. right, hband, hsub + vsub, xm);
  419. }
  420. void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
  421. uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
  422. const uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
  423. int l2depth, unsigned endianness, int x0, int y0)
  424. {
  425. unsigned alpha, nb_planes, nb_comp, plane, comp;
  426. int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
  427. uint8_t *p0, *p;
  428. const uint8_t *m;
  429. clip_interval(dst_w, &x0, &mask_w, &xm0);
  430. clip_interval(dst_h, &y0, &mask_h, &ym0);
  431. mask += ym0 * mask_linesize;
  432. if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
  433. return;
  434. /* alpha is in the [ 0 ; 0x10203 ] range,
  435. alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */
  436. alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
  437. nb_planes = (draw->nb_planes - 1) | 1; /* eliminate alpha */
  438. for (plane = 0; plane < nb_planes; plane++) {
  439. nb_comp = draw->pixelstep[plane];
  440. p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
  441. w_sub = mask_w;
  442. h_sub = mask_h;
  443. x_sub = x0;
  444. y_sub = y0;
  445. subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
  446. subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
  447. for (comp = 0; comp < nb_comp; comp++) {
  448. if (!component_used(draw, plane, comp))
  449. continue;
  450. p = p0 + comp;
  451. m = mask;
  452. if (top) {
  453. blend_line_hv(p, draw->pixelstep[plane],
  454. color->comp[plane].u8[comp], alpha,
  455. m, mask_linesize, l2depth, w_sub,
  456. draw->hsub[plane], draw->vsub[plane],
  457. xm0, left, right, top);
  458. p += dst_linesize[plane];
  459. m += top * mask_linesize;
  460. }
  461. for (y = 0; y < h_sub; y++) {
  462. blend_line_hv(p, draw->pixelstep[plane],
  463. color->comp[plane].u8[comp], alpha,
  464. m, mask_linesize, l2depth, w_sub,
  465. draw->hsub[plane], draw->vsub[plane],
  466. xm0, left, right, 1 << draw->vsub[plane]);
  467. p += dst_linesize[plane];
  468. m += mask_linesize << draw->vsub[plane];
  469. }
  470. if (bottom)
  471. blend_line_hv(p, draw->pixelstep[plane],
  472. color->comp[plane].u8[comp], alpha,
  473. m, mask_linesize, l2depth, w_sub,
  474. draw->hsub[plane], draw->vsub[plane],
  475. xm0, left, right, bottom);
  476. }
  477. }
  478. }
  479. int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
  480. int value)
  481. {
  482. unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
  483. if (!shift)
  484. return value;
  485. if (round_dir >= 0)
  486. value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
  487. return (value >> shift) << shift;
  488. }
  489. AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
  490. {
  491. enum AVPixelFormat i;
  492. FFDrawContext draw;
  493. AVFilterFormats *fmts = NULL;
  494. int ret;
  495. for (i = 0; av_pix_fmt_desc_get(i); i++)
  496. if (ff_draw_init(&draw, i, flags) >= 0 &&
  497. (ret = ff_add_format(&fmts, i)) < 0)
  498. return NULL;
  499. return fmts;
  500. }
  501. #ifdef TEST
  502. #undef printf
  503. int main(void)
  504. {
  505. enum AVPixelFormat f;
  506. const AVPixFmtDescriptor *desc;
  507. FFDrawContext draw;
  508. FFDrawColor color;
  509. int r, i;
  510. for (f = 0; av_pix_fmt_desc_get(f); f++) {
  511. desc = av_pix_fmt_desc_get(f);
  512. if (!desc->name)
  513. continue;
  514. printf("Testing %s...%*s", desc->name,
  515. (int)(16 - strlen(desc->name)), "");
  516. r = ff_draw_init(&draw, f, 0);
  517. if (r < 0) {
  518. char buf[128];
  519. av_strerror(r, buf, sizeof(buf));
  520. printf("no: %s\n", buf);
  521. continue;
  522. }
  523. ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
  524. for (i = 0; i < sizeof(color); i++)
  525. if (((uint8_t *)&color)[i] != 128)
  526. break;
  527. if (i == sizeof(color)) {
  528. printf("fallback color\n");
  529. continue;
  530. }
  531. printf("ok\n");
  532. }
  533. return 0;
  534. }
  535. #endif