test_opus_api.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /* Copyright (c) 2011 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. static const opus_int32 opus_rates[5] = {48000,24000,16000,12000,8000};
  70. opus_int32 test_dec_api(void)
  71. {
  72. opus_uint32 dec_final_range;
  73. OpusDecoder *dec;
  74. OpusDecoder *dec2;
  75. opus_int32 i,j,cfgs;
  76. unsigned char packet[1276];
  77. #ifndef DISABLE_FLOAT_API
  78. float fbuf[960*2];
  79. #endif
  80. short sbuf[960*2];
  81. int c,err;
  82. opus_int32 *nullvalue;
  83. nullvalue=0;
  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. VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
  133. err=opus_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
  134. if(err!=OPUS_OK)test_failed();
  135. VG_CHECK(&dec_final_range,sizeof(dec_final_range));
  136. fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
  137. cfgs++;
  138. err=opus_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
  139. if(err!=OPUS_UNIMPLEMENTED)test_failed();
  140. fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
  141. cfgs++;
  142. VG_UNDEF(&i,sizeof(i));
  143. err=opus_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
  144. if(err != OPUS_OK || i!=0)test_failed();
  145. fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
  146. cfgs++;
  147. /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
  148. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
  149. if(err!=OPUS_BAD_ARG)test_failed();
  150. cfgs++;
  151. VG_UNDEF(&i,sizeof(i));
  152. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  153. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  154. cfgs++;
  155. VG_UNDEF(packet,sizeof(packet));
  156. packet[0]=63<<2;packet[1]=packet[2]=0;
  157. if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  158. cfgs++;
  159. VG_UNDEF(&i,sizeof(i));
  160. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  161. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  162. cfgs++;
  163. packet[0]=1;
  164. if(opus_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
  165. cfgs++;
  166. VG_UNDEF(&i,sizeof(i));
  167. err=opus_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  168. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  169. cfgs++;
  170. fprintf(stdout," OPUS_GET_PITCH ............................... OK.\n");
  171. VG_UNDEF(&i,sizeof(i));
  172. err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
  173. VG_CHECK(&i,sizeof(i));
  174. if(err != OPUS_OK || i!=0)test_failed();
  175. cfgs++;
  176. err=opus_decoder_ctl(dec, OPUS_GET_GAIN(nullvalue));
  177. if(err != OPUS_BAD_ARG)test_failed();
  178. cfgs++;
  179. err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-32769));
  180. if(err != OPUS_BAD_ARG)test_failed();
  181. cfgs++;
  182. err=opus_decoder_ctl(dec, OPUS_SET_GAIN(32768));
  183. if(err != OPUS_BAD_ARG)test_failed();
  184. cfgs++;
  185. err=opus_decoder_ctl(dec, OPUS_SET_GAIN(-15));
  186. if(err != OPUS_OK)test_failed();
  187. cfgs++;
  188. VG_UNDEF(&i,sizeof(i));
  189. err=opus_decoder_ctl(dec, OPUS_GET_GAIN(&i));
  190. VG_CHECK(&i,sizeof(i));
  191. if(err != OPUS_OK || i!=-15)test_failed();
  192. cfgs++;
  193. fprintf(stdout," OPUS_SET_GAIN ................................ OK.\n");
  194. fprintf(stdout," OPUS_GET_GAIN ................................ OK.\n");
  195. /*Reset the decoder*/
  196. dec2=malloc(opus_decoder_get_size(2));
  197. memcpy(dec2,dec,opus_decoder_get_size(2));
  198. if(opus_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  199. if(memcmp(dec2,dec,opus_decoder_get_size(2))==0)test_failed();
  200. free(dec2);
  201. fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
  202. cfgs++;
  203. VG_UNDEF(packet,sizeof(packet));
  204. packet[0]=0;
  205. if(opus_decoder_get_nb_samples(dec,packet,1)!=480)test_failed();
  206. cfgs++;
  207. packet[0]=(63<<2)|3;
  208. packet[1]=63;
  209. if(opus_decoder_get_nb_samples(dec,packet,2)!=OPUS_INVALID_PACKET)test_failed();
  210. fprintf(stdout," opus_decoder_get_nb_samples() ................ OK.\n");
  211. cfgs++;
  212. if(OPUS_BAD_ARG!=opus_packet_get_nb_frames(packet,0))test_failed();
  213. for(i=0;i<256;i++) {
  214. int l1res[4]={1,2,2,OPUS_INVALID_PACKET};
  215. packet[0]=i;
  216. if(l1res[packet[0]&3]!=opus_packet_get_nb_frames(packet,1))test_failed();
  217. cfgs++;
  218. for(j=0;j<256;j++) {
  219. packet[1]=j;
  220. if(((packet[0]&3)!=3?l1res[packet[0]&3]:packet[1]&63)!=opus_packet_get_nb_frames(packet,2))test_failed();
  221. cfgs++;
  222. }
  223. }
  224. fprintf(stdout," opus_packet_get_nb_frames() .................. OK.\n");
  225. for(i=0;i<256;i++) {
  226. int bw;
  227. packet[0]=i;
  228. bw=packet[0]>>4;
  229. bw=OPUS_BANDWIDTH_NARROWBAND+(((((bw&7)*9)&(63-(bw&8)))+2+12*((bw&8)!=0))>>4);
  230. if(bw!=opus_packet_get_bandwidth(packet))test_failed();
  231. cfgs++;
  232. }
  233. fprintf(stdout," opus_packet_get_bandwidth() .................. OK.\n");
  234. for(i=0;i<256;i++) {
  235. int fp3s,rate;
  236. packet[0]=i;
  237. fp3s=packet[0]>>3;
  238. fp3s=((((3-(fp3s&3))*13&119)+9)>>2)*((fp3s>13)*(3-((fp3s&3)==3))+1)*25;
  239. for(rate=0;rate<5;rate++) {
  240. if((opus_rates[rate]*3/fp3s)!=opus_packet_get_samples_per_frame(packet,opus_rates[rate]))test_failed();
  241. cfgs++;
  242. }
  243. }
  244. fprintf(stdout," opus_packet_get_samples_per_frame() .......... OK.\n");
  245. packet[0]=(63<<2)+3;
  246. packet[1]=49;
  247. for(j=2;j<51;j++)packet[j]=0;
  248. VG_UNDEF(sbuf,sizeof(sbuf));
  249. if(opus_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
  250. cfgs++;
  251. packet[0]=(63<<2);
  252. packet[1]=packet[2]=0;
  253. if(opus_decode(dec, packet, -1, sbuf, 960, 0)!=OPUS_BAD_ARG)test_failed();
  254. cfgs++;
  255. if(opus_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  256. cfgs++;
  257. if(opus_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  258. cfgs++;
  259. if(opus_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  260. cfgs++;
  261. fprintf(stdout," opus_decode() ................................ OK.\n");
  262. #ifndef DISABLE_FLOAT_API
  263. VG_UNDEF(fbuf,sizeof(fbuf));
  264. if(opus_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
  265. cfgs++;
  266. fprintf(stdout," opus_decode_float() .......................... OK.\n");
  267. #endif
  268. #if 0
  269. /*These tests are disabled because the library crashes with null states*/
  270. if(opus_decoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
  271. if(opus_decoder_init(0,48000,1) !=OPUS_INVALID_STATE)test_failed();
  272. if(opus_decode(0,packet,1,outbuf,2880,0) !=OPUS_INVALID_STATE)test_failed();
  273. if(opus_decode_float(0,packet,1,0,2880,0) !=OPUS_INVALID_STATE)test_failed();
  274. if(opus_decoder_get_nb_samples(0,packet,1) !=OPUS_INVALID_STATE)test_failed();
  275. if(opus_packet_get_nb_frames(NULL,1) !=OPUS_BAD_ARG)test_failed();
  276. if(opus_packet_get_bandwidth(NULL) !=OPUS_BAD_ARG)test_failed();
  277. if(opus_packet_get_samples_per_frame(NULL,48000)!=OPUS_BAD_ARG)test_failed();
  278. #endif
  279. opus_decoder_destroy(dec);
  280. cfgs++;
  281. fprintf(stdout," All decoder interface tests passed\n");
  282. fprintf(stdout," (%6d API invocations)\n",cfgs);
  283. return cfgs;
  284. }
  285. opus_int32 test_msdec_api(void)
  286. {
  287. opus_uint32 dec_final_range;
  288. OpusMSDecoder *dec;
  289. OpusDecoder *streamdec;
  290. opus_int32 i,j,cfgs;
  291. unsigned char packet[1276];
  292. unsigned char mapping[256];
  293. #ifndef DISABLE_FLOAT_API
  294. float fbuf[960*2];
  295. #endif
  296. short sbuf[960*2];
  297. int a,b,c,err;
  298. #if 0
  299. /*Relevant test not enabled for multistream*/
  300. int *nullvalue;
  301. nullvalue=0;
  302. #endif
  303. mapping[0]=0;
  304. mapping[1]=1;
  305. for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
  306. cfgs=0;
  307. /*First test invalid configurations which should fail*/
  308. fprintf(stdout,"\n Multistream decoder basic API tests\n");
  309. fprintf(stdout," ---------------------------------------------------\n");
  310. for(a=-1;a<4;a++)
  311. {
  312. for(b=-1;b<4;b++)
  313. {
  314. i=opus_multistream_decoder_get_size(a,b);
  315. if(((a>0&&b<=a&&b>=0)&&(i<=2048||i>((1<<16)*a)))||((a<1||b>a||b<0)&&i!=0))test_failed();
  316. fprintf(stdout," opus_multistream_decoder_get_size(%2d,%2d)=%d %sOK.\n",a,b,i,i>0?"":"... ");
  317. cfgs++;
  318. }
  319. }
  320. /*Test with unsupported sample rates*/
  321. for(c=1;c<3;c++)
  322. {
  323. for(i=-7;i<=96000;i++)
  324. {
  325. int fs;
  326. if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
  327. switch(i)
  328. {
  329. case(-5):fs=-8000;break;
  330. case(-6):fs=INT32_MAX;break;
  331. case(-7):fs=INT32_MIN;break;
  332. default:fs=i;
  333. }
  334. err = OPUS_OK;
  335. VG_UNDEF(&err,sizeof(err));
  336. dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, &err);
  337. if(err!=OPUS_BAD_ARG || dec!=NULL)test_failed();
  338. cfgs++;
  339. dec = opus_multistream_decoder_create(fs, c, 1, c-1, mapping, 0);
  340. if(dec!=NULL)test_failed();
  341. cfgs++;
  342. dec=malloc(opus_multistream_decoder_get_size(1,1));
  343. if(dec==NULL)test_failed();
  344. err = opus_multistream_decoder_init(dec,fs,c,1,c-1, mapping);
  345. if(err!=OPUS_BAD_ARG)test_failed();
  346. cfgs++;
  347. free(dec);
  348. }
  349. }
  350. for(c=0;c<2;c++)
  351. {
  352. int *ret_err;
  353. ret_err = c?0:&err;
  354. mapping[0]=0;
  355. mapping[1]=1;
  356. for(i=2;i<256;i++)VG_UNDEF(&mapping[i],sizeof(unsigned char));
  357. VG_UNDEF(ret_err,sizeof(*ret_err));
  358. dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
  359. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  360. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  361. cfgs++;
  362. VG_UNDEF(ret_err,sizeof(*ret_err));
  363. mapping[0]=mapping[1]=0;
  364. dec = opus_multistream_decoder_create(48000, 2, 1, 0, mapping, ret_err);
  365. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  366. if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
  367. cfgs++;
  368. opus_multistream_decoder_destroy(dec);
  369. cfgs++;
  370. VG_UNDEF(ret_err,sizeof(*ret_err));
  371. dec = opus_multistream_decoder_create(48000, 1, 4, 1, mapping, ret_err);
  372. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  373. if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
  374. cfgs++;
  375. err = opus_multistream_decoder_init(dec,48000, 1, 0, 0, mapping);
  376. if(err!=OPUS_BAD_ARG)test_failed();
  377. cfgs++;
  378. err = opus_multistream_decoder_init(dec,48000, 1, 1, -1, mapping);
  379. if(err!=OPUS_BAD_ARG)test_failed();
  380. cfgs++;
  381. opus_multistream_decoder_destroy(dec);
  382. cfgs++;
  383. VG_UNDEF(ret_err,sizeof(*ret_err));
  384. dec = opus_multistream_decoder_create(48000, 2, 1, 1, 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, 255, 255, 1, mapping, ret_err);
  392. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  393. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  394. cfgs++;
  395. VG_UNDEF(ret_err,sizeof(*ret_err));
  396. dec = opus_multistream_decoder_create(48000, -1, 1, 1, mapping, ret_err);
  397. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  398. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  399. cfgs++;
  400. VG_UNDEF(ret_err,sizeof(*ret_err));
  401. dec = opus_multistream_decoder_create(48000, 0, 1, 1, mapping, ret_err);
  402. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  403. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  404. cfgs++;
  405. VG_UNDEF(ret_err,sizeof(*ret_err));
  406. dec = opus_multistream_decoder_create(48000, 1, -1, 2, mapping, ret_err);
  407. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  408. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  409. cfgs++;
  410. VG_UNDEF(ret_err,sizeof(*ret_err));
  411. dec = opus_multistream_decoder_create(48000, 1, -1, -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, 256, 255, 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, 256, 255, 0, 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. mapping[0]=255;
  427. mapping[1]=1;
  428. mapping[2]=2;
  429. dec = opus_multistream_decoder_create(48000, 3, 2, 0, mapping, ret_err);
  430. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  431. if((ret_err && *ret_err!=OPUS_BAD_ARG) || dec!=NULL)test_failed();
  432. cfgs++;
  433. VG_UNDEF(ret_err,sizeof(*ret_err));
  434. mapping[0]=0;
  435. mapping[1]=0;
  436. mapping[2]=0;
  437. dec = opus_multistream_decoder_create(48000, 3, 2, 1, mapping, ret_err);
  438. if(ret_err){VG_CHECK(ret_err,sizeof(*ret_err));}
  439. if((ret_err && *ret_err!=OPUS_OK) || dec==NULL)test_failed();
  440. cfgs++;
  441. opus_multistream_decoder_destroy(dec);
  442. cfgs++;
  443. VG_UNDEF(ret_err,sizeof(*ret_err));
  444. mapping[0]=0;
  445. mapping[1]=255;
  446. mapping[2]=1;
  447. mapping[3]=2;
  448. mapping[4]=3;
  449. dec = opus_multistream_decoder_create(48001, 5, 4, 1, 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. }
  454. VG_UNDEF(&err,sizeof(err));
  455. mapping[0]=0;
  456. mapping[1]=255;
  457. mapping[2]=1;
  458. mapping[3]=2;
  459. dec = opus_multistream_decoder_create(48000, 4, 2, 1, mapping, &err);
  460. VG_CHECK(&err,sizeof(err));
  461. if(err!=OPUS_OK || dec==NULL)test_failed();
  462. cfgs++;
  463. fprintf(stdout," opus_multistream_decoder_create() ............ OK.\n");
  464. fprintf(stdout," opus_multistream_decoder_init() .............. OK.\n");
  465. VG_UNDEF(&dec_final_range,sizeof(dec_final_range));
  466. err=opus_multistream_decoder_ctl(dec, OPUS_GET_FINAL_RANGE(&dec_final_range));
  467. if(err!=OPUS_OK)test_failed();
  468. VG_CHECK(&dec_final_range,sizeof(dec_final_range));
  469. fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
  470. cfgs++;
  471. streamdec=0;
  472. VG_UNDEF(&streamdec,sizeof(streamdec));
  473. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(-1,&streamdec));
  474. if(err!=OPUS_BAD_ARG)test_failed();
  475. cfgs++;
  476. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(1,&streamdec));
  477. if(err!=OPUS_OK||streamdec==NULL)test_failed();
  478. VG_CHECK(streamdec,opus_decoder_get_size(1));
  479. cfgs++;
  480. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(2,&streamdec));
  481. if(err!=OPUS_BAD_ARG)test_failed();
  482. cfgs++;
  483. err=opus_multistream_decoder_ctl(dec, OPUS_MULTISTREAM_GET_DECODER_STATE(0,&streamdec));
  484. if(err!=OPUS_OK||streamdec==NULL)test_failed();
  485. VG_CHECK(streamdec,opus_decoder_get_size(1));
  486. fprintf(stdout," OPUS_MULTISTREAM_GET_DECODER_STATE ........... OK.\n");
  487. cfgs++;
  488. for(j=0;j<2;j++)
  489. {
  490. OpusDecoder *od;
  491. err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
  492. if(err != OPUS_OK)test_failed();
  493. VG_UNDEF(&i,sizeof(i));
  494. err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
  495. VG_CHECK(&i,sizeof(i));
  496. if(err != OPUS_OK || i!=0)test_failed();
  497. cfgs++;
  498. }
  499. err=opus_multistream_decoder_ctl(dec,OPUS_SET_GAIN(15));
  500. if(err!=OPUS_OK)test_failed();
  501. fprintf(stdout," OPUS_SET_GAIN ................................ OK.\n");
  502. for(j=0;j<2;j++)
  503. {
  504. OpusDecoder *od;
  505. err=opus_multistream_decoder_ctl(dec,OPUS_MULTISTREAM_GET_DECODER_STATE(j,&od));
  506. if(err != OPUS_OK)test_failed();
  507. VG_UNDEF(&i,sizeof(i));
  508. err=opus_decoder_ctl(od, OPUS_GET_GAIN(&i));
  509. VG_CHECK(&i,sizeof(i));
  510. if(err != OPUS_OK || i!=15)test_failed();
  511. cfgs++;
  512. }
  513. fprintf(stdout," OPUS_GET_GAIN ................................ OK.\n");
  514. VG_UNDEF(&i,sizeof(i));
  515. err=opus_multistream_decoder_ctl(dec, OPUS_GET_BANDWIDTH(&i));
  516. if(err != OPUS_OK || i!=0)test_failed();
  517. fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
  518. cfgs++;
  519. err=opus_multistream_decoder_ctl(dec,OPUS_UNIMPLEMENTED);
  520. if(err!=OPUS_UNIMPLEMENTED)test_failed();
  521. fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
  522. cfgs++;
  523. #if 0
  524. /*Currently unimplemented for multistream*/
  525. /*GET_PITCH has different execution paths depending on the previously decoded frame.*/
  526. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(nullvalue));
  527. if(err!=OPUS_BAD_ARG)test_failed();
  528. cfgs++;
  529. VG_UNDEF(&i,sizeof(i));
  530. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  531. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  532. cfgs++;
  533. VG_UNDEF(packet,sizeof(packet));
  534. packet[0]=63<<2;packet[1]=packet[2]=0;
  535. if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  536. cfgs++;
  537. VG_UNDEF(&i,sizeof(i));
  538. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  539. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  540. cfgs++;
  541. packet[0]=1;
  542. if(opus_multistream_decode(dec, packet, 1, sbuf, 960, 0)!=960)test_failed();
  543. cfgs++;
  544. VG_UNDEF(&i,sizeof(i));
  545. err=opus_multistream_decoder_ctl(dec, OPUS_GET_PITCH(&i));
  546. if(err != OPUS_OK || i>0 || i<-1)test_failed();
  547. cfgs++;
  548. fprintf(stdout," OPUS_GET_PITCH ............................... OK.\n");
  549. #endif
  550. /*Reset the decoder*/
  551. if(opus_multistream_decoder_ctl(dec, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  552. fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
  553. cfgs++;
  554. opus_multistream_decoder_destroy(dec);
  555. cfgs++;
  556. VG_UNDEF(&err,sizeof(err));
  557. dec = opus_multistream_decoder_create(48000, 2, 1, 1, mapping, &err);
  558. if(err!=OPUS_OK || dec==NULL)test_failed();
  559. cfgs++;
  560. packet[0]=(63<<2)+3;
  561. packet[1]=49;
  562. for(j=2;j<51;j++)packet[j]=0;
  563. VG_UNDEF(sbuf,sizeof(sbuf));
  564. if(opus_multistream_decode(dec, packet, 51, sbuf, 960, 0)!=OPUS_INVALID_PACKET)test_failed();
  565. cfgs++;
  566. packet[0]=(63<<2);
  567. packet[1]=packet[2]=0;
  568. 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();}
  569. cfgs++;
  570. if(opus_multistream_decode(dec, packet, 3, sbuf, 60, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  571. cfgs++;
  572. if(opus_multistream_decode(dec, packet, 3, sbuf, 480, 0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  573. cfgs++;
  574. if(opus_multistream_decode(dec, packet, 3, sbuf, 960, 0)!=960)test_failed();
  575. cfgs++;
  576. fprintf(stdout," opus_multistream_decode() .................... OK.\n");
  577. #ifndef DISABLE_FLOAT_API
  578. VG_UNDEF(fbuf,sizeof(fbuf));
  579. if(opus_multistream_decode_float(dec, packet, 3, fbuf, 960, 0)!=960)test_failed();
  580. cfgs++;
  581. fprintf(stdout," opus_multistream_decode_float() .............. OK.\n");
  582. #endif
  583. #if 0
  584. /*These tests are disabled because the library crashes with null states*/
  585. if(opus_multistream_decoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
  586. if(opus_multistream_decoder_init(0,48000,1) !=OPUS_INVALID_STATE)test_failed();
  587. if(opus_multistream_decode(0,packet,1,outbuf,2880,0) !=OPUS_INVALID_STATE)test_failed();
  588. if(opus_multistream_decode_float(0,packet,1,0,2880,0) !=OPUS_INVALID_STATE)test_failed();
  589. if(opus_multistream_decoder_get_nb_samples(0,packet,1) !=OPUS_INVALID_STATE)test_failed();
  590. #endif
  591. opus_multistream_decoder_destroy(dec);
  592. cfgs++;
  593. fprintf(stdout," All multistream decoder interface tests passed\n");
  594. fprintf(stdout," (%6d API invocations)\n",cfgs);
  595. return cfgs;
  596. }
  597. #ifdef VALGRIND
  598. #define UNDEFINE_FOR_PARSE toc=-1; \
  599. frames[0]=(unsigned char *)0; \
  600. frames[1]=(unsigned char *)0; \
  601. payload_offset=-1; \
  602. VG_UNDEF(&toc,sizeof(toc)); \
  603. VG_UNDEF(frames,sizeof(frames));\
  604. VG_UNDEF(&payload_offset,sizeof(payload_offset));
  605. #else
  606. #define UNDEFINE_FOR_PARSE toc=-1; \
  607. frames[0]=(unsigned char *)0; \
  608. frames[1]=(unsigned char *)0; \
  609. payload_offset=-1;
  610. #endif
  611. /* This test exercises the heck out of the libopus parser.
  612. It is much larger than the parser itself in part because
  613. it tries to hit a lot of corner cases that could never
  614. fail with the libopus code, but might be problematic for
  615. other implementations. */
  616. opus_int32 test_parse(void)
  617. {
  618. opus_int32 i,j,jj,sz;
  619. unsigned char packet[1276];
  620. opus_int32 cfgs,cfgs_total;
  621. unsigned char toc;
  622. const unsigned char *frames[48];
  623. short size[48];
  624. int payload_offset, ret;
  625. fprintf(stdout,"\n Packet header parsing tests\n");
  626. fprintf(stdout," ---------------------------------------------------\n");
  627. memset(packet,0,sizeof(char)*1276);
  628. packet[0]=63<<2;
  629. if(opus_packet_parse(packet,1,&toc,frames,0,&payload_offset)!=OPUS_BAD_ARG)test_failed();
  630. cfgs_total=cfgs=1;
  631. /*code 0*/
  632. for(i=0;i<64;i++)
  633. {
  634. UNDEFINE_FOR_PARSE
  635. ret=opus_packet_parse(packet,4,&toc,frames,size,&payload_offset);
  636. cfgs++;
  637. if(ret!=1)test_failed();
  638. if(size[0]!=3)test_failed();
  639. if(frames[0]!=packet+1)test_failed();
  640. }
  641. fprintf(stdout," code 0 (%2d cases) ............................ OK.\n",cfgs);
  642. cfgs_total+=cfgs;cfgs=0;
  643. /*code 1, two frames of the same size*/
  644. for(i=0;i<64;i++)
  645. {
  646. packet[0]=(i<<2)+1;
  647. for(jj=0;jj<=1275*2+3;jj++)
  648. {
  649. UNDEFINE_FOR_PARSE
  650. ret=opus_packet_parse(packet,jj,&toc,frames,size,&payload_offset);
  651. cfgs++;
  652. if((jj&1)==1 && jj<=2551)
  653. {
  654. /* Must pass if payload length even (packet length odd) and
  655. size<=2551, must fail otherwise. */
  656. if(ret!=2)test_failed();
  657. if(size[0]!=size[1] || size[0]!=((jj-1)>>1))test_failed();
  658. if(frames[0]!=packet+1)test_failed();
  659. if(frames[1]!=frames[0]+size[0])test_failed();
  660. if((toc>>2)!=i)test_failed();
  661. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  662. }
  663. }
  664. fprintf(stdout," code 1 (%6d cases) ........................ OK.\n",cfgs);
  665. cfgs_total+=cfgs;cfgs=0;
  666. for(i=0;i<64;i++)
  667. {
  668. /*code 2, length code overflow*/
  669. packet[0]=(i<<2)+2;
  670. UNDEFINE_FOR_PARSE
  671. ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
  672. cfgs++;
  673. if(ret!=OPUS_INVALID_PACKET)test_failed();
  674. packet[1]=252;
  675. UNDEFINE_FOR_PARSE
  676. ret=opus_packet_parse(packet,2,&toc,frames,size,&payload_offset);
  677. cfgs++;
  678. if(ret!=OPUS_INVALID_PACKET)test_failed();
  679. for(j=0;j<1275;j++)
  680. {
  681. if(j<252)packet[1]=j;
  682. else{packet[1]=252+(j&3);packet[2]=(j-252)>>2;}
  683. /*Code 2, one too short*/
  684. UNDEFINE_FOR_PARSE
  685. ret=opus_packet_parse(packet,j+(j<252?2:3)-1,&toc,frames,size,&payload_offset);
  686. cfgs++;
  687. if(ret!=OPUS_INVALID_PACKET)test_failed();
  688. /*Code 2, one too long*/
  689. UNDEFINE_FOR_PARSE
  690. ret=opus_packet_parse(packet,j+(j<252?2:3)+1276,&toc,frames,size,&payload_offset);
  691. cfgs++;
  692. if(ret!=OPUS_INVALID_PACKET)test_failed();
  693. /*Code 2, second zero*/
  694. UNDEFINE_FOR_PARSE
  695. ret=opus_packet_parse(packet,j+(j<252?2:3),&toc,frames,size,&payload_offset);
  696. cfgs++;
  697. if(ret!=2)test_failed();
  698. if(size[0]!=j||size[1]!=0)test_failed();
  699. if(frames[1]!=frames[0]+size[0])test_failed();
  700. if((toc>>2)!=i)test_failed();
  701. /*Code 2, normal*/
  702. UNDEFINE_FOR_PARSE
  703. ret=opus_packet_parse(packet,(j<<1)+4,&toc,frames,size,&payload_offset);
  704. cfgs++;
  705. if(ret!=2)test_failed();
  706. if(size[0]!=j||size[1]!=(j<<1)+3-j-(j<252?1:2))test_failed();
  707. if(frames[1]!=frames[0]+size[0])test_failed();
  708. if((toc>>2)!=i)test_failed();
  709. }
  710. }
  711. fprintf(stdout," code 2 (%6d cases) ........................ OK.\n",cfgs);
  712. cfgs_total+=cfgs;cfgs=0;
  713. for(i=0;i<64;i++)
  714. {
  715. packet[0]=(i<<2)+3;
  716. /*code 3, length code overflow*/
  717. UNDEFINE_FOR_PARSE
  718. ret=opus_packet_parse(packet,1,&toc,frames,size,&payload_offset);
  719. cfgs++;
  720. if(ret!=OPUS_INVALID_PACKET)test_failed();
  721. }
  722. fprintf(stdout," code 3 m-truncation (%2d cases) ............... OK.\n",cfgs);
  723. cfgs_total+=cfgs;cfgs=0;
  724. for(i=0;i<64;i++)
  725. {
  726. /*code 3, m is zero or 49-63*/
  727. packet[0]=(i<<2)+3;
  728. for(jj=49;jj<=64;jj++)
  729. {
  730. packet[1]=0+(jj&63); /*CBR, no padding*/
  731. UNDEFINE_FOR_PARSE
  732. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  733. cfgs++;
  734. if(ret!=OPUS_INVALID_PACKET)test_failed();
  735. packet[1]=128+(jj&63); /*VBR, no padding*/
  736. UNDEFINE_FOR_PARSE
  737. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  738. cfgs++;
  739. if(ret!=OPUS_INVALID_PACKET)test_failed();
  740. packet[1]=64+(jj&63); /*CBR, padding*/
  741. UNDEFINE_FOR_PARSE
  742. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  743. cfgs++;
  744. if(ret!=OPUS_INVALID_PACKET)test_failed();
  745. packet[1]=128+64+(jj&63); /*VBR, padding*/
  746. UNDEFINE_FOR_PARSE
  747. ret=opus_packet_parse(packet,1275,&toc,frames,size,&payload_offset);
  748. cfgs++;
  749. if(ret!=OPUS_INVALID_PACKET)test_failed();
  750. }
  751. }
  752. fprintf(stdout," code 3 m=0,49-64 (%2d cases) ................ OK.\n",cfgs);
  753. cfgs_total+=cfgs;cfgs=0;
  754. for(i=0;i<64;i++)
  755. {
  756. packet[0]=(i<<2)+3;
  757. /*code 3, m is one, cbr*/
  758. packet[1]=1;
  759. for(j=0;j<1276;j++)
  760. {
  761. UNDEFINE_FOR_PARSE
  762. ret=opus_packet_parse(packet,j+2,&toc,frames,size,&payload_offset);
  763. cfgs++;
  764. if(ret!=1)test_failed();
  765. if(size[0]!=j)test_failed();
  766. if((toc>>2)!=i)test_failed();
  767. }
  768. UNDEFINE_FOR_PARSE
  769. ret=opus_packet_parse(packet,1276+2,&toc,frames,size,&payload_offset);
  770. cfgs++;
  771. if(ret!=OPUS_INVALID_PACKET)test_failed();
  772. }
  773. fprintf(stdout," code 3 m=1 CBR (%2d cases) ................. OK.\n",cfgs);
  774. cfgs_total+=cfgs;cfgs=0;
  775. for(i=0;i<64;i++)
  776. {
  777. int frame_samp;
  778. /*code 3, m>1 CBR*/
  779. packet[0]=(i<<2)+3;
  780. frame_samp=opus_packet_get_samples_per_frame(packet,48000);
  781. for(j=2;j<49;j++)
  782. {
  783. packet[1]=j;
  784. for(sz=2;sz<((j+2)*1275);sz++)
  785. {
  786. UNDEFINE_FOR_PARSE
  787. ret=opus_packet_parse(packet,sz,&toc,frames,size,&payload_offset);
  788. cfgs++;
  789. /*Must be <=120ms, must be evenly divisible, can't have frames>1275 bytes*/
  790. if(frame_samp*j<=5760 && (sz-2)%j==0 && (sz-2)/j<1276)
  791. {
  792. if(ret!=j)test_failed();
  793. for(jj=1;jj<ret;jj++)if(frames[jj]!=frames[jj-1]+size[jj-1])test_failed();
  794. if((toc>>2)!=i)test_failed();
  795. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  796. }
  797. }
  798. /*Super jumbo packets*/
  799. packet[1]=5760/frame_samp;
  800. UNDEFINE_FOR_PARSE
  801. ret=opus_packet_parse(packet,1275*packet[1]+2,&toc,frames,size,&payload_offset);
  802. cfgs++;
  803. if(ret!=packet[1])test_failed();
  804. for(jj=0;jj<ret;jj++)if(size[jj]!=1275)test_failed();
  805. }
  806. fprintf(stdout," code 3 m=1-48 CBR (%2d cases) .......... OK.\n",cfgs);
  807. cfgs_total+=cfgs;cfgs=0;
  808. for(i=0;i<64;i++)
  809. {
  810. int frame_samp;
  811. /*Code 3 VBR, m one*/
  812. packet[0]=(i<<2)+3;
  813. packet[1]=128+1;
  814. frame_samp=opus_packet_get_samples_per_frame(packet,48000);
  815. for(jj=0;jj<1276;jj++)
  816. {
  817. UNDEFINE_FOR_PARSE
  818. ret=opus_packet_parse(packet,2+jj,&toc,frames,size,&payload_offset);
  819. cfgs++;
  820. if(ret!=1)test_failed();
  821. if(size[0]!=jj)test_failed();
  822. if((toc>>2)!=i)test_failed();
  823. }
  824. UNDEFINE_FOR_PARSE
  825. ret=opus_packet_parse(packet,2+1276,&toc,frames,size,&payload_offset);
  826. cfgs++;
  827. if(ret!=OPUS_INVALID_PACKET)test_failed();
  828. for(j=2;j<49;j++)
  829. {
  830. packet[1]=128+j;
  831. /*Length code overflow*/
  832. UNDEFINE_FOR_PARSE
  833. ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
  834. cfgs++;
  835. if(ret!=OPUS_INVALID_PACKET)test_failed();
  836. packet[2]=252;
  837. packet[3]=0;
  838. for(jj=4;jj<2+j;jj++)packet[jj]=0;
  839. UNDEFINE_FOR_PARSE
  840. ret=opus_packet_parse(packet,2+j,&toc,frames,size,&payload_offset);
  841. cfgs++;
  842. if(ret!=OPUS_INVALID_PACKET)test_failed();
  843. /*One byte too short*/
  844. for(jj=2;jj<2+j;jj++)packet[jj]=0;
  845. UNDEFINE_FOR_PARSE
  846. ret=opus_packet_parse(packet,2+j-2,&toc,frames,size,&payload_offset);
  847. cfgs++;
  848. if(ret!=OPUS_INVALID_PACKET)test_failed();
  849. /*One byte too short thanks to length coding*/
  850. packet[2]=252;
  851. packet[3]=0;
  852. for(jj=4;jj<2+j;jj++)packet[jj]=0;
  853. UNDEFINE_FOR_PARSE
  854. ret=opus_packet_parse(packet,2+j+252-1,&toc,frames,size,&payload_offset);
  855. cfgs++;
  856. if(ret!=OPUS_INVALID_PACKET)test_failed();
  857. /*Most expensive way of coding zeros*/
  858. for(jj=2;jj<2+j;jj++)packet[jj]=0;
  859. UNDEFINE_FOR_PARSE
  860. ret=opus_packet_parse(packet,2+j-1,&toc,frames,size,&payload_offset);
  861. cfgs++;
  862. if(frame_samp*j<=5760){
  863. if(ret!=j)test_failed();
  864. for(jj=0;jj<j;jj++)if(size[jj]!=0)test_failed();
  865. if((toc>>2)!=i)test_failed();
  866. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  867. /*Quasi-CBR use of mode 3*/
  868. for(sz=0;sz<8;sz++)
  869. {
  870. const int tsz[8]={50,201,403,700,1472,5110,20400,61298};
  871. int pos=0;
  872. int as=(tsz[sz]+i-j-2)/j;
  873. for(jj=0;jj<j-1;jj++)
  874. {
  875. if(as<252){packet[2+pos]=as;pos++;}
  876. else{packet[2+pos]=252+(as&3);packet[3+pos]=(as-252)>>2;pos+=2;}
  877. }
  878. UNDEFINE_FOR_PARSE
  879. ret=opus_packet_parse(packet,tsz[sz]+i,&toc,frames,size,&payload_offset);
  880. cfgs++;
  881. if(frame_samp*j<=5760 && as<1276 && (tsz[sz]+i-2-pos-as*(j-1))<1276){
  882. if(ret!=j)test_failed();
  883. for(jj=0;jj<j-1;jj++)if(size[jj]!=as)test_failed();
  884. if(size[j-1]!=(tsz[sz]+i-2-pos-as*(j-1)))test_failed();
  885. if((toc>>2)!=i)test_failed();
  886. } else if(ret!=OPUS_INVALID_PACKET)test_failed();
  887. }
  888. }
  889. }
  890. fprintf(stdout," code 3 m=1-48 VBR (%2d cases) ............. OK.\n",cfgs);
  891. cfgs_total+=cfgs;cfgs=0;
  892. for(i=0;i<64;i++)
  893. {
  894. packet[0]=(i<<2)+3;
  895. /*Padding*/
  896. packet[1]=128+1+64;
  897. /*Overflow the length coding*/
  898. for(jj=2;jj<127;jj++)packet[jj]=255;
  899. UNDEFINE_FOR_PARSE
  900. ret=opus_packet_parse(packet,127,&toc,frames,size,&payload_offset);
  901. cfgs++;
  902. if(ret!=OPUS_INVALID_PACKET)test_failed();
  903. for(sz=0;sz<4;sz++)
  904. {
  905. const int tsz[4]={0,72,512,1275};
  906. for(jj=sz;jj<65025;jj+=11)
  907. {
  908. int pos;
  909. for(pos=0;pos<jj/254;pos++)packet[2+pos]=255;
  910. packet[2+pos]=jj%254;
  911. pos++;
  912. if(sz==0&&i==63)
  913. {
  914. /*Code more padding than there is room in the packet*/
  915. UNDEFINE_FOR_PARSE
  916. ret=opus_packet_parse(packet,2+jj+pos-1,&toc,frames,size,&payload_offset);
  917. cfgs++;
  918. if(ret!=OPUS_INVALID_PACKET)test_failed();
  919. }
  920. UNDEFINE_FOR_PARSE
  921. ret=opus_packet_parse(packet,2+jj+tsz[sz]+i+pos,&toc,frames,size,&payload_offset);
  922. cfgs++;
  923. if(tsz[sz]+i<1276)
  924. {
  925. if(ret!=1)test_failed();
  926. if(size[0]!=tsz[sz]+i)test_failed();
  927. if((toc>>2)!=i)test_failed();
  928. } else if (ret!=OPUS_INVALID_PACKET)test_failed();
  929. }
  930. }
  931. }
  932. fprintf(stdout," code 3 padding (%2d cases) ............... OK.\n",cfgs);
  933. cfgs_total+=cfgs;
  934. fprintf(stdout," opus_packet_parse ............................ OK.\n");
  935. fprintf(stdout," All packet parsing tests passed\n");
  936. fprintf(stdout," (%d API invocations)\n",cfgs_total);
  937. return cfgs_total;
  938. }
  939. /* This is a helper macro for the encoder tests.
  940. The encoder api tests all have a pattern of set-must-fail, set-must-fail,
  941. set-must-pass, get-and-compare, set-must-pass, get-and-compare. */
  942. #define CHECK_SETGET(setcall,getcall,badv,badv2,goodv,goodv2,sok,gok) \
  943. i=(badv);\
  944. if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
  945. i=(badv2);\
  946. if(opus_encoder_ctl(enc,setcall)==OPUS_OK)test_failed();\
  947. j=i=(goodv);\
  948. if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
  949. i=-12345;\
  950. VG_UNDEF(&i,sizeof(i)); \
  951. err=opus_encoder_ctl(enc,getcall);\
  952. if(err!=OPUS_OK || i!=j)test_failed();\
  953. j=i=(goodv2);\
  954. if(opus_encoder_ctl(enc,setcall)!=OPUS_OK)test_failed();\
  955. fprintf(stdout,sok);\
  956. i=-12345;\
  957. VG_UNDEF(&i,sizeof(i)); \
  958. err=opus_encoder_ctl(enc,getcall);\
  959. if(err!=OPUS_OK || i!=j)test_failed();\
  960. fprintf(stdout,gok);\
  961. cfgs+=6;
  962. opus_int32 test_enc_api(void)
  963. {
  964. opus_uint32 enc_final_range;
  965. OpusEncoder *enc;
  966. opus_int32 i,j;
  967. unsigned char packet[1276];
  968. #ifndef DISABLE_FLOAT_API
  969. float fbuf[960*2];
  970. #endif
  971. short sbuf[960*2];
  972. int c,err,cfgs;
  973. cfgs=0;
  974. /*First test invalid configurations which should fail*/
  975. fprintf(stdout,"\n Encoder basic API tests\n");
  976. fprintf(stdout," ---------------------------------------------------\n");
  977. for(c=0;c<4;c++)
  978. {
  979. i=opus_encoder_get_size(c);
  980. if(((c==1||c==2)&&(i<=2048||i>1<<17))||((c!=1&&c!=2)&&i!=0))test_failed();
  981. fprintf(stdout," opus_encoder_get_size(%d)=%d ...............%s OK.\n",c,i,i>0?"":"....");
  982. cfgs++;
  983. }
  984. /*Test with unsupported sample rates, channel counts*/
  985. for(c=0;c<4;c++)
  986. {
  987. for(i=-7;i<=96000;i++)
  988. {
  989. int fs;
  990. if((i==8000||i==12000||i==16000||i==24000||i==48000)&&(c==1||c==2))continue;
  991. switch(i)
  992. {
  993. case(-5):fs=-8000;break;
  994. case(-6):fs=INT32_MAX;break;
  995. case(-7):fs=INT32_MIN;break;
  996. default:fs=i;
  997. }
  998. err = OPUS_OK;
  999. VG_UNDEF(&err,sizeof(err));
  1000. enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, &err);
  1001. if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
  1002. cfgs++;
  1003. enc = opus_encoder_create(fs, c, OPUS_APPLICATION_VOIP, 0);
  1004. if(enc!=NULL)test_failed();
  1005. cfgs++;
  1006. opus_encoder_destroy(enc);
  1007. enc=malloc(opus_encoder_get_size(2));
  1008. if(enc==NULL)test_failed();
  1009. err = opus_encoder_init(enc, fs, c, OPUS_APPLICATION_VOIP);
  1010. if(err!=OPUS_BAD_ARG)test_failed();
  1011. cfgs++;
  1012. free(enc);
  1013. }
  1014. }
  1015. enc = opus_encoder_create(48000, 2, OPUS_AUTO, NULL);
  1016. if(enc!=NULL)test_failed();
  1017. cfgs++;
  1018. VG_UNDEF(&err,sizeof(err));
  1019. enc = opus_encoder_create(48000, 2, OPUS_AUTO, &err);
  1020. if(err!=OPUS_BAD_ARG || enc!=NULL)test_failed();
  1021. cfgs++;
  1022. VG_UNDEF(&err,sizeof(err));
  1023. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, NULL);
  1024. if(enc==NULL)test_failed();
  1025. opus_encoder_destroy(enc);
  1026. cfgs++;
  1027. VG_UNDEF(&err,sizeof(err));
  1028. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &err);
  1029. if(err!=OPUS_OK || enc==NULL)test_failed();
  1030. cfgs++;
  1031. err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
  1032. if(err!=OPUS_OK || i<0 || i>32766)test_failed();
  1033. opus_encoder_destroy(enc);
  1034. cfgs++;
  1035. VG_UNDEF(&err,sizeof(err));
  1036. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &err);
  1037. if(err!=OPUS_OK || enc==NULL)test_failed();
  1038. cfgs++;
  1039. err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
  1040. if(err!=OPUS_OK || i<0 || i>32766)test_failed();
  1041. opus_encoder_destroy(enc);
  1042. cfgs++;
  1043. VG_UNDEF(&err,sizeof(err));
  1044. enc = opus_encoder_create(48000, 2, OPUS_APPLICATION_VOIP, &err);
  1045. if(err!=OPUS_OK || enc==NULL)test_failed();
  1046. cfgs++;
  1047. fprintf(stdout," opus_encoder_create() ........................ OK.\n");
  1048. fprintf(stdout," opus_encoder_init() .......................... OK.\n");
  1049. i=-12345;
  1050. VG_UNDEF(&i,sizeof(i));
  1051. err=opus_encoder_ctl(enc,OPUS_GET_LOOKAHEAD(&i));
  1052. if(err!=OPUS_OK || i<0 || i>32766)test_failed();
  1053. cfgs++;
  1054. fprintf(stdout," OPUS_GET_LOOKAHEAD ........................... OK.\n");
  1055. if(opus_encoder_ctl(enc,OPUS_UNIMPLEMENTED)!=OPUS_UNIMPLEMENTED)test_failed();
  1056. fprintf(stdout," OPUS_UNIMPLEMENTED ........................... OK.\n");
  1057. cfgs++;
  1058. CHECK_SETGET(OPUS_SET_APPLICATION(i),OPUS_GET_APPLICATION(&i),-1,OPUS_AUTO,
  1059. OPUS_APPLICATION_AUDIO,OPUS_APPLICATION_RESTRICTED_LOWDELAY,
  1060. " OPUS_SET_APPLICATION ......................... OK.\n",
  1061. " OPUS_GET_APPLICATION ......................... OK.\n")
  1062. if(opus_encoder_ctl(enc,OPUS_SET_BITRATE(1073741832))!=OPUS_OK)test_failed();
  1063. cfgs++;
  1064. VG_UNDEF(&i,sizeof(i));
  1065. if(opus_encoder_ctl(enc,OPUS_GET_BITRATE(&i))!=OPUS_OK)test_failed();
  1066. if(i>700000||i<256000)test_failed();
  1067. cfgs++;
  1068. CHECK_SETGET(OPUS_SET_BITRATE(i),OPUS_GET_BITRATE(&i),-12345,0,
  1069. 500,256000,
  1070. " OPUS_SET_BITRATE ............................. OK.\n",
  1071. " OPUS_GET_BITRATE ............................. OK.\n")
  1072. CHECK_SETGET(OPUS_SET_FORCE_CHANNELS(i),OPUS_GET_FORCE_CHANNELS(&i),-1,3,
  1073. 1,OPUS_AUTO,
  1074. " OPUS_SET_FORCE_CHANNELS ...................... OK.\n",
  1075. " OPUS_GET_FORCE_CHANNELS ...................... OK.\n")
  1076. i=-2;
  1077. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
  1078. cfgs++;
  1079. i=OPUS_BANDWIDTH_FULLBAND+1;
  1080. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))==OPUS_OK)test_failed();
  1081. cfgs++;
  1082. i=OPUS_BANDWIDTH_NARROWBAND;
  1083. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1084. cfgs++;
  1085. i=OPUS_BANDWIDTH_FULLBAND;
  1086. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1087. cfgs++;
  1088. i=OPUS_BANDWIDTH_WIDEBAND;
  1089. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1090. cfgs++;
  1091. i=OPUS_BANDWIDTH_MEDIUMBAND;
  1092. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1093. cfgs++;
  1094. fprintf(stdout," OPUS_SET_BANDWIDTH ........................... OK.\n");
  1095. /*We don't test if the bandwidth has actually changed.
  1096. because the change may be delayed until the encoder is advanced.*/
  1097. i=-12345;
  1098. VG_UNDEF(&i,sizeof(i));
  1099. err=opus_encoder_ctl(enc,OPUS_GET_BANDWIDTH(&i));
  1100. if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
  1101. i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
  1102. i!=OPUS_BANDWIDTH_FULLBAND&&i!=OPUS_AUTO))test_failed();
  1103. cfgs++;
  1104. if(opus_encoder_ctl(enc,OPUS_SET_BANDWIDTH(OPUS_AUTO))!=OPUS_OK)test_failed();
  1105. cfgs++;
  1106. fprintf(stdout," OPUS_GET_BANDWIDTH ........................... OK.\n");
  1107. i=-2;
  1108. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
  1109. cfgs++;
  1110. i=OPUS_BANDWIDTH_FULLBAND+1;
  1111. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))==OPUS_OK)test_failed();
  1112. cfgs++;
  1113. i=OPUS_BANDWIDTH_NARROWBAND;
  1114. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1115. cfgs++;
  1116. i=OPUS_BANDWIDTH_FULLBAND;
  1117. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1118. cfgs++;
  1119. i=OPUS_BANDWIDTH_WIDEBAND;
  1120. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1121. cfgs++;
  1122. i=OPUS_BANDWIDTH_MEDIUMBAND;
  1123. if(opus_encoder_ctl(enc,OPUS_SET_MAX_BANDWIDTH(i))!=OPUS_OK)test_failed();
  1124. cfgs++;
  1125. fprintf(stdout," OPUS_SET_MAX_BANDWIDTH ....................... OK.\n");
  1126. /*We don't test if the bandwidth has actually changed.
  1127. because the change may be delayed until the encoder is advanced.*/
  1128. i=-12345;
  1129. VG_UNDEF(&i,sizeof(i));
  1130. err=opus_encoder_ctl(enc,OPUS_GET_MAX_BANDWIDTH(&i));
  1131. if(err!=OPUS_OK || (i!=OPUS_BANDWIDTH_NARROWBAND&&
  1132. i!=OPUS_BANDWIDTH_MEDIUMBAND&&i!=OPUS_BANDWIDTH_WIDEBAND&&
  1133. i!=OPUS_BANDWIDTH_FULLBAND))test_failed();
  1134. cfgs++;
  1135. fprintf(stdout," OPUS_GET_MAX_BANDWIDTH ....................... OK.\n");
  1136. CHECK_SETGET(OPUS_SET_DTX(i),OPUS_GET_DTX(&i),-1,2,
  1137. 1,0,
  1138. " OPUS_SET_DTX ................................. OK.\n",
  1139. " OPUS_GET_DTX ................................. OK.\n")
  1140. CHECK_SETGET(OPUS_SET_COMPLEXITY(i),OPUS_GET_COMPLEXITY(&i),-1,11,
  1141. 0,10,
  1142. " OPUS_SET_COMPLEXITY .......................... OK.\n",
  1143. " OPUS_GET_COMPLEXITY .......................... OK.\n")
  1144. CHECK_SETGET(OPUS_SET_INBAND_FEC(i),OPUS_GET_INBAND_FEC(&i),-1,2,
  1145. 1,0,
  1146. " OPUS_SET_INBAND_FEC .......................... OK.\n",
  1147. " OPUS_GET_INBAND_FEC .......................... OK.\n")
  1148. CHECK_SETGET(OPUS_SET_PACKET_LOSS_PERC(i),OPUS_GET_PACKET_LOSS_PERC(&i),-1,101,
  1149. 100,0,
  1150. " OPUS_SET_PACKET_LOSS_PERC .................... OK.\n",
  1151. " OPUS_GET_PACKET_LOSS_PERC .................... OK.\n")
  1152. CHECK_SETGET(OPUS_SET_VBR(i),OPUS_GET_VBR(&i),-1,2,
  1153. 1,0,
  1154. " OPUS_SET_VBR ................................. OK.\n",
  1155. " OPUS_GET_VBR ................................. OK.\n")
  1156. /*CHECK_SETGET(OPUS_SET_VOICE_RATIO(i),OPUS_GET_VOICE_RATIO(&i),-2,101,
  1157. 0,50,
  1158. " OPUS_SET_VOICE_RATIO ......................... OK.\n",
  1159. " OPUS_GET_VOICE_RATIO ......................... OK.\n")
  1160. */
  1161. CHECK_SETGET(OPUS_SET_VBR_CONSTRAINT(i),OPUS_GET_VBR_CONSTRAINT(&i),-1,2,
  1162. 1,0,
  1163. " OPUS_SET_VBR_CONSTRAINT ...................... OK.\n",
  1164. " OPUS_GET_VBR_CONSTRAINT ...................... OK.\n")
  1165. CHECK_SETGET(OPUS_SET_SIGNAL(i),OPUS_GET_SIGNAL(&i),-12345,0x7FFFFFFF,
  1166. OPUS_SIGNAL_MUSIC,OPUS_AUTO,
  1167. " OPUS_SET_SIGNAL .............................. OK.\n",
  1168. " OPUS_GET_SIGNAL .............................. OK.\n")
  1169. CHECK_SETGET(OPUS_SET_LSB_DEPTH(i),OPUS_GET_LSB_DEPTH(&i),7,25,16,24,
  1170. " OPUS_SET_LSB_DEPTH ........................... OK.\n",
  1171. " OPUS_GET_LSB_DEPTH ........................... OK.\n")
  1172. /*OPUS_SET_FORCE_MODE is not tested here because it's not a public API, however the encoder tests use it*/
  1173. if(opus_encoder_ctl(enc,OPUS_GET_FINAL_RANGE(&enc_final_range))!=OPUS_OK)test_failed();
  1174. cfgs++;
  1175. fprintf(stdout," OPUS_GET_FINAL_RANGE ......................... OK.\n");
  1176. /*Reset the encoder*/
  1177. if(opus_encoder_ctl(enc, OPUS_RESET_STATE)!=OPUS_OK)test_failed();
  1178. cfgs++;
  1179. fprintf(stdout," OPUS_RESET_STATE ............................. OK.\n");
  1180. memset(sbuf,0,sizeof(short)*2*960);
  1181. VG_UNDEF(packet,sizeof(packet));
  1182. i=opus_encode(enc, sbuf, 960, packet, sizeof(packet));
  1183. if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
  1184. VG_CHECK(packet,i);
  1185. cfgs++;
  1186. fprintf(stdout," opus_encode() ................................ OK.\n");
  1187. #ifndef DISABLE_FLOAT_API
  1188. memset(fbuf,0,sizeof(float)*2*960);
  1189. VG_UNDEF(packet,sizeof(packet));
  1190. i=opus_encode_float(enc, fbuf, 960, packet, sizeof(packet));
  1191. if(i<1 || (i>(opus_int32)sizeof(packet)))test_failed();
  1192. VG_CHECK(packet,i);
  1193. cfgs++;
  1194. fprintf(stdout," opus_encode_float() .......................... OK.\n");
  1195. #endif
  1196. #if 0
  1197. /*These tests are disabled because the library crashes with null states*/
  1198. if(opus_encoder_ctl(0,OPUS_RESET_STATE) !=OPUS_INVALID_STATE)test_failed();
  1199. if(opus_encoder_init(0,48000,1,OPUS_APPLICATION_VOIP) !=OPUS_INVALID_STATE)test_failed();
  1200. if(opus_encode(0,sbuf,960,packet,sizeof(packet)) !=OPUS_INVALID_STATE)test_failed();
  1201. if(opus_encode_float(0,fbuf,960,packet,sizeof(packet))!=OPUS_INVALID_STATE)test_failed();
  1202. #endif
  1203. opus_encoder_destroy(enc);
  1204. cfgs++;
  1205. fprintf(stdout," All encoder interface tests passed\n");
  1206. fprintf(stdout," (%d API invocations)\n",cfgs);
  1207. return cfgs;
  1208. }
  1209. #define max_out (1276*48+48*2+2)
  1210. int test_repacketizer_api(void)
  1211. {
  1212. int ret,cfgs,i,j,k;
  1213. OpusRepacketizer *rp;
  1214. unsigned char *packet;
  1215. unsigned char *po;
  1216. cfgs=0;
  1217. fprintf(stdout,"\n Repacketizer tests\n");
  1218. fprintf(stdout," ---------------------------------------------------\n");
  1219. packet=malloc(max_out);
  1220. if(packet==NULL)test_failed();
  1221. po=malloc(max_out);
  1222. if(po==NULL)test_failed();
  1223. memset(packet,0,max_out);
  1224. i=opus_repacketizer_get_size();
  1225. if(i<=0)test_failed();
  1226. cfgs++;
  1227. fprintf(stdout," opus_repacketizer_get_size()=%d ............. OK.\n",i);
  1228. rp=malloc(i);
  1229. rp=opus_repacketizer_init(rp);
  1230. if(rp==NULL)test_failed();
  1231. cfgs++;
  1232. free(rp);
  1233. fprintf(stdout," opus_repacketizer_init ....................... OK.\n");
  1234. rp=opus_repacketizer_create();
  1235. if(rp==NULL)test_failed();
  1236. cfgs++;
  1237. fprintf(stdout," opus_repacketizer_create ..................... OK.\n");
  1238. if(opus_repacketizer_get_nb_frames(rp)!=0)test_failed();
  1239. cfgs++;
  1240. fprintf(stdout," opus_repacketizer_get_nb_frames .............. OK.\n");
  1241. /*Length overflows*/
  1242. VG_UNDEF(packet,4);
  1243. if(opus_repacketizer_cat(rp,packet,0)!=OPUS_INVALID_PACKET)test_failed(); /* Zero len */
  1244. cfgs++;
  1245. packet[0]=1;
  1246. if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Odd payload code 1 */
  1247. cfgs++;
  1248. packet[0]=2;
  1249. if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow one */
  1250. cfgs++;
  1251. packet[0]=3;
  1252. if(opus_repacketizer_cat(rp,packet,1)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 no count */
  1253. cfgs++;
  1254. packet[0]=2;
  1255. packet[1]=255;
  1256. if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow two */
  1257. cfgs++;
  1258. packet[0]=2;
  1259. packet[1]=250;
  1260. if(opus_repacketizer_cat(rp,packet,251)!=OPUS_INVALID_PACKET)test_failed(); /* Code 2 overflow three */
  1261. cfgs++;
  1262. packet[0]=3;
  1263. packet[1]=0;
  1264. if(opus_repacketizer_cat(rp,packet,2)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=0 */
  1265. cfgs++;
  1266. packet[1]=49;
  1267. if(opus_repacketizer_cat(rp,packet,100)!=OPUS_INVALID_PACKET)test_failed(); /* Code 3 m=49 */
  1268. cfgs++;
  1269. packet[0]=0;
  1270. if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
  1271. cfgs++;
  1272. packet[0]=1<<2;
  1273. if(opus_repacketizer_cat(rp,packet,3)!=OPUS_INVALID_PACKET)test_failed(); /* Change in TOC */
  1274. cfgs++;
  1275. /* Code 0,1,3 CBR -> Code 0,1,3 CBR */
  1276. opus_repacketizer_init(rp);
  1277. for(j=0;j<32;j++)
  1278. {
  1279. /* TOC types, test half with stereo */
  1280. int maxi;
  1281. packet[0]=((j<<1)+(j&1))<<2;
  1282. maxi=960/opus_packet_get_samples_per_frame(packet,8000);
  1283. for(i=1;i<=maxi;i++)
  1284. {
  1285. /* Number of CBR frames in the input packets */
  1286. int maxp;
  1287. packet[0]=((j<<1)+(j&1))<<2;
  1288. if(i>1)packet[0]+=i==2?1:3;
  1289. packet[1]=i>2?i:0;
  1290. maxp=960/(i*opus_packet_get_samples_per_frame(packet,8000));
  1291. for(k=0;k<=(1275+75);k+=3)
  1292. {
  1293. /*Payload size*/
  1294. opus_int32 cnt,rcnt;
  1295. if(k%i!=0)continue; /* Only testing CBR here, payload must be a multiple of the count */
  1296. for(cnt=0;cnt<maxp+2;cnt++)
  1297. {
  1298. if(cnt>0)
  1299. {
  1300. ret=opus_repacketizer_cat(rp,packet,k+(i>2?2:1));
  1301. if((cnt<=maxp&&k<=(1275*i))?ret!=OPUS_OK:ret!=OPUS_INVALID_PACKET)test_failed();
  1302. cfgs++;
  1303. }
  1304. rcnt=k<=(1275*i)?(cnt<maxp?cnt:maxp):0;
  1305. if(opus_repacketizer_get_nb_frames(rp)!=rcnt*i)test_failed();
  1306. cfgs++;
  1307. ret=opus_repacketizer_out_range(rp,0,rcnt*i,po,max_out);
  1308. if(rcnt>0)
  1309. {
  1310. int len;
  1311. len=k*rcnt+((rcnt*i)>2?2:1);
  1312. if(ret!=len)test_failed();
  1313. if((rcnt*i)<2&&(po[0]&3)!=0)test_failed(); /* Code 0 */
  1314. if((rcnt*i)==2&&(po[0]&3)!=1)test_failed(); /* Code 1 */
  1315. if((rcnt*i)>2&&(((po[0]&3)!=3)||(po[1]!=rcnt*i)))test_failed(); /* Code 3 CBR */
  1316. cfgs++;
  1317. if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
  1318. cfgs++;
  1319. if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1320. cfgs++;
  1321. if(len>1)
  1322. {
  1323. if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1324. cfgs++;
  1325. }
  1326. if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1327. cfgs++;
  1328. } else if (ret!=OPUS_BAD_ARG)test_failed(); /* M must not be 0 */
  1329. }
  1330. opus_repacketizer_init(rp);
  1331. }
  1332. }
  1333. }
  1334. /*Change in input count code, CBR out*/
  1335. opus_repacketizer_init(rp);
  1336. packet[0]=0;
  1337. if(opus_repacketizer_cat(rp,packet,5)!=OPUS_OK)test_failed();
  1338. cfgs++;
  1339. packet[0]+=1;
  1340. if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
  1341. cfgs++;
  1342. i=opus_repacketizer_out(rp,po,max_out);
  1343. if((i!=(4+8+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=0))test_failed();
  1344. cfgs++;
  1345. i=opus_repacketizer_out_range(rp,0,1,po,max_out);
  1346. if(i!=5||(po[0]&3)!=0)test_failed();
  1347. cfgs++;
  1348. i=opus_repacketizer_out_range(rp,1,2,po,max_out);
  1349. if(i!=5||(po[0]&3)!=0)test_failed();
  1350. cfgs++;
  1351. /*Change in input count code, VBR out*/
  1352. opus_repacketizer_init(rp);
  1353. packet[0]=1;
  1354. if(opus_repacketizer_cat(rp,packet,9)!=OPUS_OK)test_failed();
  1355. cfgs++;
  1356. packet[0]=0;
  1357. if(opus_repacketizer_cat(rp,packet,3)!=OPUS_OK)test_failed();
  1358. cfgs++;
  1359. i=opus_repacketizer_out(rp,po,max_out);
  1360. if((i!=(2+8+2+2))||((po[0]&3)!=3)||((po[1]&63)!=3)||((po[1]>>7)!=1))test_failed();
  1361. cfgs++;
  1362. /*VBR in, VBR out*/
  1363. opus_repacketizer_init(rp);
  1364. packet[0]=2;
  1365. packet[1]=4;
  1366. if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
  1367. cfgs++;
  1368. if(opus_repacketizer_cat(rp,packet,8)!=OPUS_OK)test_failed();
  1369. cfgs++;
  1370. i=opus_repacketizer_out(rp,po,max_out);
  1371. if((i!=(2+1+1+1+4+2+4+2))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=1))test_failed();
  1372. cfgs++;
  1373. /*VBR in, CBR out*/
  1374. opus_repacketizer_init(rp);
  1375. packet[0]=2;
  1376. packet[1]=4;
  1377. if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
  1378. cfgs++;
  1379. if(opus_repacketizer_cat(rp,packet,10)!=OPUS_OK)test_failed();
  1380. cfgs++;
  1381. i=opus_repacketizer_out(rp,po,max_out);
  1382. if((i!=(2+4+4+4+4))||((po[0]&3)!=3)||((po[1]&63)!=4)||((po[1]>>7)!=0))test_failed();
  1383. cfgs++;
  1384. /*Count 0 in, VBR out*/
  1385. for(j=0;j<32;j++)
  1386. {
  1387. /* TOC types, test half with stereo */
  1388. int maxi,sum,rcnt;
  1389. packet[0]=((j<<1)+(j&1))<<2;
  1390. maxi=960/opus_packet_get_samples_per_frame(packet,8000);
  1391. sum=0;
  1392. rcnt=0;
  1393. opus_repacketizer_init(rp);
  1394. for(i=1;i<=maxi+2;i++)
  1395. {
  1396. int len;
  1397. ret=opus_repacketizer_cat(rp,packet,i);
  1398. if(rcnt<maxi)
  1399. {
  1400. if(ret!=OPUS_OK)test_failed();
  1401. rcnt++;
  1402. sum+=i-1;
  1403. } else if (ret!=OPUS_INVALID_PACKET)test_failed();
  1404. cfgs++;
  1405. len=sum+(rcnt<2?1:rcnt<3?2:2+rcnt-1);
  1406. if(opus_repacketizer_out(rp,po,max_out)!=len)test_failed();
  1407. if(rcnt>2&&(po[1]&63)!=rcnt)test_failed();
  1408. if(rcnt==2&&(po[0]&3)!=2)test_failed();
  1409. if(rcnt==1&&(po[0]&3)!=0)test_failed();
  1410. cfgs++;
  1411. if(opus_repacketizer_out(rp,po,len)!=len)test_failed();
  1412. cfgs++;
  1413. if(opus_repacketizer_out(rp,po,len-1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1414. cfgs++;
  1415. if(len>1)
  1416. {
  1417. if(opus_repacketizer_out(rp,po,1)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1418. cfgs++;
  1419. }
  1420. if(opus_repacketizer_out(rp,po,0)!=OPUS_BUFFER_TOO_SMALL)test_failed();
  1421. cfgs++;
  1422. }
  1423. }
  1424. fprintf(stdout," opus_repacketizer_cat ........................ OK.\n");
  1425. fprintf(stdout," opus_repacketizer_out ........................ OK.\n");
  1426. fprintf(stdout," opus_repacketizer_out_range .................. OK.\n");
  1427. opus_repacketizer_destroy(rp);
  1428. cfgs++;
  1429. free(packet);
  1430. free(po);
  1431. fprintf(stdout," All repacketizer tests passed\n");
  1432. fprintf(stdout," (%7d API invocations)\n",cfgs);
  1433. return cfgs;
  1434. }
  1435. #ifdef MALLOC_FAIL
  1436. /* GLIBC 2.14 declares __malloc_hook as deprecated, generating a warning
  1437. * under GCC. However, this is the cleanest way to test malloc failure
  1438. * handling in our codebase, and the lack of thread safety isn't an
  1439. * issue here. We therefore disable the warning for this function.
  1440. */
  1441. #if OPUS_GNUC_PREREQ(4,6)
  1442. /* Save the current warning settings */
  1443. #pragma GCC diagnostic push
  1444. #endif
  1445. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  1446. typedef void *(*mhook)(size_t __size, __const __malloc_ptr_t);
  1447. #endif
  1448. int test_malloc_fail(void)
  1449. {
  1450. #ifdef MALLOC_FAIL
  1451. OpusDecoder *dec;
  1452. OpusEncoder *enc;
  1453. OpusRepacketizer *rp;
  1454. unsigned char mapping[256] = {0,1};
  1455. OpusMSDecoder *msdec;
  1456. OpusMSEncoder *msenc;
  1457. int rate,c,app,cfgs,err,useerr;
  1458. int *ep;
  1459. mhook orig_malloc;
  1460. cfgs=0;
  1461. #endif
  1462. fprintf(stdout,"\n malloc() failure tests\n");
  1463. fprintf(stdout," ---------------------------------------------------\n");
  1464. #ifdef MALLOC_FAIL
  1465. orig_malloc=__malloc_hook;
  1466. __malloc_hook=malloc_hook;
  1467. ep=(int *)opus_alloc(sizeof(int));
  1468. if(ep!=NULL)
  1469. {
  1470. if(ep)free(ep);
  1471. __malloc_hook=orig_malloc;
  1472. #endif
  1473. fprintf(stdout," opus_decoder_create() ................... SKIPPED.\n");
  1474. fprintf(stdout," opus_encoder_create() ................... SKIPPED.\n");
  1475. fprintf(stdout," opus_repacketizer_create() .............. SKIPPED.\n");
  1476. fprintf(stdout," opus_multistream_decoder_create() ....... SKIPPED.\n");
  1477. fprintf(stdout," opus_multistream_encoder_create() ....... SKIPPED.\n");
  1478. fprintf(stdout,"(Test only supported with GLIBC and without valgrind)\n");
  1479. return 0;
  1480. #ifdef MALLOC_FAIL
  1481. }
  1482. for(useerr=0;useerr<2;useerr++)
  1483. {
  1484. ep=useerr?&err:0;
  1485. for(rate=0;rate<5;rate++)
  1486. {
  1487. for(c=1;c<3;c++)
  1488. {
  1489. err=1;
  1490. if(useerr)
  1491. {
  1492. VG_UNDEF(&err,sizeof(err));
  1493. }
  1494. dec=opus_decoder_create(opus_rates[rate], c, ep);
  1495. if(dec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1496. {
  1497. __malloc_hook=orig_malloc;
  1498. test_failed();
  1499. }
  1500. cfgs++;
  1501. msdec=opus_multistream_decoder_create(opus_rates[rate], c, 1, c-1, mapping, ep);
  1502. if(msdec!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1503. {
  1504. __malloc_hook=orig_malloc;
  1505. test_failed();
  1506. }
  1507. cfgs++;
  1508. for(app=0;app<3;app++)
  1509. {
  1510. if(useerr)
  1511. {
  1512. VG_UNDEF(&err,sizeof(err));
  1513. }
  1514. enc=opus_encoder_create(opus_rates[rate], c, opus_apps[app],ep);
  1515. if(enc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1516. {
  1517. __malloc_hook=orig_malloc;
  1518. test_failed();
  1519. }
  1520. cfgs++;
  1521. msenc=opus_multistream_encoder_create(opus_rates[rate], c, 1, c-1, mapping, opus_apps[app],ep);
  1522. if(msenc!=NULL||(useerr&&err!=OPUS_ALLOC_FAIL))
  1523. {
  1524. __malloc_hook=orig_malloc;
  1525. test_failed();
  1526. }
  1527. cfgs++;
  1528. }
  1529. }
  1530. }
  1531. }
  1532. rp=opus_repacketizer_create();
  1533. if(rp!=NULL)
  1534. {
  1535. __malloc_hook=orig_malloc;
  1536. test_failed();
  1537. }
  1538. cfgs++;
  1539. __malloc_hook=orig_malloc;
  1540. fprintf(stdout," opus_decoder_create() ........................ OK.\n");
  1541. fprintf(stdout," opus_encoder_create() ........................ OK.\n");
  1542. fprintf(stdout," opus_repacketizer_create() ................... OK.\n");
  1543. fprintf(stdout," opus_multistream_decoder_create() ............ OK.\n");
  1544. fprintf(stdout," opus_multistream_encoder_create() ............ OK.\n");
  1545. fprintf(stdout," All malloc failure tests passed\n");
  1546. fprintf(stdout," (%2d API invocations)\n",cfgs);
  1547. return cfgs;
  1548. #endif
  1549. }
  1550. #ifdef MALLOC_FAIL
  1551. #if __GNUC_PREREQ(4,6)
  1552. #pragma GCC diagnostic pop /* restore -Wdeprecated-declarations */
  1553. #endif
  1554. #endif
  1555. int main(int _argc, char **_argv)
  1556. {
  1557. opus_int32 total;
  1558. const char * oversion;
  1559. if(_argc>1)
  1560. {
  1561. fprintf(stderr,"Usage: %s\n",_argv[0]);
  1562. return 1;
  1563. }
  1564. iseed=0;
  1565. oversion=opus_get_version_string();
  1566. if(!oversion)test_failed();
  1567. fprintf(stderr,"Testing the %s API deterministically\n", oversion);
  1568. if(opus_strerror(-32768)==NULL)test_failed();
  1569. if(opus_strerror(32767)==NULL)test_failed();
  1570. if(strlen(opus_strerror(0))<1)test_failed();
  1571. total=4;
  1572. total+=test_dec_api();
  1573. total+=test_msdec_api();
  1574. total+=test_parse();
  1575. total+=test_enc_api();
  1576. total+=test_repacketizer_api();
  1577. total+=test_malloc_fail();
  1578. fprintf(stderr,"\nAll API tests passed.\nThe libopus API was invoked %d times.\n",total);
  1579. return 0;
  1580. }