blob: 00b0fb568fde2f74e1a39e4f0049da71e204acf3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/*! @file companding.c
*
* @brief Implementation of the routines for computing the companding curves
* that is applied to the quantized coefficient magnitudes.
*
* @version 1.0.0
*
* (C) Copyright 2018 GoPro Inc (http://gopro.com/).
*
* Licensed under either:
* - Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
* - MIT license, http://opensource.org/licenses/MIT
* at your option.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common.h"
//TODO: Change the companding parameter into a global constant
#ifndef COMPANDING
#define COMPANDING 1
#define COMPANDING_MORE (54) // Zero means no companding (54 is a good value)
#endif
/*!
@brief Maximum coefficient magnitude in the codebook
@todo Need to calculate the maximum value from the codebook
*/
const int maximum_codebook_value = 255;
/*!
@brief Apply the default companding curve to the specified value
Note that this companding curve has been superceeded by the cubic curve.
*/
int32_t CompandedValue(int32_t value)
{
const int midpoint_rounding = 2;
int32_t magnitude = absolute(value);
#if COMPANDING
if (magnitude >= 40)
{
magnitude -= 40;
magnitude += midpoint_rounding;
magnitude >>= 2;
magnitude += 40;
#if COMPANDING_MORE
if (magnitude >= COMPANDING_MORE)
{
magnitude -= COMPANDING_MORE;
magnitude += midpoint_rounding;
magnitude >>= 2;
magnitude += COMPANDING_MORE;
}
#endif
}
#endif
// Restore the sign to the companded value
return ((value >= 0) ? magnitude : neg(magnitude));
}
/*!
@brief Return the parameter that controls the companding curve
This parameter does not apply if the cubic companding curve is used.
*/
uint32_t CompandingParameter()
{
return COMPANDING_MORE;
}
/*!
@brief Compute a table of values for the cubic companding curve
The companding curve is f(x) = x + (x ^ 3 / (255 ^ 3)) * 768
so the range of coefficient magnitudes from 0 to 255 becomess 0 to 1023.
*/
CODEC_ERROR ComputeCubicTable(int16_t cubic_table[], int cubic_table_length, int16_t maximum_value)
{
size_t cubic_table_size = cubic_table_length * sizeof(cubic_table[0]);
int last_cubic_table_index = cubic_table_length - 2;
int16_t last_magnitude;
int16_t index;
// Clear the cubic table
memset(cubic_table, 0, cubic_table_size);
for (index = 1; index <= maximum_value; index++)
{
double cubic = index;
int magnitude = index;
cubic *= index;
cubic *= index;
cubic *= 768;
//cubic /= 256*256*256;
cubic /= 255*255*255;
magnitude += (int)cubic;
//if (mag > 1023) mag = 1023;
if (magnitude > last_cubic_table_index) {
magnitude = last_cubic_table_index;
}
cubic_table[magnitude] = index;
}
// Fill unused entries in the cubic table
last_magnitude = 0;
for (index = 0; index < cubic_table_length; index++)
{
if (cubic_table[index])
{
last_magnitude = cubic_table[index];
}
else
{
cubic_table[index] = last_magnitude;
}
}
return CODEC_ERROR_OKAY;
}
/*!
@brief Invert the companding curve applied during encoding
*/
int32_t UncompandedValue(int32_t value)
{
#if 1 //CUBIC_COMPANDING
double cubic;
int32_t magnitude = absolute(value);
cubic = magnitude;
cubic *= magnitude;
cubic *= magnitude;
cubic *= 768;
//cubic /= 256*256*256;
cubic /= 255*255*255;
magnitude += (int32_t)cubic;
// Restore the sign
value = ((value < 0) ? neg(magnitude) : magnitude);
#else
if (40 <= value && value < 264)
{
#if COMPANDING_MORE
if (value >= COMPANDING_MORE)
{
value -= COMPANDING_MORE;
value <<= 2;
value += COMPANDING_MORE;
}
#endif
value -= 40;
value <<= 2;
value += 40;
}
else if (value <= -40)
{
// Apply the inverse companding formula to the absolute value
value = -value;
#if COMPANDING_MORE
if (value >= COMPANDING_MORE)
{
value -= COMPANDING_MORE;
value <<= 2;
value += COMPANDING_MORE;
}
#endif
value -= 40;
value <<= 2;
value += 40;
// Restore the sign
value = -value;
}
#endif
return value;
}
/*!
@brief Invert the companding curve applied to a pixel
*/
PIXEL UncompandedPixel(PIXEL value)
{
#if 1 //CUBIC_COMPANDING
double cubic;
int32_t magnitude = absolute(value);
cubic = magnitude;
cubic *= magnitude;
cubic *= magnitude;
cubic *= 768;
//cubic /= 256*256*256;
cubic /= 255*255*255;
magnitude += (int32_t)cubic;
// Restore the sign
value = ClampPixel((value < 0) ? neg(magnitude) : magnitude);
#else
if (40 <= value && value < 264)
{
#if COMPANDING_MORE
if (value >= COMPANDING_MORE)
{
value -= COMPANDING_MORE;
value <<= 2;
value += COMPANDING_MORE;
}
#endif
value -= 40;
value <<= 2;
value += 40;
}
else if (value <= -40)
{
// Apply the inverse companding formula to the absolute value
value = neg(value);
#if COMPANDING_MORE
if (value >= COMPANDING_MORE)
{
value -= COMPANDING_MORE;
value <<= 2;
value += COMPANDING_MORE;
}
#endif
value -= 40;
value <<= 2;
value += 40;
// Restore the sign
value = neg(value);
}
#endif
return value;
}
|