test_opus_api.c 69 KB

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