Skip to content

Commit 43efb99

Browse files
committed
Reduce SILK encoder state size for mono
1 parent 23b3805 commit 43efb99

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

silk/API.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ typedef struct {
6060
/* Get size in bytes of the Silk encoder state */
6161
/***********************************************/
6262
opus_int silk_Get_Encoder_Size( /* O Returns error code */
63-
opus_int *encSizeBytes /* O Number of bytes in SILK encoder state */
63+
opus_int *encSizeBytes, /* O Number of bytes in SILK encoder state */
64+
opus_int channels /* I Number of channels */
6465
);
6566

6667
/*************************/
6768
/* Init or reset encoder */
6869
/*************************/
6970
opus_int silk_InitEncoder( /* O Returns error code */
7071
void *encState, /* I/O State */
72+
int channels, /* I Number of channels */
7173
int arch, /* I Run-time architecture */
7274
silk_EncControlStruct *encStatus /* O Encoder Status */
7375
);

silk/enc_API.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,17 @@ static opus_int silk_QueryEncoder( /* O Returns error co
5858
/****************************************/
5959

6060
opus_int silk_Get_Encoder_Size( /* O Returns error code */
61-
opus_int *encSizeBytes /* O Number of bytes in SILK encoder state */
61+
opus_int *encSizeBytes, /* O Number of bytes in SILK encoder state */
62+
opus_int channels /* I Number of channels */
6263
)
6364
{
6465
opus_int ret = SILK_NO_ERROR;
6566

6667
*encSizeBytes = sizeof( silk_encoder );
68+
/* Skip second encoder state for mono. */
69+
if ( channels == 1 ) {
70+
*encSizeBytes -= sizeof( silk_encoder_state_Fxx );
71+
}
6772

6873
return ret;
6974
}
@@ -73,6 +78,7 @@ opus_int silk_Get_Encoder_Size( /* O Returns error co
7378
/*************************/
7479
opus_int silk_InitEncoder( /* O Returns error code */
7580
void *encState, /* I/O State */
81+
int channels, /* I Number of channels */
7682
int arch, /* I Run-time architecture */
7783
silk_EncControlStruct *encStatus /* O Encoder Status */
7884
)
@@ -82,9 +88,9 @@ opus_int silk_InitEncoder( /* O Returns error co
8288

8389
psEnc = (silk_encoder *)encState;
8490

85-
/* Reset encoder */
86-
silk_memset( psEnc, 0, sizeof( silk_encoder ) );
87-
for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) {
91+
/* Reset encoder. Skip second encoder state for mono. */
92+
silk_memset( psEnc, 0, sizeof( silk_encoder ) - (channels==1)*sizeof( silk_encoder_state_Fxx ) );
93+
for( n = 0; n < channels; n++ ) {
8894
if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) {
8995
celt_assert( 0 );
9096
}
@@ -162,13 +168,16 @@ opus_int silk_Encode( /* O Returns error co
162168
opus_int transition, curr_block, tot_blocks;
163169
SAVE_STACK;
164170

171+
celt_assert( encControl->nChannelsAPI >= encControl->nChannelsInternal && encControl->nChannelsAPI >= psEnc->nChannelsInternal );
165172
if (encControl->reducedDependency)
166173
{
167-
psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1;
168-
psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1;
174+
for( n = 0; n < encControl->nChannelsAPI; n++ ) {
175+
psEnc->state_Fxx[ n ].sCmn.first_frame_after_reset = 1;
176+
}
177+
}
178+
for( n = 0; n < encControl->nChannelsAPI; n++ ) {
179+
psEnc->state_Fxx[ n ].sCmn.nFramesEncoded = 0;
169180
}
170-
psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0;
171-
172181
/* Check values in encoder control structure */
173182
if( ( ret = check_control_input( encControl ) ) != 0 ) {
174183
celt_assert( 0 );

silk/fixed/structs_FIX.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ typedef struct {
9696
/* Encoder Super Struct */
9797
/************************/
9898
typedef struct {
99-
silk_encoder_state_FIX state_Fxx[ ENCODER_NUM_CHANNELS ];
10099
stereo_enc_state sStereo;
101100
opus_int32 nBitsUsedLBRR;
102101
opus_int32 nBitsExceeded;
@@ -106,6 +105,8 @@ typedef struct {
106105
opus_int timeSinceSwitchAllowed_ms;
107106
opus_int allowBandwidthSwitch;
108107
opus_int prev_decode_only_middle;
108+
/* This needs to be last so we can skip the second state for mono. */
109+
silk_encoder_state_FIX state_Fxx[ ENCODER_NUM_CHANNELS ];
109110
} silk_encoder;
110111

111112

silk/float/structs_FLP.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ typedef struct {
9393
/* Encoder Super Struct */
9494
/************************/
9595
typedef struct {
96-
silk_encoder_state_FLP state_Fxx[ ENCODER_NUM_CHANNELS ];
9796
stereo_enc_state sStereo;
9897
opus_int32 nBitsUsedLBRR;
9998
opus_int32 nBitsExceeded;
@@ -103,6 +102,8 @@ typedef struct {
103102
opus_int timeSinceSwitchAllowed_ms;
104103
opus_int allowBandwidthSwitch;
105104
opus_int prev_decode_only_middle;
105+
/* This needs to be last so we can skip the second state for mono. */
106+
silk_encoder_state_FLP state_Fxx[ ENCODER_NUM_CHANNELS ];
106107
} silk_encoder;
107108

108109
#ifdef __cplusplus

src/opus_encoder.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ int opus_encoder_get_size(int channels)
198198
int ret;
199199
if (channels<1 || channels > 2)
200200
return 0;
201-
ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
201+
ret = silk_Get_Encoder_Size( &silkEncSizeBytes, channels );
202202
if (ret)
203203
return 0;
204204
silkEncSizeBytes = align(silkEncSizeBytes);
@@ -224,7 +224,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
224224

225225
OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
226226
/* Create SILK encoder */
227-
ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
227+
ret = silk_Get_Encoder_Size( &silkEncSizeBytes, channels );
228228
if (ret)
229229
return OPUS_BAD_ARG;
230230
silkEncSizeBytes = align(silkEncSizeBytes);
@@ -239,7 +239,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
239239

240240
st->arch = opus_select_arch();
241241

242-
ret = silk_InitEncoder( silk_enc, st->arch, &st->silk_mode );
242+
ret = silk_InitEncoder( silk_enc, st->channels, st->arch, &st->silk_mode );
243243
if(ret)return OPUS_INTERNAL_ERROR;
244244

245245
/* default SILK parameters */
@@ -1520,7 +1520,7 @@ opus_int32 opus_encode_native(OpusEncoder *st, const opus_res *pcm, int frame_si
15201520
if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY)
15211521
{
15221522
silk_EncControlStruct dummy;
1523-
silk_InitEncoder( silk_enc, st->arch, &dummy);
1523+
silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy);
15241524
prefill=1;
15251525
}
15261526

@@ -3151,7 +3151,7 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
31513151
OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
31523152

31533153
celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
3154-
silk_InitEncoder( silk_enc, st->arch, &dummy );
3154+
silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy );
31553155
#ifdef ENABLE_DRED
31563156
/* Initialize DRED Encoder */
31573157
dred_encoder_reset( &st->dred_encoder );

0 commit comments

Comments
 (0)