File: SampleEncoderLinear.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 (193 lines) | stat: -rw-r--r-- 7,153 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
188
189
190
191
192
193
/*************************************************************************
 SampleEncoderLinear.cpp  -  encoder for all non-compressed linear formats
                             -------------------
    begin                : Tue Apr 18 2006
    copyright            : (C) 2006 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/SampleEncoderLinear.h"
#include "libkwave/SampleFormat.h"
#include "libkwave/Utils.h"

//***************************************************************************
static void encode_NULL(const sample_t *src, quint8 *dst, unsigned int count)
{
    Q_UNUSED(src)
    Q_UNUSED(dst)
    Q_UNUSED(count)
//     qWarning("call to encode_NULL");
}

//***************************************************************************
/**
 * Template for encoding 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 samples in Kwave's format
 * @param dst array that receives the raw data
 * @param count the number of samples to be encoded
 */
template<const unsigned int bits, const bool is_signed,
         const bool is_little_endian>
void encode_linear(const sample_t *src, quint8 *dst, unsigned int count)
{
    for ( ; count; --count) {
        // read from source buffer
        sample_t s = *(src++);

        // convert to unsigned if necessary
        if (!is_signed)
            s += 1 << (SAMPLE_BITS - 1);

        // shrink 18/20 bits and similar down, otherwise it does not work
        // with ALSA for some dubious reason !?
        if (bits == 20)
            s >>= 4;
        if (bits == 18) // don't ask me why... !!!???
            s >>= 6;

        if (is_little_endian) {
            // little endian
            if (bits > 24)
                *(dst++) = 0x00;
            if (bits > 16)
                *(dst++) = static_cast<quint8>(s & 0xFF);
            if (bits > 8)
                *(dst++) = static_cast<quint8>(s >> 8);
            if (bits >= 8)
                *(dst++) = static_cast<quint8>(s >> 16);
        } else {
            // big endian
            if (bits >= 8)
                *(dst++) = static_cast<quint8>(s >> 16);
            if (bits > 8)
                *(dst++) = static_cast<quint8>(s >> 8);
            if (bits > 16)
                *(dst++) = static_cast<quint8>(s & 0xFF);
            if (bits > 24)
                *(dst++) = 0x00;
        }
    }
}

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

//***************************************************************************
Kwave::SampleEncoderLinear::SampleEncoderLinear(
    Kwave::SampleFormat::Format sample_format,
    unsigned int bits_per_sample,
    Kwave::byte_order_t endianness
)
    :SampleEncoder(),
     m_bytes_per_sample((bits_per_sample + 7) >> 3),
     m_encoder(encode_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

//     qDebug("SampleEncoderLinear::SampleEncoderLinear(fmt=%s, "
//            "%u bit [%u bytes], endian=%s)",
//            (sample_format == Kwave::SampleFormat::Signed) ?
//             "signed" : "unsigned",
//            bits_per_sample, m_bytes_per_sample,
//            (endianness == Kwave::BigEndian) ? "BE" : "LE");

    switch (bits_per_sample) {
        case 8:
            MAKE_ENCODER(8)
            break;
        case 16:
            MAKE_ENCODER(16)
            break;
        case 18:
            MAKE_ENCODER(18)
            break;
        case 20:
            MAKE_ENCODER(20)
            break;
        case 24:
            MAKE_ENCODER(24)
            break;
        case 32:
            MAKE_ENCODER(32)
            break;
        DEFAULT_IGNORE;
    }

    Q_ASSERT(m_encoder != encode_NULL);
}

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

//***************************************************************************
void Kwave::SampleEncoderLinear::encode(const Kwave::SampleArray &samples,
                                        unsigned int count,
                                        QByteArray &raw_data)
{
    Q_ASSERT(m_encoder);
    if (!m_encoder) return;

    Q_ASSERT(count * m_bytes_per_sample <= Kwave::toUint(raw_data.size()));
    if (count * m_bytes_per_sample > Kwave::toUint(raw_data.size())) return;

    const sample_t *src = samples.constData();
    quint8 *dst = reinterpret_cast<quint8 *>(raw_data.data());

    m_encoder(src, dst, count);
}

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

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