File: ALAC.cpp

package info (click to toggle)
audiofile 0.3.6-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,980 kB
  • sloc: cpp: 36,534; sh: 11,090; ansic: 6,060; makefile: 439
file content (383 lines) | stat: -rw-r--r-- 10,281 bytes parent folder | download | duplicates (6)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
	Audio File Library
	Copyright (C) 2013 Michael Pruett <michael@68k.org>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library 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
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the
	Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
	Boston, MA  02110-1301  USA
*/

#include "config.h"
#include "ALAC.h"

#include "Buffer.h"
#include "Compiler.h"
#include "File.h"
#include "FileModule.h"
#include "PacketTable.h"
#include "SimpleModule.h"
#include "Track.h"
#include "afinternal.h"
#include "audiofile.h"
#include "byteorder.h"
#include "compression.h"
#include "units.h"
#include "util.h"

#include "../alac/ALACBitUtilities.h"
#include "../alac/ALACDecoder.h"
#include "../alac/ALACEncoder.h"

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

enum
{
	kALACFormatFlag_16BitSourceData = 1,
	kALACFormatFlag_20BitSourceData = 2,
	kALACFormatFlag_24BitSourceData = 3,
	kALACFormatFlag_32BitSourceData = 4
};

class ALAC : public FileModule
{
public:
	static ALAC *createDecompress(Track *, File *, bool canSeek,
		bool headerless, AFframecount *chunkFrames);
	static ALAC *createCompress(Track *, File *, bool canSeek,
		bool headerless, AFframecount *chunkFrames);

	virtual ~ALAC();

	virtual const char *name() const OVERRIDE
	{
		return mode() == Compress ?  "alac_compress" : "alac_decompress";
	}
	virtual void describe() OVERRIDE;
	virtual void runPull() OVERRIDE;
	virtual void reset1() OVERRIDE;
	virtual void reset2() OVERRIDE;
	virtual void runPush() OVERRIDE;
	virtual void sync1() OVERRIDE;
	virtual void sync2() OVERRIDE;
	virtual int bufferSize() const OVERRIDE;

private:
	AFframecount m_framesToIgnore;
	AFfileoffset m_savedPositionNextFrame;
	AFframecount m_savedNextFrame;

	SharedPtr<Buffer> m_codecData;
	ALACDecoder *m_decoder;
	ALACEncoder *m_encoder;
	int m_currentPacket;

	ALAC(Mode mode, Track *track, File *fh, bool canSeek, Buffer *codecData);
	void initDecoder();
	void initEncoder();

	AudioFormatDescription outputFormat() const;
};

ALAC::ALAC(Mode mode, Track *track, File *fh, bool canSeek, Buffer *codecData) :
	FileModule(mode, track, fh, canSeek),
	m_savedPositionNextFrame(-1),
	m_savedNextFrame(-1),
	m_codecData(codecData),
	m_decoder(NULL),
	m_encoder(NULL),
	m_currentPacket(0)
{
	if (mode == Decompress)
		initDecoder();
	else
		initEncoder();
}

ALAC::~ALAC()
{
	delete m_decoder;
	delete m_encoder;
}

void ALAC::initDecoder()
{
	m_decoder = new ALACDecoder();
	m_decoder->Init(m_codecData->data(), m_codecData->size());
}

void ALAC::initEncoder()
{
	m_encoder = new ALACEncoder();
	m_encoder->SetFrameSize(m_track->f.framesPerPacket);
	m_encoder->InitializeEncoder(outputFormat());

	uint32_t cookieSize = m_encoder->GetMagicCookieSize(m_track->f.channelCount);
	assert(cookieSize == m_codecData->size());
	m_encoder->GetMagicCookie(m_codecData->data(), &cookieSize);

	void *v = NULL;
	_af_pv_getptr(m_track->f.compressionParams, _AF_CODEC_DATA, &v);
	::memcpy(v, m_codecData->data(), cookieSize);
}

AudioFormatDescription ALAC::outputFormat() const
{
	AudioFormatDescription outputFormat;
	outputFormat.mSampleRate = m_track->f.sampleRate;
	outputFormat.mFormatID = kALACFormatAppleLossless;
	switch (m_track->f.sampleWidth)
	{
		case 16:
			outputFormat.mFormatFlags = kALACFormatFlag_16BitSourceData; break;
		case 20:
			outputFormat.mFormatFlags = kALACFormatFlag_20BitSourceData; break;
		case 24:
			outputFormat.mFormatFlags = kALACFormatFlag_24BitSourceData; break;
		case 32:
			outputFormat.mFormatFlags = kALACFormatFlag_32BitSourceData; break;
		default:
			outputFormat.mFormatFlags = 0; break;
	}
	outputFormat.mFramesPerPacket = m_track->f.framesPerPacket;
	outputFormat.mChannelsPerFrame = m_track->f.channelCount;
	outputFormat.mBytesPerPacket = 0;
	outputFormat.mBytesPerFrame = 0;
	outputFormat.mBitsPerChannel = 0;
	outputFormat.mReserved = 0;
	return outputFormat;
}

void ALAC::describe()
{
	m_outChunk->f.byteOrder = _AF_BYTEORDER_NATIVE;
	m_outChunk->f.compressionType = AF_COMPRESSION_NONE;
	m_outChunk->f.compressionParams = AU_NULL_PVLIST;
}

ALAC *ALAC::createDecompress(Track *track, File *fh,
	bool canSeek, bool headerless, AFframecount *chunkFrames)
{
	assert(fh->tell() == track->fpos_first_frame);

	AUpvlist pv = (AUpvlist) track->f.compressionParams;
	long codecDataSize;
	if (!_af_pv_getlong(pv, _AF_CODEC_DATA_SIZE, &codecDataSize))
	{
		_af_error(AF_BAD_CODEC_CONFIG, "codec data size not set");
		return NULL;
	}

	SharedPtr<Buffer> codecData = new Buffer(codecDataSize);

	void *data;
	if (!_af_pv_getptr(pv, _AF_CODEC_DATA, &data))
	{
		_af_error(AF_BAD_CODEC_CONFIG, "codec data not set");
		return NULL;
	}

	memcpy(codecData->data(), data, codecDataSize);

	*chunkFrames = track->f.framesPerPacket;

	return new ALAC(Decompress, track, fh, canSeek, codecData.get());
}

ALAC *ALAC::createCompress(Track *track, File *fh,
	bool canSeek, bool headerless, AFframecount *chunkFrames)
{
	assert(fh->tell() == track->fpos_first_frame);

	AUpvlist pv = (AUpvlist) track->f.compressionParams;
	long codecDataSize;
	if (!_af_pv_getlong(pv, _AF_CODEC_DATA_SIZE, &codecDataSize))
	{
		_af_error(AF_BAD_CODEC_CONFIG, "codec data size not set");
		return NULL;
	}

	SharedPtr<Buffer> codecData = new Buffer(codecDataSize);

	void *data;
	if (!_af_pv_getptr(pv, _AF_CODEC_DATA, &data))
	{
		_af_error(AF_BAD_CODEC_CONFIG, "codec data not set");
		return NULL;
	}

	memcpy(codecData->data(), data, codecDataSize);

	*chunkFrames = track->f.framesPerPacket;

	return new ALAC(Compress, track, fh, canSeek, codecData.get());
}

void ALAC::runPull()
{
	SharedPtr<PacketTable> packetTable = m_track->m_packetTable;
	if (m_currentPacket >= static_cast<int>(packetTable->numPackets()))
	{
		m_outChunk->frameCount = 0;
		return;
	}
	assert(m_currentPacket < static_cast<int>(packetTable->numPackets()));

	ssize_t bytesPerPacket = packetTable->bytesPerPacket(m_currentPacket);
	assert(bytesPerPacket <= bufferSize());

	if (read(m_inChunk->buffer, bytesPerPacket) < bytesPerPacket)
	{
		reportReadError(0, m_track->f.framesPerPacket);
		return;
	}

	BitBuffer bitBuffer;
	BitBufferInit(&bitBuffer, static_cast<uint8_t *>(m_inChunk->buffer),
		bytesPerPacket);

	uint32_t numFrames;
	m_decoder->Decode(&bitBuffer, static_cast<uint8_t *>(m_outChunk->buffer),
		m_track->f.framesPerPacket, m_track->f.channelCount, &numFrames);
	m_outChunk->frameCount = numFrames;

	m_currentPacket++;
}

void ALAC::reset1()
{
	AFframecount nextFrame = m_track->nextfframe;
	m_currentPacket = nextFrame / m_track->f.framesPerPacket;
	m_track->nextfframe = m_currentPacket * m_track->f.framesPerPacket;
	m_framesToIgnore = nextFrame - m_track->nextfframe;
}

void ALAC::reset2()
{
	m_track->fpos_next_frame = m_track->fpos_first_frame +
		m_track->m_packetTable->startOfPacket(m_currentPacket);
	m_track->frames2ignore += m_framesToIgnore;
}

int ALAC::bufferSize() const
{
	return m_track->f.framesPerPacket * m_track->f.channelCount *
		((10 + m_track->f.sampleWidth) / 8) + 1;
}

void ALAC::runPush()
{
	AudioFormatDescription inputFormat;
	inputFormat.mSampleRate = m_track->f.sampleRate;
	inputFormat.mFormatID = kALACFormatLinearPCM;
	inputFormat.mFormatFlags = kALACFormatFlagsNativeEndian;
	inputFormat.mBytesPerPacket = _af_format_frame_size_uncompressed(&m_track->f, false);
	inputFormat.mFramesPerPacket = 1;
	inputFormat.mBytesPerFrame = _af_format_frame_size_uncompressed(&m_track->f, false);
	inputFormat.mChannelsPerFrame = m_track->f.channelCount;
	inputFormat.mBitsPerChannel = m_track->f.sampleWidth;
	inputFormat.mReserved = 0;

	int32_t numBytes = m_inChunk->frameCount * inputFormat.mBytesPerFrame;
	int32_t result = m_encoder->Encode(inputFormat, outputFormat(),
		static_cast<uint8_t *>(m_inChunk->buffer),
		static_cast<uint8_t *>(m_outChunk->buffer),
		&numBytes);
	if (result)
	{
		_af_error(AF_BAD_CODEC_STATE, "error encoding ALAC audio data");
		m_track->filemodhappy = false;
		return;
	}

	assert(numBytes <= bufferSize());

	ssize_t bytesWritten = write(m_outChunk->buffer, numBytes);
	if (bytesWritten != numBytes)
	{
		reportWriteError(0, m_track->f.framesPerPacket);
		return;
	}

	PacketTable *packetTable = m_track->m_packetTable.get();

	packetTable->append(numBytes);

	packetTable->setNumValidFrames(packetTable->numValidFrames() +
		m_inChunk->frameCount);
}

void ALAC::sync1()
{
	m_savedPositionNextFrame = m_track->fpos_next_frame;
	m_savedNextFrame = m_track->nextfframe;
}

void ALAC::sync2()
{
	assert(!canSeek() || (tell() == m_track->fpos_next_frame));

	m_track->fpos_after_data = tell();

	m_track->fpos_next_frame = m_savedPositionNextFrame;
	m_track->nextfframe = m_savedNextFrame;
}

bool _af_alac_format_ok (AudioFormat *f)
{
	if (f->channelCount > kALACMaxChannels)
	{
		_af_error(AF_BAD_CHANNELS,
			"ALAC compression supports a maximum of 8 channels");
		return false;
	}

	if (f->sampleFormat != AF_SAMPFMT_TWOSCOMP)
	{
		_af_error(AF_BAD_COMPRESSION,
			"ALAC compression requires signed integer audio data");
		return false;
	}

	if (f->sampleWidth != 16 &&
		f->sampleWidth != 20 &&
		f->sampleWidth != 24 &&
		f->sampleWidth != 32)
	{
		_af_error(AF_BAD_WIDTH,
			"ALAC compression requires 16, 20, 24, or 32 bits per sample");
		return false;
	}

	if (f->byteOrder != _AF_BYTEORDER_NATIVE)
	{
		_af_error(AF_BAD_COMPRESSION,
			"ALAC compression requires native-endian format");
		f->byteOrder = _AF_BYTEORDER_NATIVE;
	}

	return true;
}

FileModule *_af_alac_init_decompress (Track *track, File *fh,
	bool canSeek, bool headerless, AFframecount *chunkFrames)
{
	return ALAC::createDecompress(track, fh, canSeek, headerless, chunkFrames);
}

FileModule *_af_alac_init_compress (Track *track, File *fh,
	bool canSeek, bool headerless, AFframecount *chunkFrames)
{
	return ALAC::createCompress(track, fh, canSeek, headerless, chunkFrames);
}