drawutils.c 19 KB

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