celt.c 83 KB

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