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