summaryrefslogtreecommitdiff
path: root/src/encoder/plugins/WaveEncoderPlugin.cxx
blob: 77b172990542204c8e48fc450357ebdd084cd347 (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright 2003-2020 The Music Player Daemon Project
 * 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.
 */

#include "WaveEncoderPlugin.hxx"
#include "../EncoderAPI.hxx"
#include "util/ByteOrder.hxx"
#include "util/DynamicFifoBuffer.hxx"

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

static constexpr uint16_t WAVE_FORMAT_PCM = 1;

class WaveEncoder final : public Encoder {
	unsigned bits;

	DynamicFifoBuffer<uint8_t> buffer;

public:
	WaveEncoder(AudioFormat &audio_format) noexcept;

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

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

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

	const char *GetMimeType() const noexcept override {
		return "audio/wav";
	}
};

struct WaveHeader {
	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
fill_wave_header(WaveHeader *header, int channels, int bits,
		int freq, int block_size) noexcept
{
	int data_size = 0x0FFFFFFF;

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

	/* wave format */
	header->format = ToLE16(WAVE_FORMAT_PCM);
	header->channels = ToLE16(channels);
	header->bits = ToLE16(bits);
	header->freq = ToLE32(freq);
	header->blocksize = ToLE16(block_size);
	header->byterate = ToLE32(freq * block_size);

	/* chunk sizes (fake data length) */
	header->fmt_size = ToLE32(16);
	header->data_size = ToLE32(data_size);
	header->riff_size = ToLE32(4 + (8 + 16) + (8 + data_size));
}

static PreparedEncoder *
wave_encoder_init(gcc_unused const ConfigBlock &block)
{
	return new PreparedWaveEncoder();
}

WaveEncoder::WaveEncoder(AudioFormat &audio_format) noexcept
	:Encoder(false),
	 buffer(8192)
{
	assert(audio_format.IsValid());

	switch (audio_format.format) {
	case SampleFormat::S8:
		bits = 8;
		break;

	case SampleFormat::S16:
		bits = 16;
		break;

	case SampleFormat::S24_P32:
		bits = 24;
		break;

	case SampleFormat::S32:
		bits = 32;
		break;

	default:
		audio_format.format = SampleFormat::S16;
		bits = 16;
		break;
	}

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

	/* create PCM wave header in initial buffer */
	fill_wave_header(header,
			 audio_format.channels,
			 bits,
			 audio_format.sample_rate,
			 (bits / 8) * audio_format.channels);

	buffer.Append(sizeof(*header));
}

static size_t
pcm16_to_wave(uint16_t *dst16, const uint16_t *src16, size_t length)
{
	size_t cnt = length >> 1;
	while (cnt > 0) {
		*dst16++ = ToLE16(*src16++);
		cnt--;
	}
	return length;
}

static size_t
pcm32_to_wave(uint32_t *dst32, const uint32_t *src32, size_t length) noexcept
{
	size_t cnt = length >> 2;
	while (cnt > 0){
		*dst32++ = ToLE32(*src32++);
		cnt--;
	}
	return length;
}

static size_t
pcm24_to_wave(uint8_t *dst8, const uint32_t *src32, size_t length) noexcept
{
	uint32_t value;
	uint8_t *dst_old = dst8;

	length = length >> 2;
	while (length > 0){
		value = *src32++;
		*dst8++ = (value) & 0xFF;
		*dst8++ = (value >> 8) & 0xFF;
		*dst8++ = (value >> 16) & 0xFF;
		length--;
	}
	//correct buffer length
	return (dst8 - dst_old);
}

void
WaveEncoder::Write(const void *src, size_t length)
{
	uint8_t *dst = buffer.Write(length);

	if (IsLittleEndian()) {
		switch (bits) {
		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 {
		switch (bits) {
		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;
		}
	}

	buffer.Append(length);
}

const EncoderPlugin wave_encoder_plugin = {
	"wave",
	wave_encoder_init,
};