[FFmpeg-cvslog] g723.1: optimise scale_vector()

Mans Rullgard git at videolan.org
Mon Aug 13 14:49:44 CEST 2012


ffmpeg | branch: master | Mans Rullgard <mans at mansr.com> | Fri Aug 10 18:15:41 2012 +0100| [4aca716a531b0bc1f05c96209cf30577d6e48baa] | committer: Mans Rullgard

g723.1: optimise scale_vector()

Firstly, nothing in this function can overflow 32 bits so the use
of a 64-bit type is completely unnecessary.  Secondly, the scale
is either a power of two or 0x7fff.  Doing separate loops for these
cases avoids using multiplications.  Finally, since only the number
of bits, not the actual value, of the maximum value is needed, the
bitwise or of all the values serves the purpose while being faster.

It is worth noting that even if overflow could happen, it was not
handled correctly anyway.

Signed-off-by: Mans Rullgard <mans at mansr.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=4aca716a531b0bc1f05c96209cf30577d6e48baa
---

 libavcodec/g723_1.c |   12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/libavcodec/g723_1.c b/libavcodec/g723_1.c
index f91f629..5be4fe0 100644
--- a/libavcodec/g723_1.c
+++ b/libavcodec/g723_1.c
@@ -281,19 +281,21 @@ static int normalize_bits(int num, int width)
 static int scale_vector(int16_t *vector, int length)
 {
     int bits, max = 0;
-    int64_t scale;
     int i;
 
 
     for (i = 0; i < length; i++)
-        max = FFMAX(max, FFABS(vector[i]));
+        max |= FFABS(vector[i]);
 
     max   = FFMIN(max, 0x7FFF);
     bits  = normalize_bits(max, 15);
-    scale = (bits == 15) ? 0x7FFF : (1 << bits);
 
-    for (i = 0; i < length; i++)
-        vector[i] = av_clipl_int32(vector[i] * scale << 1) >> 4;
+    if (bits == 15)
+        for (i = 0; i < length; i++)
+            vector[i] = vector[i] * 0x7fff >> 3;
+    else
+        for (i = 0; i < length; i++)
+            vector[i] = vector[i] << bits >> 3;
 
     return bits - 3;
 }



More information about the ffmpeg-cvslog mailing list