forked from scummvm/scummvm
/
g711.cpp
128 lines (105 loc) · 4.12 KB
/
g711.cpp
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
/* 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 "audio/decoders/g711.h"
#include "audio/audiostream.h"
#include "common/stream.h"
#include "common/util.h"
/* from g711.c by SUN microsystems (unrestricted use) */
#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
#define QUANT_MASK (0xf) /* Quantization field mask. */
#define SEG_SHIFT (4) /* Left shift for segment number. */
#define SEG_MASK (0x70) /* Segment field mask. */
#define BIAS (0x84) /* Bias for linear code. */
namespace Audio {
/**
* Logarithmic PCM (G.711)
* https://en.wikipedia.org/wiki/G.711
* https://wiki.multimedia.cx/index.php/PCM#Logarithmic_PCM
*/
class G711AudioStream : public SeekableAudioStream {
Common::DisposablePtr<Common::SeekableReadStream> _stream;
const int _rate;
const int _channels;
protected:
virtual int16 decodeSample(uint8 val) = 0;
public:
G711AudioStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, int rate, int channels) :
_stream(stream, disposeAfterUse),
_rate(rate),
_channels(channels) {
}
int readBuffer(int16 *buffer, const int numSamples) override {
int samples;
for (samples = 0; samples < numSamples; samples++) {
uint8 val = _stream->readByte();
if (endOfData())
break;
buffer[samples] = decodeSample(val);
}
return samples;
}
bool isStereo() const override { return (_channels == 2); }
int getRate() const override { return _rate; }
bool endOfData() const override { return _stream->eos(); }
bool seek(const Timestamp &where) override {
const uint32 seekSample = convertTimeToStreamPos(where, getRate(), isStereo()).totalNumberOfFrames();
return _stream->seek(seekSample, SEEK_SET);
}
Timestamp getLength() const override {
return Timestamp(0, _stream->size() / _channels, _rate);
}
};
class G711ALawStream : public G711AudioStream {
int16 decodeSample(uint8 val) override {
val ^= 0x55;
int t = val & QUANT_MASK;
int seg = ((unsigned)val & SEG_MASK) >> SEG_SHIFT;
if (seg)
t = (t + t + 1 + 32) << (seg + 2);
else
t = (t + t + 1) << 3;
return (val & SIGN_BIT) ? t : -t;
}
public:
G711ALawStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, int rate, int channels) :
G711AudioStream(stream, disposeAfterUse, rate, channels) {
}
};
SeekableAudioStream *makeALawStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, int rate, int channels) {
return new G711ALawStream(stream, disposeAfterUse, rate, channels);
}
class G711MuLawStream : public G711AudioStream {
int16 decodeSample(uint8 val) override {
val = ~val;
int t = ((val & QUANT_MASK) << 3) + BIAS;
t <<= ((unsigned)val & SEG_MASK) >> SEG_SHIFT;
return (val & SIGN_BIT) ? (BIAS - t) : (t - BIAS);
}
public:
G711MuLawStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, int rate, int channels) :
G711AudioStream(stream, disposeAfterUse, rate, channels) {
}
};
SeekableAudioStream *makeMuLawStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, int rate, int channels) {
return new G711MuLawStream(stream, disposeAfterUse, rate, channels);
}
} // End of namespace Audio