WaveEncoderPlugin.cxx 4.97 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 27

#include <assert.h>
#include <string.h>

28 29
static constexpr uint16_t WAVE_FORMAT_PCM = 1;

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

33
	DynamicFifoBuffer<uint8_t> buffer;
34

35 36 37 38
public:
	WaveEncoder(AudioFormat &audio_format);

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

	size_t Read(void *dest, size_t length) override {
		return buffer.Read((uint8_t *)dest, length);
	}
};

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

52 53 54
	const char *GetMimeType() const override {
		return "audio/wav";
	}
55 56
};

57
struct WaveHeader {
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
	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
74
fill_wave_header(WaveHeader *header, int channels, int bits,
75 76 77 78 79
		int freq, int block_size)
{
	int data_size = 0x0FFFFFFF;

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

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

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

99
static PreparedEncoder *
100
wave_encoder_init(gcc_unused const ConfigBlock &block)
101
{
102
	return new PreparedWaveEncoder();
103 104
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

193
	if (IsLittleEndian()) {
194
		switch (bits) {
195 196 197 198 199 200 201 202 203 204
		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 {
205
		switch (bits) {
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
		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;
		}
221 222
	}

223
	buffer.Append(length);
224 225
}

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