NetBSD の src/sys/dev/ic/msm6258.c のメモ。
/* $Id: msm6258.c,v 1.3 2009/11/19 13:29:36 isaki Exp $ */
/*
* X68000 の内蔵 ADPCM 音源チップである MSM6258 の NetBSD 用の
* ドライバに解説をつけたものです。
* コアな関数に NetBSD 依存部分はないので簡単に流用できると思います。
* 元ネタは菅原さんによる「X68000 資料室」です。
* より正確な解説はそちらを参考にして下さい。
* ※ 2009年現在、未到達。
*/
/* $NetBSD: msm6258.c,v 1.10 2002/04/13 12:40:50 isaki Exp $ */
/*
* Copyright (c) 2001 Tetsuya Isaki. 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Tetsuya Isaki.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* 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.
*/
/*
* OKI MSM6258 ADPCM voice synthesizer codec.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: msm6258.c,v 1.10 2002/04/13 12:40:50 isaki Exp $");
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/select.h>
#include <sys/audioio.h>
#include <dev/audio_if.h>
#include <dev/auconv.h>
#include <dev/audiovar.h>
#include <dev/mulaw.h>
#include <dev/ic/msm6258var.h>
struct msm6258_codecvar {
/* ADPCM stream must be converted in order. */
/*
* ADPCM をリニアPCMに変換するのは先頭から順番に変換する必要がある。
* sw_code は入力と出力のバッファが同じ場所である必要があるため
* テンポラリのバッファが必要。
* XXX: しかしこのサイズ決め打ちなのはどうしたもんかなあ。
* XXX: しかもバッファあふれのチェック一切してないし(汗
*/
u_char mc_buf[AU_RING_SIZE]; /* XXX */
short mc_amp;
char mc_estim;
};
/*
* vs0 では struct vs_softc として宣言されている構造体を
* このソースでは struct msm6258_softc として参照する。
* ちょっと強引だけど、半ば共用体みたいな使いかた。
* 1つ目のメンバは NetBSD のデバイスドライバならすべて struct device
* なので問題ない。
* 2つ目のメンバは MD コード (現在は sys/arch/x68k/dev/vs.c だけ) では
* コーデック用変数へのポインタ (void *) として空けてある。
*/
struct msm6258_softc {
struct device sc_dev;
struct msm6258_codecvar *sc_mc;
/* MD vars follow */
};
static inline u_char pcm2adpcm_step(struct msm6258_codecvar *, short);
static inline short adpcm2pcm_step(struct msm6258_codecvar *, u_char);
/*
* 次回の PCM 値を予測するためのテーブル。
*
* 本当はこの 1/2 値だったんだけど、使われる箇所ではすべて
* 2倍して使われてたのでテーブルごと2倍した。
*/
static int adpcm_estimindex[16] = {
2, 6, 10, 14, 18, 22, 26, 30,
-2, -6, -10, -14, -18, -22, -26, -30
};
/*
* 量子化幅。
* adpcm_estim って名前はちょっと違うような気もする。
* たぶんこのテーブルは沖電気による非公開らしいので
* どっから出てきたのかよく知らない。
*/
static int adpcm_estim[49] = {
16, 17, 19, 21, 23, 25, 28, 31, 34, 37,
41, 45, 50, 55, 60, 66, 73, 80, 88, 97,
107, 118, 130, 143, 157, 173, 190, 209, 230, 253,
279, 307, 337, 371, 408, 449, 494, 544, 598, 658,
724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552
};
/*
* 何するテーブル?
*/
static int adpcm_estimstep[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8
};
/*
* MSM6258 依存の初期化ルーチン。
* MD ドライバの初期化時に呼ばれる。
*/
void *
msm6258_codec_init (void)
{
struct msm6258_codecvar *r;
r = malloc (sizeof(*r), M_DEVBUF, M_NOWAIT);
if (r == 0)
return 0;
r->mc_amp = r->mc_estim = 0;
return r;
}
/*
* MSM6258 依存のオープン時の処理。
* MD ドライバからデバイスオープンした時に呼ばれる。
* amp と estim を 0 に初期化する。必要なのかなあ(汗
*/
int
msm6258_codec_open(void *hdl)
{
struct msm6258_softc *sc = hdl;
struct msm6258_codecvar *mc = sc->sc_mc;
mc->mc_amp = 0;
mc->mc_estim = 0;
return 0;
}
/*
* signed 16bit linear PCM -> OkiADPCM
* slinear16 の1サンプルを OkiADPCM 1サンプルに変換する。
*
* MSM6258 が期待する PCM は 12bit 符号付 PCM (slinear12 にあたる) である。
* 従ってこの関数では同時に振幅変換(16bit -> 12bit) も行なっている。
*/
static inline u_char
pcm2adpcm_step(struct msm6258_codecvar *mc, short a)
{
/*
* mc->mc_estim には、前回の差分比予測値インデックスが入っている
*/
int estim = (int)mc->mc_estim;
int df;
short dl, c;
unsigned char b;
unsigned char s;
/* df は、実 PCM 値 a と前回の予測 PCM 値との差分である */
df = a - mc->mc_amp;
/*
* dl は、差分比予測値テーブル adpcm_estim[] から、予測された
* 差分比を取り出したもの。
*/
dl = adpcm_estim[estim];
/*
* c は、差分を 12bit に変換し、(dl/8) に対する比率を出したもの。
* /16 は 16bit と 12bit との差、すなわち 2^4 である。
* *8 は、dl すなわち adpcm_estim[] の値が8倍値で記録してあること
* に起因する。
*/
c = (df / 16) * 8 / dl;
/*
* c が正か負かによって処理が変わる。
* ただし c は除算により 0 になっているかも知れず、その場合正として
* 扱われてしまうのを避けるため符号判定には df を用いている。
*
* 実際にエンコードする ADPCM データは、符号ビットと振幅ビットの
* 組み合わせである。振幅ビットは c を 2 で割ったもの。
*/
if (df < 0) {
b = (unsigned char)(-c) / 2;
s = 0x08;
} else {
b = (unsigned char)(c) / 2;
s = 0;
}
/* 振幅は 3bit なので、7 で制限する */
if (b > 7)
b = 7;
/*
* こうしておくことで、以後 s は符号つき 4bit。
* b は符号なしの絶対値として使い分けることができる。
*/
s |= b;
/*
* ここまでの変換で、振幅ビット b の実際の比率 c との関係は、
* b : 比率範囲 c
* 0 : 0 <= 比率 < 2
* 1 : 2 <= 比率 < 4
* 2 : 4 <= 比率 < 6
* 3 : 6 <= 比率 < 8
* 4 : 8 <= 比率 < 10
* 5 : 10 <= 比率 < 12
* 6 : 12 <= 比率 < 14
* 7 : 14 <= 比率
* のようになる。
*/
/*
* 次回のPCM値を予測する。すごくすっきりしているが、実際には
*
* static int adpcm_estimindex_0[16] = {
* 1, 3, 5, 7, 9, 11, 13, 15,
* -1, -3, -5, -7, -9, -11, -13, -15
* };
* mc->mc_amp += (short)(adpcm_estimindex_0[(int)s] * 16 / 8 * dl);
*
* なのである。adpcm_estimindex_0[] は adpcm_estimindex[] の
* 1/2 値であり、この数列の意味は上記比率範囲の中央値である。
* この比率に、16=2^4を掛けて16bit化し、(dl/8)を掛けることに
* よって予測差分値が求められ、これを記録するのである。
* そういうわけで、adpcm_estimindex[] をあらかじめ2倍しておくと
* すっきりするのである。
*/
mc->mc_amp += (short)(adpcm_estimindex[(int)s] * dl);
/*
* b の値に従って、次回使用する差分比を予測し mc->mc_estim に
* 保存しておく。全部で 49 段階。
* 余談であるが、ここだけだと adpcm_estimstep[16] は [8] でよい。
* [16] なのは adpcm2pcm との関係上必要だからである。
*/
estim += adpcm_estimstep[b];
if (estim < 0)
estim = 0;
else if (estim > 48)
estim = 48;
mc->mc_estim = estim;
return s;
}
/*
* ホストエンディアンの slinear16 を ADPCM に変換する。
*/
void
msm6258_slinear16_host_to_adpcm(void *hdl, u_char *p, int cc)
{
struct msm6258_softc *sc = hdl;
struct msm6258_codecvar *mc = sc->sc_mc;
short *s = (short *)p;
int i;
u_char f;
for (i = 0; i < cc; i += 4) {
/* MSM6258 の 4bit ADPCM は下の nibble が先 */
f = pcm2adpcm_step(mc, *s++);
f |= pcm2adpcm_step(mc, *s++) << 4;
*p++ = f;
}
}
/*
* slinear16_le を ADPCM に変換する。
* リトルエンディアンのマシンならそのまま。
* ビッグエンディアンのマシンなら先にバイトスワップをかます。
*/
void
msm6258_slinear16_le_to_adpcm(void *hdl, u_char *p, int cc)
{
#if BYTE_ORDER == BIG_ENDIAN
swap_bytes(hdl, p, cc);
#endif
msm6258_slinear16_host_to_adpcm(hdl, p, cc);
}
/*
* slinear16_be を ADPCM に変換する。
* リトルエンディアンのマシンなら先にバイトスワップをかます。
* ビッグエンディアンのマシンならそのまま。
*/
void
msm6258_slinear16_be_to_adpcm(void *hdl, u_char *p, int cc)
{
#if BYTE_ORDER == LITTLE_ENDIAN
swap_bytes(hdl, p, cc);
#endif
msm6258_slinear16_host_to_adpcm(hdl, p, cc);
}
/*
* slinear8 を ADPCM に変換する。
* 本来なら slinear8 を slinear16 に変換してから
* msm6258_slinear16_to_adpcm() を呼ぶべきであろうが最終的に減ると
* 申告してあるデータサイズが、変換途中で一旦増えるのは確保してある
* バッファのサイズを考えると危険であるため 8bit フォーマットを変換
* する際には msm6258_slinear16_to_adpcm() を利用せずに 8bit 用の
* 関数を別に用意している。
*/
void
msm6258_slinear8_to_adpcm(void *hdl, u_char *p, int cc)
{
struct msm6258_softc *sc = hdl;
struct msm6258_codecvar *mc = sc->sc_mc;
u_char *s = p;
int i;
u_char f;
for (i = 0; i < cc; i += 2) {
/* u_char で取り出した *s を256倍して 16bit 値にする */
f = pcm2adpcm_step(mc, (short)(*s++) * 256);
f |= pcm2adpcm_step(mc, (short)(*s++) * 256) << 4;
*p++ = f;
}
}
void
msm6258_ulinear8_to_adpcm(void *hdl, u_char *p, int cc)
{
change_sign8(hdl, p, cc);
msm6258_slinear8_to_adpcm(hdl, p, cc);
}
void
msm6258_mulaw_to_adpcm(void *hdl, u_char *p, int cc)
{
mulaw_to_slinear8(hdl, p, cc);
msm6258_slinear8_to_adpcm(hdl, p, cc);
}
/*
* OkiADPCM -> signed 16bit linear PCM
* OkiADPCM 1サンプルを slinear16 の1サンプルに変換する。
*
* MSM6258 が出力する PCM は 12bit 符号付 PCM (slinear12 にあたる) である。
* 従ってこの関数では同時に振幅変換(12bit -> 16bit) も行なっている。
*/
static inline short
adpcm2pcm_step(struct msm6258_codecvar *mc, u_char b)
{
/*
* mc->mc_estim には、前回の差分比予測値インデックスが入っている。
*/
int estim = (int)mc->mc_estim;
/*
* 実 PCM 値を計算している。本来の式は
*
* mc->mc_amp += adpcm_estim[estim] / 8 * adpcm_estimindex_0[b] * 16;
*
* である。pcm2adpcm_step() でも述べた通り adpcm_estim[] は8倍値で
* あるので 8 で割る。それに比率である adpcm_estimindex_0[] を掛ける。
* 更に 12bit -> 16bit 変換のために 16 (= 2^4) を掛けている。
* adpcm_estimindex_0[] * 2 は adpcm_estimindex[] であるので、
* 上式は実際に使われている以下の式となる。
*/
mc->mc_amp += adpcm_estim[estim] * adpcm_estimindex[b];
/*
* 次回の差分比を予測して mc->mc_estim に保存しておく。
*/
estim += adpcm_estimstep[b];
if (estim < 0)
estim = 0;
else if (estim > 48)
estim = 48;
mc->mc_estim = estim;
return mc->mc_amp;
}
void
msm6258_adpcm_to_slinear16_host(void *hdl, u_char *p, int cc)
{
struct msm6258_softc *sc = hdl;
struct msm6258_codecvar *mc = sc->sc_mc;
short *d = (short *)p;
int i;
u_char a;
/* XXX alignment ? */
memcpy(mc->mc_buf, p, cc / 4);
for (i = 0; i < cc / 4;) {
a = mc->mc_buf[i++];
*d++ = adpcm2pcm_step(mc, a & 0x0f);
*d++ = adpcm2pcm_step(mc, (a >> 4) & 0x0f);
}
}
void
msm6258_adpcm_to_slinear16_le(void *hdl, u_char *p, int cc)
{
msm6258_adpcm_to_slinear16_host(hdl, p, cc);
#if BYTE_ORDER == BIG_ENDIAN
swap_bytes(hdl, p, cc);
#endif
}
void
msm6258_adpcm_to_slinear16_be(void *hdl, u_char *p, int cc)
{
msm6258_adpcm_to_slinear16_host(hdl, p, cc);
#if BYTE_ORDER == LITTLE_ENDIAN
swap_bytes(hdl, p, cc);
#endif
}
void
msm6258_adpcm_to_slinear8(void *hdl, u_char *p, int cc)
{
struct msm6258_softc *sc = hdl;
struct msm6258_codecvar *mc = sc->sc_mc;
char *d = (char *)p;
int i;
u_char a;
/* cc may be even. XXX alignment ? */
memcpy(mc->mc_buf, p, cc / 2);
for (i = 0; i < cc / 2;) {
a = mc->mc_buf[i++];
*d++ = adpcm2pcm_step(mc, a & 0x0f) / 256;
*d++ = adpcm2pcm_step(mc, (a >> 4) & 0x0f) / 256;
}
}
void
msm6258_adpcm_to_ulinear8(void *hdl, u_char *p, int cc)
{
msm6258_adpcm_to_slinear8(hdl, p, cc);
change_sign8(hdl, p, cc);
}
void
msm6258_adpcm_to_mulaw(void *hdl, u_char *p, int cc)
{
msm6258_adpcm_to_slinear8(hdl, p, cc);
slinear8_to_mulaw(hdl, p, cc);
}
目次へ戻る
isaki@NetBSD.org