1
1
#include " atrac1_bitalloc.h"
2
+ #include " atrac_psy_common.h"
2
3
#include " atrac_scale.h"
3
4
#include " atrac1.h"
4
5
#include < math.h>
5
6
#include < cassert>
6
7
#include " ../bitstream/bitstream.h"
8
+
9
+ namespace NAtracDEnc {
7
10
namespace NAtrac1 {
8
11
9
12
using std::vector;
10
13
using std::cerr;
11
14
using std::endl;
12
15
using std::pair;
13
16
14
- static const uint32_t FixedBitAllocTableLong[MAX_BFUS ] = {
17
+ static const uint32_t FixedBitAllocTableLong[TAtrac1BitStreamWriter::MaxBfus ] = {
15
18
7 , 7 , 7 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 6 ,
16
19
6 , 6 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 4 ,
17
20
4 , 4 , 3 , 3 , 3 , 3 , 3 , 3 , 2 , 2 , 2 , 1 , 1 , 0 , 0 , 0
18
21
};
19
22
20
- static const uint32_t FixedBitAllocTableShort[MAX_BFUS ] = {
23
+ static const uint32_t FixedBitAllocTableShort[TAtrac1BitStreamWriter::MaxBfus ] = {
21
24
6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 ,
22
25
6 , 6 , 6 , 6 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 ,
23
26
4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
24
27
};
25
28
26
- static const uint32_t BitBoostMask[MAX_BFUS ] = {
29
+ static const uint32_t BitBoostMask[TAtrac1BitStreamWriter::MaxBfus ] = {
27
30
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 ,
28
31
1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 ,
29
32
1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
30
33
};
31
34
32
- // returns 1 for tone-like, 0 - noise-like
33
- static double AnalizeSpread (const std::vector<TScaledBlock>& scaledBlocks) {
34
- double s = 0.0 ;
35
- for (size_t i = 0 ; i < scaledBlocks.size (); ++i) {
36
- s += scaledBlocks[i].ScaleFactorIndex ;
37
- }
38
- s /= scaledBlocks.size ();
39
- double sigma = 0.0 ;
40
- double xxx = 0.0 ;
41
- for (size_t i = 0 ; i < scaledBlocks.size (); ++i) {
42
- xxx = (scaledBlocks[i].ScaleFactorIndex - s);
43
- xxx *= xxx;
44
- sigma += xxx;
45
- }
46
- sigma /= scaledBlocks.size ();
47
- sigma = sqrt (sigma);
48
- if (sigma > 14.0 )
49
- sigma = 14.0 ;
50
- return sigma/14.0 ;
51
- }
52
-
53
35
TBitsBooster::TBitsBooster () {
54
- for (uint32_t i = 0 ; i < MAX_BFUS ; ++i) {
36
+ for (uint32_t i = 0 ; i < MaxBfus ; ++i) {
55
37
if (BitBoostMask[i] == 0 )
56
38
continue ;
57
39
const uint32_t nBits = SpecsPerBlock[i];
@@ -68,15 +50,13 @@ uint32_t TBitsBooster::ApplyBoost(std::vector<uint32_t>* bitsPerEachBlock, uint3
68
50
// the key too low
69
51
if (maxIt == BitsBoostMap.begin ())
70
52
return surplus;
71
- // std::cout << "key: " << key << " min key: " << MinKey << " it pos: " << maxIt->first << endl;
72
53
73
54
while (surplus >= MinKey) {
74
55
bool done = true ;
75
56
for (std::multimap<uint32_t , uint32_t >::iterator it = BitsBoostMap.begin (); it != maxIt; ++it) {
76
57
const uint32_t curBits = it->first ;
77
58
const uint32_t curPos = it->second ;
78
59
79
- // std::cout << "key: " << key << " curBits: " << curBits << endl;
80
60
assert (key >= curBits);
81
61
if (curPos >= bitsPerEachBlock->size ())
82
62
break ;
@@ -90,23 +70,25 @@ uint32_t TBitsBooster::ApplyBoost(std::vector<uint32_t>* bitsPerEachBlock, uint3
90
70
(*bitsPerEachBlock)[curPos] += nBitsPerSpec;
91
71
surplus -= curBits * nBitsPerSpec;
92
72
93
- // std::cout << "added: " << curPos << " " << nBitsPerSpec << " got: " << (*bitsPerEachBlock)[curPos] << endl;
94
73
done = false ;
95
74
}
96
75
if (done)
97
76
break ;
98
77
}
99
78
100
- // std::cout << "boost: " << surplus << " was " << target - cur << endl;
101
79
return surplus;
102
80
}
103
81
104
82
105
- vector<uint32_t > TAtrac1SimpleBitAlloc::CalcBitsAllocation (const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum, const double spread, const double shift, const TBlockSize& blockSize) {
83
+ vector<uint32_t > TAtrac1SimpleBitAlloc::CalcBitsAllocation (const std::vector<TScaledBlock>& scaledBlocks,
84
+ const uint32_t bfuNum,
85
+ const TFloat spread,
86
+ const TFloat shift,
87
+ const TBlockSize& blockSize) {
106
88
vector<uint32_t > bitsPerEachBlock (bfuNum);
107
89
for (size_t i = 0 ; i < bitsPerEachBlock.size (); ++i) {
108
90
const uint32_t fix = blockSize.LogCount [BfuToBand (i)] ? FixedBitAllocTableShort[i] : FixedBitAllocTableLong[i];
109
- int tmp = spread * ( (double )scaledBlocks[i].ScaleFactorIndex /3.2 ) + (1.0 - spread) * fix - shift;
91
+ int tmp = spread * ( (TFloat )scaledBlocks[i].ScaleFactorIndex /3.2 ) + (1.0 - spread) * fix - shift;
110
92
if (tmp > 16 ) {
111
93
bitsPerEachBlock[i] = 16 ;
112
94
} else if (tmp < 2 ) {
@@ -143,40 +125,43 @@ uint32_t TAtrac1SimpleBitAlloc::GetMaxUsedBfuId(const vector<uint32_t>& bitsPerE
143
125
return idx;
144
126
}
145
127
146
- uint32_t TAtrac1SimpleBitAlloc::CheckBfuUsage (bool * changed, uint32_t curBfuId, const vector<uint32_t >& bitsPerEachBlock) {
128
+ uint32_t TAtrac1SimpleBitAlloc::CheckBfuUsage (bool * changed,
129
+ uint32_t curBfuId, const vector<uint32_t >& bitsPerEachBlock) {
147
130
uint32_t usedBfuId = GetMaxUsedBfuId (bitsPerEachBlock);
148
131
if (usedBfuId < curBfuId) {
149
132
*changed = true ;
150
133
curBfuId = FastBfuNumSearch ? usedBfuId : (curBfuId - 1 );
151
134
}
152
135
return curBfuId;
153
136
}
137
+
154
138
uint32_t TAtrac1SimpleBitAlloc::Write (const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize) {
155
139
uint32_t bfuIdx = BfuIdxConst ? BfuIdxConst - 1 : 7 ;
156
140
bool autoBfu = !BfuIdxConst;
157
- double spread = AnalizeSpread (scaledBlocks);
141
+ TFloat spread = AnalizeScaleFactorSpread (scaledBlocks);
158
142
159
143
vector<uint32_t > bitsPerEachBlock (BfuAmountTab[bfuIdx]);
160
144
uint32_t targetBitsPerBfus;
161
145
uint32_t curBitsPerBfus;
162
146
for (;;) {
163
147
bitsPerEachBlock.resize (BfuAmountTab[bfuIdx]);
164
- const uint32_t bitsAvaliablePerBfus = SoundUnitSize * 8 - BitsPerBfuAmountTabIdx - 32 - 2 - 3 - bitsPerEachBlock.size () * (BitsPerIDWL + BitsPerIDSF);
165
- double maxShift = 15 ;
166
- double minShift = -3 ;
167
- double shift = 3.0 ;
148
+ const uint32_t bitsAvaliablePerBfus = SoundUnitSize * 8 - BitsPerBfuAmountTabIdx - 32 - 2 - 3 -
149
+ bitsPerEachBlock.size () * (BitsPerIDWL + BitsPerIDSF);
150
+ TFloat maxShift = 15 ;
151
+ TFloat minShift = -3 ;
152
+ TFloat shift = 3.0 ;
168
153
const uint32_t maxBits = bitsAvaliablePerBfus;
169
154
const uint32_t minBits = bitsAvaliablePerBfus - 110 ;
170
155
171
156
bool bfuNumChanged = false ;
172
157
for (;;) {
173
- const vector<uint32_t >& tmpAlloc = CalcBitsAllocation (scaledBlocks, BfuAmountTab[bfuIdx], spread, shift, blockSize);
158
+ const vector<uint32_t >& tmpAlloc = CalcBitsAllocation (scaledBlocks, BfuAmountTab[bfuIdx],
159
+ spread, shift, blockSize);
174
160
uint32_t bitsUsed = 0 ;
175
161
for (size_t i = 0 ; i < tmpAlloc.size (); i++) {
176
162
bitsUsed += SpecsPerBlock[i] * tmpAlloc[i];
177
163
}
178
164
179
- // std::cout << spread << " bitsUsed: " << bitsUsed << " min " << minBits << " max " << maxBits << " " << maxShift << " " << minShift << " " << endl;
180
165
if (bitsUsed < minBits) {
181
166
if (maxShift - minShift < 0.1 ) {
182
167
if (autoBfu) {
@@ -214,7 +199,10 @@ uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlo
214
199
return BfuAmountTab[bfuIdx];
215
200
}
216
201
217
- void TAtrac1BitStreamWriter::WriteBitStream (const vector<uint32_t >& bitsPerEachBlock, const std::vector<TScaledBlock>& scaledBlocks, uint32_t bfuAmountIdx, const TBlockSize& blockSize) {
202
+ void TAtrac1BitStreamWriter::WriteBitStream (const vector<uint32_t >& bitsPerEachBlock,
203
+ const std::vector<TScaledBlock>& scaledBlocks,
204
+ uint32_t bfuAmountIdx,
205
+ const TBlockSize& blockSize) {
218
206
NBitStream::TBitStream bitStream;
219
207
size_t bitUsed = 0 ;
220
208
if (bfuAmountIdx >= (1 << BitsPerBfuAmountTabIdx)) {
@@ -252,8 +240,8 @@ void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachB
252
240
if (wordLength == 0 || wordLength == 1 )
253
241
continue ;
254
242
255
- const double multiple = ((1 << (wordLength - 1 )) - 1 );
256
- for (const double val : scaledBlocks[i].Values ) {
243
+ const TFloat multiple = ((1 << (wordLength - 1 )) - 1 );
244
+ for (const TFloat val : scaledBlocks[i].Values ) {
257
245
const int tmp = round (val * multiple);
258
246
const uint32_t testwl = bitsPerEachBlock[i] ? (bitsPerEachBlock[i] - 1 ) : 0 ;
259
247
const uint32_t a = !!testwl + testwl;
@@ -280,4 +268,5 @@ void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachB
280
268
Container->WriteFrame (bitStream.GetBytes ());
281
269
}
282
270
283
- }
271
+ } // namespace NAtrac1
272
+ } // namespace NAtracDEnc
0 commit comments