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.

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