comparison adpcm.c @ 5525:bc4791868c52 libavcodec

various simplifications around recent av_clip_int16() usage
author aurel
date Sat, 11 Aug 2007 23:17:03 +0000
parents c2ab2ac31edb
children 9c37f7a3ff48
comparison
equal deleted inserted replaced
5524:f47bc5359101 5525:bc4791868c52
206 206
207 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample) 207 static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
208 { 208 {
209 int delta = sample - c->prev_sample; 209 int delta = sample - c->prev_sample;
210 int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8; 210 int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
211 c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8); 211 c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
212 c->prev_sample = av_clip_int16(c->prev_sample); 212 c->prev_sample = av_clip_int16(c->prev_sample);
213 c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88); 213 c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
214 return nibble; 214 return nibble;
215 } 215 }
216 216
226 226
227 nibble= (nibble + bias) / c->idelta; 227 nibble= (nibble + bias) / c->idelta;
228 nibble= av_clip(nibble, -8, 7)&0x0F; 228 nibble= av_clip(nibble, -8, 7)&0x0F;
229 229
230 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; 230 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
231 predictor = av_clip_int16(predictor);
232 231
233 c->sample2 = c->sample1; 232 c->sample2 = c->sample1;
234 c->sample1 = predictor; 233 c->sample1 = av_clip_int16(predictor);
235 234
236 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; 235 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
237 if (c->idelta < 16) c->idelta = 16; 236 if (c->idelta < 16) c->idelta = 16;
238 237
239 return nibble; 238 return nibble;
250 249
251 delta = sample - c->predictor; 250 delta = sample - c->predictor;
252 251
253 nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8; 252 nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
254 253
255 c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8); 254 c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
256 c->predictor = av_clip_int16(c->predictor); 255 c->predictor = av_clip_int16(c->predictor);
257 c->step = (c->step * yamaha_indexscale[nibble]) >> 8; 256 c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
258 c->step = av_clip(c->step, 127, 24567); 257 c->step = av_clip(c->step, 127, 24567);
259 258
260 return nibble; 259 return nibble;
668 diff = ((2 * delta + 1) * step) >> shift; 667 diff = ((2 * delta + 1) * step) >> shift;
669 predictor = c->predictor; 668 predictor = c->predictor;
670 if (sign) predictor -= diff; 669 if (sign) predictor -= diff;
671 else predictor += diff; 670 else predictor += diff;
672 671
673 predictor = av_clip_int16(predictor); 672 c->predictor = av_clip_int16(predictor);
674 c->predictor = predictor;
675 c->step_index = step_index; 673 c->step_index = step_index;
676 674
677 return (short)predictor; 675 return (short)c->predictor;
678 } 676 }
679 677
680 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble) 678 static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
681 { 679 {
682 int predictor; 680 int predictor;
683 681
684 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256; 682 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
685 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; 683 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
686 predictor = av_clip_int16(predictor);
687 684
688 c->sample2 = c->sample1; 685 c->sample2 = c->sample1;
689 c->sample1 = predictor; 686 c->sample1 = av_clip_int16(predictor);
690 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; 687 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
691 if (c->idelta < 16) c->idelta = 16; 688 if (c->idelta < 16) c->idelta = 16;
692 689
693 return (short)predictor; 690 return c->sample1;
694 } 691 }
695 692
696 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble) 693 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
697 { 694 {
698 int predictor; 695 int predictor;
717 if(c->step < 511) 714 if(c->step < 511)
718 c->step = 511; 715 c->step = 511;
719 if(c->step > 32767) 716 if(c->step > 32767)
720 c->step = 32767; 717 c->step = 32767;
721 718
722 predictor = av_clip_int16(predictor); 719 c->predictor = av_clip_int16(predictor);
723 c->predictor = predictor; 720 return (short)c->predictor;
724 return (short)predictor;
725 } 721 }
726 722
727 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift) 723 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
728 { 724 {
729 int sign, delta, diff; 725 int sign, delta, diff;
787 for(j=0;j<28;j++) { 783 for(j=0;j<28;j++) {
788 d = in[16+i+j*4]; 784 d = in[16+i+j*4];
789 785
790 t = (signed char)(d<<4)>>4; 786 t = (signed char)(d<<4)>>4;
791 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); 787 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
792 s = av_clip_int16(s); 788 s_2 = s_1;
793 *out = s; 789 s_1 = av_clip_int16(s);
790 *out = s_1;
794 out += inc; 791 out += inc;
795 s_2 = s_1;
796 s_1 = s;
797 } 792 }
798 793
799 if (inc==2) { /* stereo */ 794 if (inc==2) { /* stereo */
800 left->sample1 = s_1; 795 left->sample1 = s_1;
801 left->sample2 = s_2; 796 left->sample2 = s_2;
813 for(j=0;j<28;j++) { 808 for(j=0;j<28;j++) {
814 d = in[16+i+j*4]; 809 d = in[16+i+j*4];
815 810
816 t = (signed char)d >> 4; 811 t = (signed char)d >> 4;
817 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); 812 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
818 s = av_clip_int16(s); 813 s_2 = s_1;
819 *out = s; 814 s_1 = av_clip_int16(s);
815 *out = s_1;
820 out += inc; 816 out += inc;
821 s_2 = s_1;
822 s_1 = s;
823 } 817 }
824 818
825 if (inc==2) { /* stereo */ 819 if (inc==2) { /* stereo */
826 right->sample1 = s_1; 820 right->sample1 = s_1;
827 right->sample2 = s_2; 821 right->sample2 = s_2;
1179 (current_left_sample * coeff1l) + 1173 (current_left_sample * coeff1l) +
1180 (previous_left_sample * coeff2l) + 0x80) >> 8; 1174 (previous_left_sample * coeff2l) + 0x80) >> 8;
1181 next_right_sample = (next_right_sample + 1175 next_right_sample = (next_right_sample +
1182 (current_right_sample * coeff1r) + 1176 (current_right_sample * coeff1r) +
1183 (previous_right_sample * coeff2r) + 0x80) >> 8; 1177 (previous_right_sample * coeff2r) + 0x80) >> 8;
1184 next_left_sample = av_clip_int16(next_left_sample);
1185 next_right_sample = av_clip_int16(next_right_sample);
1186 1178
1187 previous_left_sample = current_left_sample; 1179 previous_left_sample = current_left_sample;
1188 current_left_sample = next_left_sample; 1180 current_left_sample = av_clip_int16(next_left_sample);
1189 previous_right_sample = current_right_sample; 1181 previous_right_sample = current_right_sample;
1190 current_right_sample = next_right_sample; 1182 current_right_sample = av_clip_int16(next_right_sample);
1191 *samples++ = (unsigned short)current_left_sample; 1183 *samples++ = (unsigned short)current_left_sample;
1192 *samples++ = (unsigned short)current_right_sample; 1184 *samples++ = (unsigned short)current_right_sample;
1193 } 1185 }
1194 } 1186 }
1195 break; 1187 break;
1384 if(n&1) sampledat= *src++ <<28; 1376 if(n&1) sampledat= *src++ <<28;
1385 else sampledat= (*src&0xF0)<<24; 1377 else sampledat= (*src&0xF0)<<24;
1386 1378
1387 sampledat = ((prev[ch][0]*factor1 1379 sampledat = ((prev[ch][0]*factor1
1388 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp); 1380 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
1389 sampledat = av_clip_int16(sampledat); 1381 *samples = av_clip_int16(sampledat);
1390 *samples = sampledat;
1391 prev[ch][1] = prev[ch][0]; 1382 prev[ch][1] = prev[ch][0];
1392 prev[ch][0] = *samples++; 1383 prev[ch][0] = *samples++;
1393 1384
1394 /* In case of stereo, skip one sample, this sample 1385 /* In case of stereo, skip one sample, this sample
1395 is for the other channel. */ 1386 is for the other channel. */