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.

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