celt.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2010 Xiph.Org Foundation
  3. Copyright (c) 2008 Gregory Maxwell
  4. Written by Jean-Marc Valin and Gregory Maxwell */
  5. /*
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. - Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. - Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #define CELT_C
  30. #include "os_support.h"
  31. #include "mdct.h"
  32. #include <math.h>
  33. #include "celt.h"
  34. #include "pitch.h"
  35. #include "bands.h"
  36. #include "modes.h"
  37. #include "entcode.h"
  38. #include "quant_bands.h"
  39. #include "rate.h"
  40. #include "stack_alloc.h"
  41. #include "mathops.h"
  42. #include "float_cast.h"
  43. #include <stdarg.h>
  44. #include "plc.h"
  45. #include "vq.h"
  46. #ifndef OPUS_VERSION
  47. #define OPUS_VERSION "unknown"
  48. #endif
  49. static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
  50. /* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
  51. static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
  52. static const unsigned char tapset_icdf[3]={2,1,0};
  53. static const unsigned char toOpusTable[20] = {
  54. 0xE0, 0xE8, 0xF0, 0xF8,
  55. 0xC0, 0xC8, 0xD0, 0xD8,
  56. 0xA0, 0xA8, 0xB0, 0xB8,
  57. 0x00, 0x00, 0x00, 0x00,
  58. 0x80, 0x88, 0x90, 0x98,
  59. };
  60. static const unsigned char fromOpusTable[16] = {
  61. 0x80, 0x88, 0x90, 0x98,
  62. 0x40, 0x48, 0x50, 0x58,
  63. 0x20, 0x28, 0x30, 0x38,
  64. 0x00, 0x08, 0x10, 0x18
  65. };
  66. static inline int toOpus(unsigned char c)
  67. {
  68. int ret=0;
  69. if (c<0xA0)
  70. ret = toOpusTable[c>>3];
  71. if (ret == 0)
  72. return -1;
  73. else
  74. return ret|(c&0x7);
  75. }
  76. static inline int fromOpus(unsigned char c)
  77. {
  78. if (c<0x80)
  79. return -1;
  80. else
  81. return fromOpusTable[(c>>3)-16] | (c&0x7);
  82. }
  83. #define COMBFILTER_MAXPERIOD 1024
  84. #define COMBFILTER_MINPERIOD 15
  85. static int resampling_factor(opus_int32 rate)
  86. {
  87. int ret;
  88. switch (rate)
  89. {
  90. case 48000:
  91. ret = 1;
  92. break;
  93. case 24000:
  94. ret = 2;
  95. break;
  96. case 16000:
  97. ret = 3;
  98. break;
  99. case 12000:
  100. ret = 4;
  101. break;
  102. case 8000:
  103. ret = 6;
  104. break;
  105. default:
  106. ret = 0;
  107. break;
  108. }
  109. return ret;
  110. }
  111. /** Encoder state
  112. @brief Encoder state
  113. */
  114. struct OpusCustomEncoder {
  115. const OpusCustomMode *mode; /**< Mode used by the encoder */
  116. int overlap;
  117. int channels;
  118. int stream_channels;
  119. int force_intra;
  120. int clip;
  121. int disable_pf;
  122. int complexity;
  123. int upsample;
  124. int start, end;
  125. opus_int32 bitrate;
  126. int vbr;
  127. int signalling;
  128. int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
  129. int loss_rate;
  130. /* Everything beyond this point gets cleared on a reset */
  131. #define ENCODER_RESET_START rng
  132. opus_uint32 rng;
  133. int spread_decision;
  134. opus_val32 delayedIntra;
  135. int tonal_average;
  136. int lastCodedBands;
  137. int hf_average;
  138. int tapset_decision;
  139. int prefilter_period;
  140. opus_val16 prefilter_gain;
  141. int prefilter_tapset;
  142. #ifdef RESYNTH
  143. int prefilter_period_old;
  144. opus_val16 prefilter_gain_old;
  145. int prefilter_tapset_old;
  146. #endif
  147. int consec_transient;
  148. opus_val32 preemph_memE[2];
  149. opus_val32 preemph_memD[2];
  150. /* VBR-related parameters */
  151. opus_int32 vbr_reservoir;
  152. opus_int32 vbr_drift;
  153. opus_int32 vbr_offset;
  154. opus_int32 vbr_count;
  155. #ifdef RESYNTH
  156. celt_sig syn_mem[2][2*MAX_PERIOD];
  157. #endif
  158. celt_sig in_mem[1]; /* Size = channels*mode->overlap */
  159. /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_PERIOD */
  160. /* celt_sig overlap_mem[], Size = channels*mode->overlap */
  161. /* opus_val16 oldEBands[], Size = 2*channels*mode->nbEBands */
  162. };
  163. int celt_encoder_get_size(int channels)
  164. {
  165. CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
  166. return opus_custom_encoder_get_size(mode, channels);
  167. }
  168. int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
  169. {
  170. int size = sizeof(struct CELTEncoder)
  171. + (2*channels*mode->overlap-1)*sizeof(celt_sig)
  172. + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig)
  173. + 3*channels*mode->nbEBands*sizeof(opus_val16);
  174. return size;
  175. }
  176. #ifdef CUSTOM_MODES
  177. CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
  178. {
  179. int ret;
  180. CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
  181. /* init will handle the NULL case */
  182. ret = opus_custom_encoder_init(st, mode, channels);
  183. if (ret != OPUS_OK)
  184. {
  185. opus_custom_encoder_destroy(st);
  186. st = NULL;
  187. }
  188. if (error)
  189. *error = ret;
  190. return st;
  191. }
  192. #endif /* CUSTOM_MODES */
  193. int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
  194. {
  195. int ret;
  196. ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
  197. if (ret != OPUS_OK)
  198. return ret;
  199. st->upsample = resampling_factor(sampling_rate);
  200. if (st->upsample==0)
  201. return OPUS_BAD_ARG;
  202. else
  203. return OPUS_OK;
  204. }
  205. int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
  206. {
  207. if (channels < 0 || channels > 2)
  208. return OPUS_BAD_ARG;
  209. if (st==NULL || mode==NULL)
  210. return OPUS_ALLOC_FAIL;
  211. OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
  212. st->mode = mode;
  213. st->overlap = mode->overlap;
  214. st->stream_channels = st->channels = channels;
  215. st->upsample = 1;
  216. st->start = 0;
  217. st->end = st->mode->effEBands;
  218. st->signalling = 1;
  219. st->constrained_vbr = 1;
  220. st->clip = 1;
  221. st->bitrate = OPUS_BITRATE_MAX;
  222. st->vbr = 0;
  223. st->vbr_offset = 0;
  224. st->force_intra = 0;
  225. st->delayedIntra = 1;
  226. st->tonal_average = 256;
  227. st->spread_decision = SPREAD_NORMAL;
  228. st->hf_average = 0;
  229. st->tapset_decision = 0;
  230. st->complexity = 5;
  231. return OPUS_OK;
  232. }
  233. #ifdef CUSTOM_MODES
  234. void opus_custom_encoder_destroy(CELTEncoder *st)
  235. {
  236. opus_free(st);
  237. }
  238. #endif /* CUSTOM_MODES */
  239. static inline opus_val16 SIG2WORD16(celt_sig x)
  240. {
  241. #ifdef FIXED_POINT
  242. x = PSHR32(x, SIG_SHIFT);
  243. x = MAX32(x, -32768);
  244. x = MIN32(x, 32767);
  245. return EXTRACT16(x);
  246. #else
  247. return (opus_val16)x;
  248. #endif
  249. }
  250. static int transient_analysis(const opus_val32 * restrict in, int len, int C,
  251. int overlap)
  252. {
  253. int i;
  254. VARDECL(opus_val16, tmp);
  255. opus_val32 mem0=0,mem1=0;
  256. int is_transient = 0;
  257. int block;
  258. int N;
  259. VARDECL(opus_val16, bins);
  260. SAVE_STACK;
  261. ALLOC(tmp, len, opus_val16);
  262. block = overlap/2;
  263. N=len/block;
  264. ALLOC(bins, N, opus_val16);
  265. if (C==1)
  266. {
  267. for (i=0;i<len;i++)
  268. tmp[i] = SHR32(in[i],SIG_SHIFT);
  269. } else {
  270. for (i=0;i<len;i++)
  271. tmp[i] = SHR32(ADD32(in[i],in[i+len]), SIG_SHIFT+1);
  272. }
  273. /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
  274. for (i=0;i<len;i++)
  275. {
  276. opus_val32 x,y;
  277. x = tmp[i];
  278. y = ADD32(mem0, x);
  279. #ifdef FIXED_POINT
  280. mem0 = mem1 + y - SHL32(x,1);
  281. mem1 = x - SHR32(y,1);
  282. #else
  283. mem0 = mem1 + y - 2*x;
  284. mem1 = x - .5f*y;
  285. #endif
  286. tmp[i] = EXTRACT16(SHR(y,2));
  287. }
  288. /* First few samples are bad because we don't propagate the memory */
  289. for (i=0;i<12;i++)
  290. tmp[i] = 0;
  291. for (i=0;i<N;i++)
  292. {
  293. int j;
  294. opus_val16 max_abs=0;
  295. for (j=0;j<block;j++)
  296. max_abs = MAX16(max_abs, ABS16(tmp[i*block+j]));
  297. bins[i] = max_abs;
  298. }
  299. for (i=0;i<N;i++)
  300. {
  301. int j;
  302. int conseq=0;
  303. opus_val16 t1, t2, t3;
  304. t1 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
  305. t2 = MULT16_16_Q15(QCONST16(.4f, 15), bins[i]);
  306. t3 = MULT16_16_Q15(QCONST16(.15f, 15), bins[i]);
  307. for (j=0;j<i;j++)
  308. {
  309. if (bins[j] < t1)
  310. conseq++;
  311. if (bins[j] < t2)
  312. conseq++;
  313. else
  314. conseq = 0;
  315. }
  316. if (conseq>=3)
  317. is_transient=1;
  318. conseq = 0;
  319. for (j=i+1;j<N;j++)
  320. {
  321. if (bins[j] < t3)
  322. conseq++;
  323. else
  324. conseq = 0;
  325. }
  326. if (conseq>=7)
  327. is_transient=1;
  328. }
  329. RESTORE_STACK;
  330. #ifdef FUZZING
  331. is_transient = rand()&0x1;
  332. #endif
  333. return is_transient;
  334. }
  335. /** Apply window and compute the MDCT for all sub-frames and
  336. all channels in a frame */
  337. static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * restrict in, celt_sig * restrict out, int _C, int LM)
  338. {
  339. const int C = CHANNELS(_C);
  340. if (C==1 && !shortBlocks)
  341. {
  342. const int overlap = OVERLAP(mode);
  343. clt_mdct_forward(&mode->mdct, in, out, mode->window, overlap, mode->maxLM-LM, 1);
  344. } else {
  345. const int overlap = OVERLAP(mode);
  346. int N = mode->shortMdctSize<<LM;
  347. int B = 1;
  348. int b, c;
  349. if (shortBlocks)
  350. {
  351. N = mode->shortMdctSize;
  352. B = shortBlocks;
  353. }
  354. c=0; do {
  355. for (b=0;b<B;b++)
  356. {
  357. /* Interleaving the sub-frames while doing the MDCTs */
  358. clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
  359. }
  360. } while (++c<C);
  361. }
  362. }
  363. /** Compute the IMDCT and apply window for all sub-frames and
  364. all channels in a frame */
  365. static void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
  366. celt_sig * restrict out_mem[],
  367. celt_sig * restrict overlap_mem[], int _C, int LM)
  368. {
  369. int c;
  370. const int C = CHANNELS(_C);
  371. const int N = mode->shortMdctSize<<LM;
  372. const int overlap = OVERLAP(mode);
  373. VARDECL(opus_val32, x);
  374. SAVE_STACK;
  375. ALLOC(x, N+overlap, opus_val32);
  376. c=0; do {
  377. int j;
  378. int b;
  379. int N2 = N;
  380. int B = 1;
  381. if (shortBlocks)
  382. {
  383. N2 = mode->shortMdctSize;
  384. B = shortBlocks;
  385. }
  386. /* Prevents problems from the imdct doing the overlap-add */
  387. OPUS_CLEAR(x, overlap);
  388. for (b=0;b<B;b++)
  389. {
  390. /* IMDCT on the interleaved the sub-frames */
  391. clt_mdct_backward(&mode->mdct, &X[b+c*N2*B], x+N2*b, mode->window, overlap, shortBlocks ? mode->maxLM : mode->maxLM-LM, B);
  392. }
  393. for (j=0;j<overlap;j++)
  394. out_mem[c][j] = x[j] + overlap_mem[c][j];
  395. for (;j<N;j++)
  396. out_mem[c][j] = x[j];
  397. for (j=0;j<overlap;j++)
  398. overlap_mem[c][j] = x[N+j];
  399. } while (++c<C);
  400. RESTORE_STACK;
  401. }
  402. static void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int _C, int downsample, const opus_val16 *coef, celt_sig *mem)
  403. {
  404. const int C = CHANNELS(_C);
  405. int c;
  406. int count=0;
  407. c=0; do {
  408. int j;
  409. celt_sig * restrict x;
  410. opus_val16 * restrict y;
  411. celt_sig m = mem[c];
  412. x =in[c];
  413. y = pcm+c;
  414. for (j=0;j<N;j++)
  415. {
  416. celt_sig tmp = *x + m;
  417. m = MULT16_32_Q15(coef[0], tmp)
  418. - MULT16_32_Q15(coef[1], *x);
  419. tmp = SHL32(MULT16_32_Q15(coef[3], tmp), 2);
  420. x++;
  421. /* Technically the store could be moved outside of the if because
  422. the stores we don't want will just be overwritten */
  423. if (++count==downsample)
  424. {
  425. *y = SCALEOUT(SIG2WORD16(tmp));
  426. y+=C;
  427. count=0;
  428. }
  429. }
  430. mem[c] = m;
  431. } while (++c<C);
  432. }
  433. static void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
  434. opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
  435. const opus_val16 *window, int overlap)
  436. {
  437. int i;
  438. /* printf ("%d %d %f %f\n", T0, T1, g0, g1); */
  439. opus_val16 g00, g01, g02, g10, g11, g12;
  440. static const opus_val16 gains[3][3] = {
  441. {QCONST16(0.3066406250f, 15), QCONST16(0.2170410156f, 15), QCONST16(0.1296386719f, 15)},
  442. {QCONST16(0.4638671875f, 15), QCONST16(0.2680664062f, 15), QCONST16(0.f, 15)},
  443. {QCONST16(0.7998046875f, 15), QCONST16(0.1000976562f, 15), QCONST16(0.f, 15)}};
  444. g00 = MULT16_16_Q15(g0, gains[tapset0][0]);
  445. g01 = MULT16_16_Q15(g0, gains[tapset0][1]);
  446. g02 = MULT16_16_Q15(g0, gains[tapset0][2]);
  447. g10 = MULT16_16_Q15(g1, gains[tapset1][0]);
  448. g11 = MULT16_16_Q15(g1, gains[tapset1][1]);
  449. g12 = MULT16_16_Q15(g1, gains[tapset1][2]);
  450. for (i=0;i<overlap;i++)
  451. {
  452. opus_val16 f;
  453. f = MULT16_16_Q15(window[i],window[i]);
  454. y[i] = x[i]
  455. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g00),x[i-T0])
  456. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0-1])
  457. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g01),x[i-T0+1])
  458. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0-2])
  459. + MULT16_32_Q15(MULT16_16_Q15((Q15ONE-f),g02),x[i-T0+2])
  460. + MULT16_32_Q15(MULT16_16_Q15(f,g10),x[i-T1])
  461. + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1-1])
  462. + MULT16_32_Q15(MULT16_16_Q15(f,g11),x[i-T1+1])
  463. + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1-2])
  464. + MULT16_32_Q15(MULT16_16_Q15(f,g12),x[i-T1+2]);
  465. }
  466. for (i=overlap;i<N;i++)
  467. y[i] = x[i]
  468. + MULT16_32_Q15(g10,x[i-T1])
  469. + MULT16_32_Q15(g11,x[i-T1-1])
  470. + MULT16_32_Q15(g11,x[i-T1+1])
  471. + MULT16_32_Q15(g12,x[i-T1-2])
  472. + MULT16_32_Q15(g12,x[i-T1+2]);
  473. }
  474. static const signed char tf_select_table[4][8] = {
  475. {0, -1, 0, -1, 0,-1, 0,-1},
  476. {0, -1, 0, -2, 1, 0, 1,-1},
  477. {0, -2, 0, -3, 2, 0, 1,-1},
  478. {0, -2, 0, -3, 3, 0, 1,-1},
  479. };
  480. static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, int width)
  481. {
  482. int i, j;
  483. static const opus_val16 sqrtM_1[4] = {Q15ONE, QCONST16(.70710678f,15), QCONST16(0.5f,15), QCONST16(0.35355339f,15)};
  484. opus_val32 L1;
  485. opus_val16 bias;
  486. L1=0;
  487. for (i=0;i<1<<LM;i++)
  488. {
  489. opus_val32 L2 = 0;
  490. for (j=0;j<N>>LM;j++)
  491. L2 = MAC16_16(L2, tmp[(j<<LM)+i], tmp[(j<<LM)+i]);
  492. L1 += celt_sqrt(L2);
  493. }
  494. L1 = MULT16_32_Q15(sqrtM_1[LM], L1);
  495. if (width==1)
  496. bias = QCONST16(.12f,15)*LM;
  497. else if (width==2)
  498. bias = QCONST16(.05f,15)*LM;
  499. else
  500. bias = QCONST16(.02f,15)*LM;
  501. L1 = MAC16_32_Q15(L1, bias, L1);
  502. return L1;
  503. }
  504. static int tf_analysis(const CELTMode *m, int len, int C, int isTransient,
  505. int *tf_res, int nbCompressedBytes, celt_norm *X, int N0, int LM,
  506. int *tf_sum)
  507. {
  508. int i;
  509. VARDECL(int, metric);
  510. int cost0;
  511. int cost1;
  512. VARDECL(int, path0);
  513. VARDECL(int, path1);
  514. VARDECL(celt_norm, tmp);
  515. int lambda;
  516. int tf_select=0;
  517. SAVE_STACK;
  518. if (nbCompressedBytes<15*C)
  519. {
  520. *tf_sum = 0;
  521. for (i=0;i<len;i++)
  522. tf_res[i] = isTransient;
  523. return 0;
  524. }
  525. if (nbCompressedBytes<40)
  526. lambda = 12;
  527. else if (nbCompressedBytes<60)
  528. lambda = 6;
  529. else if (nbCompressedBytes<100)
  530. lambda = 4;
  531. else
  532. lambda = 3;
  533. ALLOC(metric, len, int);
  534. ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
  535. ALLOC(path0, len, int);
  536. ALLOC(path1, len, int);
  537. *tf_sum = 0;
  538. for (i=0;i<len;i++)
  539. {
  540. int j, k, N;
  541. opus_val32 L1, best_L1;
  542. int best_level=0;
  543. N = (m->eBands[i+1]-m->eBands[i])<<LM;
  544. for (j=0;j<N;j++)
  545. tmp[j] = X[j+(m->eBands[i]<<LM)];
  546. /* Just add the right channel if we're in stereo */
  547. if (C==2)
  548. for (j=0;j<N;j++)
  549. tmp[j] = ADD16(tmp[j],X[N0+j+(m->eBands[i]<<LM)]);
  550. L1 = l1_metric(tmp, N, isTransient ? LM : 0, N>>LM);
  551. best_L1 = L1;
  552. /*printf ("%f ", L1);*/
  553. for (k=0;k<LM;k++)
  554. {
  555. int B;
  556. if (isTransient)
  557. B = (LM-k-1);
  558. else
  559. B = k+1;
  560. if (isTransient)
  561. haar1(tmp, N>>(LM-k), 1<<(LM-k));
  562. else
  563. haar1(tmp, N>>k, 1<<k);
  564. L1 = l1_metric(tmp, N, B, N>>LM);
  565. if (L1 < best_L1)
  566. {
  567. best_L1 = L1;
  568. best_level = k+1;
  569. }
  570. }
  571. /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
  572. if (isTransient)
  573. metric[i] = best_level;
  574. else
  575. metric[i] = -best_level;
  576. *tf_sum += metric[i];
  577. }
  578. /*printf("\n");*/
  579. /* NOTE: Future optimized implementations could detect extreme transients and set
  580. tf_select = 1 but so far we have not found a reliable way of making this useful */
  581. tf_select = 0;
  582. cost0 = 0;
  583. cost1 = isTransient ? 0 : lambda;
  584. /* Viterbi forward pass */
  585. for (i=1;i<len;i++)
  586. {
  587. int curr0, curr1;
  588. int from0, from1;
  589. from0 = cost0;
  590. from1 = cost1 + lambda;
  591. if (from0 < from1)
  592. {
  593. curr0 = from0;
  594. path0[i]= 0;
  595. } else {
  596. curr0 = from1;
  597. path0[i]= 1;
  598. }
  599. from0 = cost0 + lambda;
  600. from1 = cost1;
  601. if (from0 < from1)
  602. {
  603. curr1 = from0;
  604. path1[i]= 0;
  605. } else {
  606. curr1 = from1;
  607. path1[i]= 1;
  608. }
  609. cost0 = curr0 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+0]);
  610. cost1 = curr1 + abs(metric[i]-tf_select_table[LM][4*isTransient+2*tf_select+1]);
  611. }
  612. tf_res[len-1] = cost0 < cost1 ? 0 : 1;
  613. /* Viterbi backward pass to check the decisions */
  614. for (i=len-2;i>=0;i--)
  615. {
  616. if (tf_res[i+1] == 1)
  617. tf_res[i] = path1[i+1];
  618. else
  619. tf_res[i] = path0[i+1];
  620. }
  621. RESTORE_STACK;
  622. #ifdef FUZZING
  623. tf_select = rand()&0x1;
  624. tf_res[0] = rand()&0x1;
  625. for (i=1;i<len;i++)
  626. tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
  627. #endif
  628. return tf_select;
  629. }
  630. static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
  631. {
  632. int curr, i;
  633. int tf_select_rsv;
  634. int tf_changed;
  635. int logp;
  636. opus_uint32 budget;
  637. opus_uint32 tell;
  638. budget = enc->storage*8;
  639. tell = ec_tell(enc);
  640. logp = isTransient ? 2 : 4;
  641. /* Reserve space to code the tf_select decision. */
  642. tf_select_rsv = LM>0 && tell+logp+1 <= budget;
  643. budget -= tf_select_rsv;
  644. curr = tf_changed = 0;
  645. for (i=start;i<end;i++)
  646. {
  647. if (tell+logp<=budget)
  648. {
  649. ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
  650. tell = ec_tell(enc);
  651. curr = tf_res[i];
  652. tf_changed |= curr;
  653. }
  654. else
  655. tf_res[i] = curr;
  656. logp = isTransient ? 4 : 5;
  657. }
  658. /* Only code tf_select if it would actually make a difference. */
  659. if (tf_select_rsv &&
  660. tf_select_table[LM][4*isTransient+0+tf_changed]!=
  661. tf_select_table[LM][4*isTransient+2+tf_changed])
  662. ec_enc_bit_logp(enc, tf_select, 1);
  663. else
  664. tf_select = 0;
  665. for (i=start;i<end;i++)
  666. tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
  667. /*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
  668. }
  669. static void tf_decode(int start, int end, int isTransient, int *tf_res, int LM, ec_dec *dec)
  670. {
  671. int i, curr, tf_select;
  672. int tf_select_rsv;
  673. int tf_changed;
  674. int logp;
  675. opus_uint32 budget;
  676. opus_uint32 tell;
  677. budget = dec->storage*8;
  678. tell = ec_tell(dec);
  679. logp = isTransient ? 2 : 4;
  680. tf_select_rsv = LM>0 && tell+logp+1<=budget;
  681. budget -= tf_select_rsv;
  682. tf_changed = curr = 0;
  683. for (i=start;i<end;i++)
  684. {
  685. if (tell+logp<=budget)
  686. {
  687. curr ^= ec_dec_bit_logp(dec, logp);
  688. tell = ec_tell(dec);
  689. tf_changed |= curr;
  690. }
  691. tf_res[i] = curr;
  692. logp = isTransient ? 4 : 5;
  693. }
  694. tf_select = 0;
  695. if (tf_select_rsv &&
  696. tf_select_table[LM][4*isTransient+0+tf_changed] !=
  697. tf_select_table[LM][4*isTransient+2+tf_changed])
  698. {
  699. tf_select = ec_dec_bit_logp(dec, 1);
  700. }
  701. for (i=start;i<end;i++)
  702. {
  703. tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
  704. }
  705. }
  706. static void init_caps(const CELTMode *m,int *cap,int LM,int C)
  707. {
  708. int i;
  709. for (i=0;i<m->nbEBands;i++)
  710. {
  711. int N;
  712. N=(m->eBands[i+1]-m->eBands[i])<<LM;
  713. cap[i] = (m->cache.caps[m->nbEBands*(2*LM+C-1)+i]+64)*C*N>>2;
  714. }
  715. }
  716. static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
  717. const opus_val16 *bandLogE, int end, int LM, int C, int N0)
  718. {
  719. int i;
  720. opus_val32 diff=0;
  721. int c;
  722. int trim_index = 5;
  723. if (C==2)
  724. {
  725. opus_val16 sum = 0; /* Q10 */
  726. /* Compute inter-channel correlation for low frequencies */
  727. for (i=0;i<8;i++)
  728. {
  729. int j;
  730. opus_val32 partial = 0;
  731. for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
  732. partial = MAC16_16(partial, X[j], X[N0+j]);
  733. sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
  734. }
  735. sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
  736. /*printf ("%f\n", sum);*/
  737. if (sum > QCONST16(.995f,10))
  738. trim_index-=4;
  739. else if (sum > QCONST16(.92f,10))
  740. trim_index-=3;
  741. else if (sum > QCONST16(.85f,10))
  742. trim_index-=2;
  743. else if (sum > QCONST16(.8f,10))
  744. trim_index-=1;
  745. }
  746. /* Estimate spectral tilt */
  747. c=0; do {
  748. for (i=0;i<end-1;i++)
  749. {
  750. diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-m->nbEBands);
  751. }
  752. } while (++c<C);
  753. /* We divide by two here to avoid making the tilt larger for stereo as a
  754. result of a bug in the loop above */
  755. diff /= 2*C*(end-1);
  756. /*printf("%f\n", diff);*/
  757. if (diff > QCONST16(2.f, DB_SHIFT))
  758. trim_index--;
  759. if (diff > QCONST16(8.f, DB_SHIFT))
  760. trim_index--;
  761. if (diff < -QCONST16(4.f, DB_SHIFT))
  762. trim_index++;
  763. if (diff < -QCONST16(10.f, DB_SHIFT))
  764. trim_index++;
  765. if (trim_index<0)
  766. trim_index = 0;
  767. if (trim_index>10)
  768. trim_index = 10;
  769. #ifdef FUZZING
  770. trim_index = rand()%11;
  771. #endif
  772. return trim_index;
  773. }
  774. static int stereo_analysis(const CELTMode *m, const celt_norm *X,
  775. int LM, int N0)
  776. {
  777. int i;
  778. int thetas;
  779. opus_val32 sumLR = EPSILON, sumMS = EPSILON;
  780. /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
  781. for (i=0;i<13;i++)
  782. {
  783. int j;
  784. for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
  785. {
  786. opus_val16 L, R, M, S;
  787. L = X[j];
  788. R = X[N0+j];
  789. M = L+R;
  790. S = L-R;
  791. sumLR += EXTEND32(ABS16(L)) + EXTEND32(ABS16(R));
  792. sumMS += EXTEND32(ABS16(M)) + EXTEND32(ABS16(S));
  793. }
  794. }
  795. sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
  796. thetas = 13;
  797. /* We don't need thetas for lower bands with LM<=1 */
  798. if (LM<=1)
  799. thetas -= 8;
  800. return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
  801. > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
  802. }
  803. int celt_encode_with_ec(CELTEncoder * restrict st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
  804. {
  805. int i, c, N;
  806. opus_int32 bits;
  807. ec_enc _enc;
  808. VARDECL(celt_sig, in);
  809. VARDECL(celt_sig, freq);
  810. VARDECL(celt_norm, X);
  811. VARDECL(celt_ener, bandE);
  812. VARDECL(opus_val16, bandLogE);
  813. VARDECL(int, fine_quant);
  814. VARDECL(opus_val16, error);
  815. VARDECL(int, pulses);
  816. VARDECL(int, cap);
  817. VARDECL(int, offsets);
  818. VARDECL(int, fine_priority);
  819. VARDECL(int, tf_res);
  820. VARDECL(unsigned char, collapse_masks);
  821. celt_sig *prefilter_mem;
  822. opus_val16 *oldBandE, *oldLogE, *oldLogE2;
  823. int shortBlocks=0;
  824. int isTransient=0;
  825. const int CC = CHANNELS(st->channels);
  826. const int C = CHANNELS(st->stream_channels);
  827. int LM, M;
  828. int tf_select;
  829. int nbFilledBytes, nbAvailableBytes;
  830. int effEnd;
  831. int codedBands;
  832. int tf_sum;
  833. int alloc_trim;
  834. int pitch_index=COMBFILTER_MINPERIOD;
  835. opus_val16 gain1 = 0;
  836. int intensity=0;
  837. int dual_stereo=0;
  838. int effectiveBytes;
  839. opus_val16 pf_threshold;
  840. int dynalloc_logp;
  841. opus_int32 vbr_rate;
  842. opus_int32 total_bits;
  843. opus_int32 total_boost;
  844. opus_int32 balance;
  845. opus_int32 tell;
  846. int prefilter_tapset=0;
  847. int pf_on;
  848. int anti_collapse_rsv;
  849. int anti_collapse_on=0;
  850. int silence=0;
  851. ALLOC_STACK;
  852. if (nbCompressedBytes<2 || pcm==NULL)
  853. return OPUS_BAD_ARG;
  854. frame_size *= st->upsample;
  855. for (LM=0;LM<=st->mode->maxLM;LM++)
  856. if (st->mode->shortMdctSize<<LM==frame_size)
  857. break;
  858. if (LM>st->mode->maxLM)
  859. return OPUS_BAD_ARG;
  860. M=1<<LM;
  861. N = M*st->mode->shortMdctSize;
  862. prefilter_mem = st->in_mem+CC*(st->overlap);
  863. oldBandE = (opus_val16*)(st->in_mem+CC*(2*st->overlap+COMBFILTER_MAXPERIOD));
  864. oldLogE = oldBandE + CC*st->mode->nbEBands;
  865. oldLogE2 = oldLogE + CC*st->mode->nbEBands;
  866. if (enc==NULL)
  867. {
  868. tell=1;
  869. nbFilledBytes=0;
  870. } else {
  871. tell=ec_tell(enc);
  872. nbFilledBytes=(tell+4)>>3;
  873. }
  874. if (st->signalling && enc==NULL)
  875. {
  876. int tmp = (st->mode->effEBands-st->end)>>1;
  877. st->end = IMAX(1, st->mode->effEBands-tmp);
  878. compressed[0] = tmp<<5;
  879. compressed[0] |= LM<<3;
  880. compressed[0] |= (C==2)<<2;
  881. /* Convert "standard mode" to Opus header */
  882. if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
  883. {
  884. int c0 = toOpus(compressed[0]);
  885. if (c0<0)
  886. return OPUS_BAD_ARG;
  887. compressed[0] = c0;
  888. }
  889. compressed++;
  890. nbCompressedBytes--;
  891. }
  892. /* Can't produce more than 1275 output bytes */
  893. nbCompressedBytes = IMIN(nbCompressedBytes,1275);
  894. nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
  895. if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
  896. {
  897. opus_int32 den=st->mode->Fs>>BITRES;
  898. vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
  899. if (st->signalling)
  900. vbr_rate -= 8<<BITRES;
  901. effectiveBytes = vbr_rate>>(3+BITRES);
  902. } else {
  903. opus_int32 tmp;
  904. vbr_rate = 0;
  905. tmp = st->bitrate*frame_size;
  906. if (tell>1)
  907. tmp += tell;
  908. if (st->bitrate!=OPUS_BITRATE_MAX)
  909. nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
  910. (tmp+4*st->mode->Fs)/(8*st->mode->Fs)-!!st->signalling));
  911. effectiveBytes = nbCompressedBytes;
  912. }
  913. if (enc==NULL)
  914. {
  915. ec_enc_init(&_enc, compressed, nbCompressedBytes);
  916. enc = &_enc;
  917. }
  918. if (vbr_rate>0)
  919. {
  920. /* Computes the max bit-rate allowed in VBR mode to avoid violating the
  921. target rate and buffering.
  922. We must do this up front so that bust-prevention logic triggers
  923. correctly if we don't have enough bits. */
  924. if (st->constrained_vbr)
  925. {
  926. opus_int32 vbr_bound;
  927. opus_int32 max_allowed;
  928. /* We could use any multiple of vbr_rate as bound (depending on the
  929. delay).
  930. This is clamped to ensure we use at least two bytes if the encoder
  931. was entirely empty, but to allow 0 in hybrid mode. */
  932. vbr_bound = vbr_rate;
  933. max_allowed = IMIN(IMAX(tell==1?2:0,
  934. (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
  935. nbAvailableBytes);
  936. if(max_allowed < nbAvailableBytes)
  937. {
  938. nbCompressedBytes = nbFilledBytes+max_allowed;
  939. nbAvailableBytes = max_allowed;
  940. ec_enc_shrink(enc, nbCompressedBytes);
  941. }
  942. }
  943. }
  944. total_bits = nbCompressedBytes*8;
  945. effEnd = st->end;
  946. if (effEnd > st->mode->effEBands)
  947. effEnd = st->mode->effEBands;
  948. ALLOC(in, CC*(N+st->overlap), celt_sig);
  949. /* Find pitch period and gain */
  950. {
  951. VARDECL(celt_sig, _pre);
  952. celt_sig *pre[2];
  953. SAVE_STACK;
  954. ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
  955. pre[0] = _pre;
  956. pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
  957. silence = 1;
  958. c=0; do {
  959. int count = 0;
  960. const opus_val16 * restrict pcmp = pcm+c;
  961. celt_sig * restrict inp = in+c*(N+st->overlap)+st->overlap;
  962. for (i=0;i<N;i++)
  963. {
  964. celt_sig x, tmp;
  965. x = SCALEIN(*pcmp);
  966. #ifndef FIXED_POINT
  967. if (!(x==x))
  968. x = 0;
  969. if (st->clip)
  970. x = MAX32(-65536.f, MIN32(65536.f,x));
  971. #endif
  972. if (++count==st->upsample)
  973. {
  974. count=0;
  975. pcmp+=CC;
  976. } else {
  977. x = 0;
  978. }
  979. /* Apply pre-emphasis */
  980. tmp = MULT16_16(st->mode->preemph[2], x);
  981. *inp = tmp + st->preemph_memE[c];
  982. st->preemph_memE[c] = MULT16_32_Q15(st->mode->preemph[1], *inp)
  983. - MULT16_32_Q15(st->mode->preemph[0], tmp);
  984. silence = silence && *inp == 0;
  985. inp++;
  986. }
  987. OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
  988. OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
  989. } while (++c<CC);
  990. #ifdef FUZZING
  991. if ((rand()&0x3F)==0)
  992. silence = 1;
  993. #endif
  994. if (tell==1)
  995. ec_enc_bit_logp(enc, silence, 15);
  996. else
  997. silence=0;
  998. if (silence)
  999. {
  1000. /*In VBR mode there is no need to send more than the minimum. */
  1001. if (vbr_rate>0)
  1002. {
  1003. effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
  1004. total_bits=nbCompressedBytes*8;
  1005. nbAvailableBytes=2;
  1006. ec_enc_shrink(enc, nbCompressedBytes);
  1007. }
  1008. /* Pretend we've filled all the remaining bits with zeros
  1009. (that's what the initialiser did anyway) */
  1010. tell = nbCompressedBytes*8;
  1011. enc->nbits_total+=tell-ec_tell(enc);
  1012. }
  1013. if (nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf && st->complexity >= 5)
  1014. {
  1015. VARDECL(opus_val16, pitch_buf);
  1016. ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
  1017. pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
  1018. pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
  1019. COMBFILTER_MAXPERIOD-COMBFILTER_MINPERIOD, &pitch_index);
  1020. pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
  1021. gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
  1022. N, &pitch_index, st->prefilter_period, st->prefilter_gain);
  1023. if (pitch_index > COMBFILTER_MAXPERIOD-2)
  1024. pitch_index = COMBFILTER_MAXPERIOD-2;
  1025. gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
  1026. if (st->loss_rate>2)
  1027. gain1 = HALF32(gain1);
  1028. if (st->loss_rate>4)
  1029. gain1 = HALF32(gain1);
  1030. if (st->loss_rate>8)
  1031. gain1 = 0;
  1032. prefilter_tapset = st->tapset_decision;
  1033. } else {
  1034. gain1 = 0;
  1035. }
  1036. /* Gain threshold for enabling the prefilter/postfilter */
  1037. pf_threshold = QCONST16(.2f,15);
  1038. /* Adjusting the threshold based on rate and continuity */
  1039. if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
  1040. pf_threshold += QCONST16(.2f,15);
  1041. if (nbAvailableBytes<25)
  1042. pf_threshold += QCONST16(.1f,15);
  1043. if (nbAvailableBytes<35)
  1044. pf_threshold += QCONST16(.1f,15);
  1045. if (st->prefilter_gain > QCONST16(.4f,15))
  1046. pf_threshold -= QCONST16(.1f,15);
  1047. if (st->prefilter_gain > QCONST16(.55f,15))
  1048. pf_threshold -= QCONST16(.1f,15);
  1049. /* Hard threshold at 0.2 */
  1050. pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
  1051. if (gain1<pf_threshold)
  1052. {
  1053. if(st->start==0 && tell+16<=total_bits)
  1054. ec_enc_bit_logp(enc, 0, 1);
  1055. gain1 = 0;
  1056. pf_on = 0;
  1057. } else {
  1058. /*This block is not gated by a total bits check only because
  1059. of the nbAvailableBytes check above.*/
  1060. int qg;
  1061. int octave;
  1062. if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
  1063. gain1=st->prefilter_gain;
  1064. #ifdef FIXED_POINT
  1065. qg = ((gain1+1536)>>10)/3-1;
  1066. #else
  1067. qg = (int)floor(.5f+gain1*32/3)-1;
  1068. #endif
  1069. qg = IMAX(0, IMIN(7, qg));
  1070. ec_enc_bit_logp(enc, 1, 1);
  1071. pitch_index += 1;
  1072. octave = EC_ILOG(pitch_index)-5;
  1073. ec_enc_uint(enc, octave, 6);
  1074. ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
  1075. pitch_index -= 1;
  1076. ec_enc_bits(enc, qg, 3);
  1077. if (ec_tell(enc)+2<=total_bits)
  1078. ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
  1079. else
  1080. prefilter_tapset = 0;
  1081. gain1 = QCONST16(0.09375f,15)*(qg+1);
  1082. pf_on = 1;
  1083. }
  1084. /*printf("%d %f\n", pitch_index, gain1);*/
  1085. c=0; do {
  1086. int offset = st->mode->shortMdctSize-st->mode->overlap;
  1087. st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
  1088. OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
  1089. if (offset)
  1090. comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
  1091. st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
  1092. st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
  1093. comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
  1094. st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
  1095. st->prefilter_tapset, prefilter_tapset, st->mode->window, st->mode->overlap);
  1096. OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
  1097. if (N>COMBFILTER_MAXPERIOD)
  1098. {
  1099. OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
  1100. } else {
  1101. OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
  1102. OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
  1103. }
  1104. } while (++c<CC);
  1105. RESTORE_STACK;
  1106. }
  1107. isTransient = 0;
  1108. shortBlocks = 0;
  1109. if (LM>0 && ec_tell(enc)+3<=total_bits)
  1110. {
  1111. if (st->complexity > 1)
  1112. {
  1113. isTransient = transient_analysis(in, N+st->overlap, CC,
  1114. st->overlap);
  1115. if (isTransient)
  1116. shortBlocks = M;
  1117. }
  1118. ec_enc_bit_logp(enc, isTransient, 3);
  1119. }
  1120. ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
  1121. ALLOC(bandE,st->mode->nbEBands*CC, celt_ener);
  1122. ALLOC(bandLogE,st->mode->nbEBands*CC, opus_val16);
  1123. /* Compute MDCTs */
  1124. compute_mdcts(st->mode, shortBlocks, in, freq, CC, LM);
  1125. if (CC==2&&C==1)
  1126. {
  1127. for (i=0;i<N;i++)
  1128. freq[i] = ADD32(HALF32(freq[i]), HALF32(freq[N+i]));
  1129. }
  1130. if (st->upsample != 1)
  1131. {
  1132. c=0; do
  1133. {
  1134. int bound = N/st->upsample;
  1135. for (i=0;i<bound;i++)
  1136. freq[c*N+i] *= st->upsample;
  1137. for (;i<N;i++)
  1138. freq[c*N+i] = 0;
  1139. } while (++c<C);
  1140. }
  1141. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  1142. compute_band_energies(st->mode, freq, bandE, effEnd, C, M);
  1143. amp2Log2(st->mode, effEnd, st->end, bandE, bandLogE, C);
  1144. /* Band normalisation */
  1145. normalise_bands(st->mode, freq, X, bandE, effEnd, C, M);
  1146. ALLOC(tf_res, st->mode->nbEBands, int);
  1147. tf_select = tf_analysis(st->mode, effEnd, C, isTransient, tf_res, effectiveBytes, X, N, LM, &tf_sum);
  1148. for (i=effEnd;i<st->end;i++)
  1149. tf_res[i] = tf_res[effEnd-1];
  1150. ALLOC(error, C*st->mode->nbEBands, opus_val16);
  1151. quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
  1152. oldBandE, total_bits, error, enc,
  1153. C, LM, nbAvailableBytes, st->force_intra,
  1154. &st->delayedIntra, st->complexity >= 4, st->loss_rate);
  1155. tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
  1156. st->spread_decision = SPREAD_NORMAL;
  1157. if (ec_tell(enc)+4<=total_bits)
  1158. {
  1159. if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
  1160. {
  1161. if (st->complexity == 0)
  1162. st->spread_decision = SPREAD_NONE;
  1163. } else {
  1164. st->spread_decision = spreading_decision(st->mode, X,
  1165. &st->tonal_average, st->spread_decision, &st->hf_average,
  1166. &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
  1167. }
  1168. ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
  1169. }
  1170. ALLOC(cap, st->mode->nbEBands, int);
  1171. ALLOC(offsets, st->mode->nbEBands, int);
  1172. init_caps(st->mode,cap,LM,C);
  1173. for (i=0;i<st->mode->nbEBands;i++)
  1174. offsets[i] = 0;
  1175. /* Dynamic allocation code */
  1176. /* Make sure that dynamic allocation can't make us bust the budget */
  1177. if (effectiveBytes > 50 && LM>=1)
  1178. {
  1179. int t1, t2;
  1180. if (LM <= 1)
  1181. {
  1182. t1 = 3;
  1183. t2 = 5;
  1184. } else {
  1185. t1 = 2;
  1186. t2 = 4;
  1187. }
  1188. for (i=st->start+1;i<st->end-1;i++)
  1189. {
  1190. opus_val32 d2;
  1191. d2 = 2*bandLogE[i]-bandLogE[i-1]-bandLogE[i+1];
  1192. if (C==2)
  1193. d2 = HALF32(d2 + 2*bandLogE[i+st->mode->nbEBands]-
  1194. bandLogE[i-1+st->mode->nbEBands]-bandLogE[i+1+st->mode->nbEBands]);
  1195. #ifdef FUZZING
  1196. if((rand()&0xF)==0)
  1197. {
  1198. offsets[i] += 1;
  1199. if((rand()&0x3)==0)
  1200. offsets[i] += 1+(rand()&0x3);
  1201. }
  1202. #else
  1203. if (d2 > SHL16(t1,DB_SHIFT))
  1204. offsets[i] += 1;
  1205. if (d2 > SHL16(t2,DB_SHIFT))
  1206. offsets[i] += 1;
  1207. #endif
  1208. }
  1209. }
  1210. dynalloc_logp = 6;
  1211. total_bits<<=BITRES;
  1212. total_boost = 0;
  1213. tell = ec_tell_frac(enc);
  1214. for (i=st->start;i<st->end;i++)
  1215. {
  1216. int width, quanta;
  1217. int dynalloc_loop_logp;
  1218. int boost;
  1219. int j;
  1220. width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
  1221. /* quanta is 6 bits, but no more than 1 bit/sample
  1222. and no less than 1/8 bit/sample */
  1223. quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
  1224. dynalloc_loop_logp = dynalloc_logp;
  1225. boost = 0;
  1226. for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
  1227. && boost < cap[i]; j++)
  1228. {
  1229. int flag;
  1230. flag = j<offsets[i];
  1231. ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
  1232. tell = ec_tell_frac(enc);
  1233. if (!flag)
  1234. break;
  1235. boost += quanta;
  1236. total_boost += quanta;
  1237. dynalloc_loop_logp = 1;
  1238. }
  1239. /* Making dynalloc more likely */
  1240. if (j)
  1241. dynalloc_logp = IMAX(2, dynalloc_logp-1);
  1242. offsets[i] = boost;
  1243. }
  1244. alloc_trim = 5;
  1245. if (tell+(6<<BITRES) <= total_bits - total_boost)
  1246. {
  1247. alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
  1248. st->end, LM, C, N);
  1249. ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
  1250. tell = ec_tell_frac(enc);
  1251. }
  1252. /* Variable bitrate */
  1253. if (vbr_rate>0)
  1254. {
  1255. opus_val16 alpha;
  1256. opus_int32 delta;
  1257. /* The target rate in 8th bits per frame */
  1258. opus_int32 target;
  1259. opus_int32 min_allowed;
  1260. int lm_diff = st->mode->maxLM - LM;
  1261. target = vbr_rate + (st->vbr_offset>>lm_diff) - ((40*C+20)<<BITRES);
  1262. /* Shortblocks get a large boost in bitrate, but since they
  1263. are uncommon long blocks are not greatly affected */
  1264. if (shortBlocks || tf_sum < -2*(st->end-st->start))
  1265. target = 7*target/4;
  1266. else if (tf_sum < -(st->end-st->start))
  1267. target = 3*target/2;
  1268. else if (M > 1)
  1269. target-=(target+14)/28;
  1270. /* The current offset is removed from the target and the space used
  1271. so far is added*/
  1272. target=target+tell;
  1273. /* In VBR mode the frame size must not be reduced so much that it would
  1274. result in the encoder running out of bits.
  1275. The margin of 2 bytes ensures that none of the bust-prevention logic
  1276. in the decoder will have triggered so far. */
  1277. min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
  1278. nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
  1279. nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
  1280. nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
  1281. /* By how much did we "miss" the target on that frame */
  1282. delta = target - vbr_rate;
  1283. target=nbAvailableBytes<<(BITRES+3);
  1284. /*If the frame is silent we don't adjust our drift, otherwise
  1285. the encoder will shoot to very high rates after hitting a
  1286. span of silence, but we do allow the bitres to refill.
  1287. This means that we'll undershoot our target in CVBR/VBR modes
  1288. on files with lots of silence. */
  1289. if(silence)
  1290. {
  1291. nbAvailableBytes = 2;
  1292. target = 2*8<<BITRES;
  1293. delta = 0;
  1294. }
  1295. if (st->vbr_count < 970)
  1296. {
  1297. st->vbr_count++;
  1298. alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
  1299. } else
  1300. alpha = QCONST16(.001f,15);
  1301. /* How many bits have we used in excess of what we're allowed */
  1302. if (st->constrained_vbr)
  1303. st->vbr_reservoir += target - vbr_rate;
  1304. /*printf ("%d\n", st->vbr_reservoir);*/
  1305. /* Compute the offset we need to apply in order to reach the target */
  1306. st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta<<lm_diff)-st->vbr_offset-st->vbr_drift);
  1307. st->vbr_offset = -st->vbr_drift;
  1308. /*printf ("%d\n", st->vbr_drift);*/
  1309. if (st->constrained_vbr && st->vbr_reservoir < 0)
  1310. {
  1311. /* We're under the min value -- increase rate */
  1312. int adjust = (-st->vbr_reservoir)/(8<<BITRES);
  1313. /* Unless we're just coding silence */
  1314. nbAvailableBytes += silence?0:adjust;
  1315. st->vbr_reservoir = 0;
  1316. /*printf ("+%d\n", adjust);*/
  1317. }
  1318. nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
  1319. /* This moves the raw bits to take into account the new compressed size */
  1320. ec_enc_shrink(enc, nbCompressedBytes);
  1321. }
  1322. if (C==2)
  1323. {
  1324. int effectiveRate;
  1325. /* Always use MS for 2.5 ms frames until we can do a better analysis */
  1326. if (LM!=0)
  1327. dual_stereo = stereo_analysis(st->mode, X, LM, N);
  1328. /* Account for coarse energy */
  1329. effectiveRate = (8*effectiveBytes - 80)>>LM;
  1330. /* effectiveRate in kb/s */
  1331. effectiveRate = 2*effectiveRate/5;
  1332. if (effectiveRate<35)
  1333. intensity = 8;
  1334. else if (effectiveRate<50)
  1335. intensity = 12;
  1336. else if (effectiveRate<68)
  1337. intensity = 16;
  1338. else if (effectiveRate<84)
  1339. intensity = 18;
  1340. else if (effectiveRate<102)
  1341. intensity = 19;
  1342. else if (effectiveRate<130)
  1343. intensity = 20;
  1344. else
  1345. intensity = 100;
  1346. intensity = IMIN(st->end,IMAX(st->start, intensity));
  1347. }
  1348. /* Bit allocation */
  1349. ALLOC(fine_quant, st->mode->nbEBands, int);
  1350. ALLOC(pulses, st->mode->nbEBands, int);
  1351. ALLOC(fine_priority, st->mode->nbEBands, int);
  1352. /* bits = packet size - where we are - safety*/
  1353. bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
  1354. anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
  1355. bits -= anti_collapse_rsv;
  1356. codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
  1357. alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
  1358. fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
  1359. st->lastCodedBands = codedBands;
  1360. quant_fine_energy(st->mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
  1361. #ifdef MEASURE_NORM_MSE
  1362. float X0[3000];
  1363. float bandE0[60];
  1364. c=0; do
  1365. for (i=0;i<N;i++)
  1366. X0[i+c*N] = X[i+c*N];
  1367. while (++c<C);
  1368. for (i=0;i<C*st->mode->nbEBands;i++)
  1369. bandE0[i] = bandE[i];
  1370. #endif
  1371. /* Residual quantisation */
  1372. ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
  1373. quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
  1374. bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, intensity, tf_res,
  1375. nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
  1376. if (anti_collapse_rsv > 0)
  1377. {
  1378. anti_collapse_on = st->consec_transient<2;
  1379. #ifdef FUZZING
  1380. anti_collapse_on = rand()&0x1;
  1381. #endif
  1382. ec_enc_bits(enc, anti_collapse_on, 1);
  1383. }
  1384. quant_energy_finalise(st->mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
  1385. if (silence)
  1386. {
  1387. for (i=0;i<C*st->mode->nbEBands;i++)
  1388. oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
  1389. }
  1390. #ifdef RESYNTH
  1391. /* Re-synthesis of the coded audio if required */
  1392. {
  1393. celt_sig *out_mem[2];
  1394. celt_sig *overlap_mem[2];
  1395. log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
  1396. if (silence)
  1397. {
  1398. for (i=0;i<C*st->mode->nbEBands;i++)
  1399. bandE[i] = 0;
  1400. }
  1401. #ifdef MEASURE_NORM_MSE
  1402. measure_norm_mse(st->mode, X, X0, bandE, bandE0, M, N, C);
  1403. #endif
  1404. if (anti_collapse_on)
  1405. {
  1406. anti_collapse(st->mode, X, collapse_masks, LM, C, CC, N,
  1407. st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
  1408. }
  1409. /* Synthesis */
  1410. denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
  1411. OPUS_MOVE(st->syn_mem[0], st->syn_mem[0]+N, MAX_PERIOD);
  1412. if (CC==2)
  1413. OPUS_MOVE(st->syn_mem[1], st->syn_mem[1]+N, MAX_PERIOD);
  1414. c=0; do
  1415. for (i=0;i<M*st->mode->eBands[st->start];i++)
  1416. freq[c*N+i] = 0;
  1417. while (++c<C);
  1418. c=0; do
  1419. for (i=M*st->mode->eBands[st->end];i<N;i++)
  1420. freq[c*N+i] = 0;
  1421. while (++c<C);
  1422. if (CC==2&&C==1)
  1423. {
  1424. for (i=0;i<N;i++)
  1425. freq[N+i] = freq[i];
  1426. }
  1427. out_mem[0] = st->syn_mem[0]+MAX_PERIOD;
  1428. if (CC==2)
  1429. out_mem[1] = st->syn_mem[1]+MAX_PERIOD;
  1430. overlap_mem[0] = prefilter_mem+CC*COMBFILTER_MAXPERIOD;
  1431. if (CC==2)
  1432. overlap_mem[1] = overlap_mem[0] + st->overlap;
  1433. compute_inv_mdcts(st->mode, shortBlocks, freq, out_mem, overlap_mem, CC, LM);
  1434. c=0; do {
  1435. st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
  1436. st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
  1437. comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, st->mode->shortMdctSize,
  1438. st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
  1439. st->mode->window, st->overlap);
  1440. if (LM!=0)
  1441. comb_filter(out_mem[c]+st->mode->shortMdctSize, out_mem[c]+st->mode->shortMdctSize, st->prefilter_period, pitch_index, N-st->mode->shortMdctSize,
  1442. st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
  1443. st->mode->window, st->mode->overlap);
  1444. } while (++c<CC);
  1445. deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, st->mode->preemph, st->preemph_memD);
  1446. st->prefilter_period_old = st->prefilter_period;
  1447. st->prefilter_gain_old = st->prefilter_gain;
  1448. st->prefilter_tapset_old = st->prefilter_tapset;
  1449. }
  1450. #endif
  1451. st->prefilter_period = pitch_index;
  1452. st->prefilter_gain = gain1;
  1453. st->prefilter_tapset = prefilter_tapset;
  1454. #ifdef RESYNTH
  1455. if (LM!=0)
  1456. {
  1457. st->prefilter_period_old = st->prefilter_period;
  1458. st->prefilter_gain_old = st->prefilter_gain;
  1459. st->prefilter_tapset_old = st->prefilter_tapset;
  1460. }
  1461. #endif
  1462. if (CC==2&&C==1) {
  1463. for (i=0;i<st->mode->nbEBands;i++)
  1464. oldBandE[st->mode->nbEBands+i]=oldBandE[i];
  1465. }
  1466. /* In case start or end were to change */
  1467. c=0; do
  1468. {
  1469. for (i=0;i<st->start;i++)
  1470. oldBandE[c*st->mode->nbEBands+i]=0;
  1471. for (i=st->end;i<st->mode->nbEBands;i++)
  1472. oldBandE[c*st->mode->nbEBands+i]=0;
  1473. } while (++c<CC);
  1474. if (!isTransient)
  1475. {
  1476. for (i=0;i<CC*st->mode->nbEBands;i++)
  1477. oldLogE2[i] = oldLogE[i];
  1478. for (i=0;i<CC*st->mode->nbEBands;i++)
  1479. oldLogE[i] = oldBandE[i];
  1480. } else {
  1481. for (i=0;i<CC*st->mode->nbEBands;i++)
  1482. oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
  1483. }
  1484. if (isTransient)
  1485. st->consec_transient++;
  1486. else
  1487. st->consec_transient=0;
  1488. st->rng = enc->rng;
  1489. /* If there's any room left (can only happen for very high rates),
  1490. it's already filled with zeros */
  1491. ec_enc_done(enc);
  1492. if (st->signalling)
  1493. nbCompressedBytes++;
  1494. RESTORE_STACK;
  1495. if (ec_get_error(enc))
  1496. return OPUS_INTERNAL_ERROR;
  1497. else
  1498. return nbCompressedBytes;
  1499. }
  1500. #ifdef CUSTOM_MODES
  1501. #ifdef FIXED_POINT
  1502. int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  1503. {
  1504. return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
  1505. }
  1506. #ifndef DISABLE_FLOAT_API
  1507. int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  1508. {
  1509. int j, ret, C, N;
  1510. VARDECL(opus_int16, in);
  1511. ALLOC_STACK;
  1512. if (pcm==NULL)
  1513. return OPUS_BAD_ARG;
  1514. C = CHANNELS(st->channels);
  1515. N = frame_size;
  1516. ALLOC(in, C*N, opus_int16);
  1517. for (j=0;j<C*N;j++)
  1518. in[j] = FLOAT2INT16(pcm[j]);
  1519. ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
  1520. #ifdef RESYNTH
  1521. for (j=0;j<C*N;j++)
  1522. ((float*)pcm)[j]=in[j]*(1.f/32768.f);
  1523. #endif
  1524. RESTORE_STACK;
  1525. return ret;
  1526. }
  1527. #endif /* DISABLE_FLOAT_API */
  1528. #else
  1529. int opus_custom_encode(CELTEncoder * restrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  1530. {
  1531. int j, ret, C, N;
  1532. VARDECL(celt_sig, in);
  1533. ALLOC_STACK;
  1534. if (pcm==NULL)
  1535. return OPUS_BAD_ARG;
  1536. C=CHANNELS(st->channels);
  1537. N=frame_size;
  1538. ALLOC(in, C*N, celt_sig);
  1539. for (j=0;j<C*N;j++) {
  1540. in[j] = SCALEOUT(pcm[j]);
  1541. }
  1542. ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
  1543. #ifdef RESYNTH
  1544. for (j=0;j<C*N;j++)
  1545. ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
  1546. #endif
  1547. RESTORE_STACK;
  1548. return ret;
  1549. }
  1550. int opus_custom_encode_float(CELTEncoder * restrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  1551. {
  1552. return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
  1553. }
  1554. #endif
  1555. #endif /* CUSTOM_MODES */
  1556. int opus_custom_encoder_ctl(CELTEncoder * restrict st, int request, ...)
  1557. {
  1558. va_list ap;
  1559. va_start(ap, request);
  1560. switch (request)
  1561. {
  1562. case OPUS_SET_COMPLEXITY_REQUEST:
  1563. {
  1564. int value = va_arg(ap, opus_int32);
  1565. if (value<0 || value>10)
  1566. goto bad_arg;
  1567. st->complexity = value;
  1568. }
  1569. break;
  1570. case CELT_SET_START_BAND_REQUEST:
  1571. {
  1572. opus_int32 value = va_arg(ap, opus_int32);
  1573. if (value<0 || value>=st->mode->nbEBands)
  1574. goto bad_arg;
  1575. st->start = value;
  1576. }
  1577. break;
  1578. case CELT_SET_END_BAND_REQUEST:
  1579. {
  1580. opus_int32 value = va_arg(ap, opus_int32);
  1581. if (value<1 || value>st->mode->nbEBands)
  1582. goto bad_arg;
  1583. st->end = value;
  1584. }
  1585. break;
  1586. case CELT_SET_PREDICTION_REQUEST:
  1587. {
  1588. int value = va_arg(ap, opus_int32);
  1589. if (value<0 || value>2)
  1590. goto bad_arg;
  1591. st->disable_pf = value<=1;
  1592. st->force_intra = value==0;
  1593. }
  1594. break;
  1595. case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
  1596. {
  1597. int value = va_arg(ap, opus_int32);
  1598. if (value<0 || value>100)
  1599. goto bad_arg;
  1600. st->loss_rate = value;
  1601. }
  1602. break;
  1603. case OPUS_SET_VBR_CONSTRAINT_REQUEST:
  1604. {
  1605. opus_int32 value = va_arg(ap, opus_int32);
  1606. st->constrained_vbr = value;
  1607. }
  1608. break;
  1609. case OPUS_SET_VBR_REQUEST:
  1610. {
  1611. opus_int32 value = va_arg(ap, opus_int32);
  1612. st->vbr = value;
  1613. }
  1614. break;
  1615. case OPUS_SET_BITRATE_REQUEST:
  1616. {
  1617. opus_int32 value = va_arg(ap, opus_int32);
  1618. if (value<=500 && value!=OPUS_BITRATE_MAX)
  1619. goto bad_arg;
  1620. value = IMIN(value, 260000*st->channels);
  1621. st->bitrate = value;
  1622. }
  1623. break;
  1624. case CELT_SET_CHANNELS_REQUEST:
  1625. {
  1626. opus_int32 value = va_arg(ap, opus_int32);
  1627. if (value<1 || value>2)
  1628. goto bad_arg;
  1629. st->stream_channels = value;
  1630. }
  1631. break;
  1632. case OPUS_RESET_STATE:
  1633. {
  1634. OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
  1635. opus_custom_encoder_get_size(st->mode, st->channels)-
  1636. ((char*)&st->ENCODER_RESET_START - (char*)st));
  1637. st->vbr_offset = 0;
  1638. st->delayedIntra = 1;
  1639. st->spread_decision = SPREAD_NORMAL;
  1640. st->tonal_average = 256;
  1641. }
  1642. break;
  1643. case CELT_SET_INPUT_CLIPPING_REQUEST:
  1644. {
  1645. opus_int32 value = va_arg(ap, opus_int32);
  1646. st->clip = value;
  1647. }
  1648. break;
  1649. #ifdef OPUS_BUILD
  1650. case CELT_SET_SIGNALLING_REQUEST:
  1651. {
  1652. opus_int32 value = va_arg(ap, opus_int32);
  1653. st->signalling = value;
  1654. }
  1655. break;
  1656. case CELT_GET_MODE_REQUEST:
  1657. {
  1658. const CELTMode ** value = va_arg(ap, const CELTMode**);
  1659. if (value==0)
  1660. goto bad_arg;
  1661. *value=st->mode;
  1662. }
  1663. break;
  1664. case OPUS_GET_FINAL_RANGE_REQUEST:
  1665. {
  1666. opus_uint32 * value = va_arg(ap, opus_uint32 *);
  1667. if (value==0)
  1668. goto bad_arg;
  1669. *value=st->rng;
  1670. }
  1671. break;
  1672. #endif
  1673. default:
  1674. goto bad_request;
  1675. }
  1676. va_end(ap);
  1677. return OPUS_OK;
  1678. bad_arg:
  1679. va_end(ap);
  1680. return OPUS_BAD_ARG;
  1681. bad_request:
  1682. va_end(ap);
  1683. return OPUS_UNIMPLEMENTED;
  1684. }
  1685. /**********************************************************************/
  1686. /* */
  1687. /* DECODER */
  1688. /* */
  1689. /**********************************************************************/
  1690. #define DECODE_BUFFER_SIZE 2048
  1691. /** Decoder state
  1692. @brief Decoder state
  1693. */
  1694. struct OpusCustomDecoder {
  1695. const OpusCustomMode *mode;
  1696. int overlap;
  1697. int channels;
  1698. int stream_channels;
  1699. int downsample;
  1700. int start, end;
  1701. int signalling;
  1702. /* Everything beyond this point gets cleared on a reset */
  1703. #define DECODER_RESET_START rng
  1704. opus_uint32 rng;
  1705. int error;
  1706. int last_pitch_index;
  1707. int loss_count;
  1708. int postfilter_period;
  1709. int postfilter_period_old;
  1710. opus_val16 postfilter_gain;
  1711. opus_val16 postfilter_gain_old;
  1712. int postfilter_tapset;
  1713. int postfilter_tapset_old;
  1714. celt_sig preemph_memD[2];
  1715. celt_sig _decode_mem[1]; /* Size = channels*(DECODE_BUFFER_SIZE+mode->overlap) */
  1716. /* opus_val16 lpc[], Size = channels*LPC_ORDER */
  1717. /* opus_val16 oldEBands[], Size = 2*mode->nbEBands */
  1718. /* opus_val16 oldLogE[], Size = 2*mode->nbEBands */
  1719. /* opus_val16 oldLogE2[], Size = 2*mode->nbEBands */
  1720. /* opus_val16 backgroundLogE[], Size = 2*mode->nbEBands */
  1721. };
  1722. int celt_decoder_get_size(int channels)
  1723. {
  1724. const CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
  1725. return opus_custom_decoder_get_size(mode, channels);
  1726. }
  1727. int opus_custom_decoder_get_size(const CELTMode *mode, int channels)
  1728. {
  1729. int size = sizeof(struct CELTDecoder)
  1730. + (channels*(DECODE_BUFFER_SIZE+mode->overlap)-1)*sizeof(celt_sig)
  1731. + channels*LPC_ORDER*sizeof(opus_val16)
  1732. + 4*2*mode->nbEBands*sizeof(opus_val16);
  1733. return size;
  1734. }
  1735. #ifdef CUSTOM_MODES
  1736. CELTDecoder *opus_custom_decoder_create(const CELTMode *mode, int channels, int *error)
  1737. {
  1738. int ret;
  1739. CELTDecoder *st = (CELTDecoder *)opus_alloc(opus_custom_decoder_get_size(mode, channels));
  1740. ret = opus_custom_decoder_init(st, mode, channels);
  1741. if (ret != OPUS_OK)
  1742. {
  1743. opus_custom_decoder_destroy(st);
  1744. st = NULL;
  1745. }
  1746. if (error)
  1747. *error = ret;
  1748. return st;
  1749. }
  1750. #endif /* CUSTOM_MODES */
  1751. int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels)
  1752. {
  1753. int ret;
  1754. ret = opus_custom_decoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
  1755. if (ret != OPUS_OK)
  1756. return ret;
  1757. st->downsample = resampling_factor(sampling_rate);
  1758. if (st->downsample==0)
  1759. return OPUS_BAD_ARG;
  1760. else
  1761. return OPUS_OK;
  1762. }
  1763. int opus_custom_decoder_init(CELTDecoder *st, const CELTMode *mode, int channels)
  1764. {
  1765. if (channels < 0 || channels > 2)
  1766. return OPUS_BAD_ARG;
  1767. if (st==NULL)
  1768. return OPUS_ALLOC_FAIL;
  1769. OPUS_CLEAR((char*)st, opus_custom_decoder_get_size(mode, channels));
  1770. st->mode = mode;
  1771. st->overlap = mode->overlap;
  1772. st->stream_channels = st->channels = channels;
  1773. st->downsample = 1;
  1774. st->start = 0;
  1775. st->end = st->mode->effEBands;
  1776. st->signalling = 1;
  1777. st->loss_count = 0;
  1778. return OPUS_OK;
  1779. }
  1780. #ifdef CUSTOM_MODES
  1781. void opus_custom_decoder_destroy(CELTDecoder *st)
  1782. {
  1783. opus_free(st);
  1784. }
  1785. #endif /* CUSTOM_MODES */
  1786. static void celt_decode_lost(CELTDecoder * restrict st, opus_val16 * restrict pcm, int N, int LM)
  1787. {
  1788. int c;
  1789. int pitch_index;
  1790. int overlap = st->mode->overlap;
  1791. opus_val16 fade = Q15ONE;
  1792. int i, len;
  1793. const int C = CHANNELS(st->channels);
  1794. int offset;
  1795. celt_sig *out_mem[2];
  1796. celt_sig *decode_mem[2];
  1797. celt_sig *overlap_mem[2];
  1798. opus_val16 *lpc;
  1799. opus_val32 *out_syn[2];
  1800. opus_val16 *oldBandE, *oldLogE2, *backgroundLogE;
  1801. int plc=1;
  1802. SAVE_STACK;
  1803. c=0; do {
  1804. decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
  1805. out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
  1806. overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
  1807. } while (++c<C);
  1808. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*C);
  1809. oldBandE = lpc+C*LPC_ORDER;
  1810. oldLogE2 = oldBandE + C*st->mode->nbEBands;
  1811. backgroundLogE = oldLogE2 + C*st->mode->nbEBands;
  1812. out_syn[0] = out_mem[0]+MAX_PERIOD-N;
  1813. if (C==2)
  1814. out_syn[1] = out_mem[1]+MAX_PERIOD-N;
  1815. len = N+st->mode->overlap;
  1816. if (st->loss_count >= 5)
  1817. {
  1818. VARDECL(celt_sig, freq);
  1819. VARDECL(celt_norm, X);
  1820. VARDECL(celt_ener, bandE);
  1821. opus_uint32 seed;
  1822. int effEnd;
  1823. effEnd = st->end;
  1824. if (effEnd > st->mode->effEBands)
  1825. effEnd = st->mode->effEBands;
  1826. ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
  1827. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  1828. ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
  1829. log2Amp(st->mode, st->start, st->end, bandE, backgroundLogE, C);
  1830. seed = st->rng;
  1831. for (c=0;c<C;c++)
  1832. {
  1833. for (i=0;i<(st->mode->eBands[st->start]<<LM);i++)
  1834. X[c*N+i] = 0;
  1835. for (i=0;i<st->mode->effEBands;i++)
  1836. {
  1837. int j;
  1838. int boffs;
  1839. int blen;
  1840. boffs = N*c+(st->mode->eBands[i]<<LM);
  1841. blen = (st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
  1842. for (j=0;j<blen;j++)
  1843. {
  1844. seed = celt_lcg_rand(seed);
  1845. X[boffs+j] = (celt_norm)((opus_int32)seed>>20);
  1846. }
  1847. renormalise_vector(X+boffs, blen, Q15ONE);
  1848. }
  1849. for (i=(st->mode->eBands[st->end]<<LM);i<N;i++)
  1850. X[c*N+i] = 0;
  1851. }
  1852. st->rng = seed;
  1853. denormalise_bands(st->mode, X, freq, bandE, st->mode->effEBands, C, 1<<LM);
  1854. c=0; do
  1855. for (i=0;i<st->mode->eBands[st->start]<<LM;i++)
  1856. freq[c*N+i] = 0;
  1857. while (++c<C);
  1858. c=0; do {
  1859. int bound = st->mode->eBands[effEnd]<<LM;
  1860. if (st->downsample!=1)
  1861. bound = IMIN(bound, N/st->downsample);
  1862. for (i=bound;i<N;i++)
  1863. freq[c*N+i] = 0;
  1864. } while (++c<C);
  1865. compute_inv_mdcts(st->mode, 0, freq, out_syn, overlap_mem, C, LM);
  1866. plc = 0;
  1867. } else if (st->loss_count == 0)
  1868. {
  1869. opus_val16 pitch_buf[DECODE_BUFFER_SIZE>>1];
  1870. /* Corresponds to a min pitch of 67 Hz. It's possible to save CPU in this
  1871. search by using only part of the decode buffer */
  1872. int poffset = 720;
  1873. pitch_downsample(decode_mem, pitch_buf, DECODE_BUFFER_SIZE, C);
  1874. /* Max pitch is 100 samples (480 Hz) */
  1875. pitch_search(pitch_buf+((poffset)>>1), pitch_buf, DECODE_BUFFER_SIZE-poffset,
  1876. poffset-100, &pitch_index);
  1877. pitch_index = poffset-pitch_index;
  1878. st->last_pitch_index = pitch_index;
  1879. } else {
  1880. pitch_index = st->last_pitch_index;
  1881. fade = QCONST16(.8f,15);
  1882. }
  1883. if (plc)
  1884. {
  1885. c=0; do {
  1886. VARDECL(opus_val32, e);
  1887. opus_val16 exc[MAX_PERIOD];
  1888. opus_val32 ac[LPC_ORDER+1];
  1889. opus_val16 decay = 1;
  1890. opus_val32 S1=0;
  1891. opus_val16 mem[LPC_ORDER]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1892. ALLOC(e, MAX_PERIOD+2*st->mode->overlap, opus_val32);
  1893. offset = MAX_PERIOD-pitch_index;
  1894. for (i=0;i<MAX_PERIOD;i++)
  1895. exc[i] = ROUND16(out_mem[c][i], SIG_SHIFT);
  1896. if (st->loss_count == 0)
  1897. {
  1898. _celt_autocorr(exc, ac, st->mode->window, st->mode->overlap,
  1899. LPC_ORDER, MAX_PERIOD);
  1900. /* Noise floor -40 dB */
  1901. #ifdef FIXED_POINT
  1902. ac[0] += SHR32(ac[0],13);
  1903. #else
  1904. ac[0] *= 1.0001f;
  1905. #endif
  1906. /* Lag windowing */
  1907. for (i=1;i<=LPC_ORDER;i++)
  1908. {
  1909. /*ac[i] *= exp(-.5*(2*M_PI*.002*i)*(2*M_PI*.002*i));*/
  1910. #ifdef FIXED_POINT
  1911. ac[i] -= MULT16_32_Q15(2*i*i, ac[i]);
  1912. #else
  1913. ac[i] -= ac[i]*(.008f*i)*(.008f*i);
  1914. #endif
  1915. }
  1916. _celt_lpc(lpc+c*LPC_ORDER, ac, LPC_ORDER);
  1917. }
  1918. for (i=0;i<LPC_ORDER;i++)
  1919. mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
  1920. celt_fir(exc, lpc+c*LPC_ORDER, exc, MAX_PERIOD, LPC_ORDER, mem);
  1921. /*for (i=0;i<MAX_PERIOD;i++)printf("%d ", exc[i]); printf("\n");*/
  1922. /* Check if the waveform is decaying (and if so how fast) */
  1923. {
  1924. opus_val32 E1=1, E2=1;
  1925. int period;
  1926. if (pitch_index <= MAX_PERIOD/2)
  1927. period = pitch_index;
  1928. else
  1929. period = MAX_PERIOD/2;
  1930. for (i=0;i<period;i++)
  1931. {
  1932. E1 += SHR32(MULT16_16(exc[MAX_PERIOD-period+i],exc[MAX_PERIOD-period+i]),8);
  1933. E2 += SHR32(MULT16_16(exc[MAX_PERIOD-2*period+i],exc[MAX_PERIOD-2*period+i]),8);
  1934. }
  1935. if (E1 > E2)
  1936. E1 = E2;
  1937. decay = celt_sqrt(frac_div32(SHR(E1,1),E2));
  1938. }
  1939. /* Copy excitation, taking decay into account */
  1940. for (i=0;i<len+st->mode->overlap;i++)
  1941. {
  1942. opus_val16 tmp;
  1943. if (offset+i >= MAX_PERIOD)
  1944. {
  1945. offset -= pitch_index;
  1946. decay = MULT16_16_Q15(decay, decay);
  1947. }
  1948. e[i] = SHL32(EXTEND32(MULT16_16_Q15(decay, exc[offset+i])), SIG_SHIFT);
  1949. tmp = ROUND16(out_mem[c][offset+i],SIG_SHIFT);
  1950. S1 += SHR32(MULT16_16(tmp,tmp),8);
  1951. }
  1952. for (i=0;i<LPC_ORDER;i++)
  1953. mem[i] = ROUND16(out_mem[c][MAX_PERIOD-1-i], SIG_SHIFT);
  1954. for (i=0;i<len+st->mode->overlap;i++)
  1955. e[i] = MULT16_32_Q15(fade, e[i]);
  1956. celt_iir(e, lpc+c*LPC_ORDER, e, len+st->mode->overlap, LPC_ORDER, mem);
  1957. {
  1958. opus_val32 S2=0;
  1959. for (i=0;i<len+overlap;i++)
  1960. {
  1961. opus_val16 tmp = ROUND16(e[i],SIG_SHIFT);
  1962. S2 += SHR32(MULT16_16(tmp,tmp),8);
  1963. }
  1964. /* This checks for an "explosion" in the synthesis */
  1965. #ifdef FIXED_POINT
  1966. if (!(S1 > SHR32(S2,2)))
  1967. #else
  1968. /* Float test is written this way to catch NaNs at the same time */
  1969. if (!(S1 > 0.2f*S2))
  1970. #endif
  1971. {
  1972. for (i=0;i<len+overlap;i++)
  1973. e[i] = 0;
  1974. } else if (S1 < S2)
  1975. {
  1976. opus_val16 ratio = celt_sqrt(frac_div32(SHR32(S1,1)+1,S2+1));
  1977. for (i=0;i<len+overlap;i++)
  1978. e[i] = MULT16_32_Q15(ratio, e[i]);
  1979. }
  1980. }
  1981. /* Apply post-filter to the MDCT overlap of the previous frame */
  1982. comb_filter(out_mem[c]+MAX_PERIOD, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
  1983. st->postfilter_gain, st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
  1984. NULL, 0);
  1985. for (i=0;i<MAX_PERIOD+st->mode->overlap-N;i++)
  1986. out_mem[c][i] = out_mem[c][N+i];
  1987. /* Apply TDAC to the concealed audio so that it blends with the
  1988. previous and next frames */
  1989. for (i=0;i<overlap/2;i++)
  1990. {
  1991. opus_val32 tmp;
  1992. tmp = MULT16_32_Q15(st->mode->window[i], e[N+overlap-1-i]) +
  1993. MULT16_32_Q15(st->mode->window[overlap-i-1], e[N+i ]);
  1994. out_mem[c][MAX_PERIOD+i] = MULT16_32_Q15(st->mode->window[overlap-i-1], tmp);
  1995. out_mem[c][MAX_PERIOD+overlap-i-1] = MULT16_32_Q15(st->mode->window[i], tmp);
  1996. }
  1997. for (i=0;i<N;i++)
  1998. out_mem[c][MAX_PERIOD-N+i] = e[i];
  1999. /* Apply pre-filter to the MDCT overlap for the next frame (post-filter will be applied then) */
  2000. comb_filter(e, out_mem[c]+MAX_PERIOD, st->postfilter_period, st->postfilter_period, st->overlap,
  2001. -st->postfilter_gain, -st->postfilter_gain, st->postfilter_tapset, st->postfilter_tapset,
  2002. NULL, 0);
  2003. for (i=0;i<overlap;i++)
  2004. out_mem[c][MAX_PERIOD+i] = e[i];
  2005. } while (++c<C);
  2006. }
  2007. deemphasis(out_syn, pcm, N, C, st->downsample, st->mode->preemph, st->preemph_memD);
  2008. st->loss_count++;
  2009. RESTORE_STACK;
  2010. }
  2011. int celt_decode_with_ec(CELTDecoder * restrict st, const unsigned char *data, int len, opus_val16 * restrict pcm, int frame_size, ec_dec *dec)
  2012. {
  2013. int c, i, N;
  2014. int spread_decision;
  2015. opus_int32 bits;
  2016. ec_dec _dec;
  2017. VARDECL(celt_sig, freq);
  2018. VARDECL(celt_norm, X);
  2019. VARDECL(celt_ener, bandE);
  2020. VARDECL(int, fine_quant);
  2021. VARDECL(int, pulses);
  2022. VARDECL(int, cap);
  2023. VARDECL(int, offsets);
  2024. VARDECL(int, fine_priority);
  2025. VARDECL(int, tf_res);
  2026. VARDECL(unsigned char, collapse_masks);
  2027. celt_sig *out_mem[2];
  2028. celt_sig *decode_mem[2];
  2029. celt_sig *overlap_mem[2];
  2030. celt_sig *out_syn[2];
  2031. opus_val16 *lpc;
  2032. opus_val16 *oldBandE, *oldLogE, *oldLogE2, *backgroundLogE;
  2033. int shortBlocks;
  2034. int isTransient;
  2035. int intra_ener;
  2036. const int CC = CHANNELS(st->channels);
  2037. int LM, M;
  2038. int effEnd;
  2039. int codedBands;
  2040. int alloc_trim;
  2041. int postfilter_pitch;
  2042. opus_val16 postfilter_gain;
  2043. int intensity=0;
  2044. int dual_stereo=0;
  2045. opus_int32 total_bits;
  2046. opus_int32 balance;
  2047. opus_int32 tell;
  2048. int dynalloc_logp;
  2049. int postfilter_tapset;
  2050. int anti_collapse_rsv;
  2051. int anti_collapse_on=0;
  2052. int silence;
  2053. int C = CHANNELS(st->stream_channels);
  2054. ALLOC_STACK;
  2055. frame_size *= st->downsample;
  2056. c=0; do {
  2057. decode_mem[c] = st->_decode_mem + c*(DECODE_BUFFER_SIZE+st->overlap);
  2058. out_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE-MAX_PERIOD;
  2059. overlap_mem[c] = decode_mem[c]+DECODE_BUFFER_SIZE;
  2060. } while (++c<CC);
  2061. lpc = (opus_val16*)(st->_decode_mem+(DECODE_BUFFER_SIZE+st->overlap)*CC);
  2062. oldBandE = lpc+LPC_ORDER;
  2063. oldLogE = oldBandE + 2*st->mode->nbEBands;
  2064. oldLogE2 = oldLogE + 2*st->mode->nbEBands;
  2065. backgroundLogE = oldLogE2 + 2*st->mode->nbEBands;
  2066. if (st->signalling && data!=NULL)
  2067. {
  2068. int data0=data[0];
  2069. /* Convert "standard mode" to Opus header */
  2070. if (st->mode->Fs==48000 && st->mode->shortMdctSize==120)
  2071. {
  2072. data0 = fromOpus(data0);
  2073. if (data0<0)
  2074. return OPUS_INVALID_PACKET;
  2075. }
  2076. st->end = IMAX(1, st->mode->effEBands-2*(data0>>5));
  2077. LM = (data0>>3)&0x3;
  2078. C = 1 + ((data0>>2)&0x1);
  2079. data++;
  2080. len--;
  2081. if (LM>st->mode->maxLM)
  2082. return OPUS_INVALID_PACKET;
  2083. if (frame_size < st->mode->shortMdctSize<<LM)
  2084. return OPUS_BUFFER_TOO_SMALL;
  2085. else
  2086. frame_size = st->mode->shortMdctSize<<LM;
  2087. } else {
  2088. for (LM=0;LM<=st->mode->maxLM;LM++)
  2089. if (st->mode->shortMdctSize<<LM==frame_size)
  2090. break;
  2091. if (LM>st->mode->maxLM)
  2092. return OPUS_BAD_ARG;
  2093. }
  2094. M=1<<LM;
  2095. if (len<0 || len>1275 || pcm==NULL)
  2096. return OPUS_BAD_ARG;
  2097. N = M*st->mode->shortMdctSize;
  2098. effEnd = st->end;
  2099. if (effEnd > st->mode->effEBands)
  2100. effEnd = st->mode->effEBands;
  2101. ALLOC(freq, IMAX(CC,C)*N, celt_sig); /**< Interleaved signal MDCTs */
  2102. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  2103. ALLOC(bandE, st->mode->nbEBands*C, celt_ener);
  2104. c=0; do
  2105. for (i=0;i<M*st->mode->eBands[st->start];i++)
  2106. X[c*N+i] = 0;
  2107. while (++c<C);
  2108. c=0; do
  2109. for (i=M*st->mode->eBands[effEnd];i<N;i++)
  2110. X[c*N+i] = 0;
  2111. while (++c<C);
  2112. if (data == NULL || len<=1)
  2113. {
  2114. celt_decode_lost(st, pcm, N, LM);
  2115. RESTORE_STACK;
  2116. return frame_size/st->downsample;
  2117. }
  2118. if (dec == NULL)
  2119. {
  2120. ec_dec_init(&_dec,(unsigned char*)data,len);
  2121. dec = &_dec;
  2122. }
  2123. if (C<CC)
  2124. {
  2125. for (i=0;i<st->mode->nbEBands;i++)
  2126. oldBandE[i]=MAX16(oldBandE[i],oldBandE[st->mode->nbEBands+i]);
  2127. }
  2128. total_bits = len*8;
  2129. tell = ec_tell(dec);
  2130. if (tell >= total_bits)
  2131. silence = 1;
  2132. else if (tell==1)
  2133. silence = ec_dec_bit_logp(dec, 15);
  2134. else
  2135. silence = 0;
  2136. if (silence)
  2137. {
  2138. /* Pretend we've read all the remaining bits */
  2139. tell = len*8;
  2140. dec->nbits_total+=tell-ec_tell(dec);
  2141. }
  2142. postfilter_gain = 0;
  2143. postfilter_pitch = 0;
  2144. postfilter_tapset = 0;
  2145. if (st->start==0 && tell+16 <= total_bits)
  2146. {
  2147. if(ec_dec_bit_logp(dec, 1))
  2148. {
  2149. int qg, octave;
  2150. octave = ec_dec_uint(dec, 6);
  2151. postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave)-1;
  2152. qg = ec_dec_bits(dec, 3);
  2153. if (ec_tell(dec)+2<=total_bits)
  2154. postfilter_tapset = ec_dec_icdf(dec, tapset_icdf, 2);
  2155. postfilter_gain = QCONST16(.09375f,15)*(qg+1);
  2156. }
  2157. tell = ec_tell(dec);
  2158. }
  2159. if (LM > 0 && tell+3 <= total_bits)
  2160. {
  2161. isTransient = ec_dec_bit_logp(dec, 3);
  2162. tell = ec_tell(dec);
  2163. }
  2164. else
  2165. isTransient = 0;
  2166. if (isTransient)
  2167. shortBlocks = M;
  2168. else
  2169. shortBlocks = 0;
  2170. /* Decode the global flags (first symbols in the stream) */
  2171. intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
  2172. /* Get band energies */
  2173. unquant_coarse_energy(st->mode, st->start, st->end, oldBandE,
  2174. intra_ener, dec, C, LM);
  2175. ALLOC(tf_res, st->mode->nbEBands, int);
  2176. tf_decode(st->start, st->end, isTransient, tf_res, LM, dec);
  2177. tell = ec_tell(dec);
  2178. spread_decision = SPREAD_NORMAL;
  2179. if (tell+4 <= total_bits)
  2180. spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
  2181. ALLOC(pulses, st->mode->nbEBands, int);
  2182. ALLOC(cap, st->mode->nbEBands, int);
  2183. ALLOC(offsets, st->mode->nbEBands, int);
  2184. ALLOC(fine_priority, st->mode->nbEBands, int);
  2185. init_caps(st->mode,cap,LM,C);
  2186. dynalloc_logp = 6;
  2187. total_bits<<=BITRES;
  2188. tell = ec_tell_frac(dec);
  2189. for (i=st->start;i<st->end;i++)
  2190. {
  2191. int width, quanta;
  2192. int dynalloc_loop_logp;
  2193. int boost;
  2194. width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
  2195. /* quanta is 6 bits, but no more than 1 bit/sample
  2196. and no less than 1/8 bit/sample */
  2197. quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
  2198. dynalloc_loop_logp = dynalloc_logp;
  2199. boost = 0;
  2200. while (tell+(dynalloc_loop_logp<<BITRES) < total_bits && boost < cap[i])
  2201. {
  2202. int flag;
  2203. flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
  2204. tell = ec_tell_frac(dec);
  2205. if (!flag)
  2206. break;
  2207. boost += quanta;
  2208. total_bits -= quanta;
  2209. dynalloc_loop_logp = 1;
  2210. }
  2211. offsets[i] = boost;
  2212. /* Making dynalloc more likely */
  2213. if (boost>0)
  2214. dynalloc_logp = IMAX(2, dynalloc_logp-1);
  2215. }
  2216. ALLOC(fine_quant, st->mode->nbEBands, int);
  2217. alloc_trim = tell+(6<<BITRES) <= total_bits ?
  2218. ec_dec_icdf(dec, trim_icdf, 7) : 5;
  2219. bits = (((opus_int32)len*8)<<BITRES) - ec_tell_frac(dec) - 1;
  2220. anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
  2221. bits -= anti_collapse_rsv;
  2222. codedBands = compute_allocation(st->mode, st->start, st->end, offsets, cap,
  2223. alloc_trim, &intensity, &dual_stereo, bits, &balance, pulses,
  2224. fine_quant, fine_priority, C, LM, dec, 0, 0);
  2225. unquant_fine_energy(st->mode, st->start, st->end, oldBandE, fine_quant, dec, C);
  2226. /* Decode fixed codebook */
  2227. ALLOC(collapse_masks, C*st->mode->nbEBands, unsigned char);
  2228. quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
  2229. NULL, pulses, shortBlocks, spread_decision, dual_stereo, intensity, tf_res,
  2230. len*(8<<BITRES)-anti_collapse_rsv, balance, dec, LM, codedBands, &st->rng);
  2231. if (anti_collapse_rsv > 0)
  2232. {
  2233. anti_collapse_on = ec_dec_bits(dec, 1);
  2234. }
  2235. unquant_energy_finalise(st->mode, st->start, st->end, oldBandE,
  2236. fine_quant, fine_priority, len*8-ec_tell(dec), dec, C);
  2237. if (anti_collapse_on)
  2238. anti_collapse(st->mode, X, collapse_masks, LM, C, CC, N,
  2239. st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
  2240. log2Amp(st->mode, st->start, st->end, bandE, oldBandE, C);
  2241. if (silence)
  2242. {
  2243. for (i=0;i<C*st->mode->nbEBands;i++)
  2244. {
  2245. bandE[i] = 0;
  2246. oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
  2247. }
  2248. }
  2249. /* Synthesis */
  2250. denormalise_bands(st->mode, X, freq, bandE, effEnd, C, M);
  2251. OPUS_MOVE(decode_mem[0], decode_mem[0]+N, DECODE_BUFFER_SIZE-N);
  2252. if (CC==2)
  2253. OPUS_MOVE(decode_mem[1], decode_mem[1]+N, DECODE_BUFFER_SIZE-N);
  2254. c=0; do
  2255. for (i=0;i<M*st->mode->eBands[st->start];i++)
  2256. freq[c*N+i] = 0;
  2257. while (++c<C);
  2258. c=0; do {
  2259. int bound = M*st->mode->eBands[effEnd];
  2260. if (st->downsample!=1)
  2261. bound = IMIN(bound, N/st->downsample);
  2262. for (i=bound;i<N;i++)
  2263. freq[c*N+i] = 0;
  2264. } while (++c<C);
  2265. out_syn[0] = out_mem[0]+MAX_PERIOD-N;
  2266. if (CC==2)
  2267. out_syn[1] = out_mem[1]+MAX_PERIOD-N;
  2268. if (CC==2&&C==1)
  2269. {
  2270. for (i=0;i<N;i++)
  2271. freq[N+i] = freq[i];
  2272. }
  2273. if (CC==1&&C==2)
  2274. {
  2275. for (i=0;i<N;i++)
  2276. freq[i] = HALF32(ADD32(freq[i],freq[N+i]));
  2277. }
  2278. /* Compute inverse MDCTs */
  2279. compute_inv_mdcts(st->mode, shortBlocks, freq, out_syn, overlap_mem, CC, LM);
  2280. c=0; do {
  2281. st->postfilter_period=IMAX(st->postfilter_period, COMBFILTER_MINPERIOD);
  2282. st->postfilter_period_old=IMAX(st->postfilter_period_old, COMBFILTER_MINPERIOD);
  2283. comb_filter(out_syn[c], out_syn[c], st->postfilter_period_old, st->postfilter_period, st->mode->shortMdctSize,
  2284. st->postfilter_gain_old, st->postfilter_gain, st->postfilter_tapset_old, st->postfilter_tapset,
  2285. st->mode->window, st->overlap);
  2286. if (LM!=0)
  2287. comb_filter(out_syn[c]+st->mode->shortMdctSize, out_syn[c]+st->mode->shortMdctSize, st->postfilter_period, postfilter_pitch, N-st->mode->shortMdctSize,
  2288. st->postfilter_gain, postfilter_gain, st->postfilter_tapset, postfilter_tapset,
  2289. st->mode->window, st->mode->overlap);
  2290. } while (++c<CC);
  2291. st->postfilter_period_old = st->postfilter_period;
  2292. st->postfilter_gain_old = st->postfilter_gain;
  2293. st->postfilter_tapset_old = st->postfilter_tapset;
  2294. st->postfilter_period = postfilter_pitch;
  2295. st->postfilter_gain = postfilter_gain;
  2296. st->postfilter_tapset = postfilter_tapset;
  2297. if (LM!=0)
  2298. {
  2299. st->postfilter_period_old = st->postfilter_period;
  2300. st->postfilter_gain_old = st->postfilter_gain;
  2301. st->postfilter_tapset_old = st->postfilter_tapset;
  2302. }
  2303. if (C==1) {
  2304. for (i=0;i<st->mode->nbEBands;i++)
  2305. oldBandE[st->mode->nbEBands+i]=oldBandE[i];
  2306. }
  2307. /* In case start or end were to change */
  2308. c=0; do
  2309. {
  2310. for (i=0;i<st->start;i++)
  2311. oldBandE[c*st->mode->nbEBands+i]=0;
  2312. for (i=st->end;i<st->mode->nbEBands;i++)
  2313. oldBandE[c*st->mode->nbEBands+i]=0;
  2314. } while (++c<2);
  2315. if (!isTransient)
  2316. {
  2317. for (i=0;i<2*st->mode->nbEBands;i++)
  2318. oldLogE2[i] = oldLogE[i];
  2319. for (i=0;i<2*st->mode->nbEBands;i++)
  2320. oldLogE[i] = oldBandE[i];
  2321. for (i=0;i<2*st->mode->nbEBands;i++)
  2322. backgroundLogE[i] = MIN16(backgroundLogE[i] + M*QCONST16(0.001f,DB_SHIFT), oldBandE[i]);
  2323. } else {
  2324. for (i=0;i<2*st->mode->nbEBands;i++)
  2325. oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
  2326. }
  2327. st->rng = dec->rng;
  2328. deemphasis(out_syn, pcm, N, CC, st->downsample, st->mode->preemph, st->preemph_memD);
  2329. st->loss_count = 0;
  2330. RESTORE_STACK;
  2331. if (ec_tell(dec) > 8*len)
  2332. return OPUS_INTERNAL_ERROR;
  2333. if(ec_get_error(dec))
  2334. st->error = 1;
  2335. return frame_size/st->downsample;
  2336. }
  2337. #ifdef CUSTOM_MODES
  2338. #ifdef FIXED_POINT
  2339. int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
  2340. {
  2341. return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
  2342. }
  2343. #ifndef DISABLE_FLOAT_API
  2344. int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
  2345. {
  2346. int j, ret, C, N;
  2347. VARDECL(opus_int16, out);
  2348. ALLOC_STACK;
  2349. if (pcm==NULL)
  2350. return OPUS_BAD_ARG;
  2351. C = CHANNELS(st->channels);
  2352. N = frame_size;
  2353. ALLOC(out, C*N, opus_int16);
  2354. ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
  2355. if (ret>0)
  2356. for (j=0;j<C*ret;j++)
  2357. pcm[j]=out[j]*(1.f/32768.f);
  2358. RESTORE_STACK;
  2359. return ret;
  2360. }
  2361. #endif /* DISABLE_FLOAT_API */
  2362. #else
  2363. int opus_custom_decode_float(CELTDecoder * restrict st, const unsigned char *data, int len, float * restrict pcm, int frame_size)
  2364. {
  2365. return celt_decode_with_ec(st, data, len, pcm, frame_size, NULL);
  2366. }
  2367. int opus_custom_decode(CELTDecoder * restrict st, const unsigned char *data, int len, opus_int16 * restrict pcm, int frame_size)
  2368. {
  2369. int j, ret, C, N;
  2370. VARDECL(celt_sig, out);
  2371. ALLOC_STACK;
  2372. if (pcm==NULL)
  2373. return OPUS_BAD_ARG;
  2374. C = CHANNELS(st->channels);
  2375. N = frame_size;
  2376. ALLOC(out, C*N, celt_sig);
  2377. ret=celt_decode_with_ec(st, data, len, out, frame_size, NULL);
  2378. if (ret>0)
  2379. for (j=0;j<C*ret;j++)
  2380. pcm[j] = FLOAT2INT16 (out[j]);
  2381. RESTORE_STACK;
  2382. return ret;
  2383. }
  2384. #endif
  2385. #endif /* CUSTOM_MODES */
  2386. int opus_custom_decoder_ctl(CELTDecoder * restrict st, int request, ...)
  2387. {
  2388. va_list ap;
  2389. va_start(ap, request);
  2390. switch (request)
  2391. {
  2392. case CELT_SET_START_BAND_REQUEST:
  2393. {
  2394. opus_int32 value = va_arg(ap, opus_int32);
  2395. if (value<0 || value>=st->mode->nbEBands)
  2396. goto bad_arg;
  2397. st->start = value;
  2398. }
  2399. break;
  2400. case CELT_SET_END_BAND_REQUEST:
  2401. {
  2402. opus_int32 value = va_arg(ap, opus_int32);
  2403. if (value<1 || value>st->mode->nbEBands)
  2404. goto bad_arg;
  2405. st->end = value;
  2406. }
  2407. break;
  2408. case CELT_SET_CHANNELS_REQUEST:
  2409. {
  2410. opus_int32 value = va_arg(ap, opus_int32);
  2411. if (value<1 || value>2)
  2412. goto bad_arg;
  2413. st->stream_channels = value;
  2414. }
  2415. break;
  2416. case CELT_GET_AND_CLEAR_ERROR_REQUEST:
  2417. {
  2418. int *value = va_arg(ap, opus_int32*);
  2419. if (value==NULL)
  2420. goto bad_arg;
  2421. *value=st->error;
  2422. st->error = 0;
  2423. }
  2424. break;
  2425. case OPUS_GET_LOOKAHEAD_REQUEST:
  2426. {
  2427. int *value = va_arg(ap, opus_int32*);
  2428. if (value==NULL)
  2429. goto bad_arg;
  2430. *value = st->overlap/st->downsample;
  2431. }
  2432. break;
  2433. case OPUS_RESET_STATE:
  2434. {
  2435. OPUS_CLEAR((char*)&st->DECODER_RESET_START,
  2436. opus_custom_decoder_get_size(st->mode, st->channels)-
  2437. ((char*)&st->DECODER_RESET_START - (char*)st));
  2438. }
  2439. break;
  2440. case OPUS_GET_PITCH_REQUEST:
  2441. {
  2442. int *value = va_arg(ap, opus_int32*);
  2443. if (value==NULL)
  2444. goto bad_arg;
  2445. *value = st->postfilter_period;
  2446. }
  2447. break;
  2448. #ifdef OPUS_BUILD
  2449. case CELT_GET_MODE_REQUEST:
  2450. {
  2451. const CELTMode ** value = va_arg(ap, const CELTMode**);
  2452. if (value==0)
  2453. goto bad_arg;
  2454. *value=st->mode;
  2455. }
  2456. break;
  2457. case CELT_SET_SIGNALLING_REQUEST:
  2458. {
  2459. opus_int32 value = va_arg(ap, opus_int32);
  2460. st->signalling = value;
  2461. }
  2462. break;
  2463. case OPUS_GET_FINAL_RANGE_REQUEST:
  2464. {
  2465. opus_uint32 * value = va_arg(ap, opus_uint32 *);
  2466. if (value==0)
  2467. goto bad_arg;
  2468. *value=st->rng;
  2469. }
  2470. break;
  2471. #endif
  2472. default:
  2473. goto bad_request;
  2474. }
  2475. va_end(ap);
  2476. return OPUS_OK;
  2477. bad_arg:
  2478. va_end(ap);
  2479. return OPUS_BAD_ARG;
  2480. bad_request:
  2481. va_end(ap);
  2482. return OPUS_UNIMPLEMENTED;
  2483. }
  2484. const char *opus_strerror(int error)
  2485. {
  2486. static const char *error_strings[8] = {
  2487. "success",
  2488. "invalid argument",
  2489. "buffer too small",
  2490. "internal error",
  2491. "corrupted stream",
  2492. "request not implemented",
  2493. "invalid state",
  2494. "memory allocation failed"
  2495. };
  2496. if (error > 0 || error < -7)
  2497. return "unknown error";
  2498. else
  2499. return error_strings[-error];
  2500. }
  2501. const char *opus_get_version_string(void)
  2502. {
  2503. return "libopus " OPUS_VERSION
  2504. #ifdef FUZZING
  2505. "-fuzzing"
  2506. #endif
  2507. ;
  2508. }