aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac3_bitstream.h
blob: 24c2ca7ddcb67d4613eaf447d9c01d0fe78581cc (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
/*
 * 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 "atrac3.h"
#include "atrac1.h"
#include "../aea.h"
#include "../oma.h"
#include "../atrac/atrac1.h"
#include "atrac_scale.h"
#include <vector>
#include <utility>
#include "../env.h"

namespace NAtracDEnc {
namespace NAtrac3 {

struct TTonalBlock {
    TTonalBlock(const TAtrac3Data::TTonalVal* valPtr, const TScaledBlock& scaledBlock)
        : ValPtr(valPtr)
        , ScaledBlock(scaledBlock)
    {}
    const TAtrac3Data::TTonalVal* ValPtr = nullptr;
    TScaledBlock ScaledBlock;
};

class TAtrac3BitStreamWriter : public virtual TAtrac3Data {
public:
    struct TSingleChannelElement {
        TAtrac3Data::SubbandInfo SubbandInfo;
        std::vector<TTonalBlock> TonalBlocks;
        std::vector<TScaledBlock> ScaledBlocks;
    };
private:

    struct TTonalComponentsSubGroup {
        std::vector<uint8_t> SubGroupMap;
        std::vector<const TTonalBlock*> SubGroupPtr;
    };
    TOma* Container;
    const TContainerParams Params;
    const uint32_t BfuIdxConst;
    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 TSingleChannelElement& sce,
                                                               uint16_t bitsUsed, int mt[MaxSpecs]);

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

    void EncodeSpecs(const TSingleChannelElement& sce, NBitStream::TBitStream* bitStream,
                     uint16_t bitsUsed);

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

    uint16_t EncodeTonalComponents(const TSingleChannelElement& sce,
                                   const std::vector<uint32_t>& allocTable,
                                   NBitStream::TBitStream* bitStream);
public:
    TAtrac3BitStreamWriter(TOma* container, const TContainerParams& params, uint32_t bfuIdxConst) //no mono mode for atrac3
        : Container(container)
        , Params(params)
        , BfuIdxConst(bfuIdxConst)
    {
        NEnv::SetRoundFloat();
    }

    void WriteSoundUnit(const std::vector<TSingleChannelElement>& singleChannelElements);
};

} // namespace NAtrac3
} // namespace NAtracDEnc