aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac1_bitalloc.h
blob: ca01b13997a626bb7e042ebe59c0c08117693229 (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
/*
 * This file is part of AtracDEnc.
 *
 * AtracDEnc is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * AtracDEnc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with AtracDEnc; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#pragma once
#include "atrac_scale.h"
#include "../aea.h"
#include <vector>
#include <map>
#include <cstdint>

namespace NAtracDEnc {
namespace NAtrac1 {

using NAtracDEnc::TScaledBlock;

class IAtrac1BitAlloc {
public:
    IAtrac1BitAlloc() {};
    virtual ~IAtrac1BitAlloc() {};
    virtual uint32_t Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize, float loudness) = 0;
};

class TBitsBooster {
    std::multimap<uint32_t, uint32_t> BitsBoostMap; //bits needed -> position
    uint32_t MaxBitsPerIteration;
    uint32_t MinKey;
public:
    TBitsBooster();
    uint32_t ApplyBoost(std::vector<uint32_t>* bitsPerEachBlock, uint32_t cur, uint32_t target);
};

class TAtrac1BitStreamWriter {
    ICompressedOutput* Container;
public:
    explicit TAtrac1BitStreamWriter(ICompressedOutput* container);

    void WriteBitStream(const std::vector<uint32_t>& bitsPerEachBlock, const std::vector<TScaledBlock>& scaledBlocks,
                        uint32_t bfuAmountIdx, const TBlockSize& blockSize);
};

class TAtrac1SimpleBitAlloc : public TAtrac1BitStreamWriter, public TBitsBooster, public virtual IAtrac1BitAlloc {
    std::vector<uint32_t> CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks, const uint32_t bfuNum,
                                             const float spread, const float shift, const TBlockSize& blockSize,
                                             const float loudness);
    const uint32_t BfuIdxConst;
    const bool FastBfuNumSearch;
    static std::vector<float> ATHLong;

    uint32_t GetMaxUsedBfuId(const std::vector<uint32_t>& bitsPerEachBlock);
    uint32_t CheckBfuUsage(bool* changed, uint32_t curBfuId, const std::vector<uint32_t>& bitsPerEachBlock);
public:
    TAtrac1SimpleBitAlloc(ICompressedOutput* container, uint32_t bfuIdxConst, bool fastBfuNumSearch);
    ~TAtrac1SimpleBitAlloc() {};
    uint32_t Write(const std::vector<TScaledBlock>& scaledBlocks, const TBlockSize& blockSize, float loudness) override;
};

} //namespace NAtrac1
} //namespace NAtracDEnc