changeset 5523:c2ab2ac31edb libavcodec

use av_clip_int16() where it makes sense
author aurel
date Sat, 11 Aug 2007 22:48:55 +0000
parents acaaff7b6fb8
children f47bc5359101
files adpcm.c adx.c atrac3.c cook.c dpcm.c dsicinav.c liba52.c libvorbis.c mpegaudiodec.c ra144.c resample2.c sonic.c vmdav.c wmadec.c
diffstat 14 files changed, 34 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/adpcm.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/adpcm.c	Sat Aug 11 22:48:55 2007 +0000
@@ -50,12 +50,6 @@
 
 #define BLKSIZE 1024
 
-#define CLAMP_TO_SHORT(value) \
-if (value > 32767) \
-    value = 32767; \
-else if (value < -32768) \
-    value = -32768; \
-
 /* step_table[] and index_table[] are from the ADPCM reference source */
 /* This is the index table: */
 static const int index_table[16] = {
@@ -215,7 +209,7 @@
     int delta = sample - c->prev_sample;
     int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
     c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
-    CLAMP_TO_SHORT(c->prev_sample);
+    c->prev_sample = av_clip_int16(c->prev_sample);
     c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
     return nibble;
 }
@@ -234,7 +228,7 @@
     nibble= av_clip(nibble, -8, 7)&0x0F;
 
     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
-    CLAMP_TO_SHORT(predictor);
+    predictor = av_clip_int16(predictor);
 
     c->sample2 = c->sample1;
     c->sample1 = predictor;
@@ -259,7 +253,7 @@
     nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
 
     c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
-    CLAMP_TO_SHORT(c->predictor);
+    c->predictor = av_clip_int16(c->predictor);
     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
     c->step = av_clip(c->step, 127, 24567);
 
@@ -339,7 +333,7 @@
 #define STORE_NODE(NAME, STEP_INDEX)\
                     int d;\
                     uint32_t ssd;\
-                    CLAMP_TO_SHORT(dec_sample);\
+                    dec_sample = av_clip_int16(dec_sample);\
                     d = sample - dec_sample;\
                     ssd = nodes[j]->ssd + d*d;\
                     if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
@@ -676,7 +670,7 @@
     if (sign) predictor -= diff;
     else predictor += diff;
 
-    CLAMP_TO_SHORT(predictor);
+    predictor = av_clip_int16(predictor);
     c->predictor = predictor;
     c->step_index = step_index;
 
@@ -689,7 +683,7 @@
 
     predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
     predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
-    CLAMP_TO_SHORT(predictor);
+    predictor = av_clip_int16(predictor);
 
     c->sample2 = c->sample1;
     c->sample1 = predictor;
@@ -725,7 +719,7 @@
     if(c->step > 32767)
         c->step = 32767;
 
-    CLAMP_TO_SHORT(predictor);
+    predictor = av_clip_int16(predictor);
     c->predictor = predictor;
     return (short)predictor;
 }
@@ -766,7 +760,7 @@
     }
 
     c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
-    CLAMP_TO_SHORT(c->predictor);
+    c->predictor = av_clip_int16(c->predictor);
     c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
     c->step = av_clip(c->step, 127, 24567);
     return c->predictor;
@@ -795,7 +789,7 @@
 
             t = (signed char)(d<<4)>>4;
             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
-            CLAMP_TO_SHORT(s);
+            s = av_clip_int16(s);
             *out = s;
             out += inc;
             s_2 = s_1;
@@ -821,7 +815,7 @@
 
             t = (signed char)d >> 4;
             s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
-            CLAMP_TO_SHORT(s);
+            s = av_clip_int16(s);
             *out = s;
             out += inc;
             s_2 = s_1;
@@ -915,7 +909,7 @@
         if(cs->predictor & 0x8000)
             cs->predictor -= 0x10000;
 
-        CLAMP_TO_SHORT(cs->predictor);
+        cs->predictor = av_clip_int16(cs->predictor);
 
         cs->step_index = (*src++) & 0x7F;
 
@@ -1187,8 +1181,8 @@
                 next_right_sample = (next_right_sample +
                     (current_right_sample * coeff1r) +
                     (previous_right_sample * coeff2r) + 0x80) >> 8;
-                CLAMP_TO_SHORT(next_left_sample);
-                CLAMP_TO_SHORT(next_right_sample);
+                next_left_sample = av_clip_int16(next_left_sample);
+                next_right_sample = av_clip_int16(next_right_sample);
 
                 previous_left_sample = current_left_sample;
                 current_left_sample = next_left_sample;
@@ -1318,7 +1312,7 @@
                     c->status[i].step_index += table[delta & (~signmask)];
 
                     c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
-                    c->status[i].predictor = av_clip(c->status[i].predictor, -32768, 32767);
+                    c->status[i].predictor = av_clip_int16(c->status[i].predictor);
 
                     *samples++ = c->status[i].predictor;
                     if (samples >= samples_end) {
@@ -1392,7 +1386,7 @@
 
                     sampledat = ((prev[ch][0]*factor1
                                 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
-                    CLAMP_TO_SHORT(sampledat);
+                    sampledat = av_clip_int16(sampledat);
                     *samples = sampledat;
                     prev[ch][1] = prev[ch][0];
                     prev[ch][0] = *samples++;
--- a/adx.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/adx.c	Sat Aug 11 22:48:55 2007 +0000
@@ -46,8 +46,6 @@
 #define    SCALE1    0x7298
 #define    SCALE2    0x3350
 
-#define    CLIP(s)    if (s>32767) s=32767; else if (s<-32768) s=-32768
-
 /* 18 bytes <-> 32 samples */
 
 #ifdef CONFIG_ENCODERS
@@ -110,7 +108,7 @@
         // d>>=4; if (d&8) d-=16;
         d = ((signed char)d >> 4);
         s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
-        CLIP(s0);
+        s0 = av_clip_int16(s0);
         *out++=s0;
         s2 = s1;
         s1 = s0;
@@ -119,7 +117,7 @@
         //d&=15; if (d&8) d-=16;
         d = ((signed char)(d<<4) >> 4);
         s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
-        CLIP(s0);
+        s0 = av_clip_int16(s0);
         *out++=s0;
         s2 = s1;
         s1 = s0;
--- a/atrac3.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/atrac3.c	Sat Aug 11 22:48:55 2007 +0000
@@ -895,13 +895,13 @@
     if (q->channels == 1) {
         /* mono */
         for (i = 0; i<1024; i++)
-            samples[i] = av_clip(round(q->outSamples[i]), -32768, 32767);
+            samples[i] = av_clip_int16(round(q->outSamples[i]));
         *data_size = 1024 * sizeof(int16_t);
     } else {
         /* stereo */
         for (i = 0; i < 1024; i++) {
-            samples[i*2] = av_clip(round(q->outSamples[i]), -32768, 32767);
-            samples[i*2+1] = av_clip(round(q->outSamples[1024+i]), -32768, 32767);
+            samples[i*2] = av_clip_int16(round(q->outSamples[i]));
+            samples[i*2+1] = av_clip_int16(round(q->outSamples[1024+i]));
         }
         *data_size = 2048 * sizeof(int16_t);
     }
--- a/cook.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/cook.c	Sat Aug 11 22:48:55 2007 +0000
@@ -906,7 +906,7 @@
      */
     for (j = 0; j < q->samples_per_channel; j++) {
         out[chan + q->nb_channels * j] =
-          av_clip(lrintf(output[j]), -32768, 32767);
+          av_clip_int16(lrintf(output[j]));
     }
 }
 
--- a/dpcm.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/dpcm.c	Sat Aug 11 22:48:55 2007 +0000
@@ -46,8 +46,6 @@
     const int *sol_table;//for SOL_DPCM
 } DPCMContext;
 
-#define SATURATE_S16(x)  if (x < -32768) x = -32768; \
-  else if (x > 32767) x = 32767;
 #define SE_16BIT(x)  if (x & 0x8000) x -= 0x10000;
 
 static int interplay_delta_table[] = {
@@ -190,7 +188,7 @@
         /* decode the samples */
         for (in = 8, out = 0; in < buf_size; in++, out++) {
             predictor[channel_number] += s->roq_square_array[buf[in]];
-            SATURATE_S16(predictor[channel_number]);
+            predictor[channel_number] = av_clip_int16(predictor[channel_number]);
             output_samples[out] = predictor[channel_number];
 
             /* toggle channel */
@@ -213,7 +211,7 @@
 
         while (in < buf_size) {
             predictor[channel_number] += interplay_delta_table[buf[in++]];
-            SATURATE_S16(predictor[channel_number]);
+            predictor[channel_number] = av_clip_int16(predictor[channel_number]);
             output_samples[out++] = predictor[channel_number];
 
             /* toggle channel */
@@ -248,7 +246,7 @@
             diff >>= shift[channel_number];
             predictor[channel_number] += diff;
 
-            SATURATE_S16(predictor[channel_number]);
+            predictor[channel_number] = av_clip_int16(predictor[channel_number]);
             output_samples[out++] = predictor[channel_number];
 
             /* toggle channel */
@@ -277,7 +275,7 @@
                 n = buf[in++];
                 if (n & 0x80) s->sample[channel_number] -= s->sol_table[n & 0x7F];
                 else s->sample[channel_number] += s->sol_table[n & 0x7F];
-                SATURATE_S16(s->sample[channel_number]);
+                s->sample[channel_number] = av_clip_int16(s->sample[channel_number]);
                 output_samples[out++] = s->sample[channel_number];
                 /* toggle channel */
                 channel_number ^= s->channels - 1;
--- a/dsicinav.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/dsicinav.c	Sat Aug 11 22:48:55 2007 +0000
@@ -325,7 +325,7 @@
     }
     while (buf_size > 0) {
         cin->delta += cinaudio_delta16_table[*src++];
-        cin->delta = av_clip(cin->delta, -32768, 32767);
+        cin->delta = av_clip_int16(cin->delta);
         *samples++ = cin->delta;
         --buf_size;
     }
--- a/liba52.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/liba52.c	Sat Aug 11 22:48:55 2007 +0000
@@ -123,11 +123,7 @@
 /**** the following two functions comes from a52dec */
 static inline int blah (int32_t i)
 {
-    if (i > 0x43c07fff)
-        return 32767;
-    else if (i < 0x43bf8000)
-        return -32768;
-    return i - 0x43c00000;
+    return av_clip_int16(i - 0x43c00000);
 }
 
 static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
--- a/libvorbis.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/libvorbis.c	Sat Aug 11 22:48:55 2007 +0000
@@ -307,8 +307,7 @@
 
             val = mono[j] * 32767.f;
 
-            if(val > 32767) val = 32767 ;
-            if(val < -32768) val = -32768 ;
+            val = av_clip_int16(val);
 
             *ptr = val ;
             ptr += channels;
--- a/mpegaudiodec.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/mpegaudiodec.c	Sat Aug 11 22:48:55 2007 +0000
@@ -822,10 +822,7 @@
 #if FRAC_BITS <= 15
         /* NOTE: can cause a loss in precision if very high amplitude
            sound */
-        if (v > 32767)
-            v = 32767;
-        else if (v < -32768)
-            v = -32768;
+        v = av_clip_int16(v);
 #endif
         synth_buf[j] = v;
     }
--- a/ra144.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/ra144.c	Sat Aug 11 22:48:55 2007 +0000
@@ -486,9 +486,7 @@
     shptr=glob->output_buffer;
     while (shptr<glob->output_buffer+BLOCKSIZE) {
       s=*(shptr++)<<2;
-      *data=s;
-      if (s>32767) *data=32767;
-      if (s<-32767) *data=-32768;
+      *data=av_clip_int16(s);
       data++;
     }
     b+=30;
--- a/resample2.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/resample2.c	Sat Aug 11 22:48:55 2007 +0000
@@ -279,7 +279,7 @@
         }
 
 #ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
-        dst[dst_index] = av_clip(lrintf(val), -32768, 32767);
+        dst[dst_index] = av_clip_int16(lrintf(val));
 #else
         val = (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
         dst[dst_index] = (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 32767 : val;
--- a/sonic.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/sonic.c	Sat Aug 11 22:48:55 2007 +0000
@@ -926,14 +926,7 @@
 
     // internal -> short
     for (i = 0; i < s->frame_size; i++)
-    {
-        if (s->int_samples[i] > 32767)
-            samples[i] = 32767;
-        else if (s->int_samples[i] < -32768)
-            samples[i] = -32768;
-        else
-            samples[i] = s->int_samples[i];
-    }
+        samples[i] = av_clip_int16(s->int_samples[i]);
 
     align_get_bits(&gb);
 
--- a/vmdav.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/vmdav.c	Sat Aug 11 22:48:55 2007 +0000
@@ -458,7 +458,7 @@
             s->predictors[chan] -= vmdaudio_table[buf[i] & 0x7F];
         else
             s->predictors[chan] += vmdaudio_table[buf[i]];
-        s->predictors[chan] = av_clip(s->predictors[chan], -32768, 32767);
+        s->predictors[chan] = av_clip_int16(s->predictors[chan]);
         out[i] = s->predictors[chan];
         chan ^= stereo;
     }
--- a/wmadec.c	Fri Aug 10 19:28:28 2007 +0000
+++ b/wmadec.c	Sat Aug 11 22:48:55 2007 +0000
@@ -740,10 +740,7 @@
 
         for(i=0;i<n;i++) {
             a = lrintf(*iptr++);
-            if (a > 32767)
-                a = 32767;
-            else if (a < -32768)
-                a = -32768;
+            a = av_clip_int16(a);
             *ptr = a;
             ptr += incr;
         }