diff options
author | Paul B Mahol <onemda@gmail.com> | 2015-07-15 21:04:28 +0300 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2015-07-18 05:07:44 +0300 |
commit | efd4e5fe682027503883441382665812ded62c71 (patch) | |
tree | 025663349b6f507fc9e79da602ec28cce5d2bfa7 /libavfilter | |
parent | e03cb1e115b41f8087c91d62e39c3965fc3b9a11 (diff) |
avfilter/vf_blend: implement 16bit support
Signed-off-by: Paul B Mahol <onemda@gmail.com>
Diffstat (limited to 'libavfilter')
-rw-r--r-- | libavfilter/vf_blend.c | 325 |
1 files changed, 202 insertions, 123 deletions
diff --git a/libavfilter/vf_blend.c b/libavfilter/vf_blend.c index 9c93baf3e9..f359dffa07 100644 --- a/libavfilter/vf_blend.c +++ b/libavfilter/vf_blend.c @@ -169,24 +169,50 @@ static void blend_normal(const uint8_t *top, int top_linesize, av_image_copy_plane(dst, dst_linesize, top, top_linesize, width, end - start); } -#define DEFINE_BLEND(name, expr) \ -static void blend_## name(const uint8_t *top, int top_linesize, \ - const uint8_t *bottom, int bottom_linesize, \ - uint8_t *dst, int dst_linesize, \ - int width, int start, int end, \ - FilterParams *param, double *values) \ -{ \ - double opacity = param->opacity; \ - int i, j; \ - \ - for (i = start; i < end; i++) { \ - for (j = 0; j < width; j++) { \ - dst[j] = top[j] + ((expr) - top[j]) * opacity; \ - } \ - dst += dst_linesize; \ - top += top_linesize; \ - bottom += bottom_linesize; \ - } \ +#define DEFINE_BLEND8(name, expr) \ +static void blend_## name##_8bit(const uint8_t *top, int top_linesize, \ + const uint8_t *bottom, int bottom_linesize, \ + uint8_t *dst, int dst_linesize, \ + int width, int start, int end, \ + FilterParams *param, double *values) \ +{ \ + double opacity = param->opacity; \ + int i, j; \ + \ + for (i = start; i < end; i++) { \ + for (j = 0; j < width; j++) { \ + dst[j] = top[j] + ((expr) - top[j]) * opacity; \ + } \ + dst += dst_linesize; \ + top += top_linesize; \ + bottom += bottom_linesize; \ + } \ +} + +#define DEFINE_BLEND16(name, expr) \ +static void blend_## name##_16bit(const uint8_t *_top, int top_linesize, \ + const uint8_t *_bottom, int bottom_linesize, \ + uint8_t *_dst, int dst_linesize, \ + int width, int start, int end, \ + FilterParams *param, double *values) \ +{ \ + const uint16_t *top = (uint16_t*)_top; \ + const uint16_t *bottom = (uint16_t*)_bottom; \ + uint16_t *dst = (uint16_t*)_dst; \ + double opacity = param->opacity; \ + int i, j; \ + dst_linesize /= 2; \ + top_linesize /= 2; \ + bottom_linesize /= 2; \ + \ + for (i = start; i < end; i++) { \ + for (j = 0; j < width; j++) { \ + dst[j] = top[j] + ((expr) - top[j]) * opacity; \ + } \ + dst += dst_linesize; \ + top += top_linesize; \ + bottom += bottom_linesize; \ + } \ } #define A top[j] @@ -197,57 +223,105 @@ static void blend_## name(const uint8_t *top, int top_linesize, \ #define BURN(a, b) (((a) == 0) ? (a) : FFMAX(0, 255 - ((255 - (b)) << 8) / (a))) #define DODGE(a, b) (((a) == 255) ? (a) : FFMIN(255, (((b) << 8) / (255 - (a))))) -DEFINE_BLEND(addition, FFMIN(255, A + B)) -DEFINE_BLEND(average, (A + B) / 2) -DEFINE_BLEND(subtract, FFMAX(0, A - B)) -DEFINE_BLEND(multiply, MULTIPLY(1, A, B)) -DEFINE_BLEND(negation, 255 - FFABS(255 - A - B)) -DEFINE_BLEND(difference, FFABS(A - B)) -DEFINE_BLEND(difference128, av_clip_uint8(128 + A - B)) -DEFINE_BLEND(screen, SCREEN(1, A, B)) -DEFINE_BLEND(overlay, (A < 128) ? MULTIPLY(2, A, B) : SCREEN(2, A, B)) -DEFINE_BLEND(hardlight, (B < 128) ? MULTIPLY(2, B, A) : SCREEN(2, B, A)) -DEFINE_BLEND(hardmix, (A < (255 - B)) ? 0: 255) -DEFINE_BLEND(darken, FFMIN(A, B)) -DEFINE_BLEND(lighten, FFMAX(A, B)) -DEFINE_BLEND(divide, av_clip_uint8(((float)A / ((float)B) * 255))) -DEFINE_BLEND(dodge, DODGE(A, B)) -DEFINE_BLEND(burn, BURN(A, B)) -DEFINE_BLEND(softlight, (A > 127) ? B + (255 - B) * (A - 127.5) / 127.5 * (0.5 - FFABS(B - 127.5) / 255): B - B * ((127.5 - A) / 127.5) * (0.5 - FFABS(B - 127.5)/255)) -DEFINE_BLEND(exclusion, A + B - 2 * A * B / 255) -DEFINE_BLEND(pinlight, (B < 128) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 128))) -DEFINE_BLEND(phoenix, FFMIN(A, B) - FFMAX(A, B) + 255) -DEFINE_BLEND(reflect, (B == 255) ? B : FFMIN(255, (A * A / (255 - B)))) -DEFINE_BLEND(glow, (A == 255) ? A : FFMIN(255, (B * B / (255 - A)))) -DEFINE_BLEND(and, A & B) -DEFINE_BLEND(or, A | B) -DEFINE_BLEND(xor, A ^ B) -DEFINE_BLEND(vividlight, (A < 128) ? BURN(2 * A, B) : DODGE(2 * (A - 128), B)) -DEFINE_BLEND(linearlight,av_clip_uint8((B < 128) ? B + 2 * A - 255 : B + 2 * (A - 128))) - -static void blend_expr(const uint8_t *top, int top_linesize, - const uint8_t *bottom, int bottom_linesize, - uint8_t *dst, int dst_linesize, - int width, int start, int end, - FilterParams *param, double *values) -{ - AVExpr *e = param->e; - int y, x; - - for (y = start; y < end; y++) { - values[VAR_Y] = y; - for (x = 0; x < width; x++) { - values[VAR_X] = x; - values[VAR_TOP] = values[VAR_A] = top[x]; - values[VAR_BOTTOM] = values[VAR_B] = bottom[x]; - dst[x] = av_expr_eval(e, values, NULL); - } - dst += dst_linesize; - top += top_linesize; - bottom += bottom_linesize; - } +DEFINE_BLEND8(addition, FFMIN(255, A + B)) +DEFINE_BLEND8(average, (A + B) / 2) +DEFINE_BLEND8(subtract, FFMAX(0, A - B)) +DEFINE_BLEND8(multiply, MULTIPLY(1, A, B)) +DEFINE_BLEND8(negation, 255 - FFABS(255 - A - B)) +DEFINE_BLEND8(difference, FFABS(A - B)) +DEFINE_BLEND8(difference128, av_clip_uint8(128 + A - B)) +DEFINE_BLEND8(screen, SCREEN(1, A, B)) +DEFINE_BLEND8(overlay, (A < 128) ? MULTIPLY(2, A, B) : SCREEN(2, A, B)) +DEFINE_BLEND8(hardlight, (B < 128) ? MULTIPLY(2, B, A) : SCREEN(2, B, A)) +DEFINE_BLEND8(hardmix, (A < (255 - B)) ? 0: 255) +DEFINE_BLEND8(darken, FFMIN(A, B)) +DEFINE_BLEND8(lighten, FFMAX(A, B)) +DEFINE_BLEND8(divide, av_clip_uint8(((float)A / ((float)B) * 255))) +DEFINE_BLEND8(dodge, DODGE(A, B)) +DEFINE_BLEND8(burn, BURN(A, B)) +DEFINE_BLEND8(softlight, (A > 127) ? B + (255 - B) * (A - 127.5) / 127.5 * (0.5 - FFABS(B - 127.5) / 255): B - B * ((127.5 - A) / 127.5) * (0.5 - FFABS(B - 127.5)/255)) +DEFINE_BLEND8(exclusion, A + B - 2 * A * B / 255) +DEFINE_BLEND8(pinlight, (B < 128) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 128))) +DEFINE_BLEND8(phoenix, FFMIN(A, B) - FFMAX(A, B) + 255) +DEFINE_BLEND8(reflect, (B == 255) ? B : FFMIN(255, (A * A / (255 - B)))) +DEFINE_BLEND8(glow, (A == 255) ? A : FFMIN(255, (B * B / (255 - A)))) +DEFINE_BLEND8(and, A & B) +DEFINE_BLEND8(or, A | B) +DEFINE_BLEND8(xor, A ^ B) +DEFINE_BLEND8(vividlight, (A < 128) ? BURN(2 * A, B) : DODGE(2 * (A - 128), B)) +DEFINE_BLEND8(linearlight,av_clip_uint8((B < 128) ? B + 2 * A - 255 : B + 2 * (A - 128))) + +#undef MULTIPLY +#undef SCREEN +#undef BURN +#undef DODGE + +#define MULTIPLY(x, a, b) ((x) * (((a) * (b)) / 65535)) +#define SCREEN(x, a, b) (65535 - (x) * ((65535 - (a)) * (65535 - (b)) / 65535)) +#define BURN(a, b) (((a) == 0) ? (a) : FFMAX(0, 65535 - ((65535 - (b)) << 16) / (a))) +#define DODGE(a, b) (((a) == 65535) ? (a) : FFMIN(65535, (((b) << 16) / (65535 - (a))))) + +DEFINE_BLEND16(addition, FFMIN(65535, A + B)) +DEFINE_BLEND16(average, (A + B) / 2) +DEFINE_BLEND16(subtract, FFMAX(0, A - B)) +DEFINE_BLEND16(multiply, MULTIPLY(1, A, B)) +DEFINE_BLEND16(negation, 65535 - FFABS(65535 - A - B)) +DEFINE_BLEND16(difference, FFABS(A - B)) +DEFINE_BLEND16(difference128, av_clip_uint16(32768 + A - B)) +DEFINE_BLEND16(screen, SCREEN(1, A, B)) +DEFINE_BLEND16(overlay, (A < 32768) ? MULTIPLY(2, A, B) : SCREEN(2, A, B)) +DEFINE_BLEND16(hardlight, (B < 32768) ? MULTIPLY(2, B, A) : SCREEN(2, B, A)) +DEFINE_BLEND16(hardmix, (A < (65535 - B)) ? 0: 65535) +DEFINE_BLEND16(darken, FFMIN(A, B)) +DEFINE_BLEND16(lighten, FFMAX(A, B)) +DEFINE_BLEND16(divide, av_clip_uint16(((float)A / ((float)B) * 65535))) +DEFINE_BLEND16(dodge, DODGE(A, B)) +DEFINE_BLEND16(burn, BURN(A, B)) +DEFINE_BLEND16(softlight, (A > 32767) ? B + (65535 - B) * (A - 32767.5) / 32767.5 * (0.5 - FFABS(B - 32767.5) / 65535): B - B * ((32767.5 - A) / 32767.5) * (0.5 - FFABS(B - 32767.5)/65535)) +DEFINE_BLEND16(exclusion, A + B - 2 * A * B / 65535) +DEFINE_BLEND16(pinlight, (B < 32768) ? FFMIN(A, 2 * B) : FFMAX(A, 2 * (B - 32768))) +DEFINE_BLEND16(phoenix, FFMIN(A, B) - FFMAX(A, B) + 65535) +DEFINE_BLEND16(reflect, (B == 65535) ? B : FFMIN(65535, (A * A / (65535 - B)))) +DEFINE_BLEND16(glow, (A == 65535) ? A : FFMIN(65535, (B * B / (65535 - A)))) +DEFINE_BLEND16(and, A & B) +DEFINE_BLEND16(or, A | B) +DEFINE_BLEND16(xor, A ^ B) +DEFINE_BLEND16(vividlight, (A < 32768) ? BURN(2 * A, B) : DODGE(2 * (A - 32768), B)) +DEFINE_BLEND16(linearlight,av_clip_uint16((B < 32768) ? B + 2 * A - 65535 : B + 2 * (A - 32768))) + +#define DEFINE_BLEND_EXPR(type, name, div) \ +static void blend_expr_## name(const uint8_t *_top, int top_linesize, \ + const uint8_t *_bottom, int bottom_linesize, \ + uint8_t *_dst, int dst_linesize, \ + int width, int start, int end, \ + FilterParams *param, double *values) \ +{ \ + const type *top = (type*)_top; \ + const type *bottom = (type*)_bottom; \ + type *dst = (type*)_dst; \ + AVExpr *e = param->e; \ + int y, x; \ + dst_linesize /= div; \ + top_linesize /= div; \ + bottom_linesize /= div; \ + \ + for (y = start; y < end; y++) { \ + values[VAR_Y] = y; \ + for (x = 0; x < width; x++) { \ + values[VAR_X] = x; \ + values[VAR_TOP] = values[VAR_A] = top[x]; \ + values[VAR_BOTTOM] = values[VAR_B] = bottom[x]; \ + dst[x] = av_expr_eval(e, values, NULL); \ + } \ + dst += dst_linesize; \ + top += top_linesize; \ + bottom += bottom_linesize; \ + } \ } +DEFINE_BLEND_EXPR(uint8_t, 8bit, 1) +DEFINE_BLEND_EXPR(uint16_t, 16bit, 2) + static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) { ThreadData *td = arg; @@ -311,63 +385,9 @@ static AVFrame *blend_frame(AVFilterContext *ctx, AVFrame *top_buf, static av_cold int init(AVFilterContext *ctx) { BlendContext *b = ctx->priv; - int ret, plane; b->tblend = !strcmp(ctx->filter->name, "tblend"); - for (plane = 0; plane < FF_ARRAY_ELEMS(b->params); plane++) { - FilterParams *param = &b->params[plane]; - - if (b->all_mode >= 0) - param->mode = b->all_mode; - if (b->all_opacity < 1) - param->opacity = b->all_opacity; - - switch (param->mode) { - case BLEND_ADDITION: param->blend = blend_addition; break; - case BLEND_AND: param->blend = blend_and; break; - case BLEND_AVERAGE: param->blend = blend_average; break; - case BLEND_BURN: param->blend = blend_burn; break; - case BLEND_DARKEN: param->blend = blend_darken; break; - case BLEND_DIFFERENCE: param->blend = blend_difference; break; - case BLEND_DIFFERENCE128: param->blend = blend_difference128; break; - case BLEND_DIVIDE: param->blend = blend_divide; break; - case BLEND_DODGE: param->blend = blend_dodge; break; - case BLEND_EXCLUSION: param->blend = blend_exclusion; break; - case BLEND_GLOW: param->blend = blend_glow; break; - case BLEND_HARDLIGHT: param->blend = blend_hardlight; break; - case BLEND_HARDMIX: param->blend = blend_hardmix; break; - case BLEND_LIGHTEN: param->blend = blend_lighten; break; - case BLEND_LINEARLIGHT:param->blend = blend_linearlight;break; - case BLEND_MULTIPLY: param->blend = blend_multiply; break; - case BLEND_NEGATION: param->blend = blend_negation; break; - case BLEND_NORMAL: param->blend = blend_normal; break; - case BLEND_OR: param->blend = blend_or; break; - case BLEND_OVERLAY: param->blend = blend_overlay; break; - case BLEND_PHOENIX: param->blend = blend_phoenix; break; - case BLEND_PINLIGHT: param->blend = blend_pinlight; break; - case BLEND_REFLECT: param->blend = blend_reflect; break; - case BLEND_SCREEN: param->blend = blend_screen; break; - case BLEND_SOFTLIGHT: param->blend = blend_softlight; break; - case BLEND_SUBTRACT: param->blend = blend_subtract; break; - case BLEND_VIVIDLIGHT: param->blend = blend_vividlight; break; - case BLEND_XOR: param->blend = blend_xor; break; - } - - if (b->all_expr && !param->expr_str) { - param->expr_str = av_strdup(b->all_expr); - if (!param->expr_str) - return AVERROR(ENOMEM); - } - if (param->expr_str) { - ret = av_expr_parse(¶m->e, param->expr_str, var_names, - NULL, NULL, NULL, NULL, 0, ctx); - if (ret < 0) - return ret; - param->blend = blend_expr; - } - } - b->dinput.process = blend_frame; return 0; } @@ -378,7 +398,11 @@ static int query_formats(AVFilterContext *ctx) AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, - AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, + AV_PIX_FMT_GBRP16, AV_PIX_FMT_GRAY16, + AV_PIX_FMT_NONE }; AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts); @@ -408,7 +432,7 @@ static int config_output(AVFilterLink *outlink) AVFilterLink *bottomlink = ctx->inputs[BOTTOM]; BlendContext *b = ctx->priv; const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(toplink->format); - int ret; + int ret, plane, is_16bit; if (toplink->format != bottomlink->format) { av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n"); @@ -438,11 +462,66 @@ static int config_output(AVFilterLink *outlink) b->hsub = pix_desc->log2_chroma_w; b->vsub = pix_desc->log2_chroma_h; + + is_16bit = pix_desc->comp[0].depth_minus1 == 15; b->nb_planes = av_pix_fmt_count_planes(toplink->format); if ((ret = ff_dualinput_init(ctx, &b->dinput)) < 0) return ret; + for (plane = 0; plane < FF_ARRAY_ELEMS(b->params); plane++) { + FilterParams *param = &b->params[plane]; + + if (b->all_mode >= 0) + param->mode = b->all_mode; + if (b->all_opacity < 1) + param->opacity = b->all_opacity; + + switch (param->mode) { + case BLEND_ADDITION: param->blend = is_16bit ? blend_addition_16bit : blend_addition_8bit; break; + case BLEND_AND: param->blend = is_16bit ? blend_and_16bit : blend_and_8bit; break; + case BLEND_AVERAGE: param->blend = is_16bit ? blend_average_16bit : blend_average_8bit; break; + case BLEND_BURN: param->blend = is_16bit ? blend_burn_16bit : blend_burn_8bit; break; + case BLEND_DARKEN: param->blend = is_16bit ? blend_darken_16bit : blend_darken_8bit; break; + case BLEND_DIFFERENCE: param->blend = is_16bit ? blend_difference_16bit : blend_difference_8bit; break; + case BLEND_DIFFERENCE128: param->blend = is_16bit ? blend_difference128_16bit: blend_difference128_8bit; break; + case BLEND_DIVIDE: param->blend = is_16bit ? blend_divide_16bit : blend_divide_8bit; break; + case BLEND_DODGE: param->blend = is_16bit ? blend_dodge_16bit : blend_dodge_8bit; break; + case BLEND_EXCLUSION: param->blend = is_16bit ? blend_exclusion_16bit : blend_exclusion_8bit; break; + case BLEND_GLOW: param->blend = is_16bit ? blend_glow_16bit : blend_glow_8bit; break; + case BLEND_HARDLIGHT: param->blend = is_16bit ? blend_hardlight_16bit : blend_hardlight_8bit; break; + case BLEND_HARDMIX: param->blend = is_16bit ? blend_hardmix_16bit : blend_hardmix_8bit; break; + case BLEND_LIGHTEN: param->blend = is_16bit ? blend_lighten_16bit : blend_lighten_8bit; break; + case BLEND_LINEARLIGHT:param->blend = is_16bit ? blend_linearlight_16bit: blend_linearlight_8bit;break; + case BLEND_MULTIPLY: param->blend = is_16bit ? blend_multiply_16bit : blend_multiply_8bit; break; + case BLEND_NEGATION: param->blend = is_16bit ? blend_negation_16bit : blend_negation_8bit; break; + case BLEND_NORMAL: param->blend = blend_normal; break; + case BLEND_OR: param->blend = is_16bit ? blend_or_16bit : blend_or_8bit; break; + case BLEND_OVERLAY: param->blend = is_16bit ? blend_overlay_16bit : blend_overlay_8bit; break; + case BLEND_PHOENIX: param->blend = is_16bit ? blend_phoenix_16bit : blend_phoenix_8bit; break; + case BLEND_PINLIGHT: param->blend = is_16bit ? blend_pinlight_16bit : blend_pinlight_8bit; break; + case BLEND_REFLECT: param->blend = is_16bit ? blend_reflect_16bit : blend_reflect_8bit; break; + case BLEND_SCREEN: param->blend = is_16bit ? blend_screen_16bit : blend_screen_8bit; break; + case BLEND_SOFTLIGHT: param->blend = is_16bit ? blend_softlight_16bit : blend_softlight_8bit; break; + case BLEND_SUBTRACT: param->blend = is_16bit ? blend_subtract_16bit : blend_subtract_8bit; break; + case BLEND_VIVIDLIGHT: param->blend = is_16bit ? blend_vividlight_16bit : blend_vividlight_8bit; break; + case BLEND_XOR: param->blend = is_16bit ? blend_xor_16bit : blend_xor_8bit; break; + } + + if (b->all_expr && !param->expr_str) { + param->expr_str = av_strdup(b->all_expr); + if (!param->expr_str) + return AVERROR(ENOMEM); + } + if (param->expr_str) { + ret = av_expr_parse(¶m->e, param->expr_str, var_names, + NULL, NULL, NULL, NULL, 0, ctx); + if (ret < 0) + return ret; + param->blend = is_16bit? blend_expr_16bit : blend_expr_8bit; + } + } + return 0; } |