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.

313 lines
11KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/avutil.h"
  19. #include "libavutil/colorspace.h"
  20. #include "libavutil/pixdesc.h"
  21. #include "drawutils.h"
  22. enum { RED = 0, GREEN, BLUE, ALPHA };
  23. int ff_fill_rgba_map(uint8_t *rgba_map, enum PixelFormat pix_fmt)
  24. {
  25. switch (pix_fmt) {
  26. case PIX_FMT_0RGB:
  27. case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
  28. case PIX_FMT_0BGR:
  29. case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
  30. case PIX_FMT_RGB0:
  31. case PIX_FMT_RGBA:
  32. case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
  33. case PIX_FMT_BGRA:
  34. case PIX_FMT_BGR0:
  35. case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
  36. default: /* unsupported */
  37. return AVERROR(EINVAL);
  38. }
  39. return 0;
  40. }
  41. int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
  42. enum PixelFormat pix_fmt, uint8_t rgba_color[4],
  43. int *is_packed_rgba, uint8_t rgba_map_ptr[4])
  44. {
  45. uint8_t rgba_map[4] = {0};
  46. int i;
  47. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
  48. int hsub = pix_desc->log2_chroma_w;
  49. *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
  50. if (*is_packed_rgba) {
  51. pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
  52. for (i = 0; i < 4; i++)
  53. dst_color[rgba_map[i]] = rgba_color[i];
  54. line[0] = av_malloc(w * pixel_step[0]);
  55. for (i = 0; i < w; i++)
  56. memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
  57. if (rgba_map_ptr)
  58. memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
  59. } else {
  60. int plane;
  61. dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
  62. dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  63. dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
  64. dst_color[3] = rgba_color[3];
  65. for (plane = 0; plane < 4; plane++) {
  66. int line_size;
  67. int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
  68. pixel_step[plane] = 1;
  69. line_size = (w >> hsub1) * pixel_step[plane];
  70. line[plane] = av_malloc(line_size);
  71. memset(line[plane], dst_color[plane], line_size);
  72. }
  73. }
  74. return 0;
  75. }
  76. void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
  77. uint8_t *src[4], int pixelstep[4],
  78. int hsub, int vsub, int x, int y, int w, int h)
  79. {
  80. int i, plane;
  81. uint8_t *p;
  82. for (plane = 0; plane < 4 && dst[plane]; plane++) {
  83. int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
  84. int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
  85. p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
  86. for (i = 0; i < (h >> vsub1); i++) {
  87. memcpy(p + (x >> hsub1) * pixelstep[plane],
  88. src[plane], (w >> hsub1) * pixelstep[plane]);
  89. p += dst_linesize[plane];
  90. }
  91. }
  92. }
  93. void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
  94. uint8_t *src[4], int src_linesize[4], int pixelstep[4],
  95. int hsub, int vsub, int x, int y, int y2, int w, int h)
  96. {
  97. int i, plane;
  98. uint8_t *p;
  99. for (plane = 0; plane < 4 && dst[plane]; plane++) {
  100. int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
  101. int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
  102. p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
  103. for (i = 0; i < (h >> vsub1); i++) {
  104. memcpy(p + (x >> hsub1) * pixelstep[plane],
  105. src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * pixelstep[plane]);
  106. p += dst_linesize[plane];
  107. }
  108. }
  109. }
  110. int ff_draw_init(FFDrawContext *draw, enum PixelFormat format, unsigned flags)
  111. {
  112. const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
  113. const AVComponentDescriptor *c;
  114. unsigned i, nb_planes = 0;
  115. int pixelstep[MAX_PLANES] = { 0 };
  116. if (desc->flags & ~(PIX_FMT_PLANAR | PIX_FMT_RGB))
  117. return AVERROR(ENOSYS);
  118. for (i = 0; i < desc->nb_components; i++) {
  119. c = &desc->comp[i];
  120. /* for now, only 8-bits formats */
  121. if (c->depth_minus1 != 8 - 1)
  122. return AVERROR(ENOSYS);
  123. if (c->plane >= MAX_PLANES)
  124. return AVERROR(ENOSYS);
  125. /* strange interleaving */
  126. if (pixelstep[c->plane] != 0 &&
  127. pixelstep[c->plane] != c->step_minus1 + 1)
  128. return AVERROR(ENOSYS);
  129. pixelstep[c->plane] = c->step_minus1 + 1;
  130. nb_planes = FFMAX(nb_planes, c->plane + 1);
  131. }
  132. if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
  133. return AVERROR(ENOSYS); /* exclude NV12 and NV21 */
  134. memset(draw, 0, sizeof(*draw));
  135. draw->desc = desc;
  136. draw->format = format;
  137. draw->nb_planes = nb_planes;
  138. memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
  139. if (nb_planes >= 3 && !(desc->flags & PIX_FMT_RGB)) {
  140. draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
  141. draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
  142. }
  143. return 0;
  144. }
  145. void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, uint8_t rgba[4])
  146. {
  147. unsigned i;
  148. uint8_t rgba_map[4];
  149. if ((draw->desc->flags & PIX_FMT_RGB) && draw->nb_planes == 1 &&
  150. ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
  151. for (i = 0; i < 4; i++)
  152. color->comp[0].u8[rgba_map[i]] = rgba[i];
  153. } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
  154. /* assume YUV */
  155. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  156. color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
  157. color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
  158. color->comp[3].u8[0] = rgba[3];
  159. } else if (draw->format == PIX_FMT_GRAY8 || draw->format == PIX_FMT_GRAY8A) {
  160. color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
  161. color->comp[1].u8[0] = rgba[3];
  162. } else {
  163. av_log(NULL, AV_LOG_WARNING,
  164. "Color conversion not implemented for %s\n", draw->desc->name);
  165. memset(color, 128, sizeof(*color));
  166. }
  167. }
  168. static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
  169. int plane, int x, int y)
  170. {
  171. return data[plane] +
  172. (y >> draw->vsub[plane]) * linesize[plane] +
  173. (x >> draw->hsub[plane]) * draw->pixelstep[plane];
  174. }
  175. void ff_copy_rectangle2(FFDrawContext *draw,
  176. uint8_t *dst[], int dst_linesize[],
  177. uint8_t *src[], int src_linesize[],
  178. int dst_x, int dst_y, int src_x, int src_y,
  179. int w, int h)
  180. {
  181. int plane, y, wp, hp;
  182. uint8_t *p, *q;
  183. for (plane = 0; plane < draw->nb_planes; plane++) {
  184. p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
  185. q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  186. wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
  187. hp = (h >> draw->vsub[plane]);
  188. for (y = 0; y < hp; y++) {
  189. memcpy(q, p, wp);
  190. p += src_linesize[plane];
  191. q += dst_linesize[plane];
  192. }
  193. }
  194. }
  195. void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
  196. uint8_t *dst[], int dst_linesize[],
  197. int dst_x, int dst_y, int w, int h)
  198. {
  199. int plane, x, y, wp, hp;
  200. uint8_t *p0, *p;
  201. for (plane = 0; plane < draw->nb_planes; plane++) {
  202. p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
  203. wp = (w >> draw->hsub[plane]);
  204. hp = (h >> draw->vsub[plane]);
  205. if (!hp)
  206. return;
  207. p = p0;
  208. /* copy first line from color */
  209. for (x = 0; x < wp; x++) {
  210. memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
  211. p += draw->pixelstep[plane];
  212. }
  213. wp *= draw->pixelstep[plane];
  214. /* copy next lines from first line */
  215. p = p0 + dst_linesize[plane];
  216. for (y = 1; y < hp; y++) {
  217. memcpy(p, p0, wp);
  218. p += dst_linesize[plane];
  219. }
  220. }
  221. }
  222. int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
  223. int value)
  224. {
  225. unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
  226. if (!shift)
  227. return value;
  228. if (round_dir >= 0)
  229. value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
  230. return (value >> shift) << shift;
  231. }
  232. AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
  233. {
  234. enum PixelFormat i, pix_fmts[PIX_FMT_NB + 1];
  235. unsigned n = 0;
  236. FFDrawContext draw;
  237. for (i = 0; i < PIX_FMT_NB; i++)
  238. if (ff_draw_init(&draw, i, flags) >= 0)
  239. pix_fmts[n++] = i;
  240. pix_fmts[n++] = PIX_FMT_NONE;
  241. return avfilter_make_format_list(pix_fmts);
  242. }
  243. #ifdef TEST
  244. #undef printf
  245. int main(void)
  246. {
  247. enum PixelFormat f;
  248. const AVPixFmtDescriptor *desc;
  249. FFDrawContext draw;
  250. FFDrawColor color;
  251. int r, i;
  252. for (f = 0; f < PIX_FMT_NB; f++) {
  253. desc = &av_pix_fmt_descriptors[f];
  254. if (!desc->name)
  255. continue;
  256. printf("Testing %s...%*s", desc->name,
  257. (int)(16 - strlen(desc->name)), "");
  258. r = ff_draw_init(&draw, f, 0);
  259. if (r < 0) {
  260. char buf[128];
  261. av_strerror(r, buf, sizeof(buf));
  262. printf("no: %s\n", buf);
  263. continue;
  264. }
  265. ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
  266. for (i = 0; i < sizeof(color); i++)
  267. if (((uint8_t *)&color)[i] != 128)
  268. break;
  269. if (i == sizeof(color)) {
  270. printf("fallback color\n");
  271. continue;
  272. }
  273. printf("ok\n");
  274. }
  275. return 0;
  276. }
  277. #endif