Skip to content
Snippets Groups Projects
test_opus_api.c 68.9 KiB
Newer Older
/* Copyright (c) 2011-2013 Xiph.Org Foundation
   Written by Gregory Maxwell */
/*
   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.
*/

/* This tests the API presented by the libopus system.
   It does not attempt to extensively exercise the codec internals.
   The strategy here is to simply the API interface invariants:
   That sane options are accepted, insane options are rejected,
   and that nothing blows up. In particular we don't actually test
   that settings are heeded by the codec (though we do check that
   get after set returns a sane value when it should). Other
   tests check the actual codec behavior.
   In cases where its reasonable to do so we test exhaustively,
   but its not reasonable to do so in all cases.
   Although these tests are simple they found several library bugs
   when they were initially developed. */

/* These tests are more sensitive if compiled with -DVALGRIND and
   run inside valgrind. Malloc failure testing requires glibc. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "opus.h"
#include "test_opus_common.h"

#ifdef VALGRIND
#include <valgrind/memcheck.h>
#define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
#define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
#else
#define VG_UNDEF(x,y)
#define VG_CHECK(x,y)
#endif

#if defined(HAVE___MALLOC_HOOK)
#define MALLOC_FAIL
#include "os_support.h"
#include <malloc.h>

static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
       OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};

void *malloc_hook(__attribute__((unused)) size_t size,
                  __attribute__((unused)) const void *caller)
{
   return 0;
}
#endif

Mark Harris's avatar
Mark Harris committed
opus_int32 *null_int_ptr = (opus_int32 *)NULL;
opus_uint32 *null_uint_ptr = (opus_uint32 *)NULL;

static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};

opus_int32 test_dec_api(void)
{
   opus_uint32 dec_final_range;
   OpusDecoder *dec;
   OpusDecoder *dec2;
   opus_int32 i,j,cfgs;
   unsigned char packet[1276];
#ifndef DISABLE_FLOAT_API
   float fbuf[960*2];
#endif
   short sbuf[960*2];
   int c,err;

   cfgs=0;
   /*First test invalid configurations which should fail*/
   fprintf(stdout,"\n  Decoder basic API tests\n");
   fprintf(stdout,"  ---------------------------------------------------\n");
      if(((c==1||c==2)&&(i<=2048||i>1<<18))||((c!=1&&c!=2)&&i!=0))test_failed();
      fprintf(stdout,"    opus_decoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
      cfgs++;
   }

   /*Test with unsupported sample rates*/
   for(c=0;c<4;c++)
   {
      for(i=-7;i<=96000;i++)
      {
         int fs;
         if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
         switch(i)
         {
           case(-5):fs=-8000;break;
           case(-6):fs=INT32_MAX;break;
           case(-7):fs=INT32_MIN;break;
           default:fs=i;
         }
         err = OPUS_OK;
         VG_UNDEF(&err,sizeof(err));
         dec = opus_decoder_create(fs, c, &err);
         if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
         cfgs++;
         dec = opus_decoder_create(fs, c, 0);
         if(dec!=NULL)test_failed();
         cfgs++;
         dec=malloc(opus_decoder_get_size(2));
         if(dec==NULL)test_failed();
         err = opus_decoder_init(dec,fs,c);
         if(err!=OPUS_BAD_ARG)test_failed();
         cfgs++;
         free(dec);
      }
   }

   VG_UNDEF(&err,sizeof(err));
   dec = opus_decoder_create(48000, 2, &err);
   if(err!=OPUS_OK || dec==NULL)test_failed();
   VG_CHECK(dec,opus_decoder_get_size(2));
   cfgs++;

   fprintf(stdout,"    opus_decoder_create() ........................ OK.\n");
   fprintf(stdout,"    opus_decoder_init() .......................... OK.\n");
Mark Harris's avatar
Mark Harris committed
   err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(null_uint_ptr));
   if(err != OPUS_BAD_ARG)test_failed();
   VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
   err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
   if(err!=OPUS_OK)test_failed();
   VG_CHECK(&dec_final_range,sizeof(dec_final_range));
   fprintf(stdout,"    OPUS_GET_FINAL_RANGE ......................... OK.\n");
   cfgs++;

   err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
   if(err!=OPUS_UNIMPLEMENTED)test_failed();
   fprintf(stdout,"    OPUS_UNIMPLEMENTED ........................... OK.\n");
Mark Harris's avatar
Mark Harris committed
   err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(null_int_ptr));
   if(err != OPUS_BAD_ARG)test_failed();
   VG_UNDEF(&i,sizeof(i));
   err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
   if(err != OPUS_OK || i!=0)test_failed();
   fprintf(stdout,"    OPUS_GET_BANDWIDTH ........................... OK.\n");
Mark Harris's avatar
Mark Harris committed
   err=opus_decoder_ctl(dec, OPUS_GET_SAMPLE_RATE(null_int_ptr));
   if(err != OPUS_BAD_ARG)test_failed();
   VG_UNDEF(&i,sizeof(i));
   err=opus_decoder_ctl(dec, OPUS_GET_SAMPLE_RATE(&i));
   if(err != OPUS_OK || i!=48000)test_failed();
   fprintf(stdout,"    OPUS_GET_SAMPLE_RATE ......................... OK.\n");
   cfgs++;

   /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
Mark Harris's avatar
Mark Harris committed
   err=opus_decoder_ctl(dec, OPUS_GET_PITCH(null_int_ptr));
   if(err!=OPUS_BAD_ARG)test_failed();
   cfgs++;
   VG_UNDEF(&i,sizeof(i));
   err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
   if(err != OPUS_OK || i>0 || i<-1)test_failed();
   cfgs++;
   VG_UNDEF(packet,sizeof(packet));
   packet[0]=63<<2;packet[1]=packet[2]=0;
   if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
   cfgs++;
   VG_UNDEF(&i,sizeof(i));
   err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
   if(err != OPUS_OK || i>0 || i<-1)test_failed();
   cfgs++;
   packet[0]=1;
   if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
   cfgs++;
   VG_UNDEF(&i,sizeof(i));
Loading
Loading full blame...