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.

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