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