aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac3_bitstream.h
blob: 225d98ca5741c5bc1230c6aefb992f27dbfbaa85 (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
#pragma once
#include "atrac3.h"
#include "atrac1.h"
#include "../aea.h"
#include "../oma.h"
#include "../atrac/atrac1.h"
#include "atrac_scale.h"
#include <vector>
#include <utility>

namespace NAtracDEnc {
namespace NAtrac3 {

struct TTonalComponent {
    TTonalComponent(const TAtrac3Data::TTonalVal* valPtr, uint8_t quantIdx, const TScaledBlock& scaledBlock)
        : ValPtr(valPtr)
        , QuantIdx(quantIdx)
        , ScaledBlock(scaledBlock)
    {}
    const TAtrac3Data::TTonalVal* ValPtr = nullptr;
    uint8_t QuantIdx = 0;
    TScaledBlock ScaledBlock;
};

class TAtrac3BitStreamWriter : public virtual TAtrac3Data {
    struct TTonalComponentsSubGroup {
        std::vector<uint8_t> SubGroupMap;
        std::vector<const TTonalComponent*> SubGroupPtr;
    };
    TOma* Container;
    const TContainerParams Params;
    std::vector<char> OutBuffer;

    uint32_t CLCEnc(const uint32_t selector, const int mantissas[MaxSpecsPerBlock],
                    const uint32_t blockSize, NBitStream::TBitStream* bitStream);

    uint32_t VLCEnc(const uint32_t selector, const int mantissas[MaxSpecsPerBlock],
                    const uint32_t blockSize, NBitStream::TBitStream* bitStream);

    std::vector<uint32_t> CalcBitsAllocation(const std::vector<TScaledBlock>& scaledBlocks,
                                             uint32_t bfuNum, TFloat spread, TFloat shift);

    std::pair<uint8_t, std::vector<uint32_t>> CreateAllocation(const std::vector<TScaledBlock>& scaledBlocks,
                                                               uint16_t bitsUsed, int mt[MaxSpecs]);

    std::pair<uint8_t, uint32_t> CalcSpecsBitsConsumption(const std::vector<TScaledBlock>& scaledBlocks,
                                                          const std::vector<uint32_t>& precisionPerEachBlocks,
                                                          int* mantisas);

    void EncodeSpecs(const std::vector<TScaledBlock>& scaledBlocks, NBitStream::TBitStream* bitStream,
                     uint16_t bitsUsed);

    uint8_t GroupTonalComponents(const std::vector<TTonalComponent>& tonalComponents,
                                 TTonalComponentsSubGroup groups[64]);

    uint16_t EncodeTonalComponents(const std::vector<TTonalComponent>& tonalComponents,
                                   NBitStream::TBitStream* bitStream, uint8_t numQmfBand);
public:
    TAtrac3BitStreamWriter(TOma* container, const TContainerParams& params) //no mono mode for atrac3
        : Container(container)
        , Params(params)
    {

    }
    void WriteSoundUnit(const TAtrac3Data::SubbandInfo& subbandInfo,
                        const std::vector<TTonalComponent>& tonalComponents,
                        const std::vector<TScaledBlock>& scaledBlocks);
};

} // namespace NAtrac3
} // namespace NAtracDEnc