aboutsummaryrefslogtreecommitdiff
path: root/audio/decoders/ac3.cpp
blob: cc0b530a88a5ad8fd7c0418a460722f007bfdec6 (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 "common/inttypes.h"
#include "common/ptr.h"
#include "common/stream.h"
#include "common/textconsole.h"
#include "common/util.h"

#include "audio/audiostream.h"
#include "audio/decoders/ac3.h"
#include "audio/decoders/raw.h"

extern "C" {
#include <a52dec/a52.h>
}

namespace Audio {

class AC3Stream : public PacketizedAudioStream {
public:
	AC3Stream(double decibel);
	~AC3Stream();

	bool init(Common::SeekableReadStream &firstPacket);
	void deinit();

	// AudioStream API
	int readBuffer(int16 *buffer, const int numSamples) { return _audStream->readBuffer(buffer, numSamples); }
	bool isStereo() const { return _audStream->isStereo(); }
	int getRate() const { return _audStream->getRate(); }
	bool endOfData() const { return _audStream->endOfData(); }
	bool endOfStream() const { return _audStream->endOfStream(); }

	// PacketizedAudioStream API
	void queuePacket(Common::SeekableReadStream *data);
	void finish() { _audStream->finish(); }

private:
	Common::ScopedPtr<QueuingAudioStream> _audStream;
	a52_state_t *_a52State;
	uint32 _frameSize;
	byte _inBuf[4096];
	byte *_inBufPtr;
	int _flags;
	int _sampleRate;
	double _audioGain;
};

AC3Stream::AC3Stream(double decibel) : _a52State(0), _frameSize(0), _inBufPtr(0), _flags(0), _sampleRate(0) {
	_audioGain = pow(2, decibel / 6);
}

AC3Stream::~AC3Stream() {
	deinit();
}

enum {
	HEADER_SIZE = 7
};

bool AC3Stream::init(Common::SeekableReadStream &firstPacket) {
	deinit();

	// In theory, I should pass mm_accel() to a52_init(), but I don't know
        // where that's supposed to be defined.
	_a52State = a52_init(0);

	// Go through the header to find sync
	byte buf[HEADER_SIZE];
	_sampleRate = -1;

	for (uint i = 0; i < firstPacket.size() - sizeof(buf); i++) {
		int flags, bitRate;
		firstPacket.seek(i);
		firstPacket.read(buf, sizeof(buf));

		if (a52_syncinfo(buf, &flags, &_sampleRate, &bitRate) > 0)
			break;
	}

	// Ensure we have a valid sample rate
	if (_sampleRate <= 0) {
		deinit();
		return false;
	}

	_audStream.reset(makeQueuingAudioStream(_sampleRate, true));
	_inBufPtr = _inBuf;
	_flags = 0;
	_frameSize = 0;
	return true;
}

void AC3Stream::deinit() {
	if (!_a52State)
		return;

	_audStream.reset();
	a52_free(_a52State);
	_a52State = 0;
}

void AC3Stream::queuePacket(Common::SeekableReadStream *data) {
	Common::ScopedPtr<Common::SeekableReadStream> packet(data);

	while (packet->pos() < packet->size()) {
		uint32 leftSize = packet->size() - packet->pos();
		uint32 len = _inBufPtr - _inBuf;
		if (_frameSize == 0) {
			// No header seen: find one
			len = HEADER_SIZE - len;
			if (len > leftSize)
				len = leftSize;
			packet->read(_inBufPtr, len);
			leftSize -= len;
			_inBufPtr += len;
			if ((_inBufPtr - _inBuf) == HEADER_SIZE) {
				int sampleRate, bitRate;
				len = a52_syncinfo(_inBuf, &_flags, &sampleRate, &bitRate);
				if (len == 0) {
					memmove(_inBuf, _inBuf + 1, HEADER_SIZE - 1);
					_inBufPtr--;
				} else {
					_frameSize = len;
				}
			}
		} else if (len < _frameSize) {
			len = _frameSize - len;
			if (len > leftSize)
				len = leftSize;

			assert(len < sizeof(_inBuf) - (_inBufPtr - _inBuf));
			packet->read(_inBufPtr, len);
			leftSize -= len;
			_inBufPtr += len;
		} else {
			// TODO: Eventually support more than just stereo max
			int flags = A52_STEREO | A52_ADJUST_LEVEL;
			sample_t level = 32767 * _audioGain;

			if (a52_frame(_a52State, _inBuf, &flags, &level, 0) != 0)
				error("Frame fail");

			int16 *outputBuffer = (int16 *)malloc(6 * 256 * 2 * 2);
			int16 *outputPtr = outputBuffer;
			int outputLength = 0;
			for (int i = 0; i < 6; i++) {
				if (a52_block(_a52State) == 0) {
					sample_t *samples = a52_samples(_a52State);
					for (int j = 0; j < 256; j++) {
						*outputPtr++ = (int16)CLIP<sample_t>(samples[j], -32768, 32767);
						*outputPtr++ = (int16)CLIP<sample_t>(samples[j + 256], -32768, 32767);
					}

					outputLength += 1024;
				}
			}

			if (outputLength > 0) {
				flags = FLAG_STEREO | FLAG_16BITS;

#ifdef SCUMM_LITTLE_ENDIAN
				flags |= FLAG_LITTLE_ENDIAN;
#endif

				_audStream->queueBuffer((byte *)outputBuffer, outputLength, DisposeAfterUse::YES, flags);
			}

			_inBufPtr = _inBuf;
			_frameSize = 0;
		}
	}
}

PacketizedAudioStream *makeAC3Stream(Common::SeekableReadStream &firstPacket, double decibel) {
	Common::ScopedPtr<AC3Stream> stream(new AC3Stream(decibel));
	if (!stream->init(firstPacket))
		return 0;

	return stream.release();
}

} // End of namespace Audio