WaveEncoderPlugin.cxx 5.06 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * 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.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "WaveEncoderPlugin.hxx"
21
#include "../EncoderAPI.hxx"
22
#include "util/ByteOrder.hxx"
23
#include "util/DynamicFifoBuffer.hxx"
24

25 26
#include <cassert>

27 28
#include <string.h>

29 30
static constexpr uint16_t WAVE_FORMAT_PCM = 1;

31
class WaveEncoder final : public Encoder {
32 33
	unsigned bits;

34
	DynamicFifoBuffer<uint8_t> buffer;
35

36
public:
Max Kellermann's avatar
Max Kellermann committed
37
	explicit WaveEncoder(AudioFormat &audio_format) noexcept;
38 39

	/* virtual methods from class Encoder */
40
	void Write(const void *data, size_t length) override;
41

42
	size_t Read(void *dest, size_t length) noexcept override {
43 44 45 46
		return buffer.Read((uint8_t *)dest, length);
	}
};

47 48
class PreparedWaveEncoder final : public PreparedEncoder {
	/* virtual methods from class PreparedEncoder */
49
	Encoder *Open(AudioFormat &audio_format) override {
50 51
		return new WaveEncoder(audio_format);
	}
52

53
	[[nodiscard]] const char *GetMimeType() const noexcept override {
54 55
		return "audio/wav";
	}
56 57
};

58
struct WaveHeader {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	uint32_t id_riff;
	uint32_t riff_size;
	uint32_t id_wave;
	uint32_t id_fmt;
	uint32_t fmt_size;
	uint16_t format;
	uint16_t channels;
	uint32_t freq;
	uint32_t byterate;
	uint16_t blocksize;
	uint16_t bits;
	uint32_t id_data;
	uint32_t data_size;
};

static void
75
fill_wave_header(WaveHeader *header, int channels, int bits,
76
		int freq, int block_size) noexcept
77 78 79 80
{
	int data_size = 0x0FFFFFFF;

	/* constants */
81 82 83 84
	header->id_riff = ToLE32(0x46464952);
	header->id_wave = ToLE32(0x45564157);
	header->id_fmt = ToLE32(0x20746d66);
	header->id_data = ToLE32(0x61746164);
85

86
	/* wave format */
87
	header->format = ToLE16(WAVE_FORMAT_PCM);
88 89 90 91 92
	header->channels = ToLE16(channels);
	header->bits = ToLE16(bits);
	header->freq = ToLE32(freq);
	header->blocksize = ToLE16(block_size);
	header->byterate = ToLE32(freq * block_size);
93

94
	/* chunk sizes (fake data length) */
95 96 97
	header->fmt_size = ToLE32(16);
	header->data_size = ToLE32(data_size);
	header->riff_size = ToLE32(4 + (8 + 16) + (8 + data_size));
98 99
}

100
static PreparedEncoder *
Rosen Penev's avatar
Rosen Penev committed
101
wave_encoder_init([[maybe_unused]] const ConfigBlock &block)
102
{
103
	return new PreparedWaveEncoder();
104 105
}

106
WaveEncoder::WaveEncoder(AudioFormat &audio_format) noexcept
107 108
	:Encoder(false),
	 buffer(8192)
109
{
110
	assert(audio_format.IsValid());
111

112 113
	switch (audio_format.format) {
	case SampleFormat::S8:
114
		bits = 8;
115 116
		break;

117
	case SampleFormat::S16:
118
		bits = 16;
119 120
		break;

121
	case SampleFormat::S24_P32:
122
		bits = 24;
123 124
		break;

125
	case SampleFormat::S32:
126
		bits = 32;
127 128 129
		break;

	default:
130
		audio_format.format = SampleFormat::S16;
131
		bits = 16;
132 133
		break;
	}
134

135
	auto range = buffer.Write();
136 137
	assert(range.size >= sizeof(WaveHeader));
	auto *header = (WaveHeader *)range.data;
138

139
	/* create PCM wave header in initial buffer */
140
	fill_wave_header(header,
141
			 audio_format.channels,
142
			 bits,
143
			 audio_format.sample_rate,
144
			 (bits / 8) * audio_format.channels);
145

146
	buffer.Append(sizeof(*header));
147 148
}

149
static size_t
150
pcm16_to_wave(uint16_t *dst16, const uint16_t *src16, size_t length)
151
{
152 153
	size_t cnt = length >> 1;
	while (cnt > 0) {
154
		*dst16++ = ToLE16(*src16++);
155
		cnt--;
156 157 158 159
	}
	return length;
}

160
static size_t
161
pcm32_to_wave(uint32_t *dst32, const uint32_t *src32, size_t length) noexcept
162
{
163 164
	size_t cnt = length >> 2;
	while (cnt > 0){
165
		*dst32++ = ToLE32(*src32++);
166
		cnt--;
167 168 169 170
	}
	return length;
}

171
static size_t
172
pcm24_to_wave(uint8_t *dst8, const uint32_t *src32, size_t length) noexcept
173
{
174 175
	uint32_t value;
	uint8_t *dst_old = dst8;
176

177 178
	length = length >> 2;
	while (length > 0){
179 180 181 182
		value = *src32++;
		*dst8++ = (value) & 0xFF;
		*dst8++ = (value >> 8) & 0xFF;
		*dst8++ = (value >> 16) & 0xFF;
183
		length--;
184 185
	}
	//correct buffer length
186
	return (dst8 - dst_old);
187 188
}

189 190
void
WaveEncoder::Write(const void *src, size_t length)
191
{
192
	uint8_t *dst = buffer.Write(length);
193

194
	if (IsLittleEndian()) {
195
		switch (bits) {
196 197 198 199 200 201 202 203 204 205
		case 8:
		case 16:
		case 32:// optimized cases
			memcpy(dst, src, length);
			break;
		case 24:
			length = pcm24_to_wave(dst, (const uint32_t *)src, length);
			break;
		}
	} else {
206
		switch (bits) {
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
		case 8:
			memcpy(dst, src, length);
			break;
		case 16:
			length = pcm16_to_wave((uint16_t *)dst,
					       (const uint16_t *)src, length);
			break;
		case 24:
			length = pcm24_to_wave(dst, (const uint32_t *)src, length);
			break;
		case 32:
			length = pcm32_to_wave((uint32_t *)dst,
					       (const uint32_t *)src, length);
			break;
		}
222 223
	}

224
	buffer.Append(length);
225 226
}

227
const EncoderPlugin wave_encoder_plugin = {
228 229
	"wave",
	wave_encoder_init,
230
};