/*
* Copyright (C) 2017 Tetsuya Isaki. All rights reserved.
* Copyright (C) 2017 Y.Sugahara (moveccr). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* audio_internal_to_mulaw has two implementations.
*
* 1. Use 8bit table (MULAW_LQ_ENC)
* It's traditional implementation and its precision is 8bit.
* It's faster but the size is larger. And you can hear a little noise
* in silent part.
*
* 2. Calculation (default)
* It calculates mu-law with full spec and its precision is 14bit.
* It's about 3 times slower but the size is less than a half (on m68k,
* for example).
*
* mu-law is no longer a popular format. I think size-optimized is better.
*/
/* #define MULAW_LQ_ENC */
/*
* About mulaw32 format.
*
* The format which I call ``mulaw32'' is only used in dev/tc/bba.c .
* It is 8bit mu-law but 16bit left-shifted and its container is 32bit.
* Not mu-law calculated in 32bit.
*
* When MULAW32 is not defined (it's default), this file outputs
* audio_internal_to_mulaw() and audio_mulaw_to_internal(). When
* MULAW32 is defined, this file outputs audio_internal_to_mulaw32()
* and audio_mulaw32_to_internal() instead.
*
* Since mu-law is used as userland format and is mandatory, all audio
* drivers (including tc/bba) link this mulaw.c in ordinary procedure.
* On the other hand, only tc/bba also needs audio_internal_to_mulaw32()
* and audio_mulaw32_to_internal() as its hardware drivers codec, so
* define MULAW32 and include this file. It's a bit tricky but I think
* this is the simplest way.
*/
s = arg->src;
d = arg->dst;
sample_count = arg->count * arg->srcfmt->channels;
for (i = 0; i < sample_count; i++) {
aint_t val;
uint m;
m = *s++;
#if defined(MULAW32)
/* 32bit container used only in tc/bba.c */
m = (m >> 16) & 0xff;
#endif
val = mulaw_to_slinear16[m];
val <<= AUDIO_INTERNAL_BITS - 16;
*d++ = val;
}
}
/*
* audio_internal_to_mulaw:
* This filter performs conversion from internal format to mu-law.
*
* audio_internal_to_mulaw32:
* This filter performs conversion from internal format to mulaw32
* used only in tc/bba.c.
*/
void
#if !defined(MULAW32)
audio_internal_to_mulaw(audio_filter_arg_t *arg)
#else
audio_internal_to_mulaw32(audio_filter_arg_t *arg)
#endif
{
const aint_t *s;
#if defined(MULAW32)
uint32_t *d;
#else
uint8_t *d;
#endif
u_int sample_count;
u_int i;
s = arg->src;
d = arg->dst;
sample_count = arg->count * arg->srcfmt->channels;
for (i = 0; i < sample_count; i++) {
uint8_t m;
#if defined(MULAW_LQ_ENC)
/* 8bit (low quality, fast but fat) encoder */
uint8_t val;
val = (*s++) >> (AUDIO_INTERNAL_BITS - 8);
m = slinear8_to_mulaw[val];
#else
/* 14bit (fullspec, slow but small) encoder */
uint16_t val;
int c;
val = *s++ >> (AUDIO_INTERNAL_BITS - 16);
if ((int16_t)val < 0) {
m = 0;
} else {
val = ~val;
m = 0x80;
}
/* limit */
if ((int16_t)val < -8158 * 4)
val = -8158 * 4;
val -= 33 * 4; /* bias */
// (2) Find first zero (and align val to left)
c = 0;
if (val >= 0xf000) c += 4, val <<= 4;
if (val >= 0xc000) c += 2, val <<= 2;
if (val >= 0x8000) c += 1, val <<= 1;
// (3)
m += (c << 4);
m += (val >> 11) & 0x0f;
#endif
#if defined(MULAW32)
/* 8bit mu-law in 32bit container used only in tc/bba.c */
*d++ = m << 16;
#else
*d++ = m;
#endif
}
}