Mercurial > libavcodec.hg
comparison adpcm.c @ 5523:c2ab2ac31edb libavcodec
use av_clip_int16() where it makes sense
author | aurel |
---|---|
date | Sat, 11 Aug 2007 22:48:55 +0000 |
parents | a0dcb5a51409 |
children | bc4791868c52 |
comparison
equal
deleted
inserted
replaced
5522:acaaff7b6fb8 | 5523:c2ab2ac31edb |
---|---|
48 * readstr http://www.geocities.co.jp/Playtown/2004/ | 48 * readstr http://www.geocities.co.jp/Playtown/2004/ |
49 */ | 49 */ |
50 | 50 |
51 #define BLKSIZE 1024 | 51 #define BLKSIZE 1024 |
52 | 52 |
53 #define CLAMP_TO_SHORT(value) \ | |
54 if (value > 32767) \ | |
55 value = 32767; \ | |
56 else if (value < -32768) \ | |
57 value = -32768; \ | |
58 | |
59 /* step_table[] and index_table[] are from the ADPCM reference source */ | 53 /* step_table[] and index_table[] are from the ADPCM reference source */ |
60 /* This is the index table: */ | 54 /* This is the index table: */ |
61 static const int index_table[16] = { | 55 static const int index_table[16] = { |
62 -1, -1, -1, -1, 2, 4, 6, 8, | 56 -1, -1, -1, -1, 2, 4, 6, 8, |
63 -1, -1, -1, -1, 2, 4, 6, 8, | 57 -1, -1, -1, -1, 2, 4, 6, 8, |
213 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) |
214 { | 208 { |
215 int delta = sample - c->prev_sample; | 209 int delta = sample - c->prev_sample; |
216 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; |
217 c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8); | 211 c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8); |
218 CLAMP_TO_SHORT(c->prev_sample); | 212 c->prev_sample = av_clip_int16(c->prev_sample); |
219 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); |
220 return nibble; | 214 return nibble; |
221 } | 215 } |
222 | 216 |
223 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample) | 217 static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample) |
232 | 226 |
233 nibble= (nibble + bias) / c->idelta; | 227 nibble= (nibble + bias) / c->idelta; |
234 nibble= av_clip(nibble, -8, 7)&0x0F; | 228 nibble= av_clip(nibble, -8, 7)&0x0F; |
235 | 229 |
236 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; | 230 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; |
237 CLAMP_TO_SHORT(predictor); | 231 predictor = av_clip_int16(predictor); |
238 | 232 |
239 c->sample2 = c->sample1; | 233 c->sample2 = c->sample1; |
240 c->sample1 = predictor; | 234 c->sample1 = predictor; |
241 | 235 |
242 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; | 236 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; |
257 delta = sample - c->predictor; | 251 delta = sample - c->predictor; |
258 | 252 |
259 nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8; | 253 nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8; |
260 | 254 |
261 c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8); | 255 c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8); |
262 CLAMP_TO_SHORT(c->predictor); | 256 c->predictor = av_clip_int16(c->predictor); |
263 c->step = (c->step * yamaha_indexscale[nibble]) >> 8; | 257 c->step = (c->step * yamaha_indexscale[nibble]) >> 8; |
264 c->step = av_clip(c->step, 127, 24567); | 258 c->step = av_clip(c->step, 127, 24567); |
265 | 259 |
266 return nibble; | 260 return nibble; |
267 } | 261 } |
337 const int nibble = nidx & 0xf; | 331 const int nibble = nidx & 0xf; |
338 int dec_sample = predictor + nidx * step; | 332 int dec_sample = predictor + nidx * step; |
339 #define STORE_NODE(NAME, STEP_INDEX)\ | 333 #define STORE_NODE(NAME, STEP_INDEX)\ |
340 int d;\ | 334 int d;\ |
341 uint32_t ssd;\ | 335 uint32_t ssd;\ |
342 CLAMP_TO_SHORT(dec_sample);\ | 336 dec_sample = av_clip_int16(dec_sample);\ |
343 d = sample - dec_sample;\ | 337 d = sample - dec_sample;\ |
344 ssd = nodes[j]->ssd + d*d;\ | 338 ssd = nodes[j]->ssd + d*d;\ |
345 if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\ | 339 if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\ |
346 continue;\ | 340 continue;\ |
347 /* Collapse any two states with the same previous sample value. \ | 341 /* Collapse any two states with the same previous sample value. \ |
674 diff = ((2 * delta + 1) * step) >> shift; | 668 diff = ((2 * delta + 1) * step) >> shift; |
675 predictor = c->predictor; | 669 predictor = c->predictor; |
676 if (sign) predictor -= diff; | 670 if (sign) predictor -= diff; |
677 else predictor += diff; | 671 else predictor += diff; |
678 | 672 |
679 CLAMP_TO_SHORT(predictor); | 673 predictor = av_clip_int16(predictor); |
680 c->predictor = predictor; | 674 c->predictor = predictor; |
681 c->step_index = step_index; | 675 c->step_index = step_index; |
682 | 676 |
683 return (short)predictor; | 677 return (short)predictor; |
684 } | 678 } |
687 { | 681 { |
688 int predictor; | 682 int predictor; |
689 | 683 |
690 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256; | 684 predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256; |
691 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; | 685 predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta; |
692 CLAMP_TO_SHORT(predictor); | 686 predictor = av_clip_int16(predictor); |
693 | 687 |
694 c->sample2 = c->sample1; | 688 c->sample2 = c->sample1; |
695 c->sample1 = predictor; | 689 c->sample1 = predictor; |
696 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; | 690 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; |
697 if (c->idelta < 16) c->idelta = 16; | 691 if (c->idelta < 16) c->idelta = 16; |
723 if(c->step < 511) | 717 if(c->step < 511) |
724 c->step = 511; | 718 c->step = 511; |
725 if(c->step > 32767) | 719 if(c->step > 32767) |
726 c->step = 32767; | 720 c->step = 32767; |
727 | 721 |
728 CLAMP_TO_SHORT(predictor); | 722 predictor = av_clip_int16(predictor); |
729 c->predictor = predictor; | 723 c->predictor = predictor; |
730 return (short)predictor; | 724 return (short)predictor; |
731 } | 725 } |
732 | 726 |
733 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift) | 727 static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift) |
764 c->predictor = 0; | 758 c->predictor = 0; |
765 c->step = 127; | 759 c->step = 127; |
766 } | 760 } |
767 | 761 |
768 c->predictor += (c->step * yamaha_difflookup[nibble]) / 8; | 762 c->predictor += (c->step * yamaha_difflookup[nibble]) / 8; |
769 CLAMP_TO_SHORT(c->predictor); | 763 c->predictor = av_clip_int16(c->predictor); |
770 c->step = (c->step * yamaha_indexscale[nibble]) >> 8; | 764 c->step = (c->step * yamaha_indexscale[nibble]) >> 8; |
771 c->step = av_clip(c->step, 127, 24567); | 765 c->step = av_clip(c->step, 127, 24567); |
772 return c->predictor; | 766 return c->predictor; |
773 } | 767 } |
774 | 768 |
793 for(j=0;j<28;j++) { | 787 for(j=0;j<28;j++) { |
794 d = in[16+i+j*4]; | 788 d = in[16+i+j*4]; |
795 | 789 |
796 t = (signed char)(d<<4)>>4; | 790 t = (signed char)(d<<4)>>4; |
797 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); | 791 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); |
798 CLAMP_TO_SHORT(s); | 792 s = av_clip_int16(s); |
799 *out = s; | 793 *out = s; |
800 out += inc; | 794 out += inc; |
801 s_2 = s_1; | 795 s_2 = s_1; |
802 s_1 = s; | 796 s_1 = s; |
803 } | 797 } |
819 for(j=0;j<28;j++) { | 813 for(j=0;j<28;j++) { |
820 d = in[16+i+j*4]; | 814 d = in[16+i+j*4]; |
821 | 815 |
822 t = (signed char)d >> 4; | 816 t = (signed char)d >> 4; |
823 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); | 817 s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6); |
824 CLAMP_TO_SHORT(s); | 818 s = av_clip_int16(s); |
825 *out = s; | 819 *out = s; |
826 out += inc; | 820 out += inc; |
827 s_2 = s_1; | 821 s_2 = s_1; |
828 s_1 = s; | 822 s_1 = s; |
829 } | 823 } |
913 | 907 |
914 /* sign extension */ | 908 /* sign extension */ |
915 if(cs->predictor & 0x8000) | 909 if(cs->predictor & 0x8000) |
916 cs->predictor -= 0x10000; | 910 cs->predictor -= 0x10000; |
917 | 911 |
918 CLAMP_TO_SHORT(cs->predictor); | 912 cs->predictor = av_clip_int16(cs->predictor); |
919 | 913 |
920 cs->step_index = (*src++) & 0x7F; | 914 cs->step_index = (*src++) & 0x7F; |
921 | 915 |
922 if (cs->step_index > 88){ | 916 if (cs->step_index > 88){ |
923 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index); | 917 av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index); |
1185 (current_left_sample * coeff1l) + | 1179 (current_left_sample * coeff1l) + |
1186 (previous_left_sample * coeff2l) + 0x80) >> 8; | 1180 (previous_left_sample * coeff2l) + 0x80) >> 8; |
1187 next_right_sample = (next_right_sample + | 1181 next_right_sample = (next_right_sample + |
1188 (current_right_sample * coeff1r) + | 1182 (current_right_sample * coeff1r) + |
1189 (previous_right_sample * coeff2r) + 0x80) >> 8; | 1183 (previous_right_sample * coeff2r) + 0x80) >> 8; |
1190 CLAMP_TO_SHORT(next_left_sample); | 1184 next_left_sample = av_clip_int16(next_left_sample); |
1191 CLAMP_TO_SHORT(next_right_sample); | 1185 next_right_sample = av_clip_int16(next_right_sample); |
1192 | 1186 |
1193 previous_left_sample = current_left_sample; | 1187 previous_left_sample = current_left_sample; |
1194 current_left_sample = next_left_sample; | 1188 current_left_sample = next_left_sample; |
1195 previous_right_sample = current_right_sample; | 1189 previous_right_sample = current_right_sample; |
1196 current_right_sample = next_right_sample; | 1190 current_right_sample = next_right_sample; |
1316 c->status[i].predictor += vpdiff; | 1310 c->status[i].predictor += vpdiff; |
1317 | 1311 |
1318 c->status[i].step_index += table[delta & (~signmask)]; | 1312 c->status[i].step_index += table[delta & (~signmask)]; |
1319 | 1313 |
1320 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88); | 1314 c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88); |
1321 c->status[i].predictor = av_clip(c->status[i].predictor, -32768, 32767); | 1315 c->status[i].predictor = av_clip_int16(c->status[i].predictor); |
1322 | 1316 |
1323 *samples++ = c->status[i].predictor; | 1317 *samples++ = c->status[i].predictor; |
1324 if (samples >= samples_end) { | 1318 if (samples >= samples_end) { |
1325 av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n"); | 1319 av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n"); |
1326 return -1; | 1320 return -1; |
1390 if(n&1) sampledat= *src++ <<28; | 1384 if(n&1) sampledat= *src++ <<28; |
1391 else sampledat= (*src&0xF0)<<24; | 1385 else sampledat= (*src&0xF0)<<24; |
1392 | 1386 |
1393 sampledat = ((prev[ch][0]*factor1 | 1387 sampledat = ((prev[ch][0]*factor1 |
1394 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp); | 1388 + prev[ch][1]*factor2) >> 11) + (sampledat>>exp); |
1395 CLAMP_TO_SHORT(sampledat); | 1389 sampledat = av_clip_int16(sampledat); |
1396 *samples = sampledat; | 1390 *samples = sampledat; |
1397 prev[ch][1] = prev[ch][0]; | 1391 prev[ch][1] = prev[ch][0]; |
1398 prev[ch][0] = *samples++; | 1392 prev[ch][0] = *samples++; |
1399 | 1393 |
1400 /* In case of stereo, skip one sample, this sample | 1394 /* In case of stereo, skip one sample, this sample |