test_opus_api.c 61 KB

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