WaveEncoderPlugin.cxx 5.08 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "config.h"
21
#include "WaveEncoderPlugin.hxx"
22
#include "../EncoderAPI.hxx"
23
#include "system/ByteOrder.hxx"
24
#include "util/DynamicFifoBuffer.hxx"
25 26 27 28

#include <assert.h>
#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 37 38 39 40 41 42 43 44 45 46
public:
	WaveEncoder(AudioFormat &audio_format);

	/* virtual methods from class Encoder */
	bool Write(const void *data, size_t length, Error &) override;

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

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

53 54 55
	const char *GetMimeType() const override {
		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 77 78 79 80
		int freq, int block_size)
{
	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 *
101
wave_encoder_init(gcc_unused const ConfigBlock &block,
102
		  gcc_unused Error &error)
103
{
104
	return new PreparedWaveEncoder();
105 106
}

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

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

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

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

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

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

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

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

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

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

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

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

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

190 191
bool
WaveEncoder::Write(const void *src, size_t length,
192
		   gcc_unused Error &error)
193
{
194
	uint8_t *dst = buffer.Write(length);
195

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

226
	buffer.Append(length);
227 228 229
	return true;
}

230
const EncoderPlugin wave_encoder_plugin = {
231 232
	"wave",
	wave_encoder_init,
233
};