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