Skip to content

Commit 7c5366a

Browse files
committed
encoder: add dc_reject filter control
1 parent 9b8d5e1 commit 7c5366a

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

include/opus_defines.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ extern "C" {
179179
#define OPUS_GET_QEXT_REQUEST 4057
180180
#define OPUS_SET_IGNORE_EXTENSIONS_REQUEST 4058
181181
#define OPUS_GET_IGNORE_EXTENSIONS_REQUEST 4059
182+
#define OPUS_SET_DC_FILTER_REQUEST 4060
183+
#define OPUS_GET_DC_FILTER_REQUEST 4061
182184

183185
/** Defines for the presence of extended APIs. */
184186
#define OPUS_HAVE_OPUS_PROJECTION_H
@@ -662,6 +664,14 @@ extern "C" {
662664
* @hideinitializer */
663665
#define OPUS_GET_QEXT(x) OPUS_GET_QEXT_REQUEST, __opus_check_int_ptr(x)
664666

667+
/** Configures the encoder's use of dc_reject filter.
668+
* @hideinitializer */
669+
#define OPUS_SET_DC_FILTER(x) OPUS_SET_DC_FILTER_REQUEST, __opus_check_int(x)
670+
/** Gets the encoder's configured dc_reject filter status.
671+
* @hideinitializer */
672+
#define OPUS_GET_DC_FILTER(x) OPUS_GET_DC_FILTER_REQUEST, __opus_check_int(x)
673+
674+
665675
/**@}*/
666676

667677
/** @defgroup opus_genericctls Generic CTLs

src/opus_demo.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ void print_usage( char* argv[] )
131131
fprintf(stderr, "-d : only runs the decoder (reads the bit-stream as input)\n" );
132132
fprintf(stderr, "-cbr : enable constant bitrate; default: variable bitrate\n" );
133133
fprintf(stderr, "-cvbr : enable constrained variable bitrate; default: unconstrained\n" );
134+
fprintf(stderr, "-dc_filter : enable dc reject filter; default: false\n" );
134135
fprintf(stderr, "-delayed-decision : use look-ahead for speech/music detection (experts only); default: disabled\n" );
135136
fprintf(stderr, "-bandwidth <NB|MB|WB|SWB|FB> : audio bandwidth (from narrowband to fullband); default: sampling rate\n" );
136137
fprintf(stderr, "-framesize <2.5|5|10|20|40|60|80|100|120> : frame size in ms; default: 20 \n" );
@@ -389,6 +390,7 @@ int main(int argc, char *argv[])
389390
unsigned char *fbytes=NULL;
390391
opus_int32 sampling_rate;
391392
int use_vbr;
393+
int dc_filter;
392394
int max_payload_bytes;
393395
int complexity;
394396
int dec_complexity;
@@ -533,6 +535,7 @@ int main(int argc, char *argv[])
533535

534536
/* defaults: */
535537
use_vbr = 1;
538+
dc_filter = 0;
536539
max_payload_bytes = MAX_PACKET;
537540
complexity = 10;
538541
dec_complexity = 0;
@@ -547,6 +550,10 @@ int main(int argc, char *argv[])
547550
check_encoder_option(decode_only, "-cbr");
548551
use_vbr = 0;
549552
args++;
553+
} else if ( strcmp( argv[ args ], "-dc_filter" ) == 0 ) {
554+
check_encoder_option(decode_only, "-dc_filter");
555+
dc_filter = 1;
556+
++args;
550557
} else if( strcmp( argv[ args ], "-bandwidth" ) == 0 ) {
551558
check_encoder_option(decode_only, "-bandwidth");
552559
if (strcmp(argv[ args + 1 ], "NB")==0)
@@ -782,6 +789,7 @@ int main(int argc, char *argv[])
782789
opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate_bps));
783790
opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(bandwidth));
784791
opus_encoder_ctl(enc, OPUS_SET_VBR(use_vbr));
792+
opus_encoder_ctl(enc, OPUS_SET_DC_FILTER(dc_filter));
785793
opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cvbr));
786794
opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));
787795
opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(use_inbandfec));

src/opus_encoder.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ struct OpusEncoder {
144144
#endif
145145
int nonfinal_frame; /* current frame is not the final in a packet */
146146
opus_uint32 rangeFinal;
147+
int dc_filter;
147148
};
148149

149150
/* Transition tables for the voice and music. First column is the
@@ -298,6 +299,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
298299
st->first = 1;
299300
st->mode = MODE_HYBRID;
300301
st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
302+
st->dc_filter = 0;
301303

302304
#ifndef DISABLE_FLOAT_API
303305
tonality_analysis_init(&st->analysis, st->Fs);
@@ -1923,9 +1925,16 @@ static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm,
19231925
}
19241926
}
19251927
#endif
1928+
#ifdef ENABLE_QEXT
1929+
/* FIXME: Avoid glitching when we switch qext on/off dynamically. */
1930+
} else if (st->dc_filter && !st->enable_qext) {
1931+
#else
1932+
/* FIXME: Avoid glitching when we switch dc_filter on/off dynamically. */
1933+
} else if (st->dc_filter) {
1934+
#endif
1935+
dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
19261936
} else {
1927-
for (i=0;i<frame_size*st->channels;i++)
1928-
pcm_buf[total_buffer*st->channels + i] = pcm[i];
1937+
OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels);
19291938
}
19301939
#ifndef FIXED_POINT
19311940
if (float_api)
@@ -2916,6 +2925,26 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
29162925
*value = st->use_vbr;
29172926
}
29182927
break;
2928+
case OPUS_SET_DC_FILTER_REQUEST:
2929+
{
2930+
opus_int32 value = va_arg(ap, opus_int32);
2931+
if(value<0 || value>1)
2932+
{
2933+
goto bad_arg;
2934+
}
2935+
st->dc_filter = value;
2936+
}
2937+
break;
2938+
case OPUS_GET_DC_FILTER_REQUEST:
2939+
{
2940+
opus_int32 *value = va_arg(ap, opus_int32*);
2941+
if (!value)
2942+
{
2943+
goto bad_arg;
2944+
}
2945+
*value = st->dc_filter;
2946+
}
2947+
break;
29192948
case OPUS_SET_VOICE_RATIO_REQUEST:
29202949
{
29212950
opus_int32 value = va_arg(ap, opus_int32);

src/opus_multistream_encoder.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,7 @@ int opus_multistream_encoder_ctl_va_list(OpusMSEncoder *st, int request,
11771177
case OPUS_GET_PREDICTION_DISABLED_REQUEST:
11781178
case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
11791179
case OPUS_GET_QEXT_REQUEST:
1180+
case OPUS_GET_DC_FILTER_REQUEST:
11801181
{
11811182
OpusEncoder *enc;
11821183
/* For int32* GET params, just query the first stream */
@@ -1225,6 +1226,7 @@ int opus_multistream_encoder_ctl_va_list(OpusMSEncoder *st, int request,
12251226
case OPUS_SET_PREDICTION_DISABLED_REQUEST:
12261227
case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
12271228
case OPUS_SET_QEXT_REQUEST:
1229+
case OPUS_SET_DC_FILTER_REQUEST:
12281230
{
12291231
int s;
12301232
/* This works for int32 params */

0 commit comments

Comments
 (0)