shithub: opus

Download patch

ref: 949902fd1b0e9e9215eee2d4df4ae56b0fb9648d
parent: 88b7abb6acb83494b23f9640f706caad6e257d3e
author: Jean-Marc Valin <[email protected]>
date: Tue Mar 11 06:43:06 EDT 2008

const correctness. celt_mode_create() now makes a copy of the static mode
(if STATIC_MODES is defined) instead of attempting to modify read-only
storage.

--- a/libcelt/dump_modes.c
+++ b/libcelt/dump_modes.c
@@ -54,6 +54,8 @@
 void dump_modes(FILE *file, CELTMode **modes, int nb_modes)
 {
    int i, j;
+   fprintf(file, "/* The contents of this file is automatically generated and contains static\n");
+   fprintf(file, "   definitions for some pre-defined modes */\n");
    fprintf(file, "#include \"modes.h\"\n");
    fprintf(file, "#include \"rate.h\"\n");
 
@@ -63,7 +65,7 @@
       CELTMode *mode = modes[i];
       fprintf(file, "#ifndef DEF_EBANDS%d_%d\n", mode->Fs, mode->mdctSize);
       fprintf(file, "#define DEF_EBANDS%d_%d\n", mode->Fs, mode->mdctSize);
-      fprintf (file, "const celt_int16_t eBands%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbEBands+2);
+      fprintf (file, "static const celt_int16_t eBands%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbEBands+2);
       for (j=0;j<mode->nbEBands+2;j++)
          fprintf (file, "%d, ", mode->eBands[j]);
       fprintf (file, "};\n");
@@ -73,7 +75,7 @@
       
       fprintf(file, "#ifndef DEF_PBANDS%d_%d\n", mode->Fs, mode->mdctSize);
       fprintf(file, "#define DEF_PBANDS%d_%d\n", mode->Fs, mode->mdctSize);
-      fprintf (file, "const celt_int16_t pBands%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbPBands+2);
+      fprintf (file, "static const celt_int16_t pBands%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbPBands+2);
       for (j=0;j<mode->nbPBands+2;j++)
          fprintf (file, "%d, ", mode->pBands[j]);
       printf ("};\n");
@@ -83,7 +85,7 @@
       
       fprintf(file, "#ifndef DEF_WINDOW%d\n", mode->overlap);
       fprintf(file, "#define DEF_WINDOW%d\n", mode->overlap);
-      fprintf (file, "const celt_word16_t window%d[%d] = {\n", mode->overlap, mode->overlap);
+      fprintf (file, "static const celt_word16_t window%d[%d] = {\n", mode->overlap, mode->overlap);
       for (j=0;j<mode->overlap;j++)
          fprintf (file, WORD16 ", ", mode->window[j]);
       printf ("};\n");
@@ -92,7 +94,7 @@
       
       fprintf(file, "#ifndef DEF_PSY%d\n", mode->Fs);
       fprintf(file, "#define DEF_PSY%d\n", mode->Fs);
-      fprintf (file, "const celt_word16_t psy_decayR_%d[%d] = {\n", mode->Fs, MAX_PERIOD/2);
+      fprintf (file, "static const celt_word16_t psy_decayR_%d[%d] = {\n", mode->Fs, MAX_PERIOD/2);
       for (j=0;j<MAX_PERIOD/2;j++)
          fprintf (file, WORD16 ", ", mode->psy.decayR[j]);
       printf ("};\n");
@@ -102,7 +104,7 @@
       
       fprintf(file, "#ifndef DEF_ALLOC_VECTORS%d_%d\n", mode->Fs, mode->mdctSize);
       fprintf(file, "#define DEF_ALLOC_VECTORS%d_%d\n", mode->Fs, mode->mdctSize);
-      fprintf (file, "const celt_int16_t allocVectors%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbEBands*mode->nbAllocVectors);
+      fprintf (file, "static const celt_int16_t allocVectors%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbEBands*mode->nbAllocVectors);
       for (j=0;j<mode->nbAllocVectors;j++)
       {
          int k;
@@ -121,7 +123,7 @@
          int k;
          if (j==0 || (mode->bits[j] != mode->bits[j-1]))
          {
-            fprintf (file, "const celt_int16_t allocCache_band%d_%d_%d_%d[MAX_PULSES] = {\n", j, mode->Fs, mode->mdctSize, mode->nbChannels);
+            fprintf (file, "static const celt_int16_t allocCache_band%d_%d_%d_%d[MAX_PULSES] = {\n", j, mode->Fs, mode->mdctSize, mode->nbChannels);
             for (k=0;k<MAX_PULSES;k++)
                fprintf (file, "%2d, ", mode->bits[j][k]);
             fprintf (file, "};\n");
@@ -129,7 +131,7 @@
             fprintf (file, "#define allocCache_band%d_%d_%d_%d allocCache_band%d_%d_%d_%d\n", j, mode->Fs, mode->mdctSize, mode->nbChannels, j-1, mode->Fs, mode->mdctSize, mode->nbChannels);
          }
       }
-      fprintf (file, "const celt_int16_t *allocCache%d_%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbChannels, mode->nbEBands);
+      fprintf (file, "static const celt_int16_t *allocCache%d_%d_%d[%d] = {\n", mode->Fs, mode->mdctSize, mode->nbChannels, mode->nbEBands);
       for (j=0;j<mode->nbEBands;j++)
       {
          fprintf (file, "allocCache_band%d_%d_%d_%d, ", j, mode->Fs, mode->mdctSize, mode->nbChannels);
@@ -139,7 +141,7 @@
       fprintf(file, "\n");
 
       
-      fprintf(file, "CELTMode mode%d_%d_%d_%d = {\n", mode->Fs, mode->nbChannels, mode->mdctSize, mode->overlap);
+      fprintf(file, "static const CELTMode mode%d_%d_%d_%d = {\n", mode->Fs, mode->nbChannels, mode->mdctSize, mode->overlap);
       fprintf(file, "0x%x,\t/* marker */\n", 0xa110ca7e);
       fprintf(file, INT32 ",\t/* Fs */\n", mode->Fs);
       fprintf(file, "%d,\t/* overlap */\n", mode->overlap);
@@ -164,7 +166,7 @@
    fprintf(file, "\n");
    fprintf(file, "/* List of all the available modes */\n");
    fprintf(file, "#define TOTAL_MODES %d\n", nb_modes);
-   fprintf(file, "const CELTMode *static_mode_list[TOTAL_MODES] = {\n");
+   fprintf(file, "static const CELTMode * const static_mode_list[TOTAL_MODES] = {\n");
    for (i=0;i<nb_modes;i++)
    {
       CELTMode *mode = modes[i];
--- a/libcelt/modes.c
+++ b/libcelt/modes.c
@@ -222,7 +222,8 @@
 CELTMode *celt_mode_create(celt_int32_t Fs, int channels, int frame_size, int lookahead, int *error)
 {
 #ifdef STATIC_MODES
-   CELTMode *mode = NULL;
+   const CELTMode *m = NULL;
+   CELTMode *mode=NULL;
    int i;
    for (i=0;i<TOTAL_MODES;i++)
    {
@@ -231,11 +232,11 @@
           frame_size == static_mode_list[i]->mdctSize &&
           lookahead == static_mode_list[i]->overlap)
       {
-         mode = static_mode_list[i];
+         m = static_mode_list[i];
          break;
       }
    }
-   if (mode == NULL)
+   if (m == NULL)
    {
       celt_warning("Mode not included as part of the static modes");
       if (error)
@@ -242,6 +243,8 @@
          *error = CELT_BAD_ARG;
       return NULL;
    }
+   mode = (CELTMode*)celt_alloc(sizeof(CELTMode));
+   CELT_COPY(mode, m, 1);
 #else
    int res;
    int i;
@@ -319,6 +322,7 @@
 
 void celt_mode_destroy(CELTMode *mode)
 {
+   mdct_clear(&mode->mdct);
 #ifndef STATIC_MODES
    int i;
    const celt_int16_t *prevPtr = NULL;
@@ -331,7 +335,6 @@
       }
    }
    celt_free((int**)mode->bits);
-   mdct_clear(&mode->mdct);
    if (check_mode(mode) != CELT_OK)
       return;
    celt_free((int*)mode->eBands);
@@ -342,9 +345,9 @@
 
    mode->marker_start = MODEFREED;
    mode->marker_end = MODEFREED;
-   celt_free((CELTMode *)mode);
    psydecay_clear(&mode->psy);
 #endif
+   celt_free((CELTMode *)mode);
 }
 
 int check_mode(const CELTMode *mode)
--- a/libcelt/psy.c
+++ b/libcelt/psy.c
@@ -50,8 +50,8 @@
 void psydecay_init(struct PsyDecay *decay, int len, celt_int32_t Fs)
 {
    int i;
-   decay->decayR = celt_alloc(sizeof(float)*len);
-   /*decay->decayL = celt_alloc(sizeof(float)*len);*/
+   celt_word16_t *decayR = (celt_word16_t*)celt_alloc(sizeof(celt_word16_t)*len);
+   /*decay->decayL = celt_alloc(sizeof(celt_word16_t)*len);*/
    for (i=0;i<len;i++)
    {
       float f;
@@ -63,19 +63,20 @@
       /* Back to FFT bin units */
       deriv *= Fs*(1/(2.f*len));
       /* decay corresponding to -10dB/Bark */
-      decay->decayR[i] = Q15ONE*pow(.1f, deriv);
+      decayR[i] = Q15ONE*pow(.1f, deriv);
       /* decay corresponding to -25dB/Bark */
       /*decay->decayL[i] = Q15ONE*pow(0.0031623f, deriv);*/
       /*printf ("%f %f\n", decayL[i], decayR[i]);*/
    }
+   decay->decayR = decayR;
 }
-#endif
 
 void psydecay_clear(struct PsyDecay *decay)
 {
-   celt_free(decay->decayR);
+   celt_free((celt_word16_t *)decay->decayR);
    /*celt_free(decay->decayL);*/
 }
+#endif
 
 static void spreading_func(const struct PsyDecay *d, celt_word32_t *psd, celt_mask_t *mask, int len)
 {
@@ -109,7 +110,7 @@
    {
       int start,end;
       int j;
-      float Esig=0, Emask=0;
+      celt_word32_t Esig=0, Emask=0;
       start = (int)floor(fromBARK((float)i)*(2*len)/Fs);
       if (start<0)
          start = 0;
--- a/libcelt/psy.h
+++ b/libcelt/psy.h
@@ -35,7 +35,7 @@
 
 struct PsyDecay {
    /*celt_word16_t *decayL;*/
-   celt_word16_t *decayR;
+   const celt_word16_t *decayR;
 };
 
 /** Pre-compute the decay of the psycho-acoustic spreading function */
--- a/libcelt/testcelt.c
+++ b/libcelt/testcelt.c
@@ -43,7 +43,6 @@
 
 int main(int argc, char *argv[])
 {
-   int i;
    char *inFile, *outFile;
    FILE *fin, *fout;
    CELTMode *mode=NULL;
@@ -55,6 +54,7 @@
    unsigned char data[1024];
    int rate, overlap;
 #if !(defined (FIXED_POINT) && defined(STATIC_MODES))
+   int i;
    double rmsd = 0;
 #endif
    int count = 0;