Merge commit 'a6a750c7ef240b72ce01e9653343a0ddf247d196'
[ffmpeg.git] / libavfilter / vf_blend.c
index c24013d..a121267 100644 (file)
@@ -76,9 +76,11 @@ typedef struct ThreadData {
     { "divide",     "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DIVIDE},     0, 0, FLAGS, "mode" },\
     { "dodge",      "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_DODGE},      0, 0, FLAGS, "mode" },\
     { "exclusion",  "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_EXCLUSION},  0, 0, FLAGS, "mode" },\
+    { "freeze",     "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_FREEZE},     0, 0, FLAGS, "mode" },\
     { "glow",       "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_GLOW},       0, 0, FLAGS, "mode" },\
     { "hardlight",  "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_HARDLIGHT},  0, 0, FLAGS, "mode" },\
     { "hardmix",    "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_HARDMIX},    0, 0, FLAGS, "mode" },\
+    { "heat",       "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_HEAT},       0, 0, FLAGS, "mode" },\
     { "lighten",    "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_LIGHTEN},    0, 0, FLAGS, "mode" },\
     { "linearlight","", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_LINEARLIGHT},0, 0, FLAGS, "mode" },\
     { "multiply",   "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_MULTIPLY},   0, 0, FLAGS, "mode" },\
@@ -122,27 +124,28 @@ AVFILTER_DEFINE_CLASS(blend);
 static void blend_copy ## src(const uint8_t *top, ptrdiff_t top_linesize,    \
                             const uint8_t *bottom, ptrdiff_t bottom_linesize,\
                             uint8_t *dst, ptrdiff_t dst_linesize,            \
-                            ptrdiff_t width, ptrdiff_t start, ptrdiff_t end, \
-                            FilterParams *param, double *values)             \
+                            ptrdiff_t width, ptrdiff_t height,               \
+                            FilterParams *param, double *values, int starty) \
 {                                                                            \
     av_image_copy_plane(dst, dst_linesize, src, src ## _linesize,            \
-                        width, end - start);                                 \
+                        width, height);                                 \
 }
 
 COPY(top)
+COPY(bottom)
 
 #undef COPY
 
 static void blend_normal_8bit(const uint8_t *top, ptrdiff_t top_linesize,
                               const uint8_t *bottom, ptrdiff_t bottom_linesize,
                               uint8_t *dst, ptrdiff_t dst_linesize,
-                              ptrdiff_t width, ptrdiff_t start, ptrdiff_t end,
-                              FilterParams *param, double *values)
+                              ptrdiff_t width, ptrdiff_t height,
+                              FilterParams *param, double *values, int starty)
 {
     const double opacity = param->opacity;
     int i, j;
 
-    for (i = start; i < end; i++) {
+    for (i = 0; i < height; i++) {
         for (j = 0; j < width; j++) {
             dst[j] = top[j] * opacity + bottom[j] * (1. - opacity);
         }
@@ -155,8 +158,8 @@ static void blend_normal_8bit(const uint8_t *top, ptrdiff_t top_linesize,
 static void blend_normal_16bit(const uint8_t *_top, ptrdiff_t top_linesize,
                                   const uint8_t *_bottom, ptrdiff_t bottom_linesize,
                                   uint8_t *_dst, ptrdiff_t dst_linesize,
-                                  ptrdiff_t width, ptrdiff_t start, ptrdiff_t end,
-                                  FilterParams *param, double *values)
+                                  ptrdiff_t width, ptrdiff_t height,
+                                  FilterParams *param, double *values, int starty)
 {
     const uint16_t *top = (uint16_t*)_top;
     const uint16_t *bottom = (uint16_t*)_bottom;
@@ -167,7 +170,7 @@ static void blend_normal_16bit(const uint8_t *_top, ptrdiff_t top_linesize,
     top_linesize /= 2;
     bottom_linesize /= 2;
 
-    for (i = start; i < end; i++) {
+    for (i = 0; i < height; i++) {
         for (j = 0; j < width; j++) {
             dst[j] = top[j] * opacity + bottom[j] * (1. - opacity);
         }
@@ -181,13 +184,13 @@ static void blend_normal_16bit(const uint8_t *_top, ptrdiff_t top_linesize,
 static void blend_## name##_8bit(const uint8_t *top, ptrdiff_t top_linesize,         \
                                  const uint8_t *bottom, ptrdiff_t bottom_linesize,   \
                                  uint8_t *dst, ptrdiff_t dst_linesize,               \
-                                 ptrdiff_t width, ptrdiff_t start, ptrdiff_t end,                \
-                                 FilterParams *param, double *values)          \
+                                 ptrdiff_t width, ptrdiff_t height,                \
+                                 FilterParams *param, double *values, int starty) \
 {                                                                              \
     double opacity = param->opacity;                                           \
     int i, j;                                                                  \
                                                                                \
-    for (i = start; i < end; i++) {                                            \
+    for (i = 0; i < height; i++) {                                             \
         for (j = 0; j < width; j++) {                                          \
             dst[j] = top[j] + ((expr) - top[j]) * opacity;                     \
         }                                                                      \
@@ -201,8 +204,8 @@ static void blend_## name##_8bit(const uint8_t *top, ptrdiff_t top_linesize,
 static void blend_## name##_16bit(const uint8_t *_top, ptrdiff_t top_linesize,       \
                                   const uint8_t *_bottom, ptrdiff_t bottom_linesize, \
                                   uint8_t *_dst, ptrdiff_t dst_linesize,             \
-                                  ptrdiff_t width, ptrdiff_t start, ptrdiff_t end,               \
-                                  FilterParams *param, double *values)         \
+                                  ptrdiff_t width, ptrdiff_t height,           \
+                                  FilterParams *param, double *values, int starty)         \
 {                                                                              \
     const uint16_t *top = (uint16_t*)_top;                                     \
     const uint16_t *bottom = (uint16_t*)_bottom;                               \
@@ -213,7 +216,7 @@ static void blend_## name##_16bit(const uint8_t *_top, ptrdiff_t top_linesize,
     top_linesize /= 2;                                                         \
     bottom_linesize /= 2;                                                      \
                                                                                \
-    for (i = start; i < end; i++) {                                            \
+    for (i = 0; i < height; i++) {                                             \
         for (j = 0; j < width; j++) {                                          \
             dst[j] = top[j] + ((expr) - top[j]) * opacity;                     \
         }                                                                      \
@@ -244,9 +247,11 @@ 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(heat,       (A == 0) ? 0 : 255 - FFMIN(((255 - B) * (255 - B)) / A, 255))
+DEFINE_BLEND8(freeze,     (B == 0) ? 0 : 255 - FFMIN(((255 - A) * (255 - A)) / B, 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(divide,     av_clip_uint8(B == 0 ? 255 : 255 * A / B))
 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 - fabs(B - 127.5) / 255): B - B * ((127.5 - A) / 127.5) * (0.5 - fabs(B - 127.5)/255))
@@ -284,9 +289,11 @@ 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(heat,       (A == 0) ? 0 : 65535 - FFMIN(((65535 - B) * (65535 - B)) / A, 65535))
+DEFINE_BLEND16(freeze,     (B == 0) ? 0 : 65535 - FFMIN(((65535 - A) * (65535 - A)) / B, 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(divide,     av_clip_uint16(B == 0 ? 65535 : 65535 * A / B))
 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 - fabs(B - 32767.5) / 65535): B - B * ((32767.5 - A) / 32767.5) * (0.5 - fabs(B - 32767.5)/65535))
@@ -305,8 +312,8 @@ DEFINE_BLEND16(linearlight,av_clip_uint16((B < 32768) ? B + 2 * A - 65535 : B +
 static void blend_expr_## name(const uint8_t *_top, ptrdiff_t top_linesize,          \
                                const uint8_t *_bottom, ptrdiff_t bottom_linesize,    \
                                uint8_t *_dst, ptrdiff_t dst_linesize,                \
-                               ptrdiff_t width, ptrdiff_t start, ptrdiff_t end,                  \
-                               FilterParams *param, double *values)            \
+                               ptrdiff_t width, ptrdiff_t height,              \
+                               FilterParams *param, double *values, int starty) \
 {                                                                              \
     const type *top = (type*)_top;                                             \
     const type *bottom = (type*)_bottom;                                       \
@@ -317,8 +324,8 @@ static void blend_expr_## name(const uint8_t *_top, ptrdiff_t top_linesize,
     top_linesize /= div;                                                       \
     bottom_linesize /= div;                                                    \
                                                                                \
-    for (y = start; y < end; y++) {                                            \
-        values[VAR_Y] = y;                                                     \
+    for (y = 0; y < height; y++) {                                             \
+        values[VAR_Y] = y + starty;                                            \
         for (x = 0; x < width; x++) {                                          \
             values[VAR_X]      = x;                                            \
             values[VAR_TOP]    = values[VAR_A] = top[x];                       \
@@ -339,6 +346,7 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     ThreadData *td = arg;
     int slice_start = (td->h *  jobnr   ) / nb_jobs;
     int slice_end   = (td->h * (jobnr+1)) / nb_jobs;
+    int height      = slice_end - slice_start;
     const uint8_t *top    = td->top->data[td->plane];
     const uint8_t *bottom = td->bottom->data[td->plane];
     uint8_t *dst    = td->dst->data[td->plane];
@@ -357,7 +365,7 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
                      td->bottom->linesize[td->plane],
                      dst + slice_start * td->dst->linesize[td->plane],
                      td->dst->linesize[td->plane],
-                     td->w, slice_start, slice_end, td->param, &values[0]);
+                     td->w, height, td->param, &values[0], slice_start);
     return 0;
 }
 
@@ -435,6 +443,53 @@ static av_cold void uninit(AVFilterContext *ctx)
         av_expr_free(s->params[i].e);
 }
 
+void ff_blend_init(FilterParams *param, int is_16bit)
+{
+    switch (param->mode) {
+    case BLEND_ADDITION:   param->blend = is_16bit ? blend_addition_16bit   : blend_addition_8bit;   break;
+    case BLEND_ADDITION128: param->blend = is_16bit ? blend_addition128_16bit : blend_addition128_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_FREEZE:     param->blend = is_16bit ? blend_freeze_16bit     : blend_freeze_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_HEAT:       param->blend = is_16bit ? blend_heat_16bit       : blend_heat_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_MULTIPLY128:param->blend = is_16bit ? blend_multiply128_16bit: blend_multiply128_8bit;break;
+    case BLEND_NEGATION:   param->blend = is_16bit ? blend_negation_16bit   : blend_negation_8bit;   break;
+    case BLEND_NORMAL:     param->blend = param->opacity == 1 ? blend_copytop :
+                                          param->opacity == 0 ? blend_copybottom :
+                                          is_16bit ? blend_normal_16bit     : blend_normal_8bit;     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 (param->opacity == 0 && param->mode != BLEND_NORMAL) {
+        param->blend = blend_copytop;
+    }
+
+    if (ARCH_X86)
+        ff_blend_init_x86(param, is_16bit);
+}
+
 static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
@@ -491,42 +546,7 @@ static int config_output(AVFilterLink *outlink)
         if (s->all_opacity < 1)
             param->opacity = s->all_opacity;
 
-        switch (param->mode) {
-        case BLEND_ADDITION:   param->blend = is_16bit ? blend_addition_16bit   : blend_addition_8bit;   break;
-        case BLEND_ADDITION128: param->blend = is_16bit ? blend_addition128_16bit : blend_addition128_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_MULTIPLY128:param->blend = is_16bit ? blend_multiply128_16bit: blend_multiply128_8bit;break;
-        case BLEND_NEGATION:   param->blend = is_16bit ? blend_negation_16bit   : blend_negation_8bit;   break;
-        case BLEND_NORMAL:     param->blend = param->opacity == 1 ? blend_copytop :
-                                              is_16bit ? blend_normal_16bit     : blend_normal_8bit;     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 (ARCH_X86)
-            ff_blend_init_x86(param, is_16bit);
+        ff_blend_init(param, is_16bit);
 
         if (s->all_expr && !param->expr_str) {
             param->expr_str = av_strdup(s->all_expr);