Add tests for GET_LAST_PACKET_DURATION.
[opus.git] / tests / test_opus_encode.c
index ee87281..b80def3 100644 (file)
@@ -15,8 +15,8 @@
    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 FOUNDATION OR
-   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   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
 #include <math.h>
 #include <string.h>
 #include <time.h>
+#if (!defined WIN32 && !defined _WIN32) || defined(__MINGW32__)
 #include <unistd.h>
+#else
+#include <process.h>
+#define getpid _getpid
+#endif
+#include "opus_multistream.h"
 #include "opus.h"
 #include "../src/opus_private.h"
 #include "test_opus_common.h"
@@ -106,16 +112,19 @@ static inline void save_packet(unsigned char* p, int len, opus_uint32 rng)
 }
 #endif
 
-int run_test1(void)
+int run_test1(int no_fuzz)
 {
    static const int fsizes[6]={960*3,960*2,120,240,480,960};
    static const char *mstrings[3] = {"    LP","Hybrid","  MDCT"};
+   unsigned char mapping[256] = {0,1,255};
    unsigned char db62[36];
    opus_int32 i;
    int rc,j,err;
    OpusEncoder *enc;
-   OpusEncoder *enc2;
+   OpusMSEncoder *MSenc;
    OpusDecoder *dec;
+   OpusMSDecoder *MSdec;
+   OpusMSDecoder *MSdec_err;
    OpusDecoder *dec_err[10];
    short *inbuf;
    short *outbuf;
@@ -135,12 +144,54 @@ int run_test1(void)
    enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
    if(err != OPUS_OK || enc==NULL)test_failed();
 
-   enc2 = opus_encoder_create(8000, 1, OPUS_APPLICATION_VOIP, &err);
-   if(err != OPUS_OK || enc==NULL)test_failed();
+   for(i=0;i<2;i++)
+   {
+      int *ret_err;
+      ret_err = i?0:&err;
+      MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_UNIMPLEMENTED, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 0, 1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(44100, 2, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 2, 2, 3, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 2, -1, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+
+      MSenc = opus_multistream_encoder_create(8000, 256, 2, 0, mapping, OPUS_APPLICATION_VOIP, ret_err);
+      if((ret_err && *ret_err != OPUS_BAD_ARG) || MSenc!=NULL)test_failed();
+   }
+
+   MSenc = opus_multistream_encoder_create(8000, 2, 2, 0, mapping, OPUS_APPLICATION_AUDIO, &err);
+   if(err != OPUS_OK || MSenc==NULL)test_failed();
+
+   /*Some multistream encoder API tests*/
+   if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
+   if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_LSB_DEPTH(&i))!=OPUS_OK)test_failed();
+   if(i<16)test_failed();
+
+   {
+      OpusEncoder *tmp_enc;
+      if(opus_multistream_encoder_ctl(MSenc,  OPUS_MULTISTREAM_GET_ENCODER_STATE(1,&tmp_enc))!=OPUS_OK)test_failed();
+      if(opus_encoder_ctl(tmp_enc, OPUS_GET_LSB_DEPTH(&j))!=OPUS_OK)test_failed();
+      if(i!=j)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc,  OPUS_MULTISTREAM_GET_ENCODER_STATE(2,&tmp_enc))!=OPUS_BAD_ARG)test_failed();
+   }
 
    dec = opus_decoder_create(48000, 2, &err);
    if(err != OPUS_OK || dec==NULL)test_failed();
 
+   MSdec = opus_multistream_decoder_create(48000, 2, 2, 0, mapping, &err);
+   if(err != OPUS_OK || MSdec==NULL)test_failed();
+
+   MSdec_err = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, &err);
+   if(err != OPUS_OK || MSdec_err==NULL)test_failed();
+
    dec_err[0]=(OpusDecoder *)malloc(opus_decoder_get_size(2));
    memcpy(dec_err[0],dec,opus_decoder_get_size(2));
    dec_err[1] = opus_decoder_create(48000, 1, &err);
@@ -166,7 +217,7 @@ int run_test1(void)
 
    inbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
    outbuf=(short *)malloc(sizeof(short)*SAMPLES*2);
-   out2buf=(short *)malloc(sizeof(short)*MAX_FRAME_SAMP*2);
+   out2buf=(short *)malloc(sizeof(short)*MAX_FRAME_SAMP*3);
    if(inbuf==NULL || outbuf==NULL || out2buf==NULL)test_failed();
 
    generate_music(inbuf,SAMPLES);
@@ -218,7 +269,7 @@ int run_test1(void)
             if(opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
             if(enc_final_range!=dec_final_range)test_failed();
             /*LBRR decode*/
-            out_samples = opus_decode(dec_err[0], packet, len, out2buf, MAX_FRAME_SAMP, (fast_rand()&3)!=0);
+            out_samples = opus_decode(dec_err[0], packet, len, out2buf, frame_size, (fast_rand()&3)!=0);
             if(out_samples!=frame_size)test_failed();
             out_samples = opus_decode(dec_err[1], packet, (fast_rand()&3)==0?0:len, out2buf, MAX_FRAME_SAMP, (fast_rand()&7)!=0);
             if(out_samples<120)test_failed();
@@ -236,43 +287,42 @@ int run_test1(void)
 
    for(rc=0;rc<3;rc++)
    {
-      if(opus_encoder_ctl(enc2, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
-      if(opus_encoder_ctl(enc2, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
-      if(opus_encoder_ctl(enc2, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
-      if(opus_encoder_ctl(enc2, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR(rc<2))!=OPUS_OK)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_VBR_CONSTRAINT(rc==1))!=OPUS_OK)test_failed();
+      if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0))!=OPUS_OK)test_failed();
       for(j=0;j<16;j++)
       {
          int rate;
          int modes[16]={0,0,0,0,0,0,0,0,2,2,2,2,2,2,2,2};
          int rates[16]={4000,12000,32000,8000,16000,32000,48000,88000,4000,12000,32000,8000,16000,32000,48000,88000};
-         int frame[16]={160*3,160,80,160,160,80,40,20,160*3,160,80,160,160,80,40,20};
-         if(opus_encoder_ctl(enc2, OPUS_SET_INBAND_FEC(rc==0&&j==1))!=OPUS_OK)test_failed();
-         if(opus_encoder_ctl(enc2, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
+         int frame[16]={160*1,160,80,160,160,80,40,20,160*1,160,80,160,160,80,40,20};
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_INBAND_FEC(rc==0&&j==1))!=OPUS_OK)test_failed();
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_FORCE_MODE(MODE_SILK_ONLY+modes[j]))!=OPUS_OK)test_failed();
          rate=rates[j]+fast_rand()%rates[j];
-         if(opus_encoder_ctl(enc2, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
-         if(opus_encoder_ctl(enc2, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_DTX(fast_rand()&1))!=OPUS_OK)test_failed();
+         if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_BITRATE(rate))!=OPUS_OK)test_failed();
          count=i=0;
          do {
-            int len,out_samples,frame_size;
+            int len,out_samples,frame_size,loss;
             frame_size=frame[j];
-            if(opus_encoder_ctl(enc2, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
-            if(opus_encoder_ctl(enc2, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
-            len = opus_encode(enc2, &inbuf[i], frame_size, packet, MAX_PACKET);
+            if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_COMPLEXITY((count>>2)%11))!=OPUS_OK)test_failed();
+            if(opus_multistream_encoder_ctl(MSenc, OPUS_SET_PACKET_LOSS_PERC((fast_rand()&15)&(fast_rand()%15)))!=OPUS_OK)test_failed();
+            len = opus_multistream_encode(MSenc, &inbuf[i<<1], frame_size, packet, MAX_PACKET);
             if(len<0 || len>MAX_PACKET)test_failed();
-            if(opus_encoder_ctl(enc2, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
-            out_samples = opus_decode(dec, packet, len, out2buf, MAX_FRAME_SAMP, 0);
+            if(opus_multistream_encoder_ctl(MSenc, OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
+            out_samples = opus_multistream_decode(MSdec, packet, len, out2buf, MAX_FRAME_SAMP, 0);
             if(out_samples!=frame_size*6)test_failed();
-            if(opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
+            if(opus_multistream_decoder_ctl(MSdec, OPUS_GET_FINAL_RANGE(&dec_final_range))!=OPUS_OK)test_failed();
             if(enc_final_range!=dec_final_range)test_failed();
             /*LBRR decode*/
-            out_samples = opus_decode(dec_err[8], packet, len, out2buf, MAX_FRAME_SAMP, (fast_rand()&3)!=0);
-            if(out_samples!=frame_size)test_failed();
-            out_samples = opus_decode(dec_err[9], packet, (fast_rand()&3)==0?0:len, out2buf, MAX_FRAME_SAMP, (fast_rand()&7)!=0);
-            if(out_samples<20)test_failed();
+            loss=(fast_rand()&63)==0;
+            out_samples = opus_multistream_decode(MSdec_err, packet, loss?0:len, out2buf, frame_size*6, (fast_rand()&3)!=0);
+            if(out_samples!=(frame_size*6))test_failed();
             i+=frame_size;
             count++;
-         }while(i<(SSAMPLES/6-MAX_FRAME_SAMP));
-         fprintf(stdout,"    Mode %s NB encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
+         }while(i<(SSAMPLES/12-MAX_FRAME_SAMP));
+         fprintf(stdout,"    Mode %s NB dual-mono MS encode %s, %6d bps OK.\n",mstrings[modes[j]],rc==0?" VBR":rc==1?"CVBR":" CBR",rate);
       }
    }
 
@@ -314,7 +364,7 @@ int run_test1(void)
          the decoders in order to compare them. */
       if(opus_packet_parse(packet,len,&toc,frames,size,&payload_offset)<=0)test_failed();
       if((fast_rand()&1023)==0)len=0;
-      for(j=payload_offset;j<len;j++)for(jj=0;jj<8;jj++)packet[j]^=((fast_rand()&1023)==0)<<jj;
+      for(j=(frames[0]-packet);j<len;j++)for(jj=0;jj<8;jj++)packet[j]^=((!no_fuzz)&&((fast_rand()&1023)==0))<<jj;
       out_samples = opus_decode(dec_err[0], len>0?packet:NULL, len, out2buf, MAX_FRAME_SAMP, 0);
       if(out_samples<0||out_samples>MAX_FRAME_SAMP)test_failed();
       if((len>0&&out_samples!=frame_size))test_failed(); /*FIXME use lastframe*/
@@ -344,8 +394,10 @@ int run_test1(void)
    fprintf(stdout,"    All framesize pairs switching encode, %d frames OK.\n",count);
 
    opus_encoder_destroy(enc);
-   opus_encoder_destroy(enc2);
+   opus_multistream_encoder_destroy(MSenc);
    opus_decoder_destroy(dec);
+   opus_multistream_decoder_destroy(MSdec);
+   opus_multistream_decoder_destroy(MSdec_err);
    for(i=0;i<10;i++)opus_decoder_destroy(dec_err[i]);
    free(inbuf);
    free(outbuf);
@@ -356,21 +408,35 @@ int run_test1(void)
 int main(int _argc, char **_argv)
 {
    const char * oversion;
+   const char * env_seed;
+   int env_used;
+
    if(_argc>2)
    {
       fprintf(stderr,"Usage: %s [<seed>]\n",_argv[0]);
       return 1;
    }
 
+   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)^((getpid()&65535)<<16);
    Rw=Rz=iseed;
 
    oversion=opus_get_version_string();
    if(!oversion)test_failed();
    fprintf(stderr,"Testing %s encoder. Random seed: %u (%.4X)\n", oversion, iseed, fast_rand() % 65535);
+   if(env_used)fprintf(stderr,"  Random seed set from the environment (SEED=%s).\n", env_seed);
 
-   run_test1();
+   /*Setting TEST_OPUS_NOFUZZ tells the tool not to send garbage data
+     into the decoders. This is helpful because garbage data
+     may cause the decoders to clip, which angers CLANG IOC.*/
+   run_test1(getenv("TEST_OPUS_NOFUZZ")!=NULL);
 
    fprintf(stderr,"Tests completed successfully.\n");