drawutils.c 19 KB

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