[FFmpeg-devel] [PATCH 2/2] avfilter/vf_colorkey: Transform calculations to integer math

Timo Rothenpieler timo at rothenpieler.org
Sat Jun 13 15:09:08 CEST 2015


---
 libavfilter/vf_colorkey.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

diff --git a/libavfilter/vf_colorkey.c b/libavfilter/vf_colorkey.c
index 19e39ec..65d3e20 100644
--- a/libavfilter/vf_colorkey.c
+++ b/libavfilter/vf_colorkey.c
@@ -30,24 +30,28 @@ typedef struct ColorkeyContext {
 
     /* color offsets rgba */
     int co[4];
+    int i_similarity;
+    int i_blend;
 
     uint8_t colorkey_rgba[4];
     float similarity;
     float blend;
 } ColorkeyContext;
 
+#define SHIFT 8
+
 static uint8_t do_colorkey_pixel(ColorkeyContext *ctx, uint8_t r, uint8_t g, uint8_t b)
 {
-    int dr = (int)r - ctx->colorkey_rgba[0];
-    int dg = (int)g - ctx->colorkey_rgba[1];
-    int db = (int)b - ctx->colorkey_rgba[2];
+    int dr = ((int)r - ctx->colorkey_rgba[0]) << SHIFT;
+    int dg = ((int)g - ctx->colorkey_rgba[1]) << SHIFT;
+    int db = ((int)b - ctx->colorkey_rgba[2]) << SHIFT;
 
-    double diff = sqrt((dr * dr + dg * dg + db * db) / (255.0 * 255.0));
+    int diff = (int)sqrt(dr * dr + dg * dg + db * db) - ctx->i_similarity;
 
-    if (ctx->blend > 0.0001) {
-        return av_clipd((diff - ctx->similarity) / ctx->blend, 0.0, 1.0) * 255.0;
+    if (ctx->i_blend) {
+        return av_clip(diff * ctx->i_blend, 0, 255 << (SHIFT + SHIFT)) >> (SHIFT + SHIFT);
     } else {
-        return (diff > ctx->similarity) ? 255 : 0;
+        return (diff > 0) ? 255 : 0;
     }
 }
 
@@ -127,6 +131,19 @@ static av_cold int query_formats(AVFilterContext *avctx)
     return ff_set_common_formats(avctx, formats);
 }
 
+static av_cold int init(AVFilterContext *avctx)
+{
+    ColorkeyContext *ctx = avctx->priv;
+
+    ctx->i_similarity = ctx->similarity * 255.0 * 1000.0;
+    ctx->i_blend = (1.0 / ctx->blend) * 1000.0;
+
+    ctx->i_similarity = (ctx->i_similarity << SHIFT) / 1000;
+    ctx->i_blend = (ctx->i_blend << SHIFT) / 1000;
+
+    return 0;
+}
+
 static const AVFilterPad colorkey_inputs[] = {
     {
         .name = "default",
@@ -160,6 +177,7 @@ AVFILTER_DEFINE_CLASS(colorkey);
 AVFilter ff_vf_colorkey = {
     .name          = "colorkey",
     .description   = NULL_IF_CONFIG_SMALL("colorkey filter"),
+    .init          = init,
     .priv_size     = sizeof(ColorkeyContext),
     .priv_class    = &colorkey_class,
     .query_formats = query_formats,
-- 
2.4.3



More information about the ffmpeg-devel mailing list