test_opus_api.c 69 KB

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