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.

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