fftools/ffmpeg: Fallback to duration if sample rate is unavailable
[ffmpeg.git] / libavfilter / vf_blend.c
index 7bad311..ea63a20 100644 (file)
@@ -82,6 +82,7 @@ typedef struct ThreadData {
     { "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" },\
+    { "multiply128","", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_MULTIPLY128},0, 0, FLAGS, "mode" },\
     { "negation",   "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_NEGATION},   0, 0, FLAGS, "mode" },\
     { "normal",     "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_NORMAL},     0, 0, FLAGS, "mode" },\
     { "or",         "", 0, AV_OPT_TYPE_CONST, {.i64=BLEND_OR},         0, 0, FLAGS, "mode" },\
@@ -117,25 +118,32 @@ static const AVOption blend_options[] = {
 
 AVFILTER_DEFINE_CLASS(blend);
 
-static void blend_normal(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)
-{
-    av_image_copy_plane(dst, dst_linesize, top, top_linesize, width, end - start);
+#define COPY(src)                                                            \
+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 height,               \
+                            FilterParams *param, double *values)             \
+{                                                                            \
+    av_image_copy_plane(dst, dst_linesize, src, src ## _linesize,            \
+                        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,
+                              ptrdiff_t width, ptrdiff_t height,
                               FilterParams *param, double *values)
 {
     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);
         }
@@ -148,7 +156,7 @@ 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,
+                                  ptrdiff_t width, ptrdiff_t height,
                                   FilterParams *param, double *values)
 {
     const uint16_t *top = (uint16_t*)_top;
@@ -160,7 +168,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);
         }
@@ -174,13 +182,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,                \
+                                 ptrdiff_t width, ptrdiff_t height,                \
                                  FilterParams *param, double *values)          \
 {                                                                              \
     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;                     \
         }                                                                      \
@@ -194,7 +202,7 @@ 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,               \
+                                  ptrdiff_t width, ptrdiff_t height,           \
                                   FilterParams *param, double *values)         \
 {                                                                              \
     const uint16_t *top = (uint16_t*)_top;                                     \
@@ -206,7 +214,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;                     \
         }                                                                      \
@@ -229,6 +237,7 @@ DEFINE_BLEND8(addition128, av_clip_uint8(A + B - 128))
 DEFINE_BLEND8(average,    (A + B) / 2)
 DEFINE_BLEND8(subtract,   FFMAX(0, A - B))
 DEFINE_BLEND8(multiply,   MULTIPLY(1, A, B))
+DEFINE_BLEND8(multiply128,av_clip_uint8((A - 128) * B / 32. + 128))
 DEFINE_BLEND8(negation,   255 - FFABS(255 - A - B))
 DEFINE_BLEND8(difference, FFABS(A - B))
 DEFINE_BLEND8(difference128, av_clip_uint8(128 + A - B))
@@ -238,7 +247,7 @@ 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(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))
@@ -268,6 +277,7 @@ DEFINE_BLEND16(addition128, av_clip_uint16(A + B - 32768))
 DEFINE_BLEND16(average,    (A + B) / 2)
 DEFINE_BLEND16(subtract,   FFMAX(0, A - B))
 DEFINE_BLEND16(multiply,   MULTIPLY(1, A, B))
+DEFINE_BLEND16(multiply128, av_clip_uint16((A - 32768) * B / 8192. + 32768))
 DEFINE_BLEND16(negation,   65535 - FFABS(65535 - A - B))
 DEFINE_BLEND16(difference, FFABS(A - B))
 DEFINE_BLEND16(difference128, av_clip_uint16(32768 + A - B))
@@ -277,7 +287,7 @@ 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(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))
@@ -296,7 +306,7 @@ 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,                  \
+                               ptrdiff_t width, ptrdiff_t height,              \
                                FilterParams *param, double *values)            \
 {                                                                              \
     const type *top = (type*)_top;                                             \
@@ -308,7 +318,7 @@ 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++) {                                            \
+    for (y = 0; y < height; y++) {                                             \
         values[VAR_Y] = y;                                                     \
         for (x = 0; x < width; x++) {                                          \
             values[VAR_X]      = x;                                            \
@@ -330,6 +340,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];
@@ -348,7 +359,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]);
     return 0;
 }
 
@@ -426,6 +437,51 @@ 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_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 :
+                                          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;
@@ -482,41 +538,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_NEGATION:   param->blend = is_16bit ? blend_negation_16bit   : blend_negation_8bit;   break;
-        case BLEND_NORMAL:     param->blend = param->opacity == 1 ? blend_normal:
-                                              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);