libavfilter/drawutils.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 <string.h> | ||
| 23 | |||
| 24 | #include "libavutil/avassert.h" | ||
| 25 | #include "libavutil/avutil.h" | ||
| 26 | #include "libavutil/csp.h" | ||
| 27 | #include "libavutil/intreadwrite.h" | ||
| 28 | #include "libavutil/pixdesc.h" | ||
| 29 | #include "colorspace.h" | ||
| 30 | #include "drawutils.h" | ||
| 31 | #include "formats.h" | ||
| 32 | |||
| 33 | enum { RED = 0, GREEN, BLUE, ALPHA }; | ||
| 34 | |||
| 35 | 1189 | static int fill_map(const AVPixFmtDescriptor *desc, uint8_t *map) | |
| 36 | { | ||
| 37 | 1189 | if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL | | |
| 38 | AV_PIX_FMT_FLAG_BAYER | AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_PAL)) | ||
| 39 | ✗ | return AVERROR(EINVAL); | |
| 40 | 1189 | av_assert0(desc->nb_components == 3 + !!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)); | |
| 41 | 1189 | if (desc->flags & AV_PIX_FMT_FLAG_PLANAR) { | |
| 42 | 949 | if (desc->nb_components != av_pix_fmt_count_planes(av_pix_fmt_desc_get_id(desc))) | |
| 43 | 307 | return AVERROR(EINVAL); | |
| 44 | 642 | map[RED] = desc->comp[0].plane; | |
| 45 | 642 | map[GREEN] = desc->comp[1].plane; | |
| 46 | 642 | map[BLUE] = desc->comp[2].plane; | |
| 47 | 642 | map[ALPHA] = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) ? desc->comp[3].plane : 3; | |
| 48 | } else { | ||
| 49 | 240 | int had0 = 0; | |
| 50 | 240 | unsigned depthb = 0; | |
| 51 | 971 | for (unsigned i = 0; i < desc->nb_components; i++) { | |
| 52 | /* all components must have same depth in bytes */ | ||
| 53 | 734 | unsigned db = (desc->comp[i].depth + 7) / 8; | |
| 54 | 734 | unsigned pos = desc->comp[i].offset / db; | |
| 55 | 734 | if (depthb && (depthb != db)) | |
| 56 | ✗ | return AVERROR(ENOSYS); | |
| 57 | |||
| 58 | 734 | if (desc->comp[i].offset % db) | |
| 59 | 3 | return AVERROR(ENOSYS); | |
| 60 | |||
| 61 | 731 | had0 |= pos == 0; | |
| 62 | 731 | map[i] = pos; | |
| 63 | 731 | depthb = db; | |
| 64 | } | ||
| 65 | |||
| 66 | 237 | if (desc->nb_components == 3) | |
| 67 | 218 | map[ALPHA] = had0 ? 3 : 0; | |
| 68 | } | ||
| 69 | |||
| 70 | 879 | av_assert0(map[RED] != map[GREEN]); | |
| 71 | 879 | av_assert0(map[GREEN] != map[BLUE]); | |
| 72 | 879 | av_assert0(map[BLUE] != map[RED]); | |
| 73 | 879 | av_assert0(map[RED] != map[ALPHA]); | |
| 74 | 879 | av_assert0(map[GREEN] != map[ALPHA]); | |
| 75 | 879 | av_assert0(map[BLUE] != map[ALPHA]); | |
| 76 | |||
| 77 | 879 | return 0; | |
| 78 | } | ||
| 79 | |||
| 80 | 620 | int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt) | |
| 81 | { | ||
| 82 | 620 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 83 | 620 | if (!(desc->flags & AV_PIX_FMT_FLAG_RGB)) | |
| 84 | 55 | return AVERROR(EINVAL); | |
| 85 | 565 | return fill_map(desc, rgba_map); | |
| 86 | } | ||
| 87 | |||
| 88 | 624 | int ff_fill_ayuv_map(uint8_t *ayuv_map, enum AVPixelFormat pix_fmt) | |
| 89 | { | ||
| 90 | 624 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); | |
| 91 | 624 | if (desc->flags & AV_PIX_FMT_FLAG_RGB) | |
| 92 | ✗ | return AVERROR(EINVAL); | |
| 93 | 624 | return fill_map(desc, ayuv_map); | |
| 94 | } | ||
| 95 | |||
| 96 | 72144 | int ff_draw_init2(FFDrawContext *draw, enum AVPixelFormat format, enum AVColorSpace csp, | |
| 97 | enum AVColorRange range, enum AVAlphaMode alpha, unsigned flags) | ||
| 98 | { | ||
| 99 | 72144 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format); | |
| 100 | 72144 | const AVLumaCoefficients *luma = NULL; | |
| 101 | const AVComponentDescriptor *c; | ||
| 102 | 72144 | unsigned nb_planes = 0; | |
| 103 | 72144 | int pixelstep[MAX_PLANES] = { 0 }; | |
| 104 | 72144 | int depthb = 0; | |
| 105 | |||
| 106 | 72144 | if (!desc || !desc->name) | |
| 107 | ✗ | return AVERROR(EINVAL); | |
| 108 | 72144 | if (desc->flags & AV_PIX_FMT_FLAG_BE) | |
| 109 | 26334 | return AVERROR(ENOSYS); | |
| 110 | 45810 | if (desc->flags & ~(AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_ALPHA)) | |
| 111 | 11704 | return AVERROR(ENOSYS); | |
| 112 | 34106 | if (csp == AVCOL_SPC_UNSPECIFIED) | |
| 113 | 34051 | csp = (desc->flags & AV_PIX_FMT_FLAG_RGB) ? AVCOL_SPC_RGB : AVCOL_SPC_SMPTE170M; | |
| 114 | 34106 | if (!(desc->flags & AV_PIX_FMT_FLAG_RGB) && !(luma = av_csp_luma_coeffs_from_avcsp(csp))) | |
| 115 | ✗ | return AVERROR(EINVAL); | |
| 116 | 34106 | if (range == AVCOL_RANGE_UNSPECIFIED) | |
| 117 | 67022 | range = (format == AV_PIX_FMT_YUVJ420P || format == AV_PIX_FMT_YUVJ422P || | |
| 118 | 33112 | format == AV_PIX_FMT_YUVJ444P || format == AV_PIX_FMT_YUVJ411P || | |
| 119 | 32580 | format == AV_PIX_FMT_YUVJ440P || csp == AVCOL_SPC_RGB) | |
| 120 | 33910 | ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; | |
| 121 | 34106 | if (range != AVCOL_RANGE_JPEG && range != AVCOL_RANGE_MPEG) | |
| 122 | ✗ | return AVERROR(EINVAL); | |
| 123 | 117843 | for (unsigned i = 0; i < desc->nb_components; i++) { | |
| 124 | int db; | ||
| 125 | 91717 | c = &desc->comp[i]; | |
| 126 | /* for now, only 8-16 bits formats */ | ||
| 127 | 91717 | if (c->depth < 8 || c->depth > 16) | |
| 128 | 3724 | return AVERROR(ENOSYS); | |
| 129 | 87993 | if (c->plane >= MAX_PLANES) | |
| 130 | ✗ | return AVERROR(ENOSYS); | |
| 131 | /* data must either be in the high or low bits, never middle */ | ||
| 132 | 87993 | if (c->shift && ((c->shift + c->depth) & 0x7)) | |
| 133 | 1064 | return AVERROR(ENOSYS); | |
| 134 | /* mixed >8 and <=8 depth */ | ||
| 135 | 86929 | db = (c->depth + 7) / 8; | |
| 136 | 86929 | if (depthb && (depthb != db)) | |
| 137 | ✗ | return AVERROR(ENOSYS); | |
| 138 | 86929 | depthb = db; | |
| 139 | 86929 | if (db * (c->offset + 1) > 16) | |
| 140 | ✗ | return AVERROR(ENOSYS); | |
| 141 | 86929 | if (c->offset % db) | |
| 142 | ✗ | return AVERROR(ENOSYS); | |
| 143 | /* strange interleaving */ | ||
| 144 | 86929 | if (pixelstep[c->plane] != 0 && | |
| 145 | 17411 | pixelstep[c->plane] != c->step) | |
| 146 | 1862 | return AVERROR(ENOSYS); | |
| 147 | 85067 | pixelstep[c->plane] = c->step; | |
| 148 | 85067 | if (pixelstep[c->plane] >= 8) | |
| 149 | 1330 | return AVERROR(ENOSYS); | |
| 150 | 83737 | nb_planes = FFMAX(nb_planes, c->plane + 1); | |
| 151 | } | ||
| 152 | 26126 | memset(draw, 0, sizeof(*draw)); | |
| 153 | 26126 | draw->desc = desc; | |
| 154 | 26126 | draw->format = format; | |
| 155 | 26126 | draw->nb_planes = nb_planes; | |
| 156 | 26126 | draw->range = range; | |
| 157 | 26126 | draw->csp = csp; | |
| 158 | 26126 | draw->alpha = alpha; | |
| 159 | 26126 | draw->flags = flags; | |
| 160 | 26126 | if (luma) | |
| 161 | 19420 | ff_fill_rgb2yuv_table(luma, draw->rgb2yuv); | |
| 162 | 26126 | memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep)); | |
| 163 | 26126 | draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w; | |
| 164 | 26126 | draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h; | |
| 165 | 26126 | return 0; | |
| 166 | } | ||
| 167 | |||
| 168 | 323 | int ff_draw_init_from_link(FFDrawContext *draw, const AVFilterLink *link, | |
| 169 | unsigned flags) | ||
| 170 | { | ||
| 171 | 323 | return ff_draw_init2(draw, link->format, link->colorspace, link->color_range, link->alpha_mode, flags); | |
| 172 | } | ||
| 173 | |||
| 174 | 71821 | int ff_draw_init(FFDrawContext *draw, enum AVPixelFormat format, unsigned flags) | |
| 175 | { | ||
| 176 | 71821 | return ff_draw_init2(draw, format, AVCOL_SPC_UNSPECIFIED, AVCOL_RANGE_UNSPECIFIED, AVALPHA_MODE_UNSPECIFIED, flags); | |
| 177 | } | ||
| 178 | |||
| 179 | 588242 | void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4]) | |
| 180 | { | ||
| 181 | double yuvad[4]; | ||
| 182 | double rgbad[4]; | ||
| 183 | 588242 | const AVPixFmtDescriptor *desc = draw->desc; | |
| 184 | |||
| 185 | 588242 | if (rgba != color->rgba) | |
| 186 | 588242 | memcpy(color->rgba, rgba, sizeof(color->rgba)); | |
| 187 | |||
| 188 | 588242 | memset(color->comp, 0, sizeof(color->comp)); | |
| 189 | |||
| 190 | 2941210 | for (int i = 0; i < 4; i++) | |
| 191 | 2352968 | rgbad[i] = color->rgba[i] / 255.; | |
| 192 | |||
| 193 | 588242 | if (draw->alpha == AVALPHA_MODE_PREMULTIPLIED) { | |
| 194 | ✗ | for (int i = 0; i < 3; i++) | |
| 195 | ✗ | rgbad[i] *= rgbad[3]; | |
| 196 | } | ||
| 197 | |||
| 198 | 588242 | if (draw->desc->flags & AV_PIX_FMT_FLAG_RGB) | |
| 199 | 54710 | memcpy(yuvad, rgbad, sizeof(double) * 3); | |
| 200 | else | ||
| 201 | 533532 | ff_matrix_mul_3x3_vec(yuvad, rgbad, draw->rgb2yuv); | |
| 202 | |||
| 203 | 588242 | yuvad[3] = rgbad[3]; | |
| 204 | |||
| 205 | 2352968 | for (int i = 0; i < 3; i++) { | |
| 206 | 1764726 | int chroma = (!(draw->desc->flags & AV_PIX_FMT_FLAG_RGB) && i > 0); | |
| 207 | 1764726 | if (draw->range == AVCOL_RANGE_MPEG) { | |
| 208 | 1599111 | yuvad[i] *= (chroma ? 224. : 219.) / 255.; | |
| 209 | 1599111 | yuvad[i] += (chroma ? 128. : 16.) / 255.; | |
| 210 | 165615 | } else if (chroma) { | |
| 211 | 990 | yuvad[i] += 0.5; | |
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | // Ensure we place the alpha appropriately for gray formats | ||
| 216 | 588242 | if (desc->nb_components <= 2) | |
| 217 | 487 | yuvad[1] = yuvad[3]; | |
| 218 | |||
| 219 | 2378146 | for (unsigned i = 0; i < desc->nb_components; i++) { | |
| 220 | 1789904 | unsigned val = yuvad[i] * ((1 << (draw->desc->comp[i].depth + draw->desc->comp[i].shift)) - 1) + 0.5; | |
| 221 | 1789904 | if (desc->comp[i].depth > 8) | |
| 222 | 60332 | color->comp[desc->comp[i].plane].u16[desc->comp[i].offset / 2] = val; | |
| 223 | else | ||
| 224 | 1729572 | color->comp[desc->comp[i].plane].u8[desc->comp[i].offset] = val; | |
| 225 | } | ||
| 226 | 588242 | } | |
| 227 | |||
| 228 | 2204395 | static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[], | |
| 229 | int plane, int x, int y) | ||
| 230 | { | ||
| 231 | 2204395 | return data[plane] + | |
| 232 | 4408790 | (y >> draw->vsub[plane]) * linesize[plane] + | |
| 233 | 2204395 | (x >> draw->hsub[plane]) * draw->pixelstep[plane]; | |
| 234 | } | ||
| 235 | |||
| 236 | 90 | void ff_copy_rectangle2(FFDrawContext *draw, | |
| 237 | uint8_t *dst[], int dst_linesize[], | ||
| 238 | uint8_t *src[], int src_linesize[], | ||
| 239 | int dst_x, int dst_y, int src_x, int src_y, | ||
| 240 | int w, int h) | ||
| 241 | { | ||
| 242 | int wp, hp; | ||
| 243 | uint8_t *p, *q; | ||
| 244 | |||
| 245 | 360 | for (int plane = 0; plane < draw->nb_planes; plane++) { | |
| 246 | 270 | p = pointer_at(draw, src, src_linesize, plane, src_x, src_y); | |
| 247 | 270 | q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y); | |
| 248 | 270 | wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]) * draw->pixelstep[plane]; | |
| 249 | 270 | hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]); | |
| 250 | 52110 | for (int y = 0; y < hp; y++) { | |
| 251 | 51840 | memcpy(q, p, wp); | |
| 252 | 51840 | p += src_linesize[plane]; | |
| 253 | 51840 | q += dst_linesize[plane]; | |
| 254 | } | ||
| 255 | } | ||
| 256 | 90 | } | |
| 257 | |||
| 258 | 619476 | void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color, | |
| 259 | uint8_t *dst[], int dst_linesize[], | ||
| 260 | int dst_x, int dst_y, int w, int h) | ||
| 261 | { | ||
| 262 | int wp, hp; | ||
| 263 | uint8_t *p0, *p; | ||
| 264 | 619476 | FFDrawColor color_tmp = *color; | |
| 265 | |||
| 266 | 2368218 | for (int plane = 0; plane < draw->nb_planes; plane++) { | |
| 267 | 1748742 | p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y); | |
| 268 | 1748742 | wp = AV_CEIL_RSHIFT(w, draw->hsub[plane]); | |
| 269 | 1748742 | hp = AV_CEIL_RSHIFT(h, draw->vsub[plane]); | |
| 270 | 1748742 | if (!hp) | |
| 271 | ✗ | return; | |
| 272 | 1748742 | p = p0; | |
| 273 | |||
| 274 | if (HAVE_BIGENDIAN && draw->desc->comp[0].depth > 8) { | ||
| 275 | for (int x = 0; 2*x < draw->pixelstep[plane]; x++) | ||
| 276 | color_tmp.comp[plane].u16[x] = av_bswap16(color_tmp.comp[plane].u16[x]); | ||
| 277 | } | ||
| 278 | |||
| 279 | /* copy first line from color */ | ||
| 280 | 7931399 | for (int x = 0; x < wp; x++) { | |
| 281 | 6182657 | memcpy(p, color_tmp.comp[plane].u8, draw->pixelstep[plane]); | |
| 282 | 6182657 | p += draw->pixelstep[plane]; | |
| 283 | } | ||
| 284 | 1748742 | wp *= draw->pixelstep[plane]; | |
| 285 | /* copy next lines from first line */ | ||
| 286 | 1748742 | p = p0 + dst_linesize[plane]; | |
| 287 | 25315838 | for (int y = 1; y < hp; y++) { | |
| 288 | 23567096 | memcpy(p, p0, wp); | |
| 289 | 23567096 | p += dst_linesize[plane]; | |
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | /** | ||
| 295 | * Clip interval [x; x+w[ within [0; wmax[. | ||
| 296 | * The resulting w may be negative if the final interval is empty. | ||
| 297 | * dx, if not null, return the difference between in and out value of x. | ||
| 298 | */ | ||
| 299 | 316370 | static void clip_interval(int wmax, int *x, int *w, int *dx) | |
| 300 | { | ||
| 301 | 316370 | if (dx) | |
| 302 | 304738 | *dx = 0; | |
| 303 | 316370 | if (*x < 0) { | |
| 304 | ✗ | if (dx) | |
| 305 | ✗ | *dx = -*x; | |
| 306 | ✗ | *w += *x; | |
| 307 | ✗ | *x = 0; | |
| 308 | } | ||
| 309 | 316370 | if (*x + *w > wmax) | |
| 310 | 4139 | *w = wmax - *x; | |
| 311 | 316370 | } | |
| 312 | |||
| 313 | /** | ||
| 314 | * Decompose w pixels starting at x | ||
| 315 | * into start + (w starting at x) + end | ||
| 316 | * with x and w aligned on multiples of 1<<sub. | ||
| 317 | */ | ||
| 318 | 910226 | static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end) | |
| 319 | { | ||
| 320 | 910226 | int mask = (1 << sub) - 1; | |
| 321 | |||
| 322 | 910226 | *start = (-*x) & mask; | |
| 323 | 910226 | *x += *start; | |
| 324 | 910226 | *start = FFMIN(*start, *w); | |
| 325 | 910226 | *w -= *start; | |
| 326 | 910226 | *end = *w & mask; | |
| 327 | 910226 | *w >>= sub; | |
| 328 | 910226 | } | |
| 329 | |||
| 330 | /* If alpha is in the [ 0 ; 0x1010101 ] range, | ||
| 331 | then alpha * value is in the [ 0 ; 0xFFFFFFFF ] range, | ||
| 332 | and >> 24 gives a correct rounding. */ | ||
| 333 | 418844 | static void blend_line(uint8_t *dst, unsigned src, unsigned alpha, | |
| 334 | int dx, int w, unsigned hsub, int left, int right) | ||
| 335 | { | ||
| 336 | 418844 | unsigned asrc = alpha * src; | |
| 337 | 418844 | unsigned tau = 0x1010101 - alpha; | |
| 338 | |||
| 339 | 418844 | if (left) { | |
| 340 | ✗ | unsigned suba = (left * alpha) >> hsub; | |
| 341 | ✗ | *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24; | |
| 342 | ✗ | dst += dx; | |
| 343 | } | ||
| 344 | 31718046 | for (int x = 0; x < w; x++) { | |
| 345 | 31299202 | *dst = (*dst * tau + asrc) >> 24; | |
| 346 | 31299202 | dst += dx; | |
| 347 | } | ||
| 348 | 418844 | if (right) { | |
| 349 | ✗ | unsigned suba = (right * alpha) >> hsub; | |
| 350 | ✗ | *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24; | |
| 351 | } | ||
| 352 | 418844 | } | |
| 353 | |||
| 354 | 12096 | static void blend_line16(uint8_t *dst, unsigned src, unsigned alpha, | |
| 355 | int dx, int w, unsigned hsub, int left, int right) | ||
| 356 | { | ||
| 357 | 12096 | unsigned asrc = alpha * src; | |
| 358 | 12096 | unsigned tau = 0x10001 - alpha; | |
| 359 | |||
| 360 | 12096 | if (left) { | |
| 361 | ✗ | unsigned suba = (left * alpha) >> hsub; | |
| 362 | ✗ | uint16_t value = AV_RL16(dst); | |
| 363 | ✗ | AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16); | |
| 364 | ✗ | dst += dx; | |
| 365 | } | ||
| 366 | 817488 | for (int x = 0; x < w; x++) { | |
| 367 | 805392 | uint16_t value = AV_RL16(dst); | |
| 368 | 805392 | AV_WL16(dst, (value * tau + asrc) >> 16); | |
| 369 | 805392 | dst += dx; | |
| 370 | } | ||
| 371 | 12096 | if (right) { | |
| 372 | ✗ | unsigned suba = (right * alpha) >> hsub; | |
| 373 | ✗ | uint16_t value = AV_RL16(dst); | |
| 374 | ✗ | AV_WL16(dst, (value * (0x10001 - suba) + src * suba) >> 16); | |
| 375 | } | ||
| 376 | 12096 | } | |
| 377 | |||
| 378 | /* the colour as blended: its transparency is already in the coverage, so it covers the alpha plane fully */ | ||
| 379 | 158185 | static FFDrawColor blended_color(FFDrawContext *draw, const FFDrawColor *color) | |
| 380 | { | ||
| 381 | 158185 | const AVPixFmtDescriptor *desc = draw->desc; | |
| 382 | 158185 | const AVComponentDescriptor *alpha = &desc->comp[desc->nb_components - 1]; | |
| 383 | 158185 | FFDrawColor over = *color; | |
| 384 | |||
| 385 | 158185 | if (desc->flags & AV_PIX_FMT_FLAG_ALPHA && draw->flags & FF_DRAW_PROCESS_ALPHA) { | |
| 386 | 2 | if (alpha->depth <= 8) | |
| 387 | 2 | over.comp[alpha->plane].u8[alpha->offset] = 0xFF; | |
| 388 | else | ||
| 389 | ✗ | over.comp[alpha->plane].u16[alpha->offset / 2] = (1 << alpha->depth) - 1; | |
| 390 | } | ||
| 391 | 158185 | return over; | |
| 392 | } | ||
| 393 | |||
| 394 | 5816 | void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color, | |
| 395 | uint8_t *dst[], int dst_linesize[], | ||
| 396 | int dst_w, int dst_h, | ||
| 397 | int x0, int y0, int w, int h) | ||
| 398 | { | ||
| 399 | 5816 | FFDrawColor over = blended_color(draw, color); | |
| 400 | unsigned alpha, nb_planes, nb_comp; | ||
| 401 | int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom; | ||
| 402 | uint8_t *p0, *p; | ||
| 403 | |||
| 404 | 11632 | nb_comp = draw->desc->nb_components - | |
| 405 | 5816 | !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA)); | |
| 406 | |||
| 407 | /* TODO optimize if alpha = 0xFF */ | ||
| 408 | 5816 | clip_interval(dst_w, &x0, &w, NULL); | |
| 409 | 5816 | clip_interval(dst_h, &y0, &h, NULL); | |
| 410 | 5816 | if (w <= 0 || h <= 0 || !color->rgba[3]) | |
| 411 | ✗ | return; | |
| 412 | 5816 | if (draw->desc->comp[0].depth <= 8) { | |
| 413 | /* 0x10203 * alpha + 2 is in the [ 2 ; 0x1010101 - 2 ] range */ | ||
| 414 | 5700 | alpha = 0x10203 * color->rgba[3] + 0x2; | |
| 415 | } else { | ||
| 416 | /* 0x101 * alpha is in the [ 2 ; 0x1001] range */ | ||
| 417 | 116 | alpha = 0x101 * color->rgba[3] + 0x2; | |
| 418 | } | ||
| 419 | 5816 | nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA)); | |
| 420 | 5816 | nb_planes += !nb_planes; | |
| 421 | 22936 | for (unsigned plane = 0; plane < nb_planes; plane++) { | |
| 422 | 17120 | p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0); | |
| 423 | 17120 | w_sub = w; | |
| 424 | 17120 | h_sub = h; | |
| 425 | 17120 | x_sub = x0; | |
| 426 | 17120 | y_sub = y0; | |
| 427 | 17120 | subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right); | |
| 428 | 17120 | subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom); | |
| 429 | 68469 | for (unsigned comp = 0; comp < nb_comp; comp++) { | |
| 430 | 51349 | const int depth = draw->desc->comp[comp].depth; | |
| 431 | 51349 | const int offset = draw->desc->comp[comp].offset; | |
| 432 | 51349 | const int index = offset / ((depth + 7) / 8); | |
| 433 | |||
| 434 | 51349 | if (draw->desc->comp[comp].plane != plane) | |
| 435 | 33912 | continue; | |
| 436 | 17437 | p = p0 + offset; | |
| 437 | 17437 | if (top) { | |
| 438 | ✗ | if (depth <= 8) { | |
| 439 | ✗ | blend_line(p, over.comp[plane].u8[index], alpha >> 1, | |
| 440 | draw->pixelstep[plane], w_sub, | ||
| 441 | ✗ | draw->hsub[plane], left, right); | |
| 442 | } else { | ||
| 443 | ✗ | blend_line16(p, over.comp[plane].u16[index], alpha >> 1, | |
| 444 | draw->pixelstep[plane], w_sub, | ||
| 445 | ✗ | draw->hsub[plane], left, right); | |
| 446 | } | ||
| 447 | ✗ | p += dst_linesize[plane]; | |
| 448 | } | ||
| 449 | 17437 | if (depth <= 8) { | |
| 450 | 435945 | for (int y = 0; y < h_sub; y++) { | |
| 451 | 418844 | blend_line(p, over.comp[plane].u8[index], alpha, | |
| 452 | draw->pixelstep[plane], w_sub, | ||
| 453 | 418844 | draw->hsub[plane], left, right); | |
| 454 | 418844 | p += dst_linesize[plane]; | |
| 455 | } | ||
| 456 | } else { | ||
| 457 | 12432 | for (int y = 0; y < h_sub; y++) { | |
| 458 | 12096 | blend_line16(p, over.comp[plane].u16[index], alpha, | |
| 459 | draw->pixelstep[plane], w_sub, | ||
| 460 | 12096 | draw->hsub[plane], left, right); | |
| 461 | 12096 | p += dst_linesize[plane]; | |
| 462 | } | ||
| 463 | } | ||
| 464 | 17437 | if (bottom) { | |
| 465 | ✗ | if (depth <= 8) { | |
| 466 | ✗ | blend_line(p, over.comp[plane].u8[index], alpha >> 1, | |
| 467 | draw->pixelstep[plane], w_sub, | ||
| 468 | ✗ | draw->hsub[plane], left, right); | |
| 469 | } else { | ||
| 470 | ✗ | blend_line16(p, over.comp[plane].u16[index], alpha >> 1, | |
| 471 | draw->pixelstep[plane], w_sub, | ||
| 472 | ✗ | draw->hsub[plane], left, right); | |
| 473 | } | ||
| 474 | } | ||
| 475 | } | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | 1083136 | static void blend_pixel16(uint8_t *dst, unsigned src, unsigned alpha, | |
| 480 | const uint8_t *mask, int mask_linesize, int l2depth, | ||
| 481 | unsigned w, unsigned h, unsigned shift, unsigned xm0) | ||
| 482 | { | ||
| 483 | 1083136 | unsigned t = 0; | |
| 484 | 1083136 | unsigned xmshf = 3 - l2depth; | |
| 485 | 1083136 | unsigned xmmod = 7 >> l2depth; | |
| 486 | 1083136 | unsigned mbits = (1 << (1 << l2depth)) - 1; | |
| 487 | 1083136 | unsigned mmult = 255 / mbits; | |
| 488 | 1083136 | uint16_t value = AV_RL16(dst); | |
| 489 | |||
| 490 | 2166272 | for (unsigned y = 0; y < h; y++) { | |
| 491 | 1083136 | unsigned xm = xm0; | |
| 492 | 2701312 | for (unsigned x = 0; x < w; x++) { | |
| 493 | 1618176 | t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits) | |
| 494 | 1618176 | * mmult; | |
| 495 | 1618176 | xm++; | |
| 496 | } | ||
| 497 | 1083136 | mask += mask_linesize; | |
| 498 | } | ||
| 499 | 1083136 | alpha = (t >> shift) * alpha; | |
| 500 | 1083136 | AV_WL16(dst, ((0x10001 - alpha) * value + alpha * src) >> 16); | |
| 501 | 1083136 | } | |
| 502 | |||
| 503 | 32114684 | static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha, | |
| 504 | const uint8_t *mask, int mask_linesize, int l2depth, | ||
| 505 | unsigned w, unsigned h, unsigned shift, unsigned xm0) | ||
| 506 | { | ||
| 507 | 32114684 | unsigned t = 0; | |
| 508 | 32114684 | unsigned xmshf = 3 - l2depth; | |
| 509 | 32114684 | unsigned xmmod = 7 >> l2depth; | |
| 510 | 32114684 | unsigned mbits = (1 << (1 << l2depth)) - 1; | |
| 511 | 32114684 | unsigned mmult = 255 / mbits; | |
| 512 | |||
| 513 | 73625056 | for (unsigned y = 0; y < h; y++) { | |
| 514 | 41510372 | unsigned xm = xm0; | |
| 515 | 102036792 | for (unsigned x = 0; x < w; x++) { | |
| 516 | 60526420 | t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits) | |
| 517 | 60526420 | * mmult; | |
| 518 | 60526420 | xm++; | |
| 519 | } | ||
| 520 | 41510372 | mask += mask_linesize; | |
| 521 | } | ||
| 522 | 32114684 | alpha = (t >> shift) * alpha; | |
| 523 | 32114684 | *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24; | |
| 524 | 32114684 | } | |
| 525 | |||
| 526 | 165312 | static void blend_line_hv16(uint8_t *dst, int dst_delta, | |
| 527 | unsigned src, unsigned alpha, | ||
| 528 | const uint8_t *mask, int mask_linesize, int l2depth, int w, | ||
| 529 | unsigned hsub, unsigned vsub, | ||
| 530 | int xm, int left, int right, int hband) | ||
| 531 | { | ||
| 532 | |||
| 533 | 165312 | if (left) { | |
| 534 | ✗ | blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth, | |
| 535 | left, hband, hsub + vsub, xm); | ||
| 536 | ✗ | dst += dst_delta; | |
| 537 | ✗ | xm += left; | |
| 538 | } | ||
| 539 | 1248448 | for (int x = 0; x < w; x++) { | |
| 540 | 1083136 | blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth, | |
| 541 | 1083136 | 1 << hsub, hband, hsub + vsub, xm); | |
| 542 | 1083136 | dst += dst_delta; | |
| 543 | 1083136 | xm += 1 << hsub; | |
| 544 | } | ||
| 545 | 165312 | if (right) | |
| 546 | ✗ | blend_pixel16(dst, src, alpha, mask, mask_linesize, l2depth, | |
| 547 | right, hband, hsub + vsub, xm); | ||
| 548 | 165312 | } | |
| 549 | |||
| 550 | 4803214 | static void blend_line_hv(uint8_t *dst, int dst_delta, | |
| 551 | unsigned src, unsigned alpha, | ||
| 552 | const uint8_t *mask, int mask_linesize, int l2depth, int w, | ||
| 553 | unsigned hsub, unsigned vsub, | ||
| 554 | int xm, int left, int right, int hband) | ||
| 555 | { | ||
| 556 | |||
| 557 | 4803214 | if (left) { | |
| 558 | ✗ | blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth, | |
| 559 | left, hband, hsub + vsub, xm); | ||
| 560 | ✗ | dst += dst_delta; | |
| 561 | ✗ | xm += left; | |
| 562 | } | ||
| 563 | 36917898 | for (int x = 0; x < w; x++) { | |
| 564 | 32114684 | blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth, | |
| 565 | 32114684 | 1 << hsub, hband, hsub + vsub, xm); | |
| 566 | 32114684 | dst += dst_delta; | |
| 567 | 32114684 | xm += 1 << hsub; | |
| 568 | } | ||
| 569 | 4803214 | if (right) | |
| 570 | ✗ | blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth, | |
| 571 | right, hband, hsub + vsub, xm); | ||
| 572 | 4803214 | } | |
| 573 | |||
| 574 | 152369 | void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color, | |
| 575 | uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h, | ||
| 576 | const uint8_t *mask, int mask_linesize, int mask_w, int mask_h, | ||
| 577 | int l2depth, unsigned endianness, int x0, int y0) | ||
| 578 | { | ||
| 579 | 152369 | FFDrawColor over = blended_color(draw, color); | |
| 580 | unsigned alpha, nb_planes, nb_comp; | ||
| 581 | int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom; | ||
| 582 | uint8_t *p; | ||
| 583 | const uint8_t *m; | ||
| 584 | |||
| 585 | 304738 | nb_comp = draw->desc->nb_components - | |
| 586 | 152369 | !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA)); | |
| 587 | |||
| 588 | 152369 | clip_interval(dst_w, &x0, &mask_w, &xm0); | |
| 589 | 152369 | clip_interval(dst_h, &y0, &mask_h, &ym0); | |
| 590 | 152369 | mask += ym0 * mask_linesize; | |
| 591 | 152369 | if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3]) | |
| 592 | 2922 | return; | |
| 593 | 149447 | if (draw->desc->comp[0].depth <= 8) { | |
| 594 | /* alpha is in the [ 0 ; 0x10203 ] range, | ||
| 595 | alpha * mask is in the [ 0 ; 0x1010101 - 4 ] range */ | ||
| 596 | 145935 | alpha = (0x10307 * color->rgba[3] + 0x3) >> 8; | |
| 597 | } else { | ||
| 598 | 3512 | alpha = (0x101 * color->rgba[3] + 0x2) >> 8; | |
| 599 | } | ||
| 600 | 149447 | nb_planes = draw->nb_planes - !!(draw->desc->flags & AV_PIX_FMT_FLAG_ALPHA && !(draw->flags & FF_DRAW_PROCESS_ALPHA)); | |
| 601 | 149447 | nb_planes += !nb_planes; | |
| 602 | 587440 | for (unsigned plane = 0; plane < nb_planes; plane++) { | |
| 603 | 437993 | uint8_t *p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0); | |
| 604 | 437993 | w_sub = mask_w; | |
| 605 | 437993 | h_sub = mask_h; | |
| 606 | 437993 | x_sub = x0; | |
| 607 | 437993 | y_sub = y0; | |
| 608 | 437993 | subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right); | |
| 609 | 437993 | subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom); | |
| 610 | 1751769 | for (unsigned comp = 0; comp < nb_comp; comp++) { | |
| 611 | 1313776 | const int depth = draw->desc->comp[comp].depth; | |
| 612 | 1313776 | const int offset = draw->desc->comp[comp].offset; | |
| 613 | 1313776 | const int index = offset / ((depth + 7) / 8); | |
| 614 | |||
| 615 | 1313776 | if (draw->desc->comp[comp].plane != plane) | |
| 616 | 865638 | continue; | |
| 617 | 448138 | p = p0 + offset; | |
| 618 | 448138 | m = mask; | |
| 619 | 448138 | if (top) { | |
| 620 | 9114 | if (depth <= 8) { | |
| 621 | 9114 | blend_line_hv(p, draw->pixelstep[plane], | |
| 622 | 9114 | over.comp[plane].u8[index], alpha, | |
| 623 | m, mask_linesize, l2depth, w_sub, | ||
| 624 | 9114 | draw->hsub[plane], draw->vsub[plane], | |
| 625 | xm0, left, right, top); | ||
| 626 | } else { | ||
| 627 | ✗ | blend_line_hv16(p, draw->pixelstep[plane], | |
| 628 | ✗ | over.comp[plane].u16[index], alpha, | |
| 629 | m, mask_linesize, l2depth, w_sub, | ||
| 630 | ✗ | draw->hsub[plane], draw->vsub[plane], | |
| 631 | xm0, left, right, top); | ||
| 632 | } | ||
| 633 | 9114 | p += dst_linesize[plane]; | |
| 634 | 9114 | m += top * mask_linesize; | |
| 635 | } | ||
| 636 | 448138 | if (depth <= 8) { | |
| 637 | 5222792 | for (int y = 0; y < h_sub; y++) { | |
| 638 | 4784986 | blend_line_hv(p, draw->pixelstep[plane], | |
| 639 | 4784986 | over.comp[plane].u8[index], alpha, | |
| 640 | m, mask_linesize, l2depth, w_sub, | ||
| 641 | 4784986 | draw->hsub[plane], draw->vsub[plane], | |
| 642 | 4784986 | xm0, left, right, 1 << draw->vsub[plane]); | |
| 643 | 4784986 | p += dst_linesize[plane]; | |
| 644 | 4784986 | m += mask_linesize << draw->vsub[plane]; | |
| 645 | } | ||
| 646 | } else { | ||
| 647 | 175644 | for (int y = 0; y < h_sub; y++) { | |
| 648 | 165312 | blend_line_hv16(p, draw->pixelstep[plane], | |
| 649 | 165312 | over.comp[plane].u16[index], alpha, | |
| 650 | m, mask_linesize, l2depth, w_sub, | ||
| 651 | 165312 | draw->hsub[plane], draw->vsub[plane], | |
| 652 | 165312 | xm0, left, right, 1 << draw->vsub[plane]); | |
| 653 | 165312 | p += dst_linesize[plane]; | |
| 654 | 165312 | m += mask_linesize << draw->vsub[plane]; | |
| 655 | } | ||
| 656 | } | ||
| 657 | 448138 | if (bottom) { | |
| 658 | 9114 | if (depth <= 8) { | |
| 659 | 9114 | blend_line_hv(p, draw->pixelstep[plane], | |
| 660 | 9114 | over.comp[plane].u8[index], alpha, | |
| 661 | m, mask_linesize, l2depth, w_sub, | ||
| 662 | 9114 | draw->hsub[plane], draw->vsub[plane], | |
| 663 | xm0, left, right, bottom); | ||
| 664 | } else { | ||
| 665 | ✗ | blend_line_hv16(p, draw->pixelstep[plane], | |
| 666 | ✗ | over.comp[plane].u16[index], alpha, | |
| 667 | m, mask_linesize, l2depth, w_sub, | ||
| 668 | ✗ | draw->hsub[plane], draw->vsub[plane], | |
| 669 | xm0, left, right, bottom); | ||
| 670 | } | ||
| 671 | } | ||
| 672 | } | ||
| 673 | } | ||
| 674 | } | ||
| 675 | |||
| 676 | 738267 | int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir, | |
| 677 | int value) | ||
| 678 | { | ||
| 679 | 738267 | unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max; | |
| 680 | |||
| 681 | 738267 | if (!shift) | |
| 682 | 106588 | return value; | |
| 683 | 631679 | if (round_dir >= 0) | |
| 684 | 466325 | value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1); | |
| 685 | 631679 | return (value >> shift) << shift; | |
| 686 | } | ||
| 687 | |||
| 688 | 265 | AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags) | |
| 689 | { | ||
| 690 | FFDrawContext draw; | ||
| 691 | 265 | AVFilterFormats *fmts = NULL; | |
| 692 | int ret; | ||
| 693 | |||
| 694 | 71815 | for (enum AVPixelFormat i = 0; av_pix_fmt_desc_get(i); i++) | |
| 695 | 97255 | if (ff_draw_init(&draw, i, flags) >= 0 && | |
| 696 | 25705 | (ret = ff_add_format(&fmts, i)) < 0) | |
| 697 | ✗ | return NULL; | |
| 698 | 265 | return fmts; | |
| 699 | } | ||
| 700 |