Skip to content

Commit b9510f4

Browse files
committed
Merge branch 'main' of https://github.com/aikiriao/SRLA
2 parents 5cea9d4 + b3ca25e commit b9510f4

6 files changed

Lines changed: 80 additions & 9 deletions

File tree

include/srla.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#include "srla_stdint.h"
55

66
/* フォーマットバージョン */
7-
#define SRLA_FORMAT_VERSION 9
7+
#define SRLA_FORMAT_VERSION 10
88

99
/* コーデックバージョン */
10-
#define SRLA_CODEC_VERSION 17
10+
#define SRLA_CODEC_VERSION 18
1111

1212
/* ヘッダサイズ */
1313
#define SRLA_HEADER_SIZE 30

libs/srla_decoder/src/srla_decoder.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,8 @@ static SRLAApiResult SRLADecoder_DecodeCompressData(
556556
num_decode_samples, decoder->ltp_coef[ch], decoder->ltp_order[ch],
557557
decoder->ltp_period[ch], SRLA_LTP_COEFFICIENT_BITWIDTH - 1);
558558
/* デエンファシス */
559-
SRLAPreemphasisFilter_MultiStageDeemphasis(
560-
decoder->de_emphasis[ch], SRLA_NUM_PREEMPHASIS_FILTERS, buffer[ch], num_decode_samples);
559+
SRLAPreemphasisFilter_Deemphasis(
560+
decoder->de_emphasis[ch], buffer[ch], num_decode_samples);
561561
}
562562

563563
/* マルチチャンネル処理 */

libs/srla_encoder/src/srla_encoder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ static SRLAError SRLAEncoder_ComputeCoefficientsPerChannel(
997997
{
998998
const int32_t head = buffer_int[0];
999999
struct SRLAPreemphasisFilter filter[SRLA_NUM_PREEMPHASIS_FILTERS] = { 0, };
1000-
SRLAPreemphasisFilter_CalculateMultiStageCoefficients(filter, SRLA_NUM_PREEMPHASIS_FILTERS, buffer_int, num_samples);
1000+
SRLAPreemphasisFilter_CalculateCoefficient(filter, buffer_int, num_samples);
10011001
for (p = 0; p < SRLA_NUM_PREEMPHASIS_FILTERS; p++) {
10021002
filter[p].prev = head;
10031003
SRLAPreemphasisFilter_Preemphasis(&filter[p], buffer_int, num_samples);

libs/srla_internal/include/srla_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/* プリエンファシスの係数シフト量 */
1515
#define SRLA_PREEMPHASIS_COEF_SHIFT 4
1616
/* プリエンファシスフィルタの適用回数 */
17-
#define SRLA_NUM_PREEMPHASIS_FILTERS 2
17+
#define SRLA_NUM_PREEMPHASIS_FILTERS 1
1818
/* LPC係数のビット幅 */
1919
#define SRLA_LPC_COEFFICIENT_BITWIDTH 8
2020
/* LPC係数右シフト量のビット幅 */

libs/srla_internal/include/srla_utility.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ uint32_t SRLAUtility_ComputeOffsetLeftShift(
138138
/* プリエンファシスフィルタ初期化 */
139139
void SRLAPreemphasisFilter_Initialize(struct SRLAPreemphasisFilter *preem);
140140

141+
/* プリエンファシスフィルタの係数計算 */
142+
void SRLAPreemphasisFilter_CalculateCoefficient(
143+
struct SRLAPreemphasisFilter *preem, const int32_t *data, uint32_t num_samples);
144+
141145
/* 多段プリエンファシスの係数計算 */
142146
void SRLAPreemphasisFilter_CalculateMultiStageCoefficients(
143147
struct SRLAPreemphasisFilter *preem, uint32_t num_preem, const int32_t *buffer, uint32_t num_samples);
@@ -146,6 +150,10 @@ void SRLAPreemphasisFilter_CalculateMultiStageCoefficients(
146150
void SRLAPreemphasisFilter_Preemphasis(
147151
struct SRLAPreemphasisFilter *preem, int32_t *buffer, uint32_t num_samples);
148152

153+
/* デエンファシス */
154+
void SRLAPreemphasisFilter_Deemphasis(
155+
struct SRLAPreemphasisFilter *preem, int32_t *buffer, uint32_t num_samples);
156+
149157
/* デエンファシスを複数回適用 */
150158
void SRLAPreemphasisFilter_MultiStageDeemphasis(
151159
struct SRLAPreemphasisFilter *preem, uint32_t num_preem, int32_t *buffer, uint32_t num_samples);

libs/srla_internal/src/srla_utility.c

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ uint32_t SRLAUtility_ComputeOffsetLeftShift(
202202
return offset_shift;
203203
}
204204

205-
206205
/* プリエンファシスフィルタ初期化 */
207206
void SRLAPreemphasisFilter_Initialize(struct SRLAPreemphasisFilter *preem)
208207
{
@@ -211,6 +210,52 @@ void SRLAPreemphasisFilter_Initialize(struct SRLAPreemphasisFilter *preem)
211210
preem->coef = 0;
212211
}
213212

213+
/* プリエンファシスの係数計算 */
214+
void SRLAPreemphasisFilter_CalculateCoefficient(
215+
struct SRLAPreemphasisFilter *preem, const int32_t *data, uint32_t num_samples)
216+
{
217+
uint32_t i;
218+
double r0, r1;
219+
double curr, succ;
220+
double double_coef;
221+
222+
SRLA_ASSERT(preem != NULL);
223+
SRLA_ASSERT(data != NULL);
224+
225+
/* 相関の計算 */
226+
curr = data[0];
227+
succ = data[1];
228+
r0 = r1 = 0.0;
229+
for (i = 0; i < num_samples - 2; i++) {
230+
const double succsucc = data[i + 2];
231+
r0 += curr * curr;
232+
r1 += curr * succ;
233+
curr = succ;
234+
succ = succsucc;
235+
}
236+
/* i = num_samples - 1 */
237+
r0 += curr * curr;
238+
r1 += curr * succ;
239+
curr = succ;
240+
r0 += curr * curr;
241+
SRLA_ASSERT(r0 >= r1);
242+
243+
/* 分散が小さい場合は0を設定 */
244+
if (r0 < 1e-6) {
245+
preem->coef = 0;
246+
return;
247+
}
248+
249+
/* 係数計算・固定小数化 */
250+
{
251+
const double double_coef = r1 / r0;
252+
int32_t coef = (int32_t)SRLAUtility_Round(double_coef * pow(2.0f, SRLA_PREEMPHASIS_COEF_SHIFT));
253+
/* 丸め込み */
254+
coef = SRLAUTILITY_INNER_VALUE(coef, -(1 << SRLA_PREEMPHASIS_COEF_SHIFT), (1 << SRLA_PREEMPHASIS_COEF_SHIFT) - 1);
255+
preem->coef = coef;
256+
}
257+
}
258+
214259
/* 多段プリエンファシスの係数計算 */
215260
void SRLAPreemphasisFilter_CalculateMultiStageCoefficients(
216261
struct SRLAPreemphasisFilter *preem, uint32_t num_preem, const int32_t *buffer, uint32_t num_samples)
@@ -221,7 +266,6 @@ void SRLAPreemphasisFilter_CalculateMultiStageCoefficients(
221266
double double_coef[SRLA_NUM_PREEMPHASIS_FILTERS];
222267

223268
/* 注意)現段階では2回を前提 */
224-
SRLA_STATIC_ASSERT(SRLA_NUM_PREEMPHASIS_FILTERS == 2);
225269
SRLA_ASSERT(num_preem == 2);
226270

227271
SRLA_ASSERT(preem != NULL);
@@ -313,6 +357,26 @@ void SRLAPreemphasisFilter_Preemphasis(
313357
preem->prev = prev;
314358
}
315359

360+
/* デエンファシスを適用 */
361+
void SRLAPreemphasisFilter_Deemphasis(
362+
struct SRLAPreemphasisFilter *preem, int32_t *buffer, uint32_t num_samples)
363+
{
364+
uint32_t smpl;
365+
const int32_t c0 = preem[0].coef;
366+
367+
SRLA_ASSERT(buffer != NULL);
368+
SRLA_ASSERT(preem != NULL);
369+
370+
buffer[0] += (preem[0].prev * c0) >> SRLA_PREEMPHASIS_COEF_SHIFT;
371+
372+
for (smpl = 2; smpl < num_samples; smpl++) {
373+
buffer[smpl - 1] += (buffer[smpl - 2] * c0) >> SRLA_PREEMPHASIS_COEF_SHIFT;
374+
}
375+
376+
preem[0].prev = buffer[num_samples - 1];
377+
buffer[num_samples - 1] += (buffer[num_samples - 2] * c0) >> SRLA_PREEMPHASIS_COEF_SHIFT;
378+
}
379+
316380
/* デエンファシスを複数回適用 */
317381
void SRLAPreemphasisFilter_MultiStageDeemphasis(
318382
struct SRLAPreemphasisFilter *preem, uint32_t num_preem, int32_t *buffer, uint32_t num_samples)
@@ -322,7 +386,6 @@ void SRLAPreemphasisFilter_MultiStageDeemphasis(
322386
const int32_t c1 = preem[1].coef;
323387

324388
/* 注意)現段階では2回を前提 */
325-
SRLA_STATIC_ASSERT(SRLA_NUM_PREEMPHASIS_FILTERS == 2);
326389
SRLA_ASSERT(num_preem == 2);
327390

328391
SRLA_ASSERT(buffer != NULL);

0 commit comments

Comments
 (0)