mirror of
https://github.com/google/liblc3.git
synced 2026-06-02 01:47:02 +00:00
feature: Make encoder/decoder states relocatable
This commit is contained in:
@@ -107,8 +107,8 @@ struct lc3_encoder {
|
||||
lc3_ltpf_analysis_t ltpf;
|
||||
lc3_spec_analysis_t spec;
|
||||
|
||||
int16_t *xt;
|
||||
float *xs, *xd, s[1];
|
||||
int xt_off, xs_off, xd_off;
|
||||
float x[1];
|
||||
};
|
||||
|
||||
#define LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz) \
|
||||
@@ -118,7 +118,7 @@ struct lc3_encoder {
|
||||
#define LC3_ENCODER_MEM_T(dt_us, sr_hz) \
|
||||
struct { \
|
||||
struct lc3_encoder __e; \
|
||||
float __s[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
|
||||
float __x[LC3_ENCODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
|
||||
}
|
||||
|
||||
|
||||
@@ -145,7 +145,8 @@ struct lc3_decoder {
|
||||
lc3_ltpf_synthesis_t ltpf;
|
||||
lc3_plc_state_t plc;
|
||||
|
||||
float *xh, *xs, *xd, *xg, s[1];
|
||||
int xh_off, xs_off, xd_off, xg_off;
|
||||
float x[1];
|
||||
};
|
||||
|
||||
#define LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz) \
|
||||
@@ -155,7 +156,7 @@ struct lc3_decoder {
|
||||
#define LC3_DECODER_MEM_T(dt_us, sr_hz) \
|
||||
struct { \
|
||||
struct lc3_decoder __d; \
|
||||
float __s[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
|
||||
float __x[LC3_DECODER_BUFFER_COUNT(dt_us, sr_hz)-1]; \
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -157,8 +157,8 @@ static void load_s16(
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = encoder->xt;
|
||||
float *xs = encoder->xs;
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += stride)
|
||||
@@ -178,8 +178,8 @@ static void load_s24(
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = encoder->xt;
|
||||
float *xs = encoder->xs;
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += stride) {
|
||||
@@ -201,8 +201,8 @@ static void load_s24_3le(
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = encoder->xt;
|
||||
float *xs = encoder->xs;
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += 3*stride) {
|
||||
@@ -228,8 +228,8 @@ static void load_float(
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr_pcm;
|
||||
|
||||
int16_t *xt = encoder->xt;
|
||||
float *xs = encoder->xs;
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for (int i = 0; i < ns; i++, pcm += stride) {
|
||||
@@ -253,9 +253,9 @@ static void analyze(struct lc3_encoder *encoder,
|
||||
int ns = LC3_NS(dt, sr_pcm);
|
||||
int nt = LC3_NT(sr_pcm);
|
||||
|
||||
int16_t *xt = encoder->xt;
|
||||
float *xs = encoder->xs;
|
||||
float *xd = encoder->xd;
|
||||
int16_t *xt = (int16_t *)encoder->x + encoder->xt_off;
|
||||
float *xs = encoder->x + encoder->xs_off;
|
||||
float *xd = encoder->x + encoder->xd_off;
|
||||
float *xf = xs;
|
||||
|
||||
/* --- Temporal --- */
|
||||
@@ -301,7 +301,7 @@ static void encode(struct lc3_encoder *encoder,
|
||||
enum lc3_dt dt = encoder->dt;
|
||||
enum lc3_srate sr = encoder->sr;
|
||||
enum lc3_bandwidth bw = side->bw;
|
||||
float *xf = encoder->xs;
|
||||
float *xf = encoder->x + encoder->xs_off;
|
||||
|
||||
lc3_bits_t bits;
|
||||
|
||||
@@ -363,12 +363,12 @@ struct lc3_encoder *lc3_setup_encoder(
|
||||
.dt = dt, .sr = sr,
|
||||
.sr_pcm = sr_pcm,
|
||||
|
||||
.xt = (int16_t *)encoder->s + nt,
|
||||
.xs = encoder->s + (nt+ns)/2,
|
||||
.xd = encoder->s + (nt+ns)/2 + ns,
|
||||
.xt_off = nt,
|
||||
.xs_off = (nt + ns) / 2,
|
||||
.xd_off = (nt + ns) / 2 + ns,
|
||||
};
|
||||
|
||||
memset(encoder->s, 0,
|
||||
memset(encoder->x, 0,
|
||||
LC3_ENCODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
|
||||
|
||||
return encoder;
|
||||
@@ -425,7 +425,7 @@ static void store_s16(
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->xs;
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += stride) {
|
||||
@@ -447,7 +447,7 @@ static void store_s24(
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->xs;
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += stride) {
|
||||
@@ -470,7 +470,7 @@ static void store_s24_3le(
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->xs;
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += 3*stride) {
|
||||
@@ -497,7 +497,7 @@ static void store_float(
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr_pcm;
|
||||
|
||||
float *xs = decoder->xs;
|
||||
float *xs = decoder->x + decoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
|
||||
for ( ; ns > 0; ns--, xs++, pcm += stride) {
|
||||
@@ -519,7 +519,7 @@ static int decode(struct lc3_decoder *decoder,
|
||||
enum lc3_dt dt = decoder->dt;
|
||||
enum lc3_srate sr = decoder->sr;
|
||||
|
||||
float *xf = decoder->xs;
|
||||
float *xf = decoder->x + decoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr);
|
||||
int ne = LC3_NE(dt, sr);
|
||||
|
||||
@@ -566,14 +566,16 @@ static void synthesize(struct lc3_decoder *decoder,
|
||||
enum lc3_srate sr = decoder->sr;
|
||||
enum lc3_srate sr_pcm = decoder->sr_pcm;
|
||||
|
||||
float *xf = decoder->xs;
|
||||
float *xf = decoder->x + decoder->xs_off;
|
||||
int ns = LC3_NS(dt, sr_pcm);
|
||||
int ne = LC3_NE(dt, sr);
|
||||
|
||||
float *xg = decoder->xg;
|
||||
float *xd = decoder->xd;
|
||||
float *xg = decoder->x + decoder->xg_off;
|
||||
float *xs = xf;
|
||||
|
||||
float *xd = decoder->x + decoder->xd_off;
|
||||
float *xh = decoder->x + decoder->xh_off;
|
||||
|
||||
if (side) {
|
||||
enum lc3_bandwidth bw = side->bw;
|
||||
|
||||
@@ -594,7 +596,7 @@ static void synthesize(struct lc3_decoder *decoder,
|
||||
}
|
||||
|
||||
lc3_ltpf_synthesize(dt, sr_pcm, nbytes, &decoder->ltpf,
|
||||
side && side->pitch_present ? &side->ltpf : NULL, decoder->xh, xs);
|
||||
side && side->pitch_present ? &side->ltpf : NULL, xh, xs);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -608,8 +610,8 @@ static void complete(struct lc3_decoder *decoder)
|
||||
int nh = LC3_NH(dt, sr_pcm);
|
||||
int ns = LC3_NS(dt, sr_pcm);
|
||||
|
||||
decoder->xs = decoder->xs - decoder->xh < nh - ns ?
|
||||
decoder->xs + ns : decoder->xh;
|
||||
decoder->xs_off = decoder->xs_off - decoder->xh_off < nh - ns ?
|
||||
decoder->xs_off + ns : decoder->xh_off;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -650,15 +652,15 @@ struct lc3_decoder *lc3_setup_decoder(
|
||||
.dt = dt, .sr = sr,
|
||||
.sr_pcm = sr_pcm,
|
||||
|
||||
.xh = decoder->s,
|
||||
.xs = decoder->s + nh-ns,
|
||||
.xd = decoder->s + nh,
|
||||
.xg = decoder->s + nh+nd,
|
||||
.xh_off = 0,
|
||||
.xs_off = nh - ns,
|
||||
.xd_off = nh,
|
||||
.xg_off = nh + nd,
|
||||
};
|
||||
|
||||
lc3_plc_reset(&decoder->plc);
|
||||
|
||||
memset(decoder->s, 0,
|
||||
memset(decoder->x, 0,
|
||||
LC3_DECODER_BUFFER_COUNT(dt_us, sr_pcm_hz) * sizeof(float));
|
||||
|
||||
return decoder;
|
||||
|
||||
+21
-14
@@ -726,13 +726,14 @@ static PyObject *from_encoder(PyObject *obj, const struct lc3_encoder *enc)
|
||||
from_spec_analysis(NULL, &enc->spec));
|
||||
|
||||
PyDict_SetItemString(obj, "xt",
|
||||
new_1d_copy(NPY_INT16, nt+ns, enc->xt-nt));
|
||||
new_1d_copy(NPY_INT16, nt+ns,
|
||||
(int16_t *)enc->x + enc->xt_off - nt));
|
||||
|
||||
PyDict_SetItemString(obj, "xs",
|
||||
new_1d_copy(NPY_FLOAT, ns, enc->xs));
|
||||
new_1d_copy(NPY_FLOAT, ns, enc->x + enc->xs_off));
|
||||
|
||||
PyDict_SetItemString(obj, "xd",
|
||||
new_1d_copy(NPY_FLOAT, nd, enc->xd));
|
||||
new_1d_copy(NPY_FLOAT, nd, enc->x + enc->xd_off));
|
||||
|
||||
return obj;
|
||||
}
|
||||
@@ -772,15 +773,18 @@ static PyObject *to_encoder(PyObject *obj, struct lc3_encoder *enc)
|
||||
PyDict_GetItemString(obj, "quant"), &enc->spec));
|
||||
|
||||
CTYPES_CHECK("encoder.xt", xt_obj = to_1d_copy(
|
||||
PyDict_GetItemString(obj, "xt"), NPY_INT16, enc->xt-nt, ns+nt));
|
||||
PyDict_GetItemString(obj, "xt"), NPY_INT16,
|
||||
(int16_t *)enc->x + enc->xt_off - nt, ns+nt));
|
||||
PyDict_SetItemString(obj, "xt", xt_obj);
|
||||
|
||||
CTYPES_CHECK("encoder.xs", xs_obj = to_1d_copy(
|
||||
PyDict_GetItemString(obj, "xs"), NPY_FLOAT, enc->xs, ns));
|
||||
PyDict_GetItemString(obj, "xs"), NPY_FLOAT,
|
||||
enc->x + enc->xs_off, ns));
|
||||
PyDict_SetItemString(obj, "xs", xs_obj);
|
||||
|
||||
CTYPES_CHECK("encoder.xd", xd_obj = to_1d_copy(
|
||||
PyDict_GetItemString(obj, "xd"), NPY_FLOAT, enc->xd, nd));
|
||||
PyDict_GetItemString(obj, "xd"), NPY_FLOAT,
|
||||
enc->x + enc->xd_off, nd));
|
||||
PyDict_SetItemString(obj, "xd", xd_obj);
|
||||
|
||||
return obj;
|
||||
@@ -791,7 +795,7 @@ static PyObject *from_decoder(PyObject *obj, const struct lc3_decoder *dec)
|
||||
{
|
||||
unsigned dt = dec->dt, sr = dec->sr;
|
||||
unsigned sr_pcm = dec->sr_pcm;
|
||||
unsigned xs_pos = dec->xs - dec->xh;
|
||||
unsigned xs_pos = dec->xs_off - dec->xh_off;
|
||||
int nh = LC3_NH(dt, sr);
|
||||
int ns = LC3_NS(dt, sr);
|
||||
int nd = LC3_ND(dt, sr);
|
||||
@@ -814,16 +818,16 @@ static PyObject *from_decoder(PyObject *obj, const struct lc3_decoder *dec)
|
||||
new_plc_state(&dec->plc));
|
||||
|
||||
PyDict_SetItemString(obj, "xh",
|
||||
new_1d_copy(NPY_FLOAT, nh, dec->xh));
|
||||
new_1d_copy(NPY_FLOAT, nh, dec->x + dec->xh_off));
|
||||
|
||||
PyDict_SetItemString(obj, "xs_pos",
|
||||
new_scalar(NPY_INT, &xs_pos));
|
||||
|
||||
PyDict_SetItemString(obj, "xd",
|
||||
new_1d_copy(NPY_FLOAT, nd, dec->xd));
|
||||
new_1d_copy(NPY_FLOAT, nd, dec->x + dec->xd_off));
|
||||
|
||||
PyDict_SetItemString(obj, "xg",
|
||||
new_1d_copy(NPY_FLOAT, ns, dec->xg));
|
||||
new_1d_copy(NPY_FLOAT, ns, dec->x + dec->xg_off));
|
||||
|
||||
return obj;
|
||||
}
|
||||
@@ -860,19 +864,22 @@ static PyObject *to_decoder(PyObject *obj, struct lc3_decoder *dec)
|
||||
PyDict_GetItemString(obj, "plc"), &dec->plc));
|
||||
|
||||
CTYPES_CHECK("decoder.xh", xh_obj = to_1d_copy(
|
||||
PyDict_GetItemString(obj, "xh"), NPY_FLOAT, dec->xh, nh));
|
||||
PyDict_GetItemString(obj, "xh"), NPY_FLOAT,
|
||||
dec->x + dec->xh_off, nh));
|
||||
PyDict_SetItemString(obj, "xh", xh_obj);
|
||||
|
||||
CTYPES_CHECK("decoder.xs", to_scalar(
|
||||
PyDict_GetItemString(obj, "xs_pos"), NPY_INT, &xs_pos));
|
||||
dec->xs = dec->xh + xs_pos;
|
||||
dec->xs_off = dec->xh_off + xs_pos;
|
||||
|
||||
CTYPES_CHECK("decoder.xd", xd_obj = to_1d_copy(
|
||||
PyDict_GetItemString(obj, "xd"), NPY_FLOAT, dec->xd, nd));
|
||||
PyDict_GetItemString(obj, "xd"), NPY_FLOAT,
|
||||
dec->x + dec->xd_off, nd));
|
||||
PyDict_SetItemString(obj, "xd", xd_obj);
|
||||
|
||||
CTYPES_CHECK("decoder.xg", xg_obj = to_1d_copy(
|
||||
PyDict_GetItemString(obj, "xg"), NPY_FLOAT, dec->xg, ns));
|
||||
PyDict_GetItemString(obj, "xg"), NPY_FLOAT,
|
||||
dec->x + dec->xg_off, ns));
|
||||
PyDict_SetItemString(obj, "xg", xg_obj);
|
||||
|
||||
return obj;
|
||||
|
||||
Reference in New Issue
Block a user