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
|
/**********
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. (See <http://www.gnu.org/copyleft/lesser.html>.)
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.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**********/
// "liveMedia"
// Copyright (c) 1996-2005 Live Networks, Inc. All rights reserved.
// Filters for converting between raw PCM audio and uLaw
// Implementation
#include "uLawAudioFilter.hh"
////////// 16-bit PCM (in various byte orders) -> 8-bit u-Law //////////
uLawFromPCMAudioSource* uLawFromPCMAudioSource
::createNew(UsageEnvironment& env, FramedSource* inputSource, int byteOrdering) {
// "byteOrdering" must be 0, 1, or 2:
if (byteOrdering < 0 || byteOrdering > 2) {
env.setResultMsg("uLawFromPCMAudioSource::createNew(): bad \"byteOrdering\" parameter");
return NULL;
}
return new uLawFromPCMAudioSource(env, inputSource, byteOrdering);
}
uLawFromPCMAudioSource
::uLawFromPCMAudioSource(UsageEnvironment& env, FramedSource* inputSource,
int byteOrdering)
: FramedFilter(env, inputSource),
fByteOrdering(byteOrdering), fInputBuffer(NULL), fInputBufferSize(0) {
}
uLawFromPCMAudioSource::~uLawFromPCMAudioSource() {
delete[] fInputBuffer;
}
void uLawFromPCMAudioSource::doGetNextFrame() {
// Figure out how many bytes of input data to ask for, and increase
// our input buffer if necessary:
unsigned bytesToRead = fMaxSize*2; // because we're converting 16 bits->8
if (bytesToRead > fInputBufferSize) {
delete[] fInputBuffer; fInputBuffer = new unsigned char[bytesToRead];
fInputBufferSize = bytesToRead;
}
// Arrange to read samples into the input buffer:
fInputSource->getNextFrame(fInputBuffer, bytesToRead,
afterGettingFrame, this,
FramedSource::handleClosure, this);
}
void uLawFromPCMAudioSource
::afterGettingFrame(void* clientData, unsigned frameSize,
unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
uLawFromPCMAudioSource* source = (uLawFromPCMAudioSource*)clientData;
source->afterGettingFrame1(frameSize, numTruncatedBytes,
presentationTime, durationInMicroseconds);
}
#define BIAS 0x84 // the add-in bias for 16 bit samples
#define CLIP 32635
static unsigned char uLawFrom16BitLinear(short sample) {
static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
unsigned char sign = (sample >> 8) & 0x80;
if (sign != 0) sample = -sample; // get the magnitude
if (sample > CLIP) sample = CLIP; // clip the magnitude
sample += BIAS;
unsigned char exponent = exp_lut[(sample>>7) & 0xFF];
unsigned char mantissa = (sample >> (exponent+3)) & 0x0F;
unsigned char result = ~(sign | (exponent << 4) | mantissa);
if (result == 0 ) result = 0x02; // CCITT trap
return result;
}
void uLawFromPCMAudioSource
::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
// Translate raw 16-bit PCM samples (in the input buffer)
// into uLaw samples (in the output buffer).
unsigned numSamples = frameSize/2;
switch (fByteOrdering) {
case 0: { // host order
short* inputSample = (short*)fInputBuffer;
for (unsigned i = 0; i < numSamples; ++i) {
fTo[i] = uLawFrom16BitLinear(inputSample[i]);
}
break;
}
case 1: { // little-endian order
for (unsigned i = 0; i < numSamples; ++i) {
short const newValue = (fInputBuffer[2*i+1]<<8)|fInputBuffer[2*i];
fTo[i] = uLawFrom16BitLinear(newValue);
}
break;
}
case 2: { // network (i.e., big-endian) order
for (unsigned i = 0; i < numSamples; ++i) {
short const newValue = (fInputBuffer[2*i]<<8)|fInputBuffer[2*i+i];
fTo[i] = uLawFrom16BitLinear(newValue);
}
break;
}
}
// Complete delivery to the client:
fFrameSize = numSamples;
fNumTruncatedBytes = numTruncatedBytes;
fPresentationTime = presentationTime;
fDurationInMicroseconds = durationInMicroseconds;
afterGetting(this);
}
////////// u-Law -> 16-bit PCM (in host order) //////////
PCMFromuLawAudioSource* PCMFromuLawAudioSource
::createNew(UsageEnvironment& env, FramedSource* inputSource) {
return new PCMFromuLawAudioSource(env, inputSource);
}
PCMFromuLawAudioSource
::PCMFromuLawAudioSource(UsageEnvironment& env,
FramedSource* inputSource)
: FramedFilter(env, inputSource),
fInputBuffer(NULL), fInputBufferSize(0) {
}
PCMFromuLawAudioSource::~PCMFromuLawAudioSource() {
delete[] fInputBuffer;
}
void PCMFromuLawAudioSource::doGetNextFrame() {
// Figure out how many bytes of input data to ask for, and increase
// our input buffer if necessary:
unsigned bytesToRead = fMaxSize/2; // because we're converting 8 bits->16
if (bytesToRead > fInputBufferSize) {
delete[] fInputBuffer; fInputBuffer = new unsigned char[bytesToRead];
fInputBufferSize = bytesToRead;
}
// Arrange to read samples into the input buffer:
fInputSource->getNextFrame(fInputBuffer, bytesToRead,
afterGettingFrame, this,
FramedSource::handleClosure, this);
}
void PCMFromuLawAudioSource
::afterGettingFrame(void* clientData, unsigned frameSize,
unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
PCMFromuLawAudioSource* source = (PCMFromuLawAudioSource*)clientData;
source->afterGettingFrame1(frameSize, numTruncatedBytes,
presentationTime, durationInMicroseconds);
}
static short linear16FromuLaw(unsigned char uLawByte) {
static int exp_lut[8] = {0,132,396,924,1980,4092,8316,16764};
uLawByte = ~uLawByte;
Boolean sign = (uLawByte & 0x80) != 0;
unsigned char exponent = (uLawByte>>4) & 0x07;
unsigned char mantissa = uLawByte & 0x0F;
short result = exp_lut[exponent] + (mantissa << (exponent+3));
if (sign) result = -result;
return result;
}
void PCMFromuLawAudioSource
::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
// Translate uLaw samples (in the input buffer)
// into 16-bit PCM samples (in the output buffer), in host order.
unsigned numSamples = frameSize;
short* outputSample = (short*)fTo;
for (unsigned i = 0; i < numSamples; ++i) {
outputSample[i] = linear16FromuLaw(fInputBuffer[i]);
}
// Complete delivery to the client:
fFrameSize = numSamples*2;
fNumTruncatedBytes = numTruncatedBytes;
fPresentationTime = presentationTime;
fDurationInMicroseconds = durationInMicroseconds;
afterGetting(this);
}
////////// 16-bit values (in host order) -> 16-bit network order //////////
NetworkFromHostOrder16* NetworkFromHostOrder16
::createNew(UsageEnvironment& env, FramedSource* inputSource) {
return new NetworkFromHostOrder16(env, inputSource);
}
NetworkFromHostOrder16
::NetworkFromHostOrder16(UsageEnvironment& env,
FramedSource* inputSource)
: FramedFilter(env, inputSource) {
}
NetworkFromHostOrder16::~NetworkFromHostOrder16() {
}
void NetworkFromHostOrder16::doGetNextFrame() {
// Arrange to read data directly into the client's buffer:
fInputSource->getNextFrame(fTo, fMaxSize,
afterGettingFrame, this,
FramedSource::handleClosure, this);
}
void NetworkFromHostOrder16
::afterGettingFrame(void* clientData, unsigned frameSize,
unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
NetworkFromHostOrder16* source = (NetworkFromHostOrder16*)clientData;
source->afterGettingFrame1(frameSize, numTruncatedBytes,
presentationTime, durationInMicroseconds);
}
void NetworkFromHostOrder16
::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
// Translate the 16-bit values that we have just read from host
// to network order (in-place)
unsigned numValues = frameSize/2;
short* value = (short*)fTo;
for (unsigned i = 0; i < numValues; ++i) {
value[i] = htons(value[i]);
}
// Complete delivery to the client:
fFrameSize = numValues*2;
fNumTruncatedBytes = numTruncatedBytes;
fPresentationTime = presentationTime;
fDurationInMicroseconds = durationInMicroseconds;
afterGetting(this);
}
////////// 16-bit values (in network order) -> 16-bit host order //////////
HostFromNetworkOrder16* HostFromNetworkOrder16
::createNew(UsageEnvironment& env, FramedSource* inputSource) {
return new HostFromNetworkOrder16(env, inputSource);
}
HostFromNetworkOrder16
::HostFromNetworkOrder16(UsageEnvironment& env,
FramedSource* inputSource)
: FramedFilter(env, inputSource) {
}
HostFromNetworkOrder16::~HostFromNetworkOrder16() {
}
void HostFromNetworkOrder16::doGetNextFrame() {
// Arrange to read data directly into the client's buffer:
fInputSource->getNextFrame(fTo, fMaxSize,
afterGettingFrame, this,
FramedSource::handleClosure, this);
}
void HostFromNetworkOrder16
::afterGettingFrame(void* clientData, unsigned frameSize,
unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
HostFromNetworkOrder16* source = (HostFromNetworkOrder16*)clientData;
source->afterGettingFrame1(frameSize, numTruncatedBytes,
presentationTime, durationInMicroseconds);
}
void HostFromNetworkOrder16
::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
// Translate the 16-bit values that we have just read from network
// to host order (in-place):
unsigned numValues = frameSize/2;
short* value = (short*)fTo;
for (unsigned i = 0; i < numValues; ++i) {
value[i] = ntohs(value[i]);
}
// Complete delivery to the client:
fFrameSize = numValues*2;
fNumTruncatedBytes = numTruncatedBytes;
fPresentationTime = presentationTime;
fDurationInMicroseconds = durationInMicroseconds;
afterGetting(this);
}
////////// 16-bit values: little-endian <-> big-endian //////////
EndianSwap16*
EndianSwap16::createNew(UsageEnvironment& env, FramedSource* inputSource) {
return new EndianSwap16(env, inputSource);
}
EndianSwap16::EndianSwap16(UsageEnvironment& env,
FramedSource* inputSource)
: FramedFilter(env, inputSource) {
}
EndianSwap16::~EndianSwap16() {
}
void EndianSwap16::doGetNextFrame() {
// Arrange to read data directly into the client's buffer:
fInputSource->getNextFrame(fTo, fMaxSize,
afterGettingFrame, this,
FramedSource::handleClosure, this);
}
void EndianSwap16::afterGettingFrame(void* clientData, unsigned frameSize,
unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
EndianSwap16* source = (EndianSwap16*)clientData;
source->afterGettingFrame1(frameSize, numTruncatedBytes,
presentationTime, durationInMicroseconds);
}
void EndianSwap16::afterGettingFrame1(unsigned frameSize, unsigned numTruncatedBytes,
struct timeval presentationTime,
unsigned durationInMicroseconds) {
// Swap the byte order of the 16-bit values that we have just read (in place):
unsigned numValues = frameSize/2;
short* value = (short*)fTo;
for (unsigned i = 0; i < numValues; ++i) {
short const orig = value[i];
value[i] = ((orig&0xFF)<<8) | ((orig&0xFF00)>>8);
}
// Complete delivery to the client:
fFrameSize = numValues*2;
fNumTruncatedBytes = numTruncatedBytes;
fPresentationTime = presentationTime;
fDurationInMicroseconds = durationInMicroseconds;
afterGetting(this);
}
|