From aee481cebe8f95ce3789bdead6fb8ddfb142c37f Mon Sep 17 00:00:00 2001 From: Aurelien Jacobs Date: Sat, 11 Aug 2007 22:48:55 +0000 Subject: [PATCH] use av_clip_int16() where it makes sense Originally committed as revision 10078 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/adpcm.c | 36 +++++++++++++++--------------------- libavcodec/adx.c | 6 ++---- libavcodec/atrac3.c | 6 +++--- libavcodec/cook.c | 2 +- libavcodec/dpcm.c | 10 ++++------ libavcodec/dsicinav.c | 2 +- libavcodec/liba52.c | 6 +----- libavcodec/libvorbis.c | 3 +-- libavcodec/mpegaudiodec.c | 5 +---- libavcodec/ra144.c | 4 +--- libavcodec/resample2.c | 2 +- libavcodec/sonic.c | 9 +-------- libavcodec/vmdav.c | 2 +- libavcodec/wmadec.c | 5 +---- 14 files changed, 34 insertions(+), 64 deletions(-) diff --git a/libavcodec/adpcm.c b/libavcodec/adpcm.c index f022e852f4..327787a5b9 100644 --- a/libavcodec/adpcm.c +++ b/libavcodec/adpcm.c @@ -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 @@ static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, sho 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 @@ static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, shor 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 @@ static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, 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 @@ static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples, #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 @@ static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, 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 @@ static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble) 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 @@ static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble) 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 @@ static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned c } 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 @@ static void xa_decode(short *out, const unsigned char *in, t = (signed char)(d<<4)>>4; s = ( t<>6); - CLAMP_TO_SHORT(s); + s = av_clip_int16(s); *out = s; out += inc; s_2 = s_1; @@ -821,7 +815,7 @@ static void xa_decode(short *out, const unsigned char *in, t = (signed char)d >> 4; s = ( t<>6); - CLAMP_TO_SHORT(s); + s = av_clip_int16(s); *out = s; out += inc; s_2 = s_1; @@ -915,7 +909,7 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 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 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 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 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 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 @@ static int adpcm_decode_frame(AVCodecContext *avctx, 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++; diff --git a/libavcodec/adx.c b/libavcodec/adx.c index 4ea8929bc1..7185a32ce0 100644 --- a/libavcodec/adx.c +++ b/libavcodec/adx.c @@ -46,8 +46,6 @@ typedef struct { #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 @@ static void adx_decode(short *out,const unsigned char *in,PREV *prev) // 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 @@ static void adx_decode(short *out,const unsigned char *in,PREV *prev) //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; diff --git a/libavcodec/atrac3.c b/libavcodec/atrac3.c index 0b1400712b..e7239a3bab 100644 --- a/libavcodec/atrac3.c +++ b/libavcodec/atrac3.c @@ -895,13 +895,13 @@ static int atrac3_decode_frame(AVCodecContext *avctx, 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); } diff --git a/libavcodec/cook.c b/libavcodec/cook.c index 2765b12eb6..43b661d186 100644 --- a/libavcodec/cook.c +++ b/libavcodec/cook.c @@ -906,7 +906,7 @@ saturate_output_float (COOKContext *q, int chan, int16_t *out) */ 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])); } } diff --git a/libavcodec/dpcm.c b/libavcodec/dpcm.c index 6243881dee..0ce05c821a 100644 --- a/libavcodec/dpcm.c +++ b/libavcodec/dpcm.c @@ -46,8 +46,6 @@ typedef struct DPCMContext { 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 @@ static int dpcm_decode_frame(AVCodecContext *avctx, /* 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 @@ static int dpcm_decode_frame(AVCodecContext *avctx, 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 @@ static int dpcm_decode_frame(AVCodecContext *avctx, 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 @@ static int dpcm_decode_frame(AVCodecContext *avctx, 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; diff --git a/libavcodec/dsicinav.c b/libavcodec/dsicinav.c index 78cdbe93a9..ecaa94e4d0 100644 --- a/libavcodec/dsicinav.c +++ b/libavcodec/dsicinav.c @@ -325,7 +325,7 @@ static int cinaudio_decode_frame(AVCodecContext *avctx, } 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; } diff --git a/libavcodec/liba52.c b/libavcodec/liba52.c index 1fbed09d88..ce0f822d93 100644 --- a/libavcodec/liba52.c +++ b/libavcodec/liba52.c @@ -123,11 +123,7 @@ static int a52_decode_init(AVCodecContext *avctx) /**** 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) diff --git a/libavcodec/libvorbis.c b/libavcodec/libvorbis.c index 5d40e91b52..faaceb0ba7 100644 --- a/libavcodec/libvorbis.c +++ b/libavcodec/libvorbis.c @@ -307,8 +307,7 @@ static inline int conv(int samples, float **pcm, char *buf, int channels) { val = mono[j] * 32767.f; - if(val > 32767) val = 32767 ; - if(val < -32768) val = -32768 ; + val = av_clip_int16(val); *ptr = val ; ptr += channels; diff --git a/libavcodec/mpegaudiodec.c b/libavcodec/mpegaudiodec.c index 3c4b757d6a..d679b006be 100644 --- a/libavcodec/mpegaudiodec.c +++ b/libavcodec/mpegaudiodec.c @@ -822,10 +822,7 @@ void ff_mpa_synth_filter(MPA_INT *synth_buf_ptr, int *synth_buf_offset, #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; } diff --git a/libavcodec/ra144.c b/libavcodec/ra144.c index c4f4b813b3..c820c732ba 100644 --- a/libavcodec/ra144.c +++ b/libavcodec/ra144.c @@ -486,9 +486,7 @@ static int ra144_decode_frame(AVCodecContext * avctx, shptr=glob->output_buffer; while (shptroutput_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; diff --git a/libavcodec/resample2.c b/libavcodec/resample2.c index ffd6fc71c6..da1443d98f 100644 --- a/libavcodec/resample2.c +++ b/libavcodec/resample2.c @@ -279,7 +279,7 @@ int av_resample(AVResampleContext *c, short *dst, short *src, int *consumed, int } #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; diff --git a/libavcodec/sonic.c b/libavcodec/sonic.c index f3388589b6..bff21eb8d2 100644 --- a/libavcodec/sonic.c +++ b/libavcodec/sonic.c @@ -926,14 +926,7 @@ static int sonic_decode_frame(AVCodecContext *avctx, // 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); diff --git a/libavcodec/vmdav.c b/libavcodec/vmdav.c index c37e8fd48b..9507666cd4 100644 --- a/libavcodec/vmdav.c +++ b/libavcodec/vmdav.c @@ -458,7 +458,7 @@ static void vmdaudio_decode_audio(VmdAudioContext *s, unsigned char *data, 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; } diff --git a/libavcodec/wmadec.c b/libavcodec/wmadec.c index f828c789c7..01bd47f8e1 100644 --- a/libavcodec/wmadec.c +++ b/libavcodec/wmadec.c @@ -740,10 +740,7 @@ static int wma_decode_frame(WMACodecContext *s, int16_t *samples) for(i=0;i 32767) - a = 32767; - else if (a < -32768) - a = -32768; + a = av_clip_int16(a); *ptr = a; ptr += incr; }