shithub: opus

Download patch

ref: 4fbf6dcaf46bbc13ddd91a5762a486981fd0a14a
parent: 5eefa61adcc8ac34849eadb93db4e943d59ae019
author: Jean-Marc Valin <jmvalin@amazon.com>
date: Fri May 26 13:29:57 EDT 2023

Adding DRED garbage decoding test

--- a/Makefile.am
+++ b/Makefile.am
@@ -112,6 +112,7 @@
                   silk/tests/test_unit_LPC_inv_pred_gain \
                   tests/test_opus_api \
                   tests/test_opus_decode \
+                  tests/test_opus_dred \
                   tests/test_opus_encode \
                   tests/test_opus_extensions \
                   tests/test_opus_padding \
@@ -129,6 +130,7 @@
         silk/tests/test_unit_LPC_inv_pred_gain \
         tests/test_opus_api \
         tests/test_opus_decode \
+        tests/test_opus_dred \
         tests/test_opus_encode \
         tests/test_opus_extensions \
         tests/test_opus_padding \
@@ -162,6 +164,9 @@
 
 tests_test_opus_padding_SOURCES = tests/test_opus_padding.c tests/test_opus_common.h
 tests_test_opus_padding_LDADD = libopus.la $(NE10_LIBS) $(LIBM)
+
+tests_test_opus_dred_SOURCES = tests/test_opus_dred.c tests/test_opus_common.h
+tests_test_opus_dred_LDADD = libopus.la $(NE10_LIBS) $(LIBM)
 
 CELT_OBJ = $(CELT_SOURCES:.c=.lo)
 SILK_OBJ = $(SILK_SOURCES:.c=.lo)
--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -1239,6 +1239,8 @@
 {
 #ifdef ENABLE_NEURAL_FEC
   free(dec);
+#else
+  (void)dec;
 #endif
 }
 
@@ -1248,6 +1250,7 @@
    const unsigned char *payload;
    opus_int32 payload_len;
    VALIDATE_DRED_DECODER(dred_dec);
+   dred->process_stage = -1;
    payload_len = dred_find_payload(data, len, &payload);
    if (payload_len < 0)
       return payload_len;
@@ -1278,7 +1281,7 @@
 int opus_dred_process(OpusDREDDecoder *dred_dec, const OpusDRED *src, OpusDRED *dst)
 {
 #ifdef ENABLE_NEURAL_FEC
-   if (dred_dec == NULL)
+   if (dred_dec == NULL || src == NULL || dst == NULL || (src->process_stage != 1 && src->process_stage != 2))
       return OPUS_BAD_ARG;
    VALIDATE_DRED_DECODER(dred_dec);
    if (src != dst)
--- /dev/null
+++ b/tests/test_opus_dred.c
@@ -1,0 +1,105 @@
+/* Copyright (c) 2023 Amazon
+   Written by Michael Klingbeil */
+/*
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+
+   - Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#ifndef _WIN32
+#include <unistd.h>
+#else
+#include <process.h>
+#define getpid _getpid
+#endif
+
+/* including sources directly to test internal APIs */
+#define CELT_C /* to make celt_assert work */
+#include "opus.h"
+#include "test_opus_common.h"
+
+
+
+#define NB_RANDOM_EXTENSIONS 10000000
+#define MAX_EXTENSION_SIZE 200
+#define MAX_NB_EXTENSIONS 100
+
+void test_random_dred(void)
+{
+   int error;
+   int i;
+   OpusDREDDecoder *dred_dec;
+   OpusDRED *dred;
+   dred_dec = opus_dred_decoder_create(&error);
+   expect_true(error == OPUS_OK, "opus_dred_decoder_create() failed");
+   dred = opus_dred_alloc(&error);
+   expect_true(error == OPUS_OK, "opus_dred_create() failed");
+   for (i=0;i<NB_RANDOM_EXTENSIONS;i++)
+   {
+      unsigned char payload[MAX_EXTENSION_SIZE];
+      int len;
+      int j;
+      int res1, res2;
+      len = fast_rand()%(MAX_EXTENSION_SIZE+1);
+      for (j=0;j<len;j++)
+         payload[j] = fast_rand()&0xFF;
+      res1 = opus_dred_parse(dred_dec, dred, payload, len, 48000, 48000, fast_rand()&0x1);
+      if (res1 > 0)
+      {
+         res2 = opus_dred_process(dred_dec, dred, dred);
+         expect_true(res2 == OPUS_OK, "process should succeed if parse succeeds");
+      }
+   }
+   opus_dred_free(dred);
+   opus_dred_decoder_destroy(dred_dec);
+}
+
+int main(int argc, char **argv)
+{
+   int env_used;
+   char *env_seed;
+   env_used=0;
+   env_seed=getenv("SEED");
+   if(argc>1)iseed=atoi(argv[1]);
+   else if(env_seed)
+   {
+      iseed=atoi(env_seed);
+      env_used=1;
+   }
+   else iseed=(opus_uint32)time(NULL)^(((opus_uint32)getpid()&65535)<<16);
+   Rw=Rz=iseed;
+
+   fprintf(stderr,"Testing extensions. Random seed: %u (%.4X)\n", iseed, fast_rand() % 65535);
+   if(env_used)fprintf(stderr,"  Random seed set from the environment (SEED=%s).\n", env_seed);
+
+   test_random_dred();
+   fprintf(stderr,"Tests completed successfully.\n");
+   return 0;
+}
--