File: SampleDecoderLinear.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (187 lines) | stat: -rw-r--r-- 6,808 bytes parent folder | download
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*************************************************************************
 SampleDecoderLinear.cpp  -  decoder for all non-compressed linear formats
                             -------------------
    begin                : Sat Nov 01 2003
    copyright            : (C) 2003 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <QtGlobal>

#include "libkwave/Sample.h"
#include "libkwave/SampleFormat.h"
#include "libkwave/Utils.h"

#include "SampleDecoderLinear.h"

//***************************************************************************
static void decode_NULL(const quint8 *src, sample_t *dst, unsigned int count)
{
    while (count--) {
        qWarning("%02X ", Kwave::toUint(*src));
        *(dst++) = count % (1 << (SAMPLE_BITS-1));
    }
}

//***************************************************************************
// this little function is provided as inline code to avaid a compiler
// warning about negative shift value when included directly
static inline quint32 shl(const quint32 v, const int s)
{
    if (!s)
        return v;
    else if (s > 0)
        return (v << s);
    else
        return (v >> (-s));
}

//***************************************************************************
/**
 * Template for decoding a buffer with linear samples. The tricky part is
 * done in the compiler which optimizes away all unused parts of current
 * variant and does nice loop optimizing!
 * @param src array with raw data
 * @param dst array that receives the samples in Kwave's format
 * @param count the number of samples to be decoded
 */
template<const unsigned int bits, const bool is_signed,
         const bool is_little_endian>
void decode_linear(const quint8 *src, sample_t *dst, unsigned int count)
{
    const int shift = (SAMPLE_BITS - bits);
    const quint32 sign = 1 << (SAMPLE_BITS-1);
    const quint32 negative = ~(sign - 1);
    const quint32 bytes = (bits+7) >> 3;

    while (count) {
        count--;

        // read from source buffer
        quint32 s = 0;
        if (is_little_endian) {
            // little endian
            for (unsigned int byte = 0; byte < bytes; ++byte, ++src) {
                s |= static_cast<quint8>(*src) << (byte << 3);
            }
        } else {
            // big endian
            for (int byte = bytes - 1; byte >= 0; --byte, ++src) {
                s |= static_cast<quint8>(*src) << (byte << 3);
            }
        }

        // convert to signed
        if (!is_signed) s -= shl(1, bits-1)-1;

        // shift up to Kwave's bit count
        s = shl(s, shift);

        // sign correcture for negative values
        if (is_signed && (s & sign)) s |= negative;

        // write to destination buffer
        *(dst++) = static_cast<sample_t>(s);
    }
}

//***************************************************************************
#define MAKE_DECODER(bits)                             \
if (sample_format != Kwave::SampleFormat::Unsigned) {  \
    if (endianness != Kwave::BigEndian) {              \
        m_decoder = decode_linear<bits, true, true>;   \
    } else {                                           \
        m_decoder = decode_linear<bits, true, false>;  \
    }                                                  \
} else {                                               \
    if (endianness != Kwave::BigEndian) {              \
        m_decoder = decode_linear<bits, false, true>;  \
    } else {                                           \
        m_decoder = decode_linear<bits, false, false>; \
    }                                                  \
}

//***************************************************************************
Kwave::SampleDecoderLinear::SampleDecoderLinear(
    Kwave::SampleFormat::Format sample_format,
    unsigned int bits_per_sample,
    Kwave::byte_order_t endianness
)
    :Kwave::SampleDecoder(),
     m_bytes_per_sample((bits_per_sample + 7) >> 3),
     m_decoder(decode_NULL)
{
    // sanity checks: we support only signed/unsigned and big/little endian
    Q_ASSERT((sample_format == Kwave::SampleFormat::Signed) ||
             (sample_format == Kwave::SampleFormat::Unsigned));
    if ((sample_format != Kwave::SampleFormat::Signed) &&
        (sample_format != Kwave::SampleFormat::Unsigned)) return;

    // allow unknown endianness only with 8 bits
    Q_ASSERT((endianness != Kwave::UnknownEndian) || (m_bytes_per_sample == 1));
    if ((endianness == Kwave::UnknownEndian) &&
        (m_bytes_per_sample != 1)) return;

    // map cpu endianness to little or big
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
    if (endianness == Kwave::CpuEndian) endianness = Kwave::BigEndian;
#else
    if (endianness == Kwave::CpuEndian) endianness = Kwave::LittleEndian;
#endif

    switch (m_bytes_per_sample) {
        case 1:
            MAKE_DECODER(8)
            break;
        case 2:
            MAKE_DECODER(16)
            break;
        case 3:
            MAKE_DECODER(24)
            break;
        case 4:
            MAKE_DECODER(32)
            break;
        DEFAULT_IMPOSSIBLE;
    }
}

//***************************************************************************
Kwave::SampleDecoderLinear::~SampleDecoderLinear()
{
}

//***************************************************************************
void Kwave::SampleDecoderLinear::decode(QByteArray &raw_data,
                                        Kwave::SampleArray &decoded)
{
    Q_ASSERT(m_decoder);
    if (!m_decoder) return;

    unsigned int raw_size = static_cast<unsigned int>(raw_data.size());
    unsigned int samples = raw_size / m_bytes_per_sample;
    const quint8 *src  = reinterpret_cast<const quint8 *>(raw_data.constData());
    sample_t *dst = decoded.data();

    m_decoder(src, dst, samples);
}

//***************************************************************************
unsigned int Kwave::SampleDecoderLinear::rawBytesPerSample()
{
    return m_bytes_per_sample;
}

//***************************************************************************
//***************************************************************************