OpenCores
URL https://opencores.org/ocsvn/ima_adpcm_enc_dec/ima_adpcm_enc_dec/trunk

Subversion Repositories ima_adpcm_enc_dec

[/] [ima_adpcm_enc_dec/] [trunk/] [scilab/] [ima_adpcm_dec.sci] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 motilito
function out_samp = ima_adpcm_dec(in_pcm)
2
// This function decodes an IMA ADPCM compressed audio. The output is reconstructed
3
// to 16 bits per sample.
4
//
5
// Author: Moti Litochevski
6
// Date: February 17, 2010
7
//
8
 
9
// step quantizer adaptation lookup table
10
STEP_LUT = [ ...
11
                7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, ...
12
                37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, ...
13
                173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, ...
14
                658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, ...
15
                2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, ...
16
                7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, ...
17
                20350, 22385, 24623, 27086, 29794, 32767];
18
// index quantizer adaptation lookup table
19
INDEX_LUT = [-1, -1, -1, -1, 2, 4, 6, 8];
20
 
21
// prepare loop variables
22
predictor_samp = zeros(1, length(in_pcm)+1);
23
qstep_index = ones(1, length(in_pcm)+1);
24
// convert the input coded signal to binary form & calculate the PCM value (without sign)
25
pcm_bin = de2bi(in_pcm, 4);
26
pcm_val = pcm_bin(:,[2:4]) * [4, 2, 1]';
27
 
28
// decoding loop
29
for idx = [1:length(in_pcm)],
30
        // extract the current quantizer step size
31
        qstep_size = STEP_LUT(qstep_index(idx));
32
 
33
        // de-quantizer implementation starts from the middle of the current
34
        // quantization step (qstep_size/8)
35
        dequant_samp = qstep_size;
36
        // de-quantize bit by bit
37
        dequant_samp = dequant_samp + pcm_bin(idx, 2) * qstep_size * 8;
38
        dequant_samp = dequant_samp + pcm_bin(idx, 3) * qstep_size * 4;
39
        dequant_samp = dequant_samp + pcm_bin(idx, 4) * qstep_size * 2;
40
 
41
        // update the predictor output sample according to the sign bit
42
        if (pcm_bin(idx, 1)),
43
                predictor_samp(idx+1) = predictor_samp(idx) - dequant_samp;
44
        else
45
                predictor_samp(idx+1) = predictor_samp(idx) + dequant_samp;
46
        end
47
        // check for predictor sample saturation condition
48
        if (predictor_samp(idx+1) > (2^18-1)),
49
                predictor_samp(idx+1) = 2^18-1;
50
        elseif (predictor_samp(idx+1) < -2^18),
51
                predictor_samp(idx+1) = -2^18;
52
        end
53
 
54
        // update the step size index
55
        qstep_index(idx+1) = qstep_index(idx) + INDEX_LUT(pcm_val(idx)+1);
56
        // check index saturation conditions
57
        if (qstep_index(idx+1) < 1)
58
                qstep_index(idx+1) = 1;
59
        elseif (qstep_index(idx+1) > 89)
60
                qstep_index(idx+1) = 89;
61
        end
62
end
63
 
64
// output sample is just the saturated & scaled predictor output
65
out_samp = predictor_samp(2:$)/8;
66
// implement rounding
67
out_samp = round(out_samp);
68
cor_idx = find((out_samp - predictor_samp(2:$)/8) == -0.5);
69
out_samp(cor_idx) = out_samp(cor_idx) + 1;
70
// check for saturation
71
out_samp(find(predictor_samp(2:$)/8 > 32767)) = 32767;
72
out_samp(find(predictor_samp(2:$)/8 < -32768)) = -32768;
73
 
74
endfunction

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.