test_opus_api.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  1. /* Copyright (c) 2011-2013 Xiph.Org Foundation
  2. Written by Gregory Maxwell */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /* This tests the API presented by the libopus system.
  25. It does not attempt to extensively exercise the codec internals.
  26. The strategy here is to simply the API interface invariants:
  27. That sane options are accepted, insane options are rejected,
  28. and that nothing blows up. In particular we don't actually test
  29. that settings are heeded by the codec (though we do check that
  30. get after set returns a sane value when it should). Other
  31. tests check the actual codec behavior.
  32. In cases where its reasonable to do so we test exhaustively,
  33. but its not reasonable to do so in all cases.
  34. Although these tests are simple they found several library bugs
  35. when they were initially developed. */
  36. /* These tests are more sensitive if compiled with -DVALGRIND and
  37. run inside valgrind. Malloc failure testing requires glibc. */
  38. #ifdef HAVE_CONFIG_H
  39. #include "config.h"
  40. #endif
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <stdint.h>
  44. #include <string.h>
  45. #include "arch.h"
  46. #include "opus_multistream.h"
  47. #include "opus.h"
  48. #include "test_opus_common.h"
  49. #ifdef VALGRIND
  50. #include <valgrind/memcheck.h>
  51. #define VG_UNDEF(x,y) VALGRIND_MAKE_MEM_UNDEFINED((x),(y))
  52. #define VG_CHECK(x,y) VALGRIND_CHECK_MEM_IS_DEFINED((x),(y))
  53. #else
  54. #define VG_UNDEF(x,y)
  55. #define VG_CHECK(x,y)
  56. #endif
  57. #if defined(HAVE___MALLOC_HOOK)
  58. #define MALLOC_FAIL
  59. #include "os_support.h"
  60. #include <malloc.h>
  61. static const opus_int32 opus_apps[3] = {OPUS_APPLICATION_VOIP,
  62. OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY};
  63. void *malloc_hook(__attribute__((unused)) size_t size,
  64. __attribute__((unused)) const void *caller)
  65. {
  66. return 0;
  67. }
  68. #endif
  69. opus_int32 *null_int_ptr = (opus_int32 *)NULL;
  70. opus_uint32 *null_uint_ptr = (opus_uint32 *)NULL;
  71. static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
  72. opus_int32 test_dec_api(void)
  73. {
  74. opus_uint32 dec_final_range;
  75. OpusDecoder *dec;
  76. OpusDecoder *dec2;
  77. opus_int32 i,j,cfgs;
  78. unsigned char packet[1276];
  79. #ifndef DISABLE_FLOAT_API
  80. float fbuf[960*2];
  81. #endif
  82. short sbuf[960*2];
  83. int c,err;
  84. cfgs=0;
  85. /*First test invalid configurations which should fail*/
  86. fprintf(stdout,"\n Decoder basic API tests\n");
  87. fprintf(stdout," ---------------------------------------------------\n");
  88. for(c=0;c<4;c++)
  89. {
  90. i=opus_decoder_get_size(c);
  91. if(((c==1||c==2)&&(i<=2048||i>1<<16))||((c!=1&&c!=2)&&i!=0))test_failed();
  92. fprintf(stdout," opus_decoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
  93. cfgs++;
  94. }
  95. /*Test with unsupported sample rates*/
  96. for(c=0;c<4;c++)
  97. {
  98. for(i=-7;i<=96000;i++)
  99. {
  100. int fs;
  101. if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
  102. switch(i)
  103. {
  104. case(-5):fs=-8000;break;
  105. case(-6):fs=INT32_MAX;break;
  106. case(-7):fs=INT32_MIN;break;
  107. default:fs=i;
  108. }
  109. err = OPUS_OK;
  110. VG_UNDEF(&err,sizeof(err));
  111. dec = opus_decoder_create(fs, c, &err);
  112. if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
  113. cfgs++;
  114. dec = opus_decoder_create(fs, c, 0);
  115. if(dec!=NULL)test_failed();
  116. cfgs++;
  117. dec=malloc(opus_decoder_get_size(2));
  118. if(dec==NULL)test_failed();
  119. err = opus_decoder_init(dec,fs,c);
  120. if(err!=OPUS_BAD_ARG)test_failed();
  121. cfgs++;
  122. free(dec);
  123. }
  124. }
  125. VG_UNDEF(&err,sizeof(err));
  126. dec = opus_decoder_create(48000, 2, &err);
  127. if(err!=OPUS_OK || dec==NULL)test_failed();
  128. VG_CHECK(dec,opus_decoder_get_size(2));
  129. cfgs++;
  130. fprintf(stdout," opus_decoder_create() ........................ OK.\n");
  131. fprintf(stdout," opus_decoder_init() .......................... OK.\n");
  132. err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(null_uint_ptr));
  133. if(err != OPUS_BAD_ARG)test_failed();
  134. VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
  135. err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
  136. if(err!=OPUS_OK)test_failed();
  137. VG_CHECK(&dec_final_range,sizeof(dec_final_range));
  138. fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
  139. cfgs++;
  140. err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
  141. if(err!=OPUS_UNIMPLEMENTED)test_failed();
  142. fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
  143. cfgs++;
  144. err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(null_int_ptr));
  145. if(err != OPUS_BAD_ARG)test_failed();
  146. VG_UNDEF(&i,sizeof(i));
  147. err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
  148. if(err != OPUS_OK || i!=0)test_failed();
  149. fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
  150. cfgs++;
  151. err=opus_decoder_ctl(dec, OPUS_GET_SAMPLE_RATE(null_int_ptr));
  152. if(err != OPUS_BAD_ARG)test_failed();
  153. VG_UNDEF(&i,sizeof(i));
  154. err=opus_decoder_ctl(dec, OPUS_GET_SAMPLE_RATE(&i));
  155. if(err != OPUS_OK || i!=48000)test_failed();
  156. fprintf(stdout," OPUS_GET_SAMPLE_RATE ......................... OK.\n");
  157. cfgs++;
  158. /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
  159. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(null_int_ptr));
  160. if(err!=OPUS_BAD_ARG)test_failed();
  161. cfgs++;
  162. VG_UNDEF(&i,sizeof(i));
  163. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  164. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  165. cfgs++;
  166. VG_UNDEF(packet,sizeof(packet));
  167. packet[0]=63<<2;packet[1]=packet[2]=0;
  168. if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  169. cfgs++;
  170. VG_UNDEF(&i,sizeof(i));
  171. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  172. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  173. cfgs++;
  174. packet[0]=1;
  175. if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
  176. cfgs++;
  177. VG_UNDEF(&i,sizeof(i));
  178. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  179. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  180. cfgs++;
  181. fprintf(stdout," OPUS_GET_PITCH ............................... OK.\n");
  182. err=opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(null_int_ptr));
  183. if(err != OPUS_BAD_ARG)test_failed();
  184. VG_UNDEF(&i,sizeof(i));
  185. err=opus_decoder_ctl(dec, OPUS_GET_LAST_PACKET_DURATION(&i));
  186. if(err != OPUS_OK || i!=960)test_failed();
  187. cfgs++;
  188. fprintf(stdout," OPUS_GET_LAST_PACKET_DURATION ................ OK.\n");
  189. VG_UNDEF(&i,sizeof(i));
  190. err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
  191. VG_CHECK(&i,sizeof(i));
  192. if(err != OPUS_OK || i!=0)test_failed();
  193. cfgs++;
  194. err=opus_decoder_ctl(dec, OPUS_GET_GAIN(null_int_ptr));
  195. if(err != OPUS_BAD_ARG)test_failed();
  196. cfgs++;
  197. err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-32769));
  198. if(err != OPUS_BAD_ARG)test_failed();
  199. cfgs++;
  200. err=opus_decoder_ctl(dec, OPUS_SET_GAIN(32768));
  201. if(err != OPUS_BAD_ARG)test_failed();
  202. cfgs++;
  203. err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-15));
  204. if(err != OPUS_OK)test_failed();
  205. cfgs++;
  206. VG_UNDEF(&i,sizeof(i));
  207. err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
  208. VG_CHECK(&i,sizeof(i));
  209. if(err != OPUS_OK || i!=-15)test_failed();
  210. cfgs++;
  211. fprintf(stdout," OPUS_SET_GAIN ................................ OK.\n");
  212. fprintf(stdout," OPUS_GET_GAIN ................................ OK.\n");
  213. /*Reset the decoder*/
  214. dec2=malloc(opus_decoder_get_size(2));
  215. memcpy(dec2,dec,opus_decoder_get_size(2));
  216. if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  217. if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
  218. free(dec2);
  219. fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
  220. cfgs++;
  221. VG_UNDEF(packet,sizeof(packet));
  222. packet[0]=0;
  223. if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
  224. if(opus_packet_get_nb_samples(packet,1,48000)!=480)test_failed();
  225. if(opus_packet_get_nb_samples(packet,1,96000)!=960)test_failed();
  226. if(opus_packet_get_nb_samples(packet,1,32000)!=320)test_failed();
  227. if(opus_packet_get_nb_samples(packet,1,8000)!=80)test_failed();
  228. packet[0]=3;
  229. if(opus_packet_get_nb_samples(packet,1,24000)!=OPUS_INVALID_PACKET)test_failed();
  230. packet[0]=(63<<2)|3;
  231. packet[1]=63;
  232. if(opus_packet_get_nb_samples(packet,0,24000)!=OPUS_BAD_ARG)test_failed();
  233. if(opus_packet_get_nb_samples(packet,2,48000)!=OPUS_INVALID_PACKET)test_failed();
  234. if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
  235. fprintf(stdout," opus_{packet,decoder}_get_nb_samples() ....... OK.\n");
  236. cfgs+=9;
  237. if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
  238. for(i=0;i<256;i++) {
  239. int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
  240. packet[0]=i;
  241. if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
  242. cfgs++;
  243. for(j=0;j<256;j++) {
  244. packet[1]=j;
  245. if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
  246. cfgs++;
  247. }
  248. }
  249. fprintf(stdout," opus_packet_get_nb_frames() .................. OK.\n");
  250. for(i=0;i<256;i++) {
  251. int bw;
  252. packet[0]=i;
  253. bw=packet[0]>>4;
  254. bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
  255. if(bw!=opus_packet_get_bandwidth(packet))test_failed();
  256. cfgs++;
  257. }
  258. fprintf(stdout," opus_packet_get_bandwidth() .................. OK.\n");
  259. for(i=0;i<256;i++) {
  260. int fp3s,rate;
  261. packet[0]=i;
  262. fp3s=packet[0]>>3;
  263. fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
  264. for(rate=0;rate<5;rate++) {
  265. if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
  266. cfgs++;
  267. }
  268. }
  269. fprintf(stdout," opus_packet_get_samples_per_frame() .......... OK.\n");
  270. packet[0]=(63<<2)+3;
  271. packet[1]=49;
  272. for(j=2;j<51;j++)packet[j]=0;
  273. VG_UNDEF(sbuf,sizeof(sbuf));
  274. if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
  275. cfgs++;
  276. packet[0]=(63<<2);
  277. packet[1]=packet[2]=0;
  278. if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
  279. cfgs++;
  280. if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  281. cfgs++;
  282. if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  283. cfgs++;
  284. if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  285. cfgs++;
  286. fprintf(stdout," opus_decode() ................................ OK.\n");
  287. #ifndef DISABLE_FLOAT_API
  288. VG_UNDEF(fbuf,sizeof(fbuf));
  289. if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
  290. cfgs++;
  291. fprintf(stdout," opus_decode_float() .......................... OK.\n");
  292. #endif
  293. #if 0
  294. /*These tests are disabled because the library crashes with null states*/
  295. if(opus_decoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
  296. if(opus_decoder_init(0,48000,1) !=OPUS_INVALID_STATE)test_failed();
  297. if(opus_decode(0,packet,1,outbuf,2880,0) !=OPUS_INVALID_STATE)test_failed();
  298. if(opus_decode_float(0,packet,1,0,2880,0) !=OPUS_INVALID_STATE)test_failed();
  299. if(opus_decoder_get_nb_samples(0,packet,1) !=OPUS_INVALID_STATE)test_failed();
  300. if(opus_packet_get_nb_frames(NULL,1) !=OPUS_BAD_ARG)test_failed();
  301. if(opus_packet_get_bandwidth(NULL) !=OPUS_BAD_ARG)test_failed();
  302. if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
  303. #endif
  304. opus_decoder_destroy(dec);
  305. cfgs++;
  306. fprintf(stdout," All decoder interface tests passed\n");
  307. fprintf(stdout," (%6d API invocations)\n",cfgs);
  308. return cfgs;
  309. }
  310. opus_int32 test_msdec_api(void)
  311. {
  312. opus_uint32 dec_final_range;
  313. OpusMSDecoder *dec;
  314. OpusDecoder *streamdec;
  315. opus_int32 i,j,cfgs;
  316. unsigned char packet[1276];
  317. unsigned char mapping[256];
  318. #ifndef DISABLE_FLOAT_API
  319. float fbuf[960*2];
  320. #endif
  321. short sbuf[960*2];
  322. int a,b,c,err;
  323. mapping[0]=0;
  324. mapping[1]=1;
  325. for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
  326. cfgs=0;
  327. /*First test invalid configurations which should fail*/
  328. fprintf(stdout,"\n Multistream decoder basic API tests\n");
  329. fprintf(stdout," ---------------------------------------------------\n");
  330. for(a=-1;a<4;a++)
  331. {
  332. for(b=-1;b<4;b++)
  333. {
  334. i=opus_multistream_decoder_get_size(a,b);
  335. if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
  336. fprintf(stdout," opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
  337. cfgs++;
  338. }
  339. }
  340. /*Test with unsupported sample rates*/
  341. for(c=1;c<3;c++)
  342. {
  343. for(i=-7;i<=96000;i++)
  344. {
  345. int fs;
  346. if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
  347. switch(i)
  348. {
  349. case(-5):fs=-8000;break;
  350. case(-6):fs=INT32_MAX;break;
  351. case(-7):fs=INT32_MIN;break;
  352. default:fs=i;
  353. }
  354. err = OPUS_OK;
  355. VG_UNDEF(&err,sizeof(err));
  356. dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
  357. if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
  358. cfgs++;
  359. dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, 0);
  360. if(dec!=NULL)test_failed();
  361. cfgs++;
  362. dec=malloc(opus_multistream_decoder_get_size(1,1));
  363. if(dec==NULL)test_failed();
  364. err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
  365. if(err!=OPUS_BAD_ARG)test_failed();
  366. cfgs++;
  367. free(dec);
  368. }
  369. }
  370. for(c=0;c<2;c++)
  371. {
  372. int *ret_err;
  373. ret_err = c?0:&err;
  374. mapping[0]=0;
  375. mapping[1]=1;
  376. for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
  377. VG_UNDEF(ret_err,sizeof(*ret_err));
  378. dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
  379. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  380. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  381. cfgs++;
  382. VG_UNDEF(ret_err,sizeof(*ret_err));
  383. mapping[0]=mapping[1]=0;
  384. dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
  385. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  386. if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
  387. cfgs++;
  388. opus_multistream_decoder_destroy(dec);
  389. cfgs++;
  390. VG_UNDEF(ret_err,sizeof(*ret_err));
  391. dec = opus_multistream_decoder_create(48000, 1, 4, 1, mapping, ret_err);
  392. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  393. if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
  394. cfgs++;
  395. err = opus_multistream_decoder_init(dec,48000, 1, 0, 0, mapping);
  396. if(err!=OPUS_BAD_ARG)test_failed();
  397. cfgs++;
  398. err = opus_multistream_decoder_init(dec,48000, 1, 1, -1, mapping);
  399. if(err!=OPUS_BAD_ARG)test_failed();
  400. cfgs++;
  401. opus_multistream_decoder_destroy(dec);
  402. cfgs++;
  403. VG_UNDEF(ret_err,sizeof(*ret_err));
  404. dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, ret_err);
  405. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  406. if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
  407. cfgs++;
  408. opus_multistream_decoder_destroy(dec);
  409. cfgs++;
  410. VG_UNDEF(ret_err,sizeof(*ret_err));
  411. dec = opus_multistream_decoder_create(48000, 255, 255, 1, mapping, ret_err);
  412. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  413. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  414. cfgs++;
  415. VG_UNDEF(ret_err,sizeof(*ret_err));
  416. dec = opus_multistream_decoder_create(48000, -1, 1, 1, mapping, ret_err);
  417. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  418. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  419. cfgs++;
  420. VG_UNDEF(ret_err,sizeof(*ret_err));
  421. dec = opus_multistream_decoder_create(48000, 0, 1, 1, mapping, ret_err);
  422. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  423. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  424. cfgs++;
  425. VG_UNDEF(ret_err,sizeof(*ret_err));
  426. dec = opus_multistream_decoder_create(48000, 1, -1, 2, mapping, ret_err);
  427. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  428. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  429. cfgs++;
  430. VG_UNDEF(ret_err,sizeof(*ret_err));
  431. dec = opus_multistream_decoder_create(48000, 1, -1, -1, mapping, ret_err);
  432. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  433. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  434. cfgs++;
  435. VG_UNDEF(ret_err,sizeof(*ret_err));
  436. dec = opus_multistream_decoder_create(48000, 256, 255, 1, mapping, ret_err);
  437. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  438. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  439. cfgs++;
  440. VG_UNDEF(ret_err,sizeof(*ret_err));
  441. dec = opus_multistream_decoder_create(48000, 256, 255, 0, mapping, ret_err);
  442. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  443. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  444. cfgs++;
  445. VG_UNDEF(ret_err,sizeof(*ret_err));
  446. mapping[0]=255;
  447. mapping[1]=1;
  448. mapping[2]=2;
  449. dec = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, ret_err);
  450. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  451. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  452. cfgs++;
  453. VG_UNDEF(ret_err,sizeof(*ret_err));
  454. mapping[0]=0;
  455. mapping[1]=0;
  456. mapping[2]=0;
  457. dec = opus_multistream_decoder_create(48000, 3, 2, 1, mapping, ret_err);
  458. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  459. if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
  460. cfgs++;
  461. opus_multistream_decoder_destroy(dec);
  462. cfgs++;
  463. VG_UNDEF(ret_err,sizeof(*ret_err));
  464. mapping[0]=0;
  465. mapping[1]=255;
  466. mapping[2]=1;
  467. mapping[3]=2;
  468. mapping[4]=3;
  469. dec = opus_multistream_decoder_create(48001, 5, 4, 1, mapping, ret_err);
  470. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  471. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  472. cfgs++;
  473. }
  474. VG_UNDEF(&err,sizeof(err));
  475. mapping[0]=0;
  476. mapping[1]=255;
  477. mapping[2]=1;
  478. mapping[3]=2;
  479. dec = opus_multistream_decoder_create(48000, 4, 2, 1, mapping, &err);
  480. VG_CHECK(&err,sizeof(err));
  481. if(err!=OPUS_OK || dec==NULL)test_failed();
  482. cfgs++;
  483. fprintf(stdout," opus_multistream_decoder_create() ............ OK.\n");
  484. fprintf(stdout," opus_multistream_decoder_init() .............. OK.\n");
  485. VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
  486. err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
  487. if(err!=OPUS_OK)test_failed();
  488. VG_CHECK(&dec_final_range,sizeof(dec_final_range));
  489. fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
  490. cfgs++;
  491. streamdec=0;
  492. VG_UNDEF(&streamdec,sizeof(streamdec));
  493. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(-1,&streamdec));
  494. if(err!=OPUS_BAD_ARG)test_failed();
  495. cfgs++;
  496. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(1,&streamdec));
  497. if(err!=OPUS_OK||streamdec==NULL)test_failed();
  498. VG_CHECK(streamdec,opus_decoder_get_size(1));
  499. cfgs++;
  500. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(2,&streamdec));
  501. if(err!=OPUS_BAD_ARG)test_failed();
  502. cfgs++;
  503. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(0,&streamdec));
  504. if(err!=OPUS_OK||streamdec==NULL)test_failed();
  505. VG_CHECK(streamdec,opus_decoder_get_size(1));
  506. fprintf(stdout," OPUS_MULTISTREAM_GET_DECODER_STATE ........... OK.\n");
  507. cfgs++;
  508. for(j=0;j<2;j++)
  509. {
  510. OpusDecoder *od;
  511. err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
  512. if(err != OPUS_OK)test_failed();
  513. VG_UNDEF(&i,sizeof(i));
  514. err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
  515. VG_CHECK(&i,sizeof(i));
  516. if(err != OPUS_OK || i!=0)test_failed();
  517. cfgs++;
  518. }
  519. err=opus_multistream_decoder_ctl(dec,OPUS_SET_GAIN(15));
  520. if(err!=OPUS_OK)test_failed();
  521. fprintf(stdout," OPUS_SET_GAIN ................................ OK.\n");
  522. for(j=0;j<2;j++)
  523. {
  524. OpusDecoder *od;
  525. err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
  526. if(err != OPUS_OK)test_failed();
  527. VG_UNDEF(&i,sizeof(i));
  528. err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
  529. VG_CHECK(&i,sizeof(i));
  530. if(err != OPUS_OK || i!=15)test_failed();
  531. cfgs++;
  532. }
  533. fprintf(stdout," OPUS_GET_GAIN ................................ OK.\n");
  534. VG_UNDEF(&i,sizeof(i));
  535. err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
  536. if(err != OPUS_OK || i!=0)test_failed();
  537. fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
  538. cfgs++;
  539. err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
  540. if(err!=OPUS_UNIMPLEMENTED)test_failed();
  541. fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
  542. cfgs++;
  543. #if 0
  544. /*Currently unimplemented for multistream*/
  545. /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
  546. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(null_int_ptr));
  547. if(err!=OPUS_BAD_ARG)test_failed();
  548. cfgs++;
  549. VG_UNDEF(&i,sizeof(i));
  550. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  551. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  552. cfgs++;
  553. VG_UNDEF(packet,sizeof(packet));
  554. packet[0]=63<<2;packet[1]=packet[2]=0;
  555. if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  556. cfgs++;
  557. VG_UNDEF(&i,sizeof(i));
  558. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  559. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  560. cfgs++;
  561. packet[0]=1;
  562. if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
  563. cfgs++;
  564. VG_UNDEF(&i,sizeof(i));
  565. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  566. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  567. cfgs++;
  568. fprintf(stdout," OPUS_GET_PITCH ............................... OK.\n");
  569. #endif
  570. /*Reset the decoder*/
  571. if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  572. fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
  573. cfgs++;
  574. opus_multistream_decoder_destroy(dec);
  575. cfgs++;
  576. VG_UNDEF(&err,sizeof(err));
  577. dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
  578. if(err!=OPUS_OK || dec==NULL)test_failed();
  579. cfgs++;
  580. packet[0]=(63<<2)+3;
  581. packet[1]=49;
  582. for(j=2;j<51;j++)packet[j]=0;
  583. VG_UNDEF(sbuf,sizeof(sbuf));
  584. if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
  585. cfgs++;
  586. packet[0]=(63<<2);
  587. packet[1]=packet[2]=0;
  588. if(opus_multistream_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG){printf("%d\n",opus_multistream_decode(dec, packet, -1, sbuf, 960, 0));test_failed();}
  589. cfgs++;
  590. if(opus_multistream_decode(dec, packet, 3, sbuf, -960, 0)!=OPUS_BAD_ARG)test_failed();
  591. cfgs++;
  592. if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  593. cfgs++;
  594. if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  595. cfgs++;
  596. if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  597. cfgs++;
  598. fprintf(stdout," opus_multistream_decode() .................... OK.\n");
  599. #ifndef DISABLE_FLOAT_API
  600. VG_UNDEF(fbuf,sizeof(fbuf));
  601. if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
  602. cfgs++;
  603. fprintf(stdout," opus_multistream_decode_float() .............. OK.\n");
  604. #endif
  605. #if 0
  606. /*These tests are disabled because the library crashes with null states*/
  607. if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
  608. if(opus_multistream_decoder_init(0,48000,1) !=OPUS_INVALID_STATE)test_failed();
  609. if(opus_multistream_decode(0,packet,1,outbuf,2880,0) !=OPUS_INVALID_STATE)test_failed();
  610. if(opus_multistream_decode_float(0,packet,1,0,2880,0) !=OPUS_INVALID_STATE)test_failed();
  611. if(opus_multistream_decoder_get_nb_samples(0,packet,1) !=OPUS_INVALID_STATE)test_failed();
  612. #endif
  613. opus_multistream_decoder_destroy(dec);
  614. cfgs++;
  615. fprintf(stdout," All multistream decoder interface tests passed\n");
  616. fprintf(stdout," (%6d API invocations)\n",cfgs);
  617. return cfgs;
  618. }
  619. #ifdef VALGRIND
  620. #define UNDEFINE_FOR_PARSE toc=-1; \
  621. frames[0]=(unsigned char *)0; \
  622. frames[1]=(unsigned char *)0; \
  623. payload_offset=-1; \
  624. VG_UNDEF(&toc,sizeof(toc)); \
  625. VG_UNDEF(frames,sizeof(frames));\
  626. VG_UNDEF(&payload_offset,sizeof(payload_offset));
  627. #else
  628. #define UNDEFINE_FOR_PARSE toc=-1; \
  629. frames[0]=(unsigned char *)0; \
  630. frames[1]=(unsigned char *)0; \
  631. payload_offset=-1;
  632. #endif
  633. /* This test exercises the heck out of the libopus parser.
  634. It is much larger than the parser itself in part because
  635. it tries to hit a lot of corner cases that could never
  636. fail with the libopus code, but might be problematic for
  637. other implementations. */
  638. opus_int32 test_parse(void)
  639. {
  640. opus_int32 i,j,jj,sz;
  641. unsigned char packet[1276];
  642. opus_int32 cfgs,cfgs_total;
  643. unsigned char toc;
  644. const unsigned char *frames[48];
  645. short size[48];
  646. int payload_offset, ret;
  647. fprintf(stdout,"\n Packet header parsing tests\n");
  648. fprintf(stdout," ---------------------------------------------------\n");
  649. memset(packet,0,sizeof(char)*1276);
  650. packet[0]=63<<2;
  651. if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
  652. cfgs_total=cfgs=1;
  653. /*code 0*/
  654. for(i=0;i<64;i++)
  655. {
  656. packet[0]=i<<2;
  657. UNDEFINE_FOR_PARSE
  658. ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
  659. cfgs++;
  660. if(ret!=1)test_failed();
  661. if(size[0]!=3)test_failed();
  662. if(frames[0]!=packet+1)test_failed();
  663. }
  664. fprintf(stdout," code 0 (%2d cases) ............................ OK.\n",cfgs);
  665. cfgs_total+=cfgs;cfgs=0;
  666. /*code 1, two frames of the same size*/
  667. for(i=0;i<64;i++)
  668. {
  669. packet[0]=(i<<2)+1;
  670. for(jj=0;jj<=1275*2+3;jj++)
  671. {
  672. UNDEFINE_FOR_PARSE
  673. ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
  674. cfgs++;
  675. if((jj&1)==1 && jj<=2551)
  676. {
  677. /* Must pass if payload length even (packet length odd) and
  678. size<=2551, must fail otherwise. */
  679. if(ret!=2)test_failed();
  680. if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
  681. if(frames[0]!=packet+1)test_failed();
  682. if(frames[1]!=frames[0]+size[0])test_failed();
  683. if((toc>>2)!=i)test_failed();
  684. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  685. }
  686. }
  687. fprintf(stdout," code 1 (%6d cases) ........................ OK.\n",cfgs);
  688. cfgs_total+=cfgs;cfgs=0;
  689. for(i=0;i<64;i++)
  690. {
  691. /*code 2, length code overflow*/
  692. packet[0]=(i<<2)+2;
  693. UNDEFINE_FOR_PARSE
  694. ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
  695. cfgs++;
  696. if(ret!=OPUS_INVALID_PACKET)test_failed();
  697. packet[1]=252;
  698. UNDEFINE_FOR_PARSE
  699. ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
  700. cfgs++;
  701. if(ret!=OPUS_INVALID_PACKET)test_failed();
  702. for(j=0;j<1275;j++)
  703. {
  704. if(j<252)packet[1]=j;
  705. else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
  706. /*Code 2, one too short*/
  707. UNDEFINE_FOR_PARSE
  708. ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
  709. cfgs++;
  710. if(ret!=OPUS_INVALID_PACKET)test_failed();
  711. /*Code 2, one too long*/
  712. UNDEFINE_FOR_PARSE
  713. ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
  714. cfgs++;
  715. if(ret!=OPUS_INVALID_PACKET)test_failed();
  716. /*Code 2, second zero*/
  717. UNDEFINE_FOR_PARSE
  718. ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
  719. cfgs++;
  720. if(ret!=2)test_failed();
  721. if(size[0]!=j||size[1]!=0)test_failed();
  722. if(frames[1]!=frames[0]+size[0])test_failed();
  723. if((toc>>2)!=i)test_failed();
  724. /*Code 2, normal*/
  725. UNDEFINE_FOR_PARSE
  726. ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
  727. cfgs++;
  728. if(ret!=2)test_failed();
  729. if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
  730. if(frames[1]!=frames[0]+size[0])test_failed();
  731. if((toc>>2)!=i)test_failed();
  732. }
  733. }
  734. fprintf(stdout," code 2 (%6d cases) ........................ OK.\n",cfgs);
  735. cfgs_total+=cfgs;cfgs=0;
  736. for(i=0;i<64;i++)
  737. {
  738. packet[0]=(i<<2)+3;
  739. /*code 3, length code overflow*/
  740. UNDEFINE_FOR_PARSE
  741. ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
  742. cfgs++;
  743. if(ret!=OPUS_INVALID_PACKET)test_failed();
  744. }
  745. fprintf(stdout," code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
  746. cfgs_total+=cfgs;cfgs=0;
  747. for(i=0;i<64;i++)
  748. {
  749. /*code 3, m is zero or 49-63*/
  750. packet[0]=(i<<2)+3;
  751. for(jj=49;jj<=64;jj++)
  752. {
  753. packet[1]=0+(jj&63); /*CBR, no padding*/
  754. UNDEFINE_FOR_PARSE
  755. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  756. cfgs++;
  757. if(ret!=OPUS_INVALID_PACKET)test_failed();
  758. packet[1]=128+(jj&63); /*VBR, no padding*/
  759. UNDEFINE_FOR_PARSE
  760. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  761. cfgs++;
  762. if(ret!=OPUS_INVALID_PACKET)test_failed();
  763. packet[1]=64+(jj&63); /*CBR, padding*/
  764. UNDEFINE_FOR_PARSE
  765. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  766. cfgs++;
  767. if(ret!=OPUS_INVALID_PACKET)test_failed();
  768. packet[1]=128+64+(jj&63); /*VBR, padding*/
  769. UNDEFINE_FOR_PARSE
  770. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  771. cfgs++;
  772. if(ret!=OPUS_INVALID_PACKET)test_failed();
  773. }
  774. }
  775. fprintf(stdout," code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
  776. cfgs_total+=cfgs;cfgs=0;
  777. for(i=0;i<64;i++)
  778. {
  779. packet[0]=(i<<2)+3;
  780. /*code 3, m is one, cbr*/
  781. packet[1]=1;
  782. for(j=0;j<1276;j++)
  783. {
  784. UNDEFINE_FOR_PARSE
  785. ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
  786. cfgs++;
  787. if(ret!=1)test_failed();
  788. if(size[0]!=j)test_failed();
  789. if((toc>>2)!=i)test_failed();
  790. }
  791. UNDEFINE_FOR_PARSE
  792. ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
  793. cfgs++;
  794. if(ret!=OPUS_INVALID_PACKET)test_failed();
  795. }
  796. fprintf(stdout," code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
  797. cfgs_total+=cfgs;cfgs=0;
  798. for(i=0;i<64;i++)
  799. {
  800. int frame_samp;
  801. /*code 3, m>1 CBR*/
  802. packet[0]=(i<<2)+3;
  803. frame_samp=opus_packet_get_samples_per_frame(packet,48000);
  804. for(j=2;j<49;j++)
  805. {
  806. packet[1]=j;
  807. for(sz=2;sz<((j+2)*1275);sz++)
  808. {
  809. UNDEFINE_FOR_PARSE
  810. ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
  811. cfgs++;
  812. /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
  813. if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
  814. {
  815. if(ret!=j)test_failed();
  816. for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
  817. if((toc>>2)!=i)test_failed();
  818. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  819. }
  820. }
  821. /*Super jumbo packets*/
  822. packet[1]=5760/frame_samp;
  823. UNDEFINE_FOR_PARSE
  824. ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
  825. cfgs++;
  826. if(ret!=packet[1])test_failed();
  827. for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
  828. }
  829. fprintf(stdout," code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
  830. cfgs_total+=cfgs;cfgs=0;
  831. for(i=0;i<64;i++)
  832. {
  833. int frame_samp;
  834. /*Code 3 VBR, m one*/
  835. packet[0]=(i<<2)+3;
  836. packet[1]=128+1;
  837. frame_samp=opus_packet_get_samples_per_frame(packet,48000);
  838. for(jj=0;jj<1276;jj++)
  839. {
  840. UNDEFINE_FOR_PARSE
  841. ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
  842. cfgs++;
  843. if(ret!=1)test_failed();
  844. if(size[0]!=jj)test_failed();
  845. if((toc>>2)!=i)test_failed();
  846. }
  847. UNDEFINE_FOR_PARSE
  848. ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
  849. cfgs++;
  850. if(ret!=OPUS_INVALID_PACKET)test_failed();
  851. for(j=2;j<49;j++)
  852. {
  853. packet[1]=128+j;
  854. /*Length code overflow*/
  855. UNDEFINE_FOR_PARSE
  856. ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
  857. cfgs++;
  858. if(ret!=OPUS_INVALID_PACKET)test_failed();
  859. packet[2]=252;
  860. packet[3]=0;
  861. for(jj=4;jj<2+j;jj++)packet[jj]=0;
  862. UNDEFINE_FOR_PARSE
  863. ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
  864. cfgs++;
  865. if(ret!=OPUS_INVALID_PACKET)test_failed();
  866. /*One byte too short*/
  867. for(jj=2;jj<2+j;jj++)packet[jj]=0;
  868. UNDEFINE_FOR_PARSE
  869. ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
  870. cfgs++;
  871. if(ret!=OPUS_INVALID_PACKET)test_failed();
  872. /*One byte too short thanks to length coding*/
  873. packet[2]=252;
  874. packet[3]=0;
  875. for(jj=4;jj<2+j;jj++)packet[jj]=0;
  876. UNDEFINE_FOR_PARSE
  877. ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
  878. cfgs++;
  879. if(ret!=OPUS_INVALID_PACKET)test_failed();
  880. /*Most expensive way of coding zeros*/
  881. for(jj=2;jj<2+j;jj++)packet[jj]=0;
  882. UNDEFINE_FOR_PARSE
  883. ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
  884. cfgs++;
  885. if(frame_samp*j<=5760){
  886. if(ret!=j)test_failed();
  887. for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
  888. if((toc>>2)!=i)test_failed();
  889. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  890. /*Quasi-CBR use of mode 3*/
  891. for(sz=0;sz<8;sz++)
  892. {
  893. const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
  894. int pos=0;
  895. int as=(tsz[sz]+i-j-2)/j;
  896. for(jj=0;jj<j-1;jj++)
  897. {
  898. if(as<252){packet[2+pos]=as;pos++;}
  899. else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
  900. }
  901. UNDEFINE_FOR_PARSE
  902. ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
  903. cfgs++;
  904. if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
  905. if(ret!=j)test_failed();
  906. for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
  907. if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
  908. if((toc>>2)!=i)test_failed();
  909. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  910. }
  911. }
  912. }
  913. fprintf(stdout," code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
  914. cfgs_total+=cfgs;cfgs=0;
  915. for(i=0;i<64;i++)
  916. {
  917. packet[0]=(i<<2)+3;
  918. /*Padding*/
  919. packet[1]=128+1+64;
  920. /*Overflow the length coding*/
  921. for(jj=2;jj<127;jj++)packet[jj]=255;
  922. UNDEFINE_FOR_PARSE
  923. ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
  924. cfgs++;
  925. if(ret!=OPUS_INVALID_PACKET)test_failed();
  926. for(sz=0;sz<4;sz++)
  927. {
  928. const int tsz[4]={0,72,512,1275};
  929. for(jj=sz;jj<65025;jj+=11)
  930. {
  931. int pos;
  932. for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
  933. packet[2+pos]=jj%254;
  934. pos++;
  935. if(sz==0&&i==63)
  936. {
  937. /*Code more padding than there is room in the packet*/
  938. UNDEFINE_FOR_PARSE
  939. ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
  940. cfgs++;
  941. if(ret!=OPUS_INVALID_PACKET)test_failed();
  942. }
  943. UNDEFINE_FOR_PARSE
  944. ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
  945. cfgs++;
  946. if(tsz[sz]+i<1276)
  947. {
  948. if(ret!=1)test_failed();
  949. if(size[0]!=tsz[sz]+i)test_failed();
  950. if((toc>>2)!=i)test_failed();
  951. } else if (ret!=OPUS_INVALID_PACKET)test_failed();
  952. }
  953. }
  954. }
  955. fprintf(stdout," code 3 padding (%2d cases) ............... OK.\n",cfgs);
  956. cfgs_total+=cfgs;
  957. fprintf(stdout," opus_packet_parse ............................ OK.\n");
  958. fprintf(stdout," All packet parsing tests passed\n");
  959. fprintf(stdout," (%d API invocations)\n",cfgs_total);
  960. return cfgs_total;
  961. }
  962. /* This is a helper macro for the encoder tests.
  963. The encoder api tests all have a pattern of set-must-fail, set-must-fail,
  964. set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
  965. #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
  966. i=(badv);\
  967. if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
  968. i=(badv2);\
  969. if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
  970. j=i=(goodv);\
  971. if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
  972. i=-12345;\
  973. VG_UNDEF(&i,sizeof(i)); \
  974. err=opus_encoder_ctl(enc,getcall);\
  975. if(err!=OPUS_OK || i!=j)test_failed();\
  976. j=i=(goodv2);\
  977. if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
  978. fprintf(stdout,sok);\
  979. i=-12345;\
  980. VG_UNDEF(&i,sizeof(i)); \
  981. err=opus_encoder_ctl(enc,getcall);\
  982. if(err!=OPUS_OK || i!=j)test_failed();\
  983. fprintf(stdout,gok);\
  984. cfgs+=6;
  985. opus_int32 test_enc_api(void)
  986. {
  987. opus_uint32 enc_final_range;
  988. OpusEncoder *enc;
  989. opus_int32 i,j;
  990. unsigned char packet[1276];
  991. #ifndef DISABLE_FLOAT_API
  992. float fbuf[960*2];
  993. #endif
  994. short sbuf[960*2];
  995. int c,err,cfgs;
  996. cfgs=0;
  997. /*First test invalid configurations which should fail*/
  998. fprintf(stdout,"\n Encoder basic API tests\n");
  999. fprintf(stdout," ---------------------------------------------------\n");
  1000. for(c=0;c<4;c++)
  1001. {
  1002. i=opus_encoder_get_size(c);
  1003. if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
  1004. fprintf(stdout," opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
  1005. cfgs++;
  1006. }
  1007. /*Test with unsupported sample rates, channel counts*/
  1008. for(c=0;c<4;c++)
  1009. {
  1010. for(i=-7;i<=96000;i++)
  1011. {
  1012. int fs;
  1013. if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
  1014. switch(i)
  1015. {
  1016. case(-5):fs=-8000;break;
  1017. case(-6):fs=INT32_MAX;break;
  1018. case(-7):fs=INT32_MIN;break;
  1019. default:fs=i;
  1020. }
  1021. err = OPUS_OK;
  1022. VG_UNDEF(&err,sizeof(err));
  1023. enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
  1024. if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
  1025. cfgs++;
  1026. enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, 0);
  1027. if(enc!=NULL)test_failed();
  1028. cfgs++;
  1029. opus_encoder_destroy(enc);
  1030. enc=malloc(opus_encoder_get_size(2));
  1031. if(enc==NULL)test_failed();
  1032. err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
  1033. if(err!=OPUS_BAD_ARG)test_failed();
  1034. cfgs++;
  1035. free(enc);
  1036. }
  1037. }
  1038. enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
  1039. if(enc!=NULL)test_failed();
  1040. cfgs++;
  1041. VG_UNDEF(&err,sizeof(err));
  1042. enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
  1043. if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
  1044. cfgs++;
  1045. VG_UNDEF(&err,sizeof(err));
  1046. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
  1047. if(enc==NULL)test_failed();
  1048. opus_encoder_destroy(enc);
  1049. cfgs++;
  1050. VG_UNDEF(&err,sizeof(err));
  1051. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
  1052. if(err!=OPUS_OK || enc==NULL)test_failed();
  1053. cfgs++;
  1054. err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
  1055. if(err!=OPUS_OK || i<0 || i>32766)test_failed();
  1056. cfgs++;
  1057. opus_encoder_destroy(enc);
  1058. VG_UNDEF(&err,sizeof(err));
  1059. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
  1060. if(err!=OPUS_OK || enc==NULL)test_failed();
  1061. cfgs++;
  1062. err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
  1063. if(err!=OPUS_OK || i<0 || i>32766)test_failed();
  1064. opus_encoder_destroy(enc);
  1065. cfgs++;
  1066. VG_UNDEF(&err,sizeof(err));
  1067. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
  1068. if(err!=OPUS_OK || enc==NULL)test_failed();
  1069. cfgs++;
  1070. fprintf(stdout," opus_encoder_create() ........................ OK.\n");
  1071. fprintf(stdout," opus_encoder_init() .......................... OK.\n");
  1072. i=-12345;
  1073. VG_UNDEF(&i,sizeof(i));
  1074. err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
  1075. if(err!=OPUS_OK || i<0 || i>32766)test_failed();
  1076. cfgs++;
  1077. err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(null_int_ptr));
  1078. if(err!=OPUS_BAD_ARG)test_failed();
  1079. cfgs++;
  1080. fprintf(stdout," OPUS_GET_LOOKAHEAD ........................... OK.\n");
  1081. err=opus_encoder_ctl(enc,OPUS_GET_SAMPLE_RATE(&i));
  1082. if(err!=OPUS_OK || i!=48000)test_failed();
  1083. cfgs++;
  1084. err=opus_encoder_ctl(enc,OPUS_GET_SAMPLE_RATE(null_int_ptr));
  1085. if(err!=OPUS_BAD_ARG)test_failed();
  1086. cfgs++;
  1087. fprintf(stdout," OPUS_GET_SAMPLE_RATE ......................... OK.\n");
  1088. if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
  1089. fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
  1090. cfgs++;
  1091. err=opus_encoder_ctl(enc,OPUS_GET_APPLICATION(null_int_ptr));
  1092. if(err!=OPUS_BAD_ARG)test_failed();
  1093. cfgs++;
  1094. CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
  1095. OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
  1096. " OPUS_SET_APPLICATION ......................... OK.\n",
  1097. " OPUS_GET_APPLICATION ......................... OK.\n")
  1098. err=opus_encoder_ctl(enc,OPUS_GET_BITRATE(null_int_ptr));
  1099. if(err!=OPUS_BAD_ARG)test_failed();
  1100. cfgs++;
  1101. if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
  1102. cfgs++;
  1103. VG_UNDEF(&i,sizeof(i));
  1104. if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
  1105. if(i>700000||i<256000)test_failed();
  1106. cfgs++;
  1107. CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
  1108. 500,256000,
  1109. " OPUS_SET_BITRATE ............................. OK.\n",
  1110. " OPUS_GET_BITRATE ............................. OK.\n")
  1111. err=opus_encoder_ctl(enc,OPUS_GET_FORCE_CHANNELS(null_int_ptr));
  1112. if(err!=OPUS_BAD_ARG)test_failed();
  1113. cfgs++;
  1114. CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
  1115. 1,OPUS_AUTO,
  1116. " OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
  1117. " OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
  1118. i=-2;
  1119. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
  1120. cfgs++;
  1121. i=OPUS_BANDWIDTH_FULLBAND+1;
  1122. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
  1123. cfgs++;
  1124. i=OPUS_BANDWIDTH_NARROWBAND;
  1125. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1126. cfgs++;
  1127. i=OPUS_BANDWIDTH_FULLBAND;
  1128. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1129. cfgs++;
  1130. i=OPUS_BANDWIDTH_WIDEBAND;
  1131. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1132. cfgs++;
  1133. i=OPUS_BANDWIDTH_MEDIUMBAND;
  1134. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1135. cfgs++;
  1136. fprintf(stdout," OPUS_SET_BANDWIDTH ........................... OK.\n");
  1137. /*We don't test if the bandwidth has actually changed.
  1138. because the change may be delayed until the encoder is advanced.*/
  1139. i=-12345;
  1140. VG_UNDEF(&i,sizeof(i));
  1141. err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
  1142. if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
  1143. i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
  1144. i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
  1145. cfgs++;
  1146. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
  1147. cfgs++;
  1148. err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(null_int_ptr));
  1149. if(err!=OPUS_BAD_ARG)test_failed();
  1150. cfgs++;
  1151. fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
  1152. i=-2;
  1153. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
  1154. cfgs++;
  1155. i=OPUS_BANDWIDTH_FULLBAND+1;
  1156. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
  1157. cfgs++;
  1158. i=OPUS_BANDWIDTH_NARROWBAND;
  1159. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1160. cfgs++;
  1161. i=OPUS_BANDWIDTH_FULLBAND;
  1162. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1163. cfgs++;
  1164. i=OPUS_BANDWIDTH_WIDEBAND;
  1165. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1166. cfgs++;
  1167. i=OPUS_BANDWIDTH_MEDIUMBAND;
  1168. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1169. cfgs++;
  1170. fprintf(stdout," OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
  1171. /*We don't test if the bandwidth has actually changed.
  1172. because the change may be delayed until the encoder is advanced.*/
  1173. i=-12345;
  1174. VG_UNDEF(&i,sizeof(i));
  1175. err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
  1176. if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
  1177. i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
  1178. i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
  1179. cfgs++;
  1180. err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(null_int_ptr));
  1181. if(err!=OPUS_BAD_ARG)test_failed();
  1182. cfgs++;
  1183. fprintf(stdout," OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
  1184. err=opus_encoder_ctl(enc,OPUS_GET_DTX(null_int_ptr));
  1185. if(err!=OPUS_BAD_ARG)test_failed();
  1186. cfgs++;
  1187. CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
  1188. 1,0,
  1189. " OPUS_SET_DTX ................................. OK.\n",
  1190. " OPUS_GET_DTX ................................. OK.\n")
  1191. err=opus_encoder_ctl(enc,OPUS_GET_COMPLEXITY(null_int_ptr));
  1192. if(err!=OPUS_BAD_ARG)test_failed();
  1193. cfgs++;
  1194. CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
  1195. 0,10,
  1196. " OPUS_SET_COMPLEXITY .......................... OK.\n",
  1197. " OPUS_GET_COMPLEXITY .......................... OK.\n")
  1198. err=opus_encoder_ctl(enc,OPUS_GET_INBAND_FEC(null_int_ptr));
  1199. if(err!=OPUS_BAD_ARG)test_failed();
  1200. cfgs++;
  1201. CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
  1202. 1,0,
  1203. " OPUS_SET_INBAND_FEC .......................... OK.\n",
  1204. " OPUS_GET_INBAND_FEC .......................... OK.\n")
  1205. err=opus_encoder_ctl(enc,OPUS_GET_PACKET_LOSS_PERC(null_int_ptr));
  1206. if(err!=OPUS_BAD_ARG)test_failed();
  1207. cfgs++;
  1208. CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
  1209. 100,0,
  1210. " OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
  1211. " OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
  1212. err=opus_encoder_ctl(enc,OPUS_GET_VBR(null_int_ptr));
  1213. if(err!=OPUS_BAD_ARG)test_failed();
  1214. cfgs++;
  1215. CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
  1216. 1,0,
  1217. " OPUS_SET_VBR ................................. OK.\n",
  1218. " OPUS_GET_VBR ................................. OK.\n")
  1219. /* err=opus_encoder_ctl(enc,OPUS_GET_VOICE_RATIO(null_int_ptr));
  1220. if(err!=OPUS_BAD_ARG)test_failed();
  1221. cfgs++;
  1222. CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
  1223. 0,50,
  1224. " OPUS_SET_VOICE_RATIO ......................... OK.\n",
  1225. " OPUS_GET_VOICE_RATIO ......................... OK.\n")*/
  1226. err=opus_encoder_ctl(enc,OPUS_GET_VBR_CONSTRAINT(null_int_ptr));
  1227. if(err!=OPUS_BAD_ARG)test_failed();
  1228. cfgs++;
  1229. CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
  1230. 1,0,
  1231. " OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
  1232. " OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
  1233. err=opus_encoder_ctl(enc,OPUS_GET_SIGNAL(null_int_ptr));
  1234. if(err!=OPUS_BAD_ARG)test_failed();
  1235. cfgs++;
  1236. CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
  1237. OPUS_SIGNAL_MUSIC,OPUS_AUTO,
  1238. " OPUS_SET_SIGNAL .............................. OK.\n",
  1239. " OPUS_GET_SIGNAL .............................. OK.\n")
  1240. err=opus_encoder_ctl(enc,OPUS_GET_LSB_DEPTH(null_int_ptr));
  1241. if(err!=OPUS_BAD_ARG)test_failed();
  1242. cfgs++;
  1243. CHECK_SETGET(OPUS_SET_LSB_DEPTH(i),OPUS_GET_LSB_DEPTH(&i),7,25,16,24,
  1244. " OPUS_SET_LSB_DEPTH ........................... OK.\n",
  1245. " OPUS_GET_LSB_DEPTH ........................... OK.\n")
  1246. err=opus_encoder_ctl(enc,OPUS_GET_PREDICTION_DISABLED(&i));
  1247. if(i!=0)test_failed();
  1248. cfgs++;
  1249. err=opus_encoder_ctl(enc,OPUS_GET_PREDICTION_DISABLED(null_int_ptr));
  1250. if(err!=OPUS_BAD_ARG)test_failed();
  1251. cfgs++;
  1252. CHECK_SETGET(OPUS_SET_PREDICTION_DISABLED(i),OPUS_GET_PREDICTION_DISABLED(&i),-1,2,1,0,
  1253. " OPUS_SET_PREDICTION_DISABLED ................. OK.\n",
  1254. " OPUS_GET_PREDICTION_DISABLED ................. OK.\n")
  1255. err=opus_encoder_ctl(enc,OPUS_GET_EXPERT_FRAME_DURATION(null_int_ptr));
  1256. if(err!=OPUS_BAD_ARG)test_failed();
  1257. cfgs++;
  1258. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_2_5_MS));
  1259. if(err!=OPUS_OK)test_failed();
  1260. cfgs++;
  1261. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_5_MS));
  1262. if(err!=OPUS_OK)test_failed();
  1263. cfgs++;
  1264. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_10_MS));
  1265. if(err!=OPUS_OK)test_failed();
  1266. cfgs++;
  1267. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_20_MS));
  1268. if(err!=OPUS_OK)test_failed();
  1269. cfgs++;
  1270. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_40_MS));
  1271. if(err!=OPUS_OK)test_failed();
  1272. cfgs++;
  1273. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_60_MS));
  1274. if(err!=OPUS_OK)test_failed();
  1275. cfgs++;
  1276. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_80_MS));
  1277. if(err!=OPUS_OK)test_failed();
  1278. cfgs++;
  1279. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_100_MS));
  1280. if(err!=OPUS_OK)test_failed();
  1281. cfgs++;
  1282. err=opus_encoder_ctl(enc,OPUS_SET_EXPERT_FRAME_DURATION(OPUS_FRAMESIZE_120_MS));
  1283. if(err!=OPUS_OK)test_failed();
  1284. cfgs++;
  1285. CHECK_SETGET(OPUS_SET_EXPERT_FRAME_DURATION(i),OPUS_GET_EXPERT_FRAME_DURATION(&i),0,-1,
  1286. OPUS_FRAMESIZE_60_MS,OPUS_FRAMESIZE_ARG,
  1287. " OPUS_SET_EXPERT_FRAME_DURATION ............... OK.\n",
  1288. " OPUS_GET_EXPERT_FRAME_DURATION ............... OK.\n")
  1289. /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
  1290. err=opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(null_uint_ptr));
  1291. if(err!=OPUS_BAD_ARG)test_failed();
  1292. cfgs++;
  1293. if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
  1294. cfgs++;
  1295. fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
  1296. /*Reset the encoder*/
  1297. if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  1298. cfgs++;
  1299. fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
  1300. memset(sbuf,0,sizeof(short)*2*960);
  1301. VG_UNDEF(packet,sizeof(packet));
  1302. i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
  1303. if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
  1304. VG_CHECK(packet,i);
  1305. cfgs++;
  1306. fprintf(stdout," opus_encode() ................................ OK.\n");
  1307. #ifndef DISABLE_FLOAT_API
  1308. memset(fbuf,0,sizeof(float)*2*960);
  1309. VG_UNDEF(packet,sizeof(packet));
  1310. i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
  1311. if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
  1312. VG_CHECK(packet,i);
  1313. cfgs++;
  1314. fprintf(stdout," opus_encode_float() .......................... OK.\n");
  1315. #endif
  1316. #if 0
  1317. /*These tests are disabled because the library crashes with null states*/
  1318. if(opus_encoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
  1319. if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
  1320. if(opus_encode(0,sbuf,960,packet,sizeof(packet)) !=OPUS_INVALID_STATE)test_failed();
  1321. if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
  1322. #endif
  1323. opus_encoder_destroy(enc);
  1324. cfgs++;
  1325. fprintf(stdout," All encoder interface tests passed\n");
  1326. fprintf(stdout," (%d API invocations)\n",cfgs);
  1327. return cfgs;
  1328. }
  1329. #define max_out (1276*48+48*2+2)
  1330. int test_repacketizer_api(void)
  1331. {
  1332. int ret,cfgs,i,j,k;
  1333. OpusRepacketizer *rp;
  1334. unsigned char *packet;
  1335. unsigned char *po;
  1336. cfgs=0;
  1337. fprintf(stdout,"\n Repacketizer tests\n");
  1338. fprintf(stdout," ---------------------------------------------------\n");
  1339. packet=malloc(max_out);
  1340. if(packet==NULL)test_failed();
  1341. memset(packet,0,max_out);
  1342. po=malloc(max_out+256);
  1343. if(po==NULL)test_failed();
  1344. i=opus_repacketizer_get_size();
  1345. if(i<=0)test_failed();
  1346. cfgs++;
  1347. fprintf(stdout," opus_repacketizer_get_size()=%d ............. OK.\n",i);
  1348. rp=malloc(i);
  1349. rp=opus_repacketizer_init(rp);
  1350. if(rp==NULL)test_failed();
  1351. cfgs++;
  1352. free(rp);
  1353. fprintf(stdout," opus_repacketizer_init ....................... OK.\n");
  1354. rp=opus_repacketizer_create();
  1355. if(rp==NULL)test_failed();
  1356. cfgs++;
  1357. fprintf(stdout," opus_repacketizer_create ..................... OK.\n");
  1358. if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
  1359. cfgs++;
  1360. fprintf(stdout," opus_repacketizer_get_nb_frames .............. OK.\n");
  1361. /*Length overflows*/
  1362. VG_UNDEF(packet,4);
  1363. if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
  1364. cfgs++;
  1365. packet[0]=1;
  1366. if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
  1367. cfgs++;
  1368. packet[0]=2;
  1369. if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
  1370. cfgs++;
  1371. packet[0]=3;
  1372. if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
  1373. cfgs++;
  1374. packet[0]=2;
  1375. packet[1]=255;
  1376. if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
  1377. cfgs++;
  1378. packet[0]=2;
  1379. packet[1]=250;
  1380. if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
  1381. cfgs++;
  1382. packet[0]=3;
  1383. packet[1]=0;
  1384. if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
  1385. cfgs++;
  1386. packet[1]=49;
  1387. if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
  1388. cfgs++;
  1389. packet[0]=0;
  1390. if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
  1391. cfgs++;
  1392. packet[0]=1<<2;
  1393. if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
  1394. cfgs++;
  1395. /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
  1396. opus_repacketizer_init(rp);
  1397. for(j=0;j<32;j++)
  1398. {
  1399. /* TOC types, test half with stereo */
  1400. int maxi;
  1401. packet[0]=((j<<1)+(j&1))<<2;
  1402. maxi=960/opus_packet_get_samples_per_frame(packet,8000);
  1403. for(i=1;i<=maxi;i++)
  1404. {
  1405. /* Number of CBR frames in the input packets */
  1406. int maxp;
  1407. packet[0]=((j<<1)+(j&1))<<2;
  1408. if(i>1)packet[0]+=i==2?1:3;
  1409. packet[1]=i>2?i:0;
  1410. maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
  1411. for(k=0;k<=(1275+75);k+=3)
  1412. {
  1413. /*Payload size*/
  1414. opus_int32 cnt,rcnt;
  1415. if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
  1416. for(cnt=0;cnt<maxp+2;cnt++)
  1417. {
  1418. if(cnt>0)
  1419. {
  1420. ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
  1421. if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
  1422. cfgs++;
  1423. }
  1424. rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
  1425. if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
  1426. cfgs++;
  1427. ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
  1428. if(rcnt>0)
  1429. {
  1430. int len;
  1431. len=k*rcnt+((rcnt*i)>2?2:1);
  1432. if(ret!=len)test_failed();
  1433. if((rcnt*i)<2&&(po[0]&3)!=0)test_failed(); /* Code 0 */
  1434. if((rcnt*i)==2&&(po[0]&3)!=1)test_failed(); /* Code 1 */
  1435. if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
  1436. cfgs++;
  1437. if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
  1438. cfgs++;
  1439. if(opus_packet_unpad(po,len)!=len)test_failed();
  1440. cfgs++;
  1441. if(opus_packet_pad(po,len,len+1)!=OPUS_OK)test_failed();
  1442. cfgs++;
  1443. if(opus_packet_pad(po,len+1,len+256)!=OPUS_OK)test_failed();
  1444. cfgs++;
  1445. if(opus_packet_unpad(po,len+256)!=len)test_failed();
  1446. cfgs++;
  1447. if(opus_multistream_packet_unpad(po,len,1)!=len)test_failed();
  1448. cfgs++;
  1449. if(opus_multistream_packet_pad(po,len,len+1,1)!=OPUS_OK)test_failed();
  1450. cfgs++;
  1451. if(opus_multistream_packet_pad(po,len+1,len+256,1)!=OPUS_OK)test_failed();
  1452. cfgs++;
  1453. if(opus_multistream_packet_unpad(po,len+256,1)!=len)test_failed();
  1454. cfgs++;
  1455. if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1456. cfgs++;
  1457. if(len>1)
  1458. {
  1459. if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1460. cfgs++;
  1461. }
  1462. if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1463. cfgs++;
  1464. } else if (ret!=OPUS_BAD_ARG)test_failed(); /* M must not be 0 */
  1465. }
  1466. opus_repacketizer_init(rp);
  1467. }
  1468. }
  1469. }
  1470. /*Change in input count code, CBR out*/
  1471. opus_repacketizer_init(rp);
  1472. packet[0]=0;
  1473. if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
  1474. cfgs++;
  1475. packet[0]+=1;
  1476. if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
  1477. cfgs++;
  1478. i=opus_repacketizer_out(rp,po,max_out);
  1479. if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
  1480. cfgs++;
  1481. i=opus_repacketizer_out_range(rp,0,1,po,max_out);
  1482. if(i!=5||(po[0]&3)!=0)test_failed();
  1483. cfgs++;
  1484. i=opus_repacketizer_out_range(rp,1,2,po,max_out);
  1485. if(i!=5||(po[0]&3)!=0)test_failed();
  1486. cfgs++;
  1487. /*Change in input count code, VBR out*/
  1488. opus_repacketizer_init(rp);
  1489. packet[0]=1;
  1490. if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
  1491. cfgs++;
  1492. packet[0]=0;
  1493. if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
  1494. cfgs++;
  1495. i=opus_repacketizer_out(rp,po,max_out);
  1496. if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
  1497. cfgs++;
  1498. /*VBR in, VBR out*/
  1499. opus_repacketizer_init(rp);
  1500. packet[0]=2;
  1501. packet[1]=4;
  1502. if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
  1503. cfgs++;
  1504. if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
  1505. cfgs++;
  1506. i=opus_repacketizer_out(rp,po,max_out);
  1507. if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
  1508. cfgs++;
  1509. /*VBR in, CBR out*/
  1510. opus_repacketizer_init(rp);
  1511. packet[0]=2;
  1512. packet[1]=4;
  1513. if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
  1514. cfgs++;
  1515. if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
  1516. cfgs++;
  1517. i=opus_repacketizer_out(rp,po,max_out);
  1518. if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
  1519. cfgs++;
  1520. /*Count 0 in, VBR out*/
  1521. for(j=0;j<32;j++)
  1522. {
  1523. /* TOC types, test half with stereo */
  1524. int maxi,sum,rcnt;
  1525. packet[0]=((j<<1)+(j&1))<<2;
  1526. maxi=960/opus_packet_get_samples_per_frame(packet,8000);
  1527. sum=0;
  1528. rcnt=0;
  1529. opus_repacketizer_init(rp);
  1530. for(i=1;i<=maxi+2;i++)
  1531. {
  1532. int len;
  1533. ret=opus_repacketizer_cat(rp,packet,i);
  1534. if(rcnt<maxi)
  1535. {
  1536. if(ret!=OPUS_OK)test_failed();
  1537. rcnt++;
  1538. sum+=i-1;
  1539. } else if (ret!=OPUS_INVALID_PACKET)test_failed();
  1540. cfgs++;
  1541. len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
  1542. if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
  1543. if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
  1544. if(rcnt==2&&(po[0]&3)!=2)test_failed();
  1545. if(rcnt==1&&(po[0]&3)!=0)test_failed();
  1546. cfgs++;
  1547. if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
  1548. cfgs++;
  1549. if(opus_packet_unpad(po,len)!=len)test_failed();
  1550. cfgs++;
  1551. if(opus_packet_pad(po,len,len+1)!=OPUS_OK)test_failed();
  1552. cfgs++;
  1553. if(opus_packet_pad(po,len+1,len+256)!=OPUS_OK)test_failed();
  1554. cfgs++;
  1555. if(opus_packet_unpad(po,len+256)!=len)test_failed();
  1556. cfgs++;
  1557. if(opus_multistream_packet_unpad(po,len,1)!=len)test_failed();
  1558. cfgs++;
  1559. if(opus_multistream_packet_pad(po,len,len+1,1)!=OPUS_OK)test_failed();
  1560. cfgs++;
  1561. if(opus_multistream_packet_pad(po,len+1,len+256,1)!=OPUS_OK)test_failed();
  1562. cfgs++;
  1563. if(opus_multistream_packet_unpad(po,len+256,1)!=len)test_failed();
  1564. cfgs++;
  1565. if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1566. cfgs++;
  1567. if(len>1)
  1568. {
  1569. if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1570. cfgs++;
  1571. }
  1572. if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1573. cfgs++;
  1574. }
  1575. }
  1576. po[0]='O';
  1577. po[1]='p';
  1578. if(opus_packet_pad(po,4,4)!=OPUS_OK)test_failed();
  1579. cfgs++;
  1580. if(opus_multistream_packet_pad(po,4,4,1)!=OPUS_OK)test_failed();
  1581. cfgs++;
  1582. if(opus_packet_pad(po,4,5)!=OPUS_INVALID_PACKET)test_failed();
  1583. cfgs++;
  1584. if(opus_multistream_packet_pad(po,4,5,1)!=OPUS_INVALID_PACKET)test_failed();
  1585. cfgs++;
  1586. if(opus_packet_pad(po,0,5)!=OPUS_BAD_ARG)test_failed();
  1587. cfgs++;
  1588. if(opus_multistream_packet_pad(po,0,5,1)!=OPUS_BAD_ARG)test_failed();
  1589. cfgs++;
  1590. if(opus_packet_unpad(po,0)!=OPUS_BAD_ARG)test_failed();
  1591. cfgs++;
  1592. if(opus_multistream_packet_unpad(po,0,1)!=OPUS_BAD_ARG)test_failed();
  1593. cfgs++;
  1594. if(opus_packet_unpad(po,4)!=OPUS_INVALID_PACKET)test_failed();
  1595. cfgs++;
  1596. if(opus_multistream_packet_unpad(po,4,1)!=OPUS_INVALID_PACKET)test_failed();
  1597. cfgs++;
  1598. po[0]=0;
  1599. po[1]=0;
  1600. po[2]=0;
  1601. if(opus_packet_pad(po,5,4)!=OPUS_BAD_ARG)test_failed();
  1602. cfgs++;
  1603. if(opus_multistream_packet_pad(po,5,4,1)!=OPUS_BAD_ARG)test_failed();
  1604. cfgs++;
  1605. fprintf(stdout," opus_repacketizer_cat ........................ OK.\n");
  1606. fprintf(stdout," opus_repacketizer_out ........................ OK.\n");
  1607. fprintf(stdout," opus_repacketizer_out_range .................. OK.\n");
  1608. fprintf(stdout," opus_packet_pad .............................. OK.\n");
  1609. fprintf(stdout," opus_packet_unpad ............................ OK.\n");
  1610. fprintf(stdout," opus_multistream_packet_pad .................. OK.\n");
  1611. fprintf(stdout," opus_multistream_packet_unpad ................ OK.\n");
  1612. opus_repacketizer_destroy(rp);
  1613. cfgs++;
  1614. free(packet);
  1615. free(po);
  1616. fprintf(stdout," All repacketizer tests passed\n");
  1617. fprintf(stdout," (%7d API invocations)\n",cfgs);
  1618. return cfgs;
  1619. }
  1620. #ifdef MALLOC_FAIL
  1621. /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
  1622. * under GCC. However, this is the cleanest way to test malloc failure
  1623. * handling in our codebase, and the lack of thread safety isn't an
  1624. * issue here. We therefore disable the warning for this function.
  1625. */
  1626. #if OPUS_GNUC_PREREQ(4,6)
  1627. /* Save the current warning settings */
  1628. #pragma GCC diagnostic push
  1629. #endif
  1630. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1631. typedef void *(*mhook)(size_t __size, __const void *);
  1632. #endif
  1633. int test_malloc_fail(void)
  1634. {
  1635. #ifdef MALLOC_FAIL
  1636. OpusDecoder *dec;
  1637. OpusEncoder *enc;
  1638. OpusRepacketizer *rp;
  1639. unsigned char mapping[256] = {0,1};
  1640. OpusMSDecoder *msdec;
  1641. OpusMSEncoder *msenc;
  1642. int rate,c,app,cfgs,err,useerr;
  1643. int *ep;
  1644. mhook orig_malloc;
  1645. cfgs=0;
  1646. #endif
  1647. fprintf(stdout,"\n malloc() failure tests\n");
  1648. fprintf(stdout," ---------------------------------------------------\n");
  1649. #ifdef MALLOC_FAIL
  1650. orig_malloc=__malloc_hook;
  1651. __malloc_hook=malloc_hook;
  1652. ep=(int *)opus_alloc(sizeof(int));
  1653. if(ep!=NULL)
  1654. {
  1655. if(ep)free(ep);
  1656. __malloc_hook=orig_malloc;
  1657. #endif
  1658. fprintf(stdout," opus_decoder_create() ................... SKIPPED.\n");
  1659. fprintf(stdout," opus_encoder_create() ................... SKIPPED.\n");
  1660. fprintf(stdout," opus_repacketizer_create() .............. SKIPPED.\n");
  1661. fprintf(stdout," opus_multistream_decoder_create() ....... SKIPPED.\n");
  1662. fprintf(stdout," opus_multistream_encoder_create() ....... SKIPPED.\n");
  1663. fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
  1664. return 0;
  1665. #ifdef MALLOC_FAIL
  1666. }
  1667. for(useerr=0;useerr<2;useerr++)
  1668. {
  1669. ep=useerr?&err:0;
  1670. for(rate=0;rate<5;rate++)
  1671. {
  1672. for(c=1;c<3;c++)
  1673. {
  1674. err=1;
  1675. if(useerr)
  1676. {
  1677. VG_UNDEF(&err,sizeof(err));
  1678. }
  1679. dec=opus_decoder_create(opus_rates[rate], c, ep);
  1680. if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1681. {
  1682. __malloc_hook=orig_malloc;
  1683. test_failed();
  1684. }
  1685. cfgs++;
  1686. msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
  1687. if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1688. {
  1689. __malloc_hook=orig_malloc;
  1690. test_failed();
  1691. }
  1692. cfgs++;
  1693. for(app=0;app<3;app++)
  1694. {
  1695. if(useerr)
  1696. {
  1697. VG_UNDEF(&err,sizeof(err));
  1698. }
  1699. enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
  1700. if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1701. {
  1702. __malloc_hook=orig_malloc;
  1703. test_failed();
  1704. }
  1705. cfgs++;
  1706. msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
  1707. if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1708. {
  1709. __malloc_hook=orig_malloc;
  1710. test_failed();
  1711. }
  1712. cfgs++;
  1713. }
  1714. }
  1715. }
  1716. }
  1717. rp=opus_repacketizer_create();
  1718. if(rp!=NULL)
  1719. {
  1720. __malloc_hook=orig_malloc;
  1721. test_failed();
  1722. }
  1723. cfgs++;
  1724. __malloc_hook=orig_malloc;
  1725. fprintf(stdout," opus_decoder_create() ........................ OK.\n");
  1726. fprintf(stdout," opus_encoder_create() ........................ OK.\n");
  1727. fprintf(stdout," opus_repacketizer_create() ................... OK.\n");
  1728. fprintf(stdout," opus_multistream_decoder_create() ............ OK.\n");
  1729. fprintf(stdout," opus_multistream_encoder_create() ............ OK.\n");
  1730. fprintf(stdout," All malloc failure tests passed\n");
  1731. fprintf(stdout," (%2d API invocations)\n",cfgs);
  1732. return cfgs;
  1733. #endif
  1734. }
  1735. #ifdef MALLOC_FAIL
  1736. #if __GNUC_PREREQ(4,6)
  1737. #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
  1738. #endif
  1739. #endif
  1740. int main(int _argc, char **_argv)
  1741. {
  1742. opus_int32 total;
  1743. const char * oversion;
  1744. if(_argc>1)
  1745. {
  1746. fprintf(stderr,"Usage: %s\n",_argv[0]);
  1747. return 1;
  1748. }
  1749. iseed=0;
  1750. oversion=opus_get_version_string();
  1751. if(!oversion)test_failed();
  1752. fprintf(stderr,"Testing the %s API deterministically\n", oversion);
  1753. if(opus_strerror(-32768)==NULL)test_failed();
  1754. if(opus_strerror(32767)==NULL)test_failed();
  1755. if(strlen(opus_strerror(0))<1)test_failed();
  1756. total=4;
  1757. total+=test_dec_api();
  1758. total+=test_msdec_api();
  1759. total+=test_parse();
  1760. total+=test_enc_api();
  1761. total+=test_repacketizer_api();
  1762. total+=test_malloc_fail();
  1763. fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
  1764. return 0;
  1765. }