History

avfilter/vf_blend: add support for float formats

Paul B Mahol committed 6 years ago

Commit d5f87f81 parent 6c33a231 file · +108 −4Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/vf_blend.c+108−4
@@ -19,6 +19,7 @@ · 18 unmodified lines
19 */19 */
20 20
21#include "libavutil/imgutils.h"21#include "libavutil/imgutils.h"
22#include "libavutil/intfloat.h"
22#include "libavutil/eval.h"23#include "libavutil/eval.h"
23#include "libavutil/opt.h"24#include "libavutil/opt.h"
24#include "libavutil/pixfmt.h"25#include "libavutil/pixfmt.h"
@@ -138,6 +139,9 @@ COPY(bottom, 8) · 113 unmodified lines
138COPY(top, 16)139COPY(top, 16)
139COPY(bottom, 16)140COPY(bottom, 16)
140 141
142COPY(top, 32)
143COPY(bottom, 32)
144
141#undef COPY145#undef COPY
142 146
143static void blend_normal_8bit(const uint8_t *top, ptrdiff_t top_linesize,147static void blend_normal_8bit(const uint8_t *top, ptrdiff_t top_linesize,
@@ -184,6 +188,31 @@ static void blend_normal_16bit(const uint8_t *_top, ptrdiff_t top_linesize, · 40 unmodified lines
184 }188 }
185}189}
186 190
191static void blend_normal_32bit(const uint8_t *_top, ptrdiff_t top_linesize,
192 const uint8_t *_bottom, ptrdiff_t bottom_linesize,
193 uint8_t *_dst, ptrdiff_t dst_linesize,
194 ptrdiff_t width, ptrdiff_t height,
195 FilterParams *param, double *values, int starty)
196{
197 const float *top = (float*)_top;
198 const float *bottom = (float*)_bottom;
199 float *dst = (float*)_dst;
200 const double opacity = param->opacity;
201 int i, j;
202 dst_linesize /= 4;
203 top_linesize /= 4;
204 bottom_linesize /= 4;
205
206 for (i = 0; i < height; i++) {
207 for (j = 0; j < width; j++) {
208 dst[j] = top[j] * opacity + bottom[j] * (1. - opacity);
209 }
210 dst += dst_linesize;
211 top += top_linesize;
212 bottom += bottom_linesize;
213 }
214}
215
187#define DEFINE_BLEND8(name, expr) \216#define DEFINE_BLEND8(name, expr) \
188static void blend_## name##_8bit(const uint8_t *top, ptrdiff_t top_linesize, \217static void blend_## name##_8bit(const uint8_t *top, ptrdiff_t top_linesize, \
189 const uint8_t *bottom, ptrdiff_t bottom_linesize, \218 const uint8_t *bottom, ptrdiff_t bottom_linesize, \
@@ -230,6 +259,32 @@ static void blend_## name##_##depth##bit(const uint8_t *_top, ptrdiff_t top_line · 40 unmodified lines
230 } \259 } \
231}260}
232 261
262#define DEFINE_BLEND32(name, expr, depth) \
263static void blend_## name##_##depth##bit(const uint8_t *_top, ptrdiff_t top_linesize,\
264 const uint8_t *_bottom, ptrdiff_t bottom_linesize, \
265 uint8_t *_dst, ptrdiff_t dst_linesize, \
266 ptrdiff_t width, ptrdiff_t height, \
267 FilterParams *param, double *values, int starty) \
268{ \
269 const float *top = (const float*)_top; \
270 const float *bottom = (const float*)_bottom; \
271 float *dst = (float*)_dst; \
272 double opacity = param->opacity; \
273 int i, j; \
274 dst_linesize /= 4; \
275 top_linesize /= 4; \
276 bottom_linesize /= 4; \
277 \
278 for (i = 0; i < height; i++) { \
279 for (j = 0; j < width; j++) { \
280 dst[j] = top[j] + ((expr) - top[j]) * opacity; \
281 } \
282 dst += dst_linesize; \
283 top += top_linesize; \
284 bottom += bottom_linesize; \
285 } \
286}
287
233#define A top[j]288#define A top[j]
234#define B bottom[j]289#define B bottom[j]
235 290
@@ -443,6 +498,49 @@ DEFINE_BLEND16(xor, A ^ B, 9) · 207 unmodified lines
443DEFINE_BLEND16(vividlight, (A < 256) ? BURN(2 * A, B) : DODGE(2 * (A - 256), B), 9)498DEFINE_BLEND16(vividlight, (A < 256) ? BURN(2 * A, B) : DODGE(2 * (A - 256), B), 9)
444DEFINE_BLEND16(linearlight,(int)av_clip_uintp2((B < 256) ? B + 2 * A - 511 : B + 2 * (A - 256), 9), 9)499DEFINE_BLEND16(linearlight,(int)av_clip_uintp2((B < 256) ? B + 2 * A - 511 : B + 2 * (A - 256), 9), 9)
445 500
501#undef MULTIPLY
502#undef SCREEN
503#undef BURN
504#undef DODGE
505
506#define MULTIPLY(x, a, b) ((x) * (((a) * (b)) / 1.0))
507#define SCREEN(x, a, b) (1.0 - (x) * ((1.0 - (a)) * (1.0 - (b)) / 1.0))
508#define BURN(a, b) (((a) <= 0.0) ? (a) : FFMAX(0.0, 1.0 - (1.0 - (b)) / (a)))
509#define DODGE(a, b) (((a) >= 1.0) ? (a) : FFMIN(1.0, ((b) / (1.0 - (a)))))
510
511DEFINE_BLEND32(addition, A + B, 32)
512DEFINE_BLEND32(grainmerge, A + B - 0.5, 32)
513DEFINE_BLEND32(average, (A + B) / 2, 32)
514DEFINE_BLEND32(subtract, A - B, 32)
515DEFINE_BLEND32(multiply, A * B, 32)
516DEFINE_BLEND32(multiply128, (A - 0.5) * B / 0.125 + 0.5, 32)
517DEFINE_BLEND32(negation, 1.0 - FFABS(1.0 - A - B), 32)
518DEFINE_BLEND32(extremity, FFABS(1.0 - A - B), 32)
519DEFINE_BLEND32(difference, FFABS(A - B), 32)
520DEFINE_BLEND32(grainextract, 0.5 + A - B, 32)
521DEFINE_BLEND32(screen, SCREEN(1, A, B), 32)
522DEFINE_BLEND32(overlay, (A < 0.5) ? MULTIPLY(2, A, B) : SCREEN(2, A, B), 32)
523DEFINE_BLEND32(hardlight, (B < 0.5) ? MULTIPLY(2, B, A) : SCREEN(2, B, A), 32)
524DEFINE_BLEND32(hardmix, (A < (1.0 - B)) ? 0: 1.0, 32)
525DEFINE_BLEND32(heat, (A == 0) ? 0 : 1.0 - FFMIN(((1.0 - B) * (1.0 - B)) / A, 1.0), 32)
526DEFINE_BLEND32(freeze, (B == 0) ? 0 : 1.0 - FFMIN(((1.0 - A) * (1.0 - A)) / B, 1.0), 32)
527DEFINE_BLEND32(darken, FFMIN(A, B), 32)
528DEFINE_BLEND32(lighten, FFMAX(A, B), 32)
529DEFINE_BLEND32(divide, B == 0 ? 1.0 : 1.0 * A / B, 32)
530DEFINE_BLEND32(dodge, DODGE(A, B), 32)
531DEFINE_BLEND32(burn, BURN(A, B), 32)
532DEFINE_BLEND32(softlight, (A > 0.5) ? B + (1.0 - B) * (A - 0.5) / 0.5 * (0.5 - fabs(B - 0.5) / 1.0): B - B * ((0.5 - A) / 0.5) * (0.5 - fabs(B - 0.5)/1.0), 32)
533DEFINE_BLEND32(exclusion, A + B - 2 * A * B / 1.0, 32)
534DEFINE_BLEND32(pinlight, (B < 0.5) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 0.5)), 32)
535DEFINE_BLEND32(phoenix, FFMIN(A, B) - FFMAX(A, B) + 1.0, 32)
536DEFINE_BLEND32(reflect, (B == 1.0) ? B : FFMIN(1.0, (A * A / (1.0 - B))), 32)
537DEFINE_BLEND32(glow, (A == 1.0) ? A : FFMIN(1.0, (B * B / (1.0 - A))), 32)
538DEFINE_BLEND32(and, av_int2float(av_float2int(A) & av_float2int(B)), 32)
539DEFINE_BLEND32(or, av_int2float(av_float2int(A) | av_float2int(B)), 32)
540DEFINE_BLEND32(xor, av_int2float(av_float2int(A) ^ av_float2int(B)), 32)
541DEFINE_BLEND32(vividlight, (A < 0.5) ? BURN(2 * A, B) : DODGE(2 * (A - 0.5), B), 32)
542DEFINE_BLEND32(linearlight,(B < 0.5) ? B + 2 * A - 1.0 : B + 2 * (A - 0.5), 32)
543
446#define DEFINE_BLEND_EXPR(type, name, div) \544#define DEFINE_BLEND_EXPR(type, name, div) \
447static void blend_expr_## name(const uint8_t *_top, ptrdiff_t top_linesize, \545static void blend_expr_## name(const uint8_t *_top, ptrdiff_t top_linesize, \
448 const uint8_t *_bottom, ptrdiff_t bottom_linesize, \546 const uint8_t *_bottom, ptrdiff_t bottom_linesize, \
@@ -475,6 +573,7 @@ static void blend_expr_## name(const uint8_t *_top, ptrdiff_t top_linesize, · 26 unmodified lines
475 573
476DEFINE_BLEND_EXPR(uint8_t, 8bit, 1)574DEFINE_BLEND_EXPR(uint8_t, 8bit, 1)
477DEFINE_BLEND_EXPR(uint16_t, 16bit, 2)575DEFINE_BLEND_EXPR(uint16_t, 16bit, 2)
576DEFINE_BLEND_EXPR(float, 32bit, 4)
478 577
479static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)578static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
480{579{
@@ -580,6 +679,7 @@ static int query_formats(AVFilterContext *ctx) · 99 unmodified lines
580 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,679 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
581 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,680 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
582 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,681 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16, AV_PIX_FMT_GRAY16,
682 AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32, AV_PIX_FMT_GRAYF32,
583 AV_PIX_FMT_NONE683 AV_PIX_FMT_NONE
584 };684 };
585 685
@@ -645,6 +745,7 @@ DEFINE_INIT_BLEND_FUNC(9, 16) · 59 unmodified lines
645DEFINE_INIT_BLEND_FUNC(10, 16)745DEFINE_INIT_BLEND_FUNC(10, 16)
646DEFINE_INIT_BLEND_FUNC(12, 16)746DEFINE_INIT_BLEND_FUNC(12, 16)
647DEFINE_INIT_BLEND_FUNC(16, 16)747DEFINE_INIT_BLEND_FUNC(16, 16)
748DEFINE_INIT_BLEND_FUNC(32, 32)
648 749
649void ff_blend_init(FilterParams *param, int depth)750void ff_blend_init(FilterParams *param, int depth)
650{751{
@@ -664,15 +765,18 @@ void ff_blend_init(FilterParams *param, int depth) · 13 unmodified lines
664 case 16:765 case 16:
665 init_blend_func_16_16bit(param);766 init_blend_func_16_16bit(param);
666 break;767 break;
768 case 32:
769 init_blend_func_32_32bit(param);
770 break;
667 }771 }
668 772
669 if (param->opacity == 0 && param->mode != BLEND_NORMAL) {773 if (param->opacity == 0 && param->mode != BLEND_NORMAL) {
670 param->blend = depth > 8 ? blend_copytop_16 : blend_copytop_8;774 param->blend = depth > 8 ? depth > 16 ? blend_copytop_32 : blend_copytop_16 : blend_copytop_8;
671 } else if (param->mode == BLEND_NORMAL) {775 } else if (param->mode == BLEND_NORMAL) {
672 if (param->opacity == 1)776 if (param->opacity == 1)
673 param->blend = depth > 8 ? blend_copytop_16 : blend_copytop_8;777 param->blend = depth > 8 ? depth > 16 ? blend_copytop_32 : blend_copytop_16 : blend_copytop_8;
674 else if (param->opacity == 0)778 else if (param->opacity == 0)
675 param->blend = depth > 8 ? blend_copybottom_16 : blend_copybottom_8;779 param->blend = depth > 8 ? depth > 16 ? blend_copybottom_32 : blend_copybottom_16 : blend_copybottom_8;
676 }780 }
677 781
678 if (ARCH_X86)782 if (ARCH_X86)
@@ -740,7 +844,7 @@ static int config_output(AVFilterLink *outlink) · 61 unmodified lines
740 NULL, NULL, NULL, NULL, 0, ctx);844 NULL, NULL, NULL, NULL, 0, ctx);
741 if (ret < 0)845 if (ret < 0)
742 return ret;846 return ret;
743 param->blend = s->depth > 8 ? blend_expr_16bit : blend_expr_8bit;847 param->blend = s->depth > 8 ? s->depth > 16 ? blend_expr_32bit : blend_expr_16bit : blend_expr_8bit;
744 }848 }
745 }849 }
746 850

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t