shithub: opus

Download patch

ref: 8f67b20a8fe3bc3299b02c00fb3b88d7034ff63b
parent: 0a0d07c193d505e5ad9acd49834cee5a11e1bed9
author: Koen Vos <[email protected]>
date: Thu Feb 3 04:31:12 EST 2011

Testing the range coder final state

--- a/src/opus.h
+++ b/src/opus.h
@@ -47,6 +47,8 @@
 #define __check_int(x) (((void)((x) == (int)0)), (int)(x))
 #define __check_int_ptr(ptr) ((ptr) + ((ptr) - (int*)(ptr)))
 
+#define OPUS_TEST_RANGE_CODER_STATE     1
+
 #define MODE_SILK_ONLY 1000
 #define MODE_HYBRID    1001
 #define MODE_CELT_ONLY 1002
@@ -104,8 +106,9 @@
 
 OpusEncoder *opus_encoder_create(int Fs, int channels);
 
+/* returns length of data payload (in bytes) */
 int opus_encode(OpusEncoder *st, const short *pcm, int frame_size,
-		unsigned char *data, int bytes_per_packet);
+		unsigned char *data, int max_data_bytes);
 
 void opus_encoder_destroy(OpusEncoder *st);
 
@@ -113,6 +116,7 @@
 
 OpusDecoder *opus_decoder_create(int Fs, int channels);
 
+/* returns (CELT) error code */
 int opus_decode(OpusDecoder *st, const unsigned char *data, int len,
 		short *pcm, int frame_size);
 
@@ -119,6 +123,12 @@
 void opus_decoder_ctl(OpusDecoder *st, int request, ...);
 
 void opus_decoder_destroy(OpusDecoder *st);
+
+#if OPUS_TEST_RANGE_CODER_STATE
+int opus_encoder_get_final_range(OpusEncoder *st);
+int opus_decoder_get_final_range(OpusDecoder *st);
+#endif
+
 
 #ifdef __cplusplus
 }
--- a/src/opus_decoder.c
+++ b/src/opus_decoder.c
@@ -73,6 +73,7 @@
 
 	return st;
 }
+
 int opus_decode(OpusDecoder *st, const unsigned char *data,
 		int len, short *pcm, int frame_size)
 {
@@ -199,6 +200,11 @@
         for (i=0;i<frame_size*st->channels;i++)
             pcm[i] = ADD_SAT16(pcm[i], pcm_celt[i]);
     }
+
+#if OPUS_TEST_RANGE_CODER_STATE
+    st->rangeFinal = dec.rng;
+#endif
+
 	return celt_ret<0 ? celt_ret : audiosize;
 
 }
@@ -247,3 +253,10 @@
 {
 	free(st);
 }
+
+#if OPUS_TEST_RANGE_CODER_STATE
+int opus_decoder_get_final_range(OpusDecoder *st)
+{
+    return st->rangeFinal;
+}
+#endif
--- a/src/opus_decoder.h
+++ b/src/opus_decoder.h
@@ -45,6 +45,10 @@
     int          bandwidth;
     /* Sampling rate (at the API level) */
     int          Fs;
+
+#ifdef OPUS_TEST_RANGE_CODER_STATE
+    int          rangeFinal;
+#endif
 };
 
 inline short ADD_SAT16(a, b) {
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -74,7 +74,7 @@
     st->silk_mode.packetLossPercentage  = 0;
     st->silk_mode.useInBandFEC          = 0;
     st->silk_mode.useDTX                = 0;
-    st->silk_mode.complexity            = 2;
+    st->silk_mode.complexity            = 10;
 
     /* Create CELT encoder */
 	/* Initialize CELT encoder */
@@ -246,6 +246,10 @@
     data[0] |= (st->stream_channels==2)<<2;
     /*printf ("%x\n", (int)data[0]);*/
 
+#if OPUS_TEST_RANGE_CODER_STATE
+    st->rangeFinal = enc.rng;
+#endif
+
     return ret+1;
 }
 
@@ -374,3 +378,9 @@
 	free(st);
 }
 
+#if OPUS_TEST_RANGE_CODER_STATE
+int opus_encoder_get_final_range(OpusEncoder *st)
+{
+    return st->rangeFinal;
+}
+#endif
--- a/src/opus_encoder.h
+++ b/src/opus_encoder.h
@@ -54,6 +54,10 @@
     int          bitrate_bps;
 
     short        delay_buffer[ENCODER_DELAY_COMPENSATION*2];
+
+#ifdef OPUS_TEST_RANGE_CODER_STATE
+    int          rangeFinal;
+#endif
 };
 
 
--- a/src/test_opus.c
+++ b/src/test_opus.c
@@ -53,7 +53,7 @@
     fprintf(stderr, "-vbr                 : enable variable bitrate (recommended for SILK)\n" );
     fprintf(stderr, "-bandwidth <NB|MB|WB|SWB|FB>  : audio bandwidth (from narrowband to fullband)\n" );
     fprintf(stderr, "-max_payload <bytes> : maximum payload size in bytes, default: 1024\n" );
-    fprintf(stderr, "-complexity <comp>   : SILK complexity, 0: low, 1: medium, 2: high; default: 2\n" );
+    fprintf(stderr, "-complexity <comp>   : complexity, 0 (lowest) ... 10 (highest); default: 10\n" );
     fprintf(stderr, "-inbandfec           : enable SILK inband FEC\n" );
     fprintf(stderr, "-dtx                 : enable SILK DTX\n" );
     fprintf(stderr, "-loss <perc>         : simulate packet loss, in percent (0-100); default: 0\n" );
@@ -93,6 +93,7 @@
    short *in, *out;
    int mode;
    double bits=0.0, bits_act=0.0, bits2=0.0, nrg;
+   int bandwidth=-1;
 
    if (argc < 8 )
    {
@@ -108,10 +109,10 @@
 
    /* defaults: */
    use_vbr = 0;
-   int bandwidth=-1;
+   bandwidth=-1;
    internal_sampling_rate_Hz = sampling_rate;
    max_payload_bytes = MAX_PACKET;
-   complexity = 2;
+   complexity = 10;
    use_inbandfec = 0;
    use_dtx = 0;
    packet_loss_perc = 0;
@@ -147,7 +148,7 @@
         } else if( STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-inbandfec" ) == 0 ) {
             use_inbandfec = 1;
             args++;
-        } else if( STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-fec") == 0 ) {
+        } else if( STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-dtx") == 0 ) {
             use_dtx = 1;
             args++;
         } else if( STR_CASEINSENSITIVE_COMPARE( argv[ args ], "-loss" ) == 0 ) {
@@ -195,7 +196,7 @@
 
    if (mode==MODE_SILK_ONLY)
    {
-       if (bandwidth == BANDWIDTH_SUPERWIDEBAND || bandwidth == BANDWIDTH_SUPERWIDEBAND)
+       if (bandwidth == BANDWIDTH_SUPERWIDEBAND || bandwidth == BANDWIDTH_FULLBAND)
        {
            fprintf (stderr, "Predictive mode only supports up to wideband\n");
            return 1;
@@ -267,6 +268,14 @@
       }
       fwrite(out+skip, sizeof(short), (write_samples-skip)*channels, fout);
       skip = 0;
+
+#if OPUS_TEST_RANGE_CODER_STATE
+      /* compare final range encoder rng values of encoder and decoder */
+      if( opus_decoder_get_final_range( dec ) != opus_encoder_get_final_range( enc ) ) {
+          fprintf (stderr, "Error: Range coder state mismatch between encoder and decoder.\n");
+          return 0;
+      }
+#endif
 
       /* count bits */
       bits += len*8;