shithub: opus

Download patch

ref: 3171ed6fceb38a16b0e0c06016fa711c031eadf7
parent: ec06c2ae404d21a7a34fb62ef6a2b3af5de4921d
author: Jean-Marc Valin <jeanmarcv@google.com>
date: Wed Jun 12 11:21:20 EDT 2024

Cleanup Opus encode/decode API functions

--- a/celt/arch.h
+++ b/celt/arch.h
@@ -148,6 +148,7 @@
 #define FLOAT2RES(a)    float2int(32768.f*256.f*(a))
 #define RES2SIG(a)      SHL32((a), SIG_SHIFT-RES_SHIFT)
 #define MULT16_RES_Q15(a,b) MULT16_32_Q15(a,b)
+#define MAX_ENCODING_DEPTH 24
 #else
 typedef opus_val16 opus_res;
 #define RES_SHIFT 0
@@ -161,6 +162,7 @@
 #define FLOAT2RES(a)    FLOAT2INT16(a)
 #define RES2SIG(a)      SHL32(EXTEND32(a), SIG_SHIFT)
 #define MULT16_RES_Q15(a,b) MULT16_16_Q15(a,b)
+#define MAX_ENCODING_DEPTH 16
 #endif
 
 #define RES2VAL16(a)    RES2INT16(a)
@@ -334,7 +336,7 @@
 #define FLOAT2SIG(a)    ((a)*CELT_SIG_SCALE)
 #define INT16TOSIG(a)   ((float)(a))
 #define INT24TOSIG(a)   ((float)(a)*(1.f/256.f))
-
+#define MAX_ENCODING_DEPTH 24
 
 #endif /* !FIXED_POINT */
 
--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -817,59 +817,64 @@
 }
 
 #ifdef FIXED_POINT
-#ifdef ENABLE_RES24
+#define OPTIONAL_CLIP 0
+#else
+#define OPTIONAL_CLIP 1
+#endif
+
+#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
 int opus_decode(OpusDecoder *st, const unsigned char *data,
       opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
 {
-	   VARDECL(opus_res, out);
-	   int ret, i;
-	   int nb_samples;
-	   ALLOC_STACK;
-
-	   if(frame_size<=0)
-	   {
-	      RESTORE_STACK;
-	      return OPUS_BAD_ARG;
-	   }
-	   if (data != NULL && len > 0 && !decode_fec)
-	   {
-	      nb_samples = opus_decoder_get_nb_samples(st, data, len);
-	      if (nb_samples>0)
-	         frame_size = IMIN(frame_size, nb_samples);
-	      else
-	         return OPUS_INVALID_PACKET;
-	   }
-	   celt_assert(st->channels == 1 || st->channels == 2);
-	   ALLOC(out, frame_size*st->channels, opus_res);
-
-	   ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
-	   if (ret > 0)
-	   {
-	      for (i=0;i<ret*st->channels;i++)
-	         pcm[i] = RES2INT16(out[i]);
-	   }
-	   RESTORE_STACK;
-	   return ret;
-}
-
-int opus_decode24(OpusDecoder *st, const unsigned char *data,
-      opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec)
-{
    if(frame_size<=0)
       return OPUS_BAD_ARG;
    return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
 }
-
 #else
-
 int opus_decode(OpusDecoder *st, const unsigned char *data,
       opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
 {
+       VARDECL(opus_res, out);
+       int ret, i;
+       int nb_samples;
+       ALLOC_STACK;
+
+       if(frame_size<=0)
+       {
+          RESTORE_STACK;
+          return OPUS_BAD_ARG;
+       }
+       if (data != NULL && len > 0 && !decode_fec)
+       {
+          nb_samples = opus_decoder_get_nb_samples(st, data, len);
+          if (nb_samples>0)
+             frame_size = IMIN(frame_size, nb_samples);
+          else
+             return OPUS_INVALID_PACKET;
+       }
+       celt_assert(st->channels == 1 || st->channels == 2);
+       ALLOC(out, frame_size*st->channels, opus_res);
+
+       ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, OPTIONAL_CLIP, NULL, 0);
+       if (ret > 0)
+       {
+          for (i=0;i<ret*st->channels;i++)
+             pcm[i] = RES2INT16(out[i]);
+       }
+       RESTORE_STACK;
+       return ret;
+}
+#endif
+
+#if defined(FIXED_POINT) && defined(ENABLE_RES24)
+int opus_decode24(OpusDecoder *st, const unsigned char *data,
+      opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec)
+{
    if(frame_size<=0)
       return OPUS_BAD_ARG;
    return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
 }
-
+#else
 int opus_decode24(OpusDecoder *st, const unsigned char *data,
       opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec)
 {
@@ -903,11 +908,21 @@
        RESTORE_STACK;
        return ret;
 }
-
 #endif
 
+
 #ifndef DISABLE_FLOAT_API
+
+# if !defined(FIXED_POINT)
 int opus_decode_float(OpusDecoder *st, const unsigned char *data,
+      opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
+{
+   if(frame_size<=0)
+      return OPUS_BAD_ARG;
+   return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
+}
+# else
+int opus_decode_float(OpusDecoder *st, const unsigned char *data,
       opus_int32 len, float *pcm, int frame_size, int decode_fec)
 {
    VARDECL(opus_res, out);
@@ -940,89 +955,10 @@
    RESTORE_STACK;
    return ret;
 }
-#endif
+# endif
 
-
-#else
-int opus_decode(OpusDecoder *st, const unsigned char *data,
-      opus_int32 len, opus_int16 *pcm, int frame_size, int decode_fec)
-{
-   VARDECL(float, out);
-   int ret, i;
-   int nb_samples;
-   ALLOC_STACK;
-
-   if(frame_size<=0)
-   {
-      RESTORE_STACK;
-      return OPUS_BAD_ARG;
-   }
-
-   if (data != NULL && len > 0 && !decode_fec)
-   {
-      nb_samples = opus_decoder_get_nb_samples(st, data, len);
-      if (nb_samples>0)
-         frame_size = IMIN(frame_size, nb_samples);
-      else
-         return OPUS_INVALID_PACKET;
-   }
-   celt_assert(st->channels == 1 || st->channels == 2);
-   ALLOC(out, frame_size*st->channels, float);
-
-   ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1, NULL, 0);
-   if (ret > 0)
-   {
-      for (i=0;i<ret*st->channels;i++)
-         pcm[i] = RES2INT16(out[i]);
-   }
-   RESTORE_STACK;
-   return ret;
-}
-
-int opus_decode24(OpusDecoder *st, const unsigned char *data,
-      opus_int32 len, opus_int32 *pcm, int frame_size, int decode_fec)
-{
-   VARDECL(float, out);
-   int ret, i;
-   int nb_samples;
-   ALLOC_STACK;
-
-   if(frame_size<=0)
-   {
-      RESTORE_STACK;
-      return OPUS_BAD_ARG;
-   }
-
-   if (data != NULL && len > 0 && !decode_fec)
-   {
-      nb_samples = opus_decoder_get_nb_samples(st, data, len);
-      if (nb_samples>0)
-         frame_size = IMIN(frame_size, nb_samples);
-      else
-         return OPUS_INVALID_PACKET;
-   }
-   celt_assert(st->channels == 1 || st->channels == 2);
-   ALLOC(out, frame_size*st->channels, float);
-
-   ret = opus_decode_native(st, data, len, out, frame_size, decode_fec, 0, NULL, 1, NULL, 0);
-   if (ret > 0)
-   {
-      for (i=0;i<ret*st->channels;i++)
-         pcm[i] = RES2INT24(out[i]);
-   }
-   RESTORE_STACK;
-   return ret;
-}
-
-int opus_decode_float(OpusDecoder *st, const unsigned char *data,
-      opus_int32 len, opus_val16 *pcm, int frame_size, int decode_fec)
-{
-   if(frame_size<=0)
-      return OPUS_BAD_ARG;
-   return opus_decode_native(st, data, len, pcm, frame_size, decode_fec, 0, NULL, 0, NULL, 0);
-}
-
 #endif
+
 
 int opus_decoder_ctl(OpusDecoder *st, int request, ...)
 {
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -2505,35 +2505,18 @@
     return ret;
 }
 
-#ifdef FIXED_POINT
 
-#ifndef DISABLE_FLOAT_API
-opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
-      unsigned char *data, opus_int32 max_data_bytes)
+
+#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
+opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
+                unsigned char *data, opus_int32 max_data_bytes)
 {
-   int i, ret;
    int frame_size;
-   VARDECL(opus_res, in);
-   ALLOC_STACK;
-
    frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
-   if (frame_size <= 0)
-   {
-      RESTORE_STACK;
-      return OPUS_BAD_ARG;
-   }
-   ALLOC(in, frame_size*st->channels, opus_res);
-
-   for (i=0;i<frame_size*st->channels;i++)
-      in[i] = FLOAT2RES(pcm[i]);
-   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
-                            pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
-   RESTORE_STACK;
-   return ret;
+   return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16,
+                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
 }
-#endif
-
-#ifdef ENABLE_RES24
+#else
 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
                 unsigned char *data, opus_int32 max_data_bytes)
 {
@@ -2557,26 +2540,18 @@
    RESTORE_STACK;
    return ret;
 }
+#endif
 
+#if defined(FIXED_POINT) && defined(ENABLE_RES24)
 opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
                 unsigned char *data, opus_int32 max_data_bytes)
 {
    int frame_size;
    frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
-   return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16,
+   return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
                              pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 0);
 }
-
 #else
-opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
-                unsigned char *data, opus_int32 max_data_bytes)
-{
-   int frame_size;
-   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
-   return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16,
-                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
-}
-
 opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
                 unsigned char *data, opus_int32 max_data_bytes)
 {
@@ -2595,44 +2570,32 @@
 
    for (i=0;i<frame_size*st->channels;i++)
       in[i] = INT24TORES(pcm[i]);
-   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
+   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 1);
    RESTORE_STACK;
    return ret;
 }
-#endif /* ENABLE_RES24 */
+#endif
 
-#else
-opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
-      unsigned char *data, opus_int32 max_data_bytes)
+
+#ifndef DISABLE_FLOAT_API
+
+# if !defined(FIXED_POINT)
+opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
+                      unsigned char *data, opus_int32 out_data_bytes)
 {
-   int i, ret;
    int frame_size;
-   VARDECL(float, in);
-   ALLOC_STACK;
-
    frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
-   if (frame_size <= 0)
-   {
-      RESTORE_STACK;
-      return OPUS_BAD_ARG;
-   }
-   ALLOC(in, frame_size*st->channels, float);
-
-   for (i=0;i<frame_size*st->channels;i++)
-      in[i] = INT16TORES(pcm[i]);
-   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
-                            pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
-   RESTORE_STACK;
-   return ret;
+   return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, MAX_ENCODING_DEPTH,
+                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
 }
-
-opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
+# else
+opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
       unsigned char *data, opus_int32 max_data_bytes)
 {
    int i, ret;
    int frame_size;
-   VARDECL(float, in);
+   VARDECL(opus_res, in);
    ALLOC_STACK;
 
    frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
@@ -2641,24 +2604,17 @@
       RESTORE_STACK;
       return OPUS_BAD_ARG;
    }
-   ALLOC(in, frame_size*st->channels, float);
+   ALLOC(in, frame_size*st->channels, opus_res);
 
    for (i=0;i<frame_size*st->channels;i++)
-      in[i] = INT24TORES(pcm[i]);
-   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
-                            pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 0);
+      in[i] = FLOAT2RES(pcm[i]);
+   ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH,
+                            pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
    RESTORE_STACK;
    return ret;
 }
+# endif
 
-opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
-                      unsigned char *data, opus_int32 out_data_bytes)
-{
-   int frame_size;
-   frame_size = frame_size_select(analysis_frame_size, st->variable_duration, st->Fs);
-   return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
-                             pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
-}
 #endif
 
 
--