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.

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