aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac1_bitalloc.cpp
blob: d80ea9ac06124557815190d0102075ec2bab1be0 (plain) (blame)
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
#include "atrac1_bitalloc.h"
#include "atrac1_scale.h"
#include "atrac1.h"
#include <math.h>
#include "../bitstream/bitstream.h"
namespace NAtrac1 {

using std::vector;
using std::cerr;
using std::endl;

static const uint32_t FixedBitAllocTableLong[MAX_BFUS] = {
    6, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6,
    6, 6, 5, 5,  5, 5, 5, 5,  5,  5,  5,  5,  5,  4, 4,  4, 5,
    5, 4, 4,  3,  3,  3, 3, 2,  0,  0,  0,  0,  0,  0,  0
};

//returns 1 for tone-like, 0 - noise-like
static double AnalizeSpread(const std::vector<TScaledBlock>& scaledBlocks) {
    double s = 0.0;
    for (int i = 0; i < scaledBlocks.size(); ++i) {
        s += scaledBlocks[i].ScaleFactorIndex;
    }
    s /= scaledBlocks.size();
    double sigma = 0.0;
    double xxx = 0.0;
    for (int i = 0; i < scaledBlocks.size(); ++i) {
        xxx = (scaledBlocks[i].ScaleFactorIndex - s);
        xxx *= xxx;
        sigma += xxx;
    }
    sigma /= scaledBlocks.size();
    sigma = sqrt(sigma);
    if (sigma > 14.0)
        sigma = 14.0;
    return sigma/14.0;
}

vector<uint32_t> TAtrac1SimpleBitAlloc::CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const double spread, const double shift) {
    vector<uint32_t> bitsPerEachBlock;
    bitsPerEachBlock.resize(scaledBlocks.size());
    for (int i = 0; i < scaledBlocks.size(); ++i) {
        int tmp = spread * ( (double)scaledBlocks[i].ScaleFactorIndex/5.2) + (1.0 - spread) * (FixedBitAllocTableLong[i] + 1) - shift;
        if (tmp > 16) {
            bitsPerEachBlock[i] = 16;
        } else if (tmp < 2) {
            bitsPerEachBlock[i] = 0;
        } else {
            bitsPerEachBlock[i] = tmp;
        }
    }
    return bitsPerEachBlock;	
}

uint32_t TAtrac1SimpleBitAlloc::Write(const std::vector<TScaledBlock>& scaledBlocks) {
    uint32_t bfuIdx = 7;
    vector<uint32_t> bitsPerEachBlock;
    double spread = AnalizeSpread(scaledBlocks);
    bitsPerEachBlock.resize(scaledBlocks.size());
    const uint32_t bitsAvaliablePerBfus =  SoundUnitSize * 8 - BitsPerBfuAmountTabIdx - 32 - 2 - 3 - bitsPerEachBlock.size() * (BitsPerIDWL + BitsPerIDSF);
    double maxShift = 4;
    double minShift = -2;
    double shift = 3.0;
    const uint32_t maxBits = bitsAvaliablePerBfus;
    const uint32_t minBits = bitsAvaliablePerBfus - 110;
 
    for(;;) {   
        const vector<uint32_t>& tmpAlloc = CalcBitsAllocation(scaledBlocks, spread, shift);
        uint32_t bitsUsed = 0;
        for (int i = 0; i < tmpAlloc.size(); i++) {
            bitsUsed += SpecsPerBlock[i] * tmpAlloc[i];
        }

        //std::cout << spread << " bitsUsed: " << bitsUsed << " min " << minBits << " max " << maxBits << endl;
        if (bitsUsed < minBits) {
            if (maxShift - minShift < 0.1) {
                bitsPerEachBlock = tmpAlloc;
                break;
            }
            maxShift = shift;
            shift -= (shift - minShift) / 2;
        } else if (bitsUsed > maxBits) {
            minShift = shift;
            shift += (maxShift - shift) / 2;
        } else {
            bitsPerEachBlock = tmpAlloc;
            break;
        }
    }
    WriteBitStream(bitsPerEachBlock, scaledBlocks, bfuIdx);
    return BfuAmountTab[bfuIdx];
}

void TAtrac1BitStreamWriter::WriteBitStream(const vector<uint32_t>& bitsPerEachBlock, const std::vector<TScaledBlock>& scaledBlocks, uint32_t bfuAmountIdx) {
    NBitStream::TBitStream bitStream;
    int bitUsed = 0;
    if (bfuAmountIdx >= (1 << BitsPerBfuAmountTabIdx)) {
        cerr << "Wrong bfuAmountIdx (" << bfuAmountIdx << "), frame skiped" << endl;
        return;
    }
    bitStream.Write(0x2, 2);
    bitUsed+=2;

    bitStream.Write(0x2, 2);
    bitUsed+=2;

    bitStream.Write(0x3, 2);
    bitStream.Write(0, 2);
    bitUsed+=4;

    bitStream.Write(bfuAmountIdx, BitsPerBfuAmountTabIdx);
    bitUsed += BitsPerBfuAmountTabIdx;

    bitStream.Write(0, 2);
    bitStream.Write(0, 3);
    bitUsed+= 5;

    for (const auto wordLength : bitsPerEachBlock) {
        const auto tmp = wordLength ? (wordLength - 1) : 0;
        bitStream.Write(tmp, 4);
        bitUsed+=4;
    }
    for (int i = 0; i < bitsPerEachBlock.size(); ++i) {
        bitStream.Write(scaledBlocks[i].ScaleFactorIndex, 6);
        bitUsed+=6;
    }
    for (int i = 0; i < bitsPerEachBlock.size(); ++i) {
        const auto wordLength = bitsPerEachBlock[i];
        if (wordLength == 0 || wordLength == 1)
            continue;

        const double multiple = ((1 << (wordLength - 1)) - 1);
        for (const double val : scaledBlocks[i].Values) {
            const int tmp = val * multiple;
            const int testwl = bitsPerEachBlock[i] ? (bitsPerEachBlock[i] - 1) : 0;
            const int a = !!testwl + testwl;
            if (a != wordLength) {
                cerr << "wordlen error " << a << " " << wordLength << endl;
                abort();
            }
            bitStream.Write(NBitStream::MakeSign(tmp, wordLength), wordLength);
            bitUsed+=wordLength;
        }
    }

    bitStream.Write(0x0, 8);
    bitStream.Write(0x0, 8);

    bitUsed+=16;
    bitStream.Write(0x0, 8);

    bitUsed+=8;
    if (bitUsed > SoundUnitSize * 8) {
        cerr << "ATRAC1 bitstream corrupted, used: " << bitUsed << " exp: " << SoundUnitSize * 8 << endl;
        abort();
    }
    Container->WriteFrame(bitStream.GetBytes());
}

}