shithub: opus

Download patch

ref: be89c395872c57087a6fd0b004ee175677bb3d79
parent: 382cad43ce1505a8bc809dd557b8b423d0017e24
author: Jean-Marc Valin <[email protected]>
date: Tue Aug 30 08:39:51 EDT 2011

Using OPUS_ macros for <string.h> operations

Removes a bunch of system #include <...> in the process

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -115,6 +115,7 @@
       break;
    default:
       ret = 0;
+      break;
    }
    return ret;
 }
@@ -248,7 +249,7 @@
       return NULL;
    }
 
-   CELT_MEMSET((char*)st, 0, celt_encoder_get_size_custom(mode, channels));
+   OPUS_CLEAR((char*)st, celt_encoder_get_size_custom(mode, channels));
 
    st->mode = mode;
    st->overlap = mode->overlap;
@@ -440,7 +441,7 @@
          B = shortBlocks;
       }
       /* Prevents problems from the imdct doing the overlap-add */
-      CELT_MEMSET(x, 0, overlap);
+      OPUS_CLEAR(x, overlap);
 
       for (b=0;b<B;b++)
       {
@@ -1084,8 +1085,8 @@
             silence = silence && *inp == 0;
             inp++;
          }
-         CELT_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
-         CELT_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
+         OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
+         OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
       } while (++c<CC);
 
 #ifdef FUZZING
@@ -1194,7 +1195,7 @@
       c=0; do {
          int offset = st->mode->shortMdctSize-st->mode->overlap;
          st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
-         CELT_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
+         OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
          if (offset)
             comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
                   st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
@@ -1203,14 +1204,14 @@
          comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
                st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
                st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
-         CELT_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
+         OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
 
          if (N>COMBFILTER_MAXPERIOD)
          {
-            CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
+            OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
          } else {
-            CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
-            CELT_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
+            OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
+            OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
          }
       } while (++c<CC);
 
@@ -1559,9 +1560,9 @@
       /* Synthesis */
       denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
 
-      CELT_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
+      OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
       if (CC==2)
-         CELT_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
+         OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
 
       c=0; do
          for (i=0;i<M*st->mode->eBands[st->start];i++)
@@ -1809,7 +1810,7 @@
       break;
       case CELT_RESET_STATE:
       {
-         CELT_MEMSET((char*)&st->ENCODER_RESET_START, 0,
+         OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
                celt_encoder_get_size_custom(st->mode, st->channels)-
                ((char*)&st->ENCODER_RESET_START - (char*)st));
          st->vbr_offset = 0;
@@ -1972,7 +1973,7 @@
       return NULL;
    }
 
-   CELT_MEMSET((char*)st, 0, celt_decoder_get_size_custom(mode, channels));
+   OPUS_CLEAR((char*)st, celt_decoder_get_size_custom(mode, channels));
 
    st->mode = mode;
    st->overlap = mode->overlap;
@@ -2519,9 +2520,9 @@
    /* Synthesis */
    denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
 
-   CELT_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
+   OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
    if (CC==2)
-      CELT_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
+      OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
 
    c=0; do
       for (i=0;i<M*st->mode->eBands[st->start];i++)
@@ -2730,7 +2731,7 @@
       break;
       case CELT_RESET_STATE:
       {
-         CELT_MEMSET((char*)&st->DECODER_RESET_START, 0,
+         OPUS_CLEAR((char*)&st->DECODER_RESET_START,
                celt_decoder_get_size_custom(st->mode, st->channels)-
                ((char*)&st->DECODER_RESET_START - (char*)st));
       }
--- a/libcelt/cwrs.c
+++ b/libcelt/cwrs.c
@@ -32,8 +32,6 @@
 #endif
 
 #include "os_support.h"
-#include <stdlib.h>
-#include <string.h>
 #include "cwrs.h"
 #include "mathops.h"
 #include "arch.h"
@@ -616,7 +614,8 @@
       ec_enc_uint(_enc,i,nc);
       RESTORE_STACK;
 #ifndef SMALL_FOOTPRINT
-    };
+    }
+    break;
   }
 #endif
 }
@@ -629,7 +628,7 @@
     case 2:cwrsi2(_k,ec_dec_uint(_dec,ncwrs2(_k)),_y);break;
     case 3:cwrsi3(_k,ec_dec_uint(_dec,ncwrs3(_k)),_y);break;
     case 4:cwrsi4(_k,ec_dec_uint(_dec,ncwrs4(_k)),_y);break;
-      default:
+    default:
     {
 #endif
       VARDECL(opus_uint32,u);
@@ -639,6 +638,7 @@
       RESTORE_STACK;
 #ifndef SMALL_FOOTPRINT
     }
+    break;
   }
 #endif
 }
--- a/libcelt/entenc.c
+++ b/libcelt/entenc.c
@@ -235,7 +235,7 @@
 
 void ec_enc_shrink(ec_enc *_this,opus_uint32 _size){
   celt_assert(_this->offs+_this->end_offs<=_size);
-  CELT_MOVE(_this->buf+_size-_this->end_offs,
+  OPUS_MOVE(_this->buf+_size-_this->end_offs,
    _this->buf+_this->storage-_this->end_offs,_this->end_offs);
   _this->storage=_size;
 }
@@ -273,7 +273,7 @@
   }
   /*Clear any excess space and add any remaining extra bits to the last byte.*/
   if(!_this->error){
-    CELT_MEMSET(_this->buf+_this->offs,0,
+    OPUS_CLEAR(_this->buf+_this->offs,
      _this->storage-_this->offs-_this->end_offs);
     if(used>0){
       /*If there's no range coder data at all, give up.*/
--- a/libcelt/os_support.h
+++ b/libcelt/os_support.h
@@ -40,7 +40,7 @@
 #include <stdlib.h>
 
 /** Opus wrapper for malloc(). To do your own dynamic allocation, all you need to do is replace this function and opus_free */
-#ifndef OVERRIDE_CELT_ALLOC
+#ifndef OVERRIDE_OPUS_ALLOC
 static inline void *opus_alloc (size_t size)
 {
    return malloc(size);
@@ -48,7 +48,7 @@
 #endif
 
 /** Same as celt_alloc(), except that the area is only needed inside a CELT call (might cause problem with wideband though) */
-#ifndef OVERRIDE_CELT_ALLOC_SCRATCH
+#ifndef OVERRIDE_OPUS_ALLOC_SCRATCH
 static inline void *opus_alloc_scratch (size_t size)
 {
    /* Scratch space doesn't need to be cleared */
@@ -57,7 +57,7 @@
 #endif
 
 /** Opus wrapper for free(). To do your own dynamic allocation, all you need to do is replace this function and opus_alloc */
-#ifndef OVERRIDE_CELT_FREE
+#ifndef OVERRIDE_OPUS_FREE
 static inline void opus_free (void *ptr)
 {
    free(ptr);
@@ -65,19 +65,19 @@
 #endif
 
 /** Copy n bytes of memory from src to dst. The 0* term provides compile-time type checking  */
-#ifndef OVERRIDE_CELT_COPY
-#define CELT_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
+#ifndef OVERRIDE_OPUS_COPY
+#define OPUS_COPY(dst, src, n) (memcpy((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
 #endif
 
 /** Copy n bytes of memory from src to dst, allowing overlapping regions. The 0* term
     provides compile-time type checking */
-#ifndef OVERRIDE_CELT_MOVE
-#define CELT_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
+#ifndef OVERRIDE_OPUS_MOVE
+#define OPUS_MOVE(dst, src, n) (memmove((dst), (src), (n)*sizeof(*(dst)) + 0*((dst)-(src)) ))
 #endif
 
-/** Set n bytes of memory to value of c, starting at address s */
-#ifndef OVERRIDE_CELT_MEMSET
-#define CELT_MEMSET(dst, c, n) (memset((dst), (c), (n)*sizeof(*(dst))))
+/** Set n elements of dst to zero, starting at address s */
+#ifndef OVERRIDE_OPUS_CLEAR
+#define OPUS_CLEAR(dst, n) (memset((dst), 0, (n)*sizeof(*(dst))))
 #endif
 
 /*#ifdef __GNUC__
--- a/libcelt/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -295,7 +295,7 @@
 
    ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16);
    ALLOC(error_intra, C*m->nbEBands, opus_val16);
-   CELT_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
+   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
 
    if (two_pass || intra)
    {
@@ -320,7 +320,7 @@
       nintra_bytes = ec_range_bytes(&enc_intra_state);
       ALLOC(intra_bits, nintra_bytes-nstart_bytes, unsigned char);
       /* Copy bits from intra bit-stream */
-      CELT_COPY(intra_bits,
+      OPUS_COPY(intra_bits,
             ec_get_buffer(&enc_intra_state) + nstart_bytes,
             nintra_bytes - nstart_bytes);
 
@@ -333,15 +333,15 @@
       {
          *enc = enc_intra_state;
          /* Copy intra bits to bit-stream */
-         CELT_COPY(ec_get_buffer(&enc_intra_state) + nstart_bytes,
+         OPUS_COPY(ec_get_buffer(&enc_intra_state) + nstart_bytes,
                intra_bits, nintra_bytes - nstart_bytes);
-         CELT_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
-         CELT_COPY(error, error_intra, C*m->nbEBands);
+         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
+         OPUS_COPY(error, error_intra, C*m->nbEBands);
          intra = 1;
       }
    } else {
-      CELT_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
-      CELT_COPY(error, error_intra, C*m->nbEBands);
+      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
+      OPUS_COPY(error, error_intra, C*m->nbEBands);
    }
 
    if (intra)
--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -29,9 +29,6 @@
 #include "config.h"
 #endif
 
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <stdarg.h>
 #include "celt.h"
 #include "opus.h"
@@ -94,7 +91,7 @@
 
 	if (channels<1 || channels > 2)
 	    return OPUS_BAD_ARG;
-	memset(st, 0, opus_decoder_get_size(channels));
+	OPUS_CLEAR((char*)st, opus_decoder_get_size(channels));
 	/* Initialize SILK encoder */
     ret = silk_Get_Decoder_Size( &silkDecSizeBytes );
     if( ret ) {
@@ -254,7 +251,7 @@
     }
     if (audiosize > frame_size)
     {
-        fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);
+        /*fprintf(stderr, "PCM buffer too small: %d vs %d (mode = %d)\n", audiosize, frame_size, mode);*/
         RESTORE_STACK;
         return OPUS_BAD_ARG;
     } else {
@@ -743,31 +740,33 @@
 
 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
 {
-    va_list ap;
+   int ret = OPUS_OK;
+   va_list ap;
 
-    va_start(ap, request);
+   va_start(ap, request);
 
-    switch (request)
-    {
-        case OPUS_GET_BANDWIDTH_REQUEST:
-        {
-            opus_int32 *value = va_arg(ap, opus_int32*);
-            *value = st->bandwidth;
-        }
-        break;
-        case OPUS_GET_FINAL_RANGE_REQUEST:
-        {
-            opus_uint32 *value = va_arg(ap, opus_uint32*);
-            *value = st->rangeFinal;
-        }
-        break;
-        default:
-            fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);
-            break;
-    }
+   switch (request)
+   {
+   case OPUS_GET_BANDWIDTH_REQUEST:
+   {
+      opus_int32 *value = va_arg(ap, opus_int32*);
+      *value = st->bandwidth;
+   }
+   break;
+   case OPUS_GET_FINAL_RANGE_REQUEST:
+   {
+      opus_uint32 *value = va_arg(ap, opus_uint32*);
+      *value = st->rangeFinal;
+   }
+   break;
+   default:
+      /*fprintf(stderr, "unknown opus_decoder_ctl() request: %d", request);*/
+      ret = OPUS_BAD_ARG;
+      break;
+   }
 
-    va_end(ap);
-    return OPUS_OK;
+   va_end(ap);
+   return ret;
 }
 
 void opus_decoder_destroy(OpusDecoder *st)
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -29,9 +29,6 @@
 #include "config.h"
 #endif
 
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
 #include <stdarg.h>
 #include "celt.h"
 #include "entenc.h"
@@ -125,7 +122,7 @@
     if (Fs != 8000 && Fs != 12000 && Fs != 16000 && Fs != 24000 && Fs != 48000)
         return OPUS_BAD_ARG;
 
-    memset(st, 0, opus_encoder_get_size(channels));
+    OPUS_CLEAR((char*)st, opus_encoder_get_size(channels));
     /* Create SILK encoder */
     ret = silk_Get_Encoder_Size( &silkEncSizeBytes );
     if (ret)
@@ -552,8 +549,9 @@
 #endif
         ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0 );
         if( ret ) {
-            fprintf (stderr, "SILK encode error: %d\n", ret);
+            /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
             /* Handle error */
+           return OPUS_INTERNAL_ERROR;
         }
         if (nBytes==0)
         {
@@ -810,9 +808,11 @@
 
 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
 {
+    int ret;
     CELTEncoder *celt_enc;
     va_list ap;
 
+    ret = OPUS_OK;
     va_start(ap, request);
 
     celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
@@ -999,11 +999,12 @@
         }
         break;
         default:
-            fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);
+            /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
+            ret = OPUS_BAD_ARG;
             break;
     }
     va_end(ap);
-    return OPUS_OK;
+    return ret;
 bad_arg:
     va_end(ap);
     return OPUS_BAD_ARG;
--- a/src/opus_multistream.c
+++ b/src/opus_multistream.c
@@ -33,9 +33,6 @@
 #include "opus.h"
 #include "opus_private.h"
 #include "stack_alloc.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include <stdarg.h>
 #include "float_cast.h"
 #include "os_support.h"
@@ -297,7 +294,7 @@
       }
       /* IMPORTANT: Here we assume that the encoder only returned one frame */
       tot_size += len;
-      memcpy(data, &tmp_data[1], len-1);
+      OPUS_COPY(data, &tmp_data[1], len-1);
    }
    RESTORE_STACK;
    return tot_size;
--- a/src/repacketizer.c
+++ b/src/repacketizer.c
@@ -29,10 +29,6 @@
 #include "config.h"
 #endif
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #include "opus.h"
 #include "opus_private.h"
 #include "os_support.h"
@@ -181,11 +177,12 @@
          *data++ = count;
       }
    }
+   break;
    }
    /* Copy the actual data */
    for (i=0;i<count;i++)
    {
-      memcpy(data, frames[i], len[i]);
+      OPUS_COPY(data, frames[i], len[i]);
       data += len[i];
    }
    return tot_size;
--- a/src/test_opus.c
+++ b/src/test_opus.c
@@ -318,6 +318,7 @@
          break;
     default:
          bandwidth_string = "unknown";
+         break;
     }
 
     fprintf(stderr, "Encoding %d Hz input at %.3f kb/s in %s mode with %d-sample frames.\n", sampling_rate, bitrate_bps*0.001, bandwidth_string, frame_size);