1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000-2009  Josh Coalson
3  * Copyright (C) 2011-2016  Xiph.Org Foundation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Xiph.org Foundation nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <math.h>
34 #include <string.h>
35 #include "share/compat.h"
36 #include "private/bitmath.h"
37 #include "private/fixed.h"
38 #include "private/macros.h"
39 
40 #ifdef local_abs
41 #undef local_abs
42 #endif
43 #define local_abs(x) ((uint32_t)((x)<0? -(x) : (x)))
44 
FLAC__fixed_compute_residual(const FLAC__int32 data[],uint32_t data_len,uint32_t order,FLAC__int32 residual[])45 void FLAC__fixed_compute_residual(const FLAC__int32 data[], uint32_t data_len, uint32_t order, FLAC__int32 residual[])
46 {
47 	const int idata_len = (int)data_len;
48 	int i;
49 
50 	switch(order) {
51 		case 0:
52 			memcpy(residual, data, sizeof(residual[0])*data_len);
53 			break;
54 		case 1:
55 			for(i = 0; i < idata_len; i++)
56 				residual[i] = data[i] - data[i-1];
57 			break;
58 		case 2:
59 			for(i = 0; i < idata_len; i++)
60 				residual[i] = data[i] - 2*data[i-1] + data[i-2];
61 			break;
62 		case 3:
63 			for(i = 0; i < idata_len; i++)
64 				residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
65 			break;
66 		case 4:
67 			for(i = 0; i < idata_len; i++)
68 				residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
69 			break;
70 		default: break;
71 	}
72 }
73 
FLAC__fixed_restore_signal(const FLAC__int32 residual[],uint32_t data_len,uint32_t order,FLAC__int32 data[])74 void FLAC__fixed_restore_signal(const FLAC__int32 residual[], uint32_t data_len, uint32_t order, FLAC__int32 data[])
75 {
76 	int i, idata_len = (int)data_len;
77 
78 	switch(order) {
79 		case 0:
80 			memcpy(data, residual, sizeof(residual[0])*data_len);
81 			break;
82 		case 1:
83 			for(i = 0; i < idata_len; i++)
84 				data[i] = residual[i] + data[i-1];
85 			break;
86 		case 2:
87 			for(i = 0; i < idata_len; i++)
88 				data[i] = residual[i] + 2*data[i-1] - data[i-2];
89 			break;
90 		case 3:
91 			for(i = 0; i < idata_len; i++)
92 				data[i] = residual[i] + 3*data[i-1] - 3*data[i-2] + data[i-3];
93 			break;
94 		case 4:
95 			for(i = 0; i < idata_len; i++)
96 				data[i] = residual[i] + 4*data[i-1] - 6*data[i-2] + 4*data[i-3] - data[i-4];
97 			break;
98 		default: break;
99 	}
100 }
101