Add tonality_analysis_reset().
authorRalph Giles <giles@mozilla.com>
Wed, 30 Dec 2015 23:27:02 +0000 (15:27 -0800)
committerRalph Giles <giles@mozilla.com>
Thu, 31 Dec 2015 08:43:27 +0000 (00:43 -0800)
Add a reset function for the TonalityAnalysisState struct
and call it on encoder reset.

Move the state struct above the clear line in OpusEncoder
so reset doesn't clobber reusable fields. Currently this
is only the arch field, which is moved to to top of the
struct so we can use the same memset-to-the-end pattern
as OpusEncoder.

Signed-off-by: Jean-Marc Valin <jmvalin@jmvalin.ca>
src/analysis.c
src/analysis.h
src/opus_encoder.c

index f8187d0..360ebcc 100644 (file)
@@ -142,7 +142,15 @@ void tonality_analysis_init(TonalityAnalysisState *tonal)
 {
   /* Initialize reusable fields. */
   tonal->arch = opus_select_arch();
-  /* Other fields will be overwritten in use. */
+  /* Clear remaining fields. */
+  tonality_analysis_reset(tonal);
+}
+
+void tonality_analysis_reset(TonalityAnalysisState *tonal)
+{
+  /* Clear non-reusable fields. */
+  char *start = (char*)&tonal->TONALITY_ANALYSIS_RESET_START;
+  OPUS_CLEAR(start, sizeof(TonalityAnalysisState) - (start - (char*)tonal));
 }
 
 void tonality_get_info(TonalityAnalysisState *tonal, AnalysisInfo *info_out, int len)
index 9c2b978..9eae56a 100644 (file)
@@ -39,6 +39,8 @@
 #define DETECT_SIZE 200
 
 typedef struct {
+   int arch;
+#define TONALITY_ANALYSIS_RESET_START angle
    float angle[240];
    float d_angle[240];
    float d2_angle[240];
@@ -76,7 +78,6 @@ typedef struct {
    int read_pos;
    int read_subframe;
    AnalysisInfo info[DETECT_SIZE];
-   int arch;
 } TonalityAnalysisState;
 
 /** Initialize a TonalityAnalysisState struct.
@@ -87,6 +88,12 @@ typedef struct {
  */
 void tonality_analysis_init(TonalityAnalysisState *analysis);
 
+/** Reset a TonalityAnalysisState stuct.
+ *
+ * Call this when there's a discontinuity in the data.
+ */
+void tonality_analysis_reset(TonalityAnalysisState *analysis);
+
 void tonality_get_info(TonalityAnalysisState *tonal, AnalysisInfo *info_out, int len);
 
 void run_analysis(TonalityAnalysisState *analysis, const CELTMode *celt_mode, const void *analysis_pcm,
index e1ca868..4340de4 100644 (file)
@@ -82,6 +82,9 @@ struct OpusEncoder {
     int          encoder_buffer;
     int          lfe;
     int          arch;
+#ifndef DISABLE_FLOAT_API
+    TonalityAnalysisState analysis;
+#endif
 
 #define OPUS_ENCODER_RESET_START stream_channels
     int          stream_channels;
@@ -101,7 +104,6 @@ struct OpusEncoder {
     StereoWidthState width_mem;
     opus_val16   delay_buffer[MAX_ENCODER_BUFFER*2];
 #ifndef DISABLE_FLOAT_API
-    TonalityAnalysisState analysis;
     int          detected_bandwidth;
 #endif
     opus_uint32  rangeFinal;
@@ -2453,10 +2455,12 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
            void *silk_enc;
            silk_EncControlStruct dummy;
            silk_enc = (char*)st+st->silk_enc_offset;
+#ifndef DISABLE_FLOAT_API
+           tonality_analysis_reset(&st->analysis);
+#endif
 
-           OPUS_CLEAR((char*)&st->OPUS_ENCODER_RESET_START,
-                 sizeof(OpusEncoder)-
-                 ((char*)&st->OPUS_ENCODER_RESET_START - (char*)st));
+           char *start = (char*)&st->OPUS_ENCODER_RESET_START;
+           OPUS_CLEAR(start, sizeof(OpusEncoder) - (start - (char*)st));
 
            celt_encoder_ctl(celt_enc, OPUS_RESET_STATE);
            silk_InitEncoder( silk_enc, st->arch, &dummy );
@@ -2467,9 +2471,6 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
            st->mode = MODE_HYBRID;
            st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
            st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
-#ifndef DISABLE_FLOAT_API
-           tonality_analysis_init(&st->analysis);
-#endif
         }
         break;
         case OPUS_SET_FORCE_MODE_REQUEST: