[FFmpeg-devel] [PATCH] avfilter/avf_showcqt: optimize gamma

Muhammad Faiz mfcc64 at gmail.com
Sat May 2 19:13:30 CEST 2015


-------------- next part --------------
From c79989a2069966331ec65f9734554e4a2de065b9 Mon Sep 17 00:00:00 2001
From: Muhammad Faiz <mfcc64 at gmail.com>
Date: Fri, 1 May 2015 16:44:59 +0700
Subject: [PATCH] avfilter/avf_showcqt: optimize gamma calculation

benchmark (on intel core2 duo, gcc 4.9.1)
input samples duration 00:03:39.59
command: time -p ffmpeg -f f32le -ac 2 -ar 44100 -i input.pcm \
         -filter_complex showcqt=fullhd=0:gamma=$gamma \
         -f rawvideo -y /dev/null
gamma  previous  modified
  1    48.49 s   45.38 s
  2    49.33 s   48.11 s
  3    80.86 s   59.80 s
  4    80.84 s   51.25 s
  5    80.75 s   61.06 s
  6    80.93 s   61.80 s
  7    80.03 s   61.56 s
---
 libavfilter/avf_showcqt.c | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/libavfilter/avf_showcqt.c b/libavfilter/avf_showcqt.c
index e862dc7..c4eb791 100644
--- a/libavfilter/avf_showcqt.c
+++ b/libavfilter/avf_showcqt.c
@@ -567,8 +567,6 @@ static int plot_cqt(AVFilterLink *inlink)
     /* calculating cqt */
     for (x = 0; x < VIDEO_WIDTH; x++) {
         int u;
-        float g = 1.0f / s->gamma;
-        float g2 = 1.0f / s->gamma2;
         FFTComplex l = {0,0};
         FFTComplex r = {0,0};
 
@@ -584,10 +582,42 @@ static int plot_cqt(AVFilterLink *inlink)
         result[x][0] = l.re * l.re + l.im * l.im;
         result[x][2] = r.re * r.re + r.im * r.im;
         result[x][1] = 0.5f * (result[x][0] + result[x][2]);
-        result[x][3] = (g2 == 1.0f) ? result[x][1] : powf(result[x][1], g2);
-        result[x][0] = 255.0f * powf(FFMIN(1.0f,result[x][0]), g);
-        result[x][1] = 255.0f * powf(FFMIN(1.0f,result[x][1]), g);
-        result[x][2] = 255.0f * powf(FFMIN(1.0f,result[x][2]), g);
+
+        if (s->gamma2 == 1.0f)
+            result[x][3] = result[x][1];
+        else if (s->gamma2 = 2.0f)
+            result[x][3] = sqrtf(result[x][1]);
+        else if (s->gamma2 = 3.0f)
+            result[x][3] = cbrtf(result[x][1]);
+        else if (s->gamma2 = 4.0f)
+            result[x][3] = sqrtf(sqrtf(result[x][1]));
+        else
+            result[x][3] = expf(logf(result[x][1]) * (1.0f / s->gamma2));
+
+        result[x][0] = FFMIN(1.0f, result[x][0]);
+        result[x][1] = FFMIN(1.0f, result[x][1]);
+        result[x][2] = FFMIN(1.0f, result[x][2]);
+        if (s->gamma == 1.0f) {
+            result[x][0] = 255.0f * result[x][0];
+            result[x][1] = 255.0f * result[x][1];
+            result[x][2] = 255.0f * result[x][2];
+        } else if (s->gamma == 2.0f) {
+            result[x][0] = 255.0f * sqrtf(result[x][0]);
+            result[x][1] = 255.0f * sqrtf(result[x][1]);
+            result[x][2] = 255.0f * sqrtf(result[x][2]);
+        } else if (s->gamma == 3.0f) {
+            result[x][0] = 255.0f * cbrtf(result[x][0]);
+            result[x][1] = 255.0f * cbrtf(result[x][1]);
+            result[x][2] = 255.0f * cbrtf(result[x][2]);
+        } else if (s->gamma == 4.0f) {
+            result[x][0] = 255.0f * sqrtf(sqrtf(result[x][0]));
+            result[x][1] = 255.0f * sqrtf(sqrtf(result[x][1]));
+            result[x][2] = 255.0f * sqrtf(sqrtf(result[x][2]));
+        } else {
+            result[x][0] = 255.0f * expf(logf(result[x][0]) * (1.0f / s->gamma));
+            result[x][1] = 255.0f * expf(logf(result[x][1]) * (1.0f / s->gamma));
+            result[x][2] = 255.0f * expf(logf(result[x][2]) * (1.0f / s->gamma));
+        }
     }
 
     if (!s->fullhd) {
-- 
1.8.3.1


More information about the ffmpeg-devel mailing list