celt_encoder.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607
  1. /* Copyright (c) 2007-2008 CSIRO
  2. Copyright (c) 2007-2010 Xiph.Org Foundation
  3. Copyright (c) 2008 Gregory Maxwell
  4. Written by Jean-Marc Valin and Gregory Maxwell */
  5. /*
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. - Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. - Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #define CELT_ENCODER_C
  30. #include "cpu_support.h"
  31. #include "os_support.h"
  32. #include "mdct.h"
  33. #include <math.h>
  34. #include "celt.h"
  35. #include "pitch.h"
  36. #include "bands.h"
  37. #include "modes.h"
  38. #include "entcode.h"
  39. #include "quant_bands.h"
  40. #include "rate.h"
  41. #include "stack_alloc.h"
  42. #include "mathops.h"
  43. #include "float_cast.h"
  44. #include <stdarg.h>
  45. #include "celt_lpc.h"
  46. #include "vq.h"
  47. /** Encoder state
  48. @brief Encoder state
  49. */
  50. struct OpusCustomEncoder {
  51. const OpusCustomMode *mode; /**< Mode used by the encoder */
  52. int channels;
  53. int stream_channels;
  54. int force_intra;
  55. int clip;
  56. int disable_pf;
  57. int complexity;
  58. int upsample;
  59. int start, end;
  60. opus_int32 bitrate;
  61. int vbr;
  62. int signalling;
  63. int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
  64. int loss_rate;
  65. int lsb_depth;
  66. int lfe;
  67. int disable_inv;
  68. int arch;
  69. /* Everything beyond this point gets cleared on a reset */
  70. #define ENCODER_RESET_START rng
  71. opus_uint32 rng;
  72. int spread_decision;
  73. opus_val32 delayedIntra;
  74. int tonal_average;
  75. int lastCodedBands;
  76. int hf_average;
  77. int tapset_decision;
  78. int prefilter_period;
  79. opus_val16 prefilter_gain;
  80. int prefilter_tapset;
  81. #ifdef RESYNTH
  82. int prefilter_period_old;
  83. opus_val16 prefilter_gain_old;
  84. int prefilter_tapset_old;
  85. #endif
  86. int consec_transient;
  87. AnalysisInfo analysis;
  88. SILKInfo silk_info;
  89. opus_val32 preemph_memE[2];
  90. opus_val32 preemph_memD[2];
  91. /* VBR-related parameters */
  92. opus_int32 vbr_reservoir;
  93. opus_int32 vbr_drift;
  94. opus_int32 vbr_offset;
  95. opus_int32 vbr_count;
  96. opus_val32 overlap_max;
  97. opus_val16 stereo_saving;
  98. int intensity;
  99. opus_val16 *energy_mask;
  100. opus_val16 spec_avg;
  101. #ifdef RESYNTH
  102. /* +MAX_PERIOD/2 to make space for overlap */
  103. celt_sig syn_mem[2][2*MAX_PERIOD+MAX_PERIOD/2];
  104. #endif
  105. celt_sig in_mem[1]; /* Size = channels*mode->overlap */
  106. /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_MAXPERIOD */
  107. /* opus_val16 oldBandE[], Size = channels*mode->nbEBands */
  108. /* opus_val16 oldLogE[], Size = channels*mode->nbEBands */
  109. /* opus_val16 oldLogE2[], Size = channels*mode->nbEBands */
  110. /* opus_val16 energyError[], Size = channels*mode->nbEBands */
  111. };
  112. int celt_encoder_get_size(int channels)
  113. {
  114. CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
  115. return opus_custom_encoder_get_size(mode, channels);
  116. }
  117. OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
  118. {
  119. int size = sizeof(struct CELTEncoder)
  120. + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */
  121. + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
  122. + 4*channels*mode->nbEBands*sizeof(opus_val16); /* opus_val16 oldBandE[channels*mode->nbEBands]; */
  123. /* opus_val16 oldLogE[channels*mode->nbEBands]; */
  124. /* opus_val16 oldLogE2[channels*mode->nbEBands]; */
  125. /* opus_val16 energyError[channels*mode->nbEBands]; */
  126. return size;
  127. }
  128. #ifdef CUSTOM_MODES
  129. CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
  130. {
  131. int ret;
  132. CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
  133. /* init will handle the NULL case */
  134. ret = opus_custom_encoder_init(st, mode, channels);
  135. if (ret != OPUS_OK)
  136. {
  137. opus_custom_encoder_destroy(st);
  138. st = NULL;
  139. }
  140. if (error)
  141. *error = ret;
  142. return st;
  143. }
  144. #endif /* CUSTOM_MODES */
  145. static int opus_custom_encoder_init_arch(CELTEncoder *st, const CELTMode *mode,
  146. int channels, int arch)
  147. {
  148. if (channels < 0 || channels > 2)
  149. return OPUS_BAD_ARG;
  150. if (st==NULL || mode==NULL)
  151. return OPUS_ALLOC_FAIL;
  152. OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
  153. st->mode = mode;
  154. st->stream_channels = st->channels = channels;
  155. st->upsample = 1;
  156. st->start = 0;
  157. st->end = st->mode->effEBands;
  158. st->signalling = 1;
  159. st->arch = arch;
  160. st->constrained_vbr = 1;
  161. st->clip = 1;
  162. st->bitrate = OPUS_BITRATE_MAX;
  163. st->vbr = 0;
  164. st->force_intra = 0;
  165. st->complexity = 5;
  166. st->lsb_depth=24;
  167. opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
  168. return OPUS_OK;
  169. }
  170. #ifdef CUSTOM_MODES
  171. int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
  172. {
  173. return opus_custom_encoder_init_arch(st, mode, channels, opus_select_arch());
  174. }
  175. #endif
  176. int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
  177. int arch)
  178. {
  179. int ret;
  180. ret = opus_custom_encoder_init_arch(st,
  181. opus_custom_mode_create(48000, 960, NULL), channels, arch);
  182. if (ret != OPUS_OK)
  183. return ret;
  184. st->upsample = resampling_factor(sampling_rate);
  185. return OPUS_OK;
  186. }
  187. #ifdef CUSTOM_MODES
  188. void opus_custom_encoder_destroy(CELTEncoder *st)
  189. {
  190. opus_free(st);
  191. }
  192. #endif /* CUSTOM_MODES */
  193. static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
  194. opus_val16 *tf_estimate, int *tf_chan, int allow_weak_transients,
  195. int *weak_transient)
  196. {
  197. int i;
  198. VARDECL(opus_val16, tmp);
  199. opus_val32 mem0,mem1;
  200. int is_transient = 0;
  201. opus_int32 mask_metric = 0;
  202. int c;
  203. opus_val16 tf_max;
  204. int len2;
  205. /* Forward masking: 6.7 dB/ms. */
  206. #ifdef FIXED_POINT
  207. int forward_shift = 4;
  208. #else
  209. opus_val16 forward_decay = QCONST16(.0625f,15);
  210. #endif
  211. /* Table of 6*64/x, trained on real data to minimize the average error */
  212. static const unsigned char inv_table[128] = {
  213. 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
  214. 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
  215. 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8,
  216. 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
  217. 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5,
  218. 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  219. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
  220. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
  221. };
  222. SAVE_STACK;
  223. ALLOC(tmp, len, opus_val16);
  224. *weak_transient = 0;
  225. /* For lower bitrates, let's be more conservative and have a forward masking
  226. decay of 3.3 dB/ms. This avoids having to code transients at very low
  227. bitrate (mostly for hybrid), which can result in unstable energy and/or
  228. partial collapse. */
  229. if (allow_weak_transients)
  230. {
  231. #ifdef FIXED_POINT
  232. forward_shift = 5;
  233. #else
  234. forward_decay = QCONST16(.03125f,15);
  235. #endif
  236. }
  237. len2=len/2;
  238. for (c=0;c<C;c++)
  239. {
  240. opus_val32 mean;
  241. opus_int32 unmask=0;
  242. opus_val32 norm;
  243. opus_val16 maxE;
  244. mem0=0;
  245. mem1=0;
  246. /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
  247. for (i=0;i<len;i++)
  248. {
  249. opus_val32 x,y;
  250. x = SHR32(in[i+c*len],SIG_SHIFT);
  251. y = ADD32(mem0, x);
  252. #ifdef FIXED_POINT
  253. mem0 = mem1 + y - SHL32(x,1);
  254. mem1 = x - SHR32(y,1);
  255. #else
  256. mem0 = mem1 + y - 2*x;
  257. mem1 = x - .5f*y;
  258. #endif
  259. tmp[i] = SROUND16(y, 2);
  260. /*printf("%f ", tmp[i]);*/
  261. }
  262. /*printf("\n");*/
  263. /* First few samples are bad because we don't propagate the memory */
  264. OPUS_CLEAR(tmp, 12);
  265. #ifdef FIXED_POINT
  266. /* Normalize tmp to max range */
  267. {
  268. int shift=0;
  269. shift = 14-celt_ilog2(MAX16(1, celt_maxabs16(tmp, len)));
  270. if (shift!=0)
  271. {
  272. for (i=0;i<len;i++)
  273. tmp[i] = SHL16(tmp[i], shift);
  274. }
  275. }
  276. #endif
  277. mean=0;
  278. mem0=0;
  279. /* Grouping by two to reduce complexity */
  280. /* Forward pass to compute the post-echo threshold*/
  281. for (i=0;i<len2;i++)
  282. {
  283. opus_val16 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),16);
  284. mean += x2;
  285. #ifdef FIXED_POINT
  286. /* FIXME: Use PSHR16() instead */
  287. tmp[i] = mem0 + PSHR32(x2-mem0,forward_shift);
  288. #else
  289. tmp[i] = mem0 + MULT16_16_P15(forward_decay,x2-mem0);
  290. #endif
  291. mem0 = tmp[i];
  292. }
  293. mem0=0;
  294. maxE=0;
  295. /* Backward pass to compute the pre-echo threshold */
  296. for (i=len2-1;i>=0;i--)
  297. {
  298. /* Backward masking: 13.9 dB/ms. */
  299. #ifdef FIXED_POINT
  300. /* FIXME: Use PSHR16() instead */
  301. tmp[i] = mem0 + PSHR32(tmp[i]-mem0,3);
  302. #else
  303. tmp[i] = mem0 + MULT16_16_P15(QCONST16(0.125f,15),tmp[i]-mem0);
  304. #endif
  305. mem0 = tmp[i];
  306. maxE = MAX16(maxE, mem0);
  307. }
  308. /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/
  309. /* Compute the ratio of the "frame energy" over the harmonic mean of the energy.
  310. This essentially corresponds to a bitrate-normalized temporal noise-to-mask
  311. ratio */
  312. /* As a compromise with the old transient detector, frame energy is the
  313. geometric mean of the energy and half the max */
  314. #ifdef FIXED_POINT
  315. /* Costs two sqrt() to avoid overflows */
  316. mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
  317. #else
  318. mean = celt_sqrt(mean * maxE*.5*len2);
  319. #endif
  320. /* Inverse of the mean energy in Q15+6 */
  321. norm = SHL32(EXTEND32(len2),6+14)/ADD32(EPSILON,SHR32(mean,1));
  322. /* Compute harmonic mean discarding the unreliable boundaries
  323. The data is smooth, so we only take 1/4th of the samples */
  324. unmask=0;
  325. /* We should never see NaNs here. If we find any, then something really bad happened and we better abort
  326. before it does any damage later on. If these asserts are disabled (no hardening), then the table
  327. lookup a few lines below (id = ...) is likely to crash dur to an out-of-bounds read. DO NOT FIX
  328. that crash on NaN since it could result in a worse issue later on. */
  329. celt_assert(!celt_isnan(tmp[0]));
  330. celt_assert(!celt_isnan(norm));
  331. for (i=12;i<len2-5;i+=4)
  332. {
  333. int id;
  334. #ifdef FIXED_POINT
  335. id = MAX32(0,MIN32(127,MULT16_32_Q15(tmp[i]+EPSILON,norm))); /* Do not round to nearest */
  336. #else
  337. id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON)))); /* Do not round to nearest */
  338. #endif
  339. unmask += inv_table[id];
  340. }
  341. /*printf("%d\n", unmask);*/
  342. /* Normalize, compensate for the 1/4th of the sample and the factor of 6 in the inverse table */
  343. unmask = 64*unmask*4/(6*(len2-17));
  344. if (unmask>mask_metric)
  345. {
  346. *tf_chan = c;
  347. mask_metric = unmask;
  348. }
  349. }
  350. is_transient = mask_metric>200;
  351. /* For low bitrates, define "weak transients" that need to be
  352. handled differently to avoid partial collapse. */
  353. if (allow_weak_transients && is_transient && mask_metric<600) {
  354. is_transient = 0;
  355. *weak_transient = 1;
  356. }
  357. /* Arbitrary metric for VBR boost */
  358. tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
  359. /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
  360. *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)));
  361. /*printf("%d %f\n", tf_max, mask_metric);*/
  362. RESTORE_STACK;
  363. #ifdef FUZZING
  364. is_transient = rand()&0x1;
  365. #endif
  366. /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
  367. return is_transient;
  368. }
  369. /* Looks for sudden increases of energy to decide whether we need to patch
  370. the transient decision */
  371. static int patch_transient_decision(opus_val16 *newE, opus_val16 *oldE, int nbEBands,
  372. int start, int end, int C)
  373. {
  374. int i, c;
  375. opus_val32 mean_diff=0;
  376. opus_val16 spread_old[26];
  377. /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
  378. avoid false detection caused by irrelevant bands */
  379. if (C==1)
  380. {
  381. spread_old[start] = oldE[start];
  382. for (i=start+1;i<end;i++)
  383. spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), oldE[i]);
  384. } else {
  385. spread_old[start] = MAX16(oldE[start],oldE[start+nbEBands]);
  386. for (i=start+1;i<end;i++)
  387. spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT),
  388. MAX16(oldE[i],oldE[i+nbEBands]));
  389. }
  390. for (i=end-2;i>=start;i--)
  391. spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHIFT));
  392. /* Compute mean increase */
  393. c=0; do {
  394. for (i=IMAX(2,start);i<end-1;i++)
  395. {
  396. opus_val16 x1, x2;
  397. x1 = MAX16(0, newE[i + c*nbEBands]);
  398. x2 = MAX16(0, spread_old[i]);
  399. mean_diff = ADD32(mean_diff, EXTEND32(MAX16(0, SUB16(x1, x2))));
  400. }
  401. } while (++c<C);
  402. mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)));
  403. /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
  404. return mean_diff > QCONST16(1.f, DB_SHIFT);
  405. }
  406. /** Apply window and compute the MDCT for all sub-frames and
  407. all channels in a frame */
  408. static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in,
  409. celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample,
  410. int arch)
  411. {
  412. const int overlap = mode->overlap;
  413. int N;
  414. int B;
  415. int shift;
  416. int i, b, c;
  417. if (shortBlocks)
  418. {
  419. B = shortBlocks;
  420. N = mode->shortMdctSize;
  421. shift = mode->maxLM;
  422. } else {
  423. B = 1;
  424. N = mode->shortMdctSize<<LM;
  425. shift = mode->maxLM-LM;
  426. }
  427. c=0; do {
  428. for (b=0;b<B;b++)
  429. {
  430. /* Interleaving the sub-frames while doing the MDCTs */
  431. clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N,
  432. &out[b+c*N*B], mode->window, overlap, shift, B,
  433. arch);
  434. }
  435. } while (++c<CC);
  436. if (CC==2&&C==1)
  437. {
  438. for (i=0;i<B*N;i++)
  439. out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
  440. }
  441. if (upsample != 1)
  442. {
  443. c=0; do
  444. {
  445. int bound = B*N/upsample;
  446. for (i=0;i<bound;i++)
  447. out[c*B*N+i] *= upsample;
  448. OPUS_CLEAR(&out[c*B*N+bound], B*N-bound);
  449. } while (++c<C);
  450. }
  451. }
  452. void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
  453. int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip)
  454. {
  455. int i;
  456. opus_val16 coef0;
  457. celt_sig m;
  458. int Nu;
  459. coef0 = coef[0];
  460. m = *mem;
  461. /* Fast path for the normal 48kHz case and no clipping */
  462. if (coef[1] == 0 && upsample == 1 && !clip)
  463. {
  464. for (i=0;i<N;i++)
  465. {
  466. opus_val16 x;
  467. x = SCALEIN(pcmp[CC*i]);
  468. /* Apply pre-emphasis */
  469. inp[i] = SHL32(x, SIG_SHIFT) - m;
  470. m = SHR32(MULT16_16(coef0, x), 15-SIG_SHIFT);
  471. }
  472. *mem = m;
  473. return;
  474. }
  475. Nu = N/upsample;
  476. if (upsample!=1)
  477. {
  478. OPUS_CLEAR(inp, N);
  479. }
  480. for (i=0;i<Nu;i++)
  481. inp[i*upsample] = SCALEIN(pcmp[CC*i]);
  482. #ifndef FIXED_POINT
  483. if (clip)
  484. {
  485. /* Clip input to avoid encoding non-portable files */
  486. for (i=0;i<Nu;i++)
  487. inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
  488. }
  489. #else
  490. (void)clip; /* Avoids a warning about clip being unused. */
  491. #endif
  492. #ifdef CUSTOM_MODES
  493. if (coef[1] != 0)
  494. {
  495. opus_val16 coef1 = coef[1];
  496. opus_val16 coef2 = coef[2];
  497. for (i=0;i<N;i++)
  498. {
  499. celt_sig x, tmp;
  500. x = inp[i];
  501. /* Apply pre-emphasis */
  502. tmp = MULT16_16(coef2, x);
  503. inp[i] = tmp + m;
  504. m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
  505. }
  506. } else
  507. #endif
  508. {
  509. for (i=0;i<N;i++)
  510. {
  511. opus_val16 x;
  512. x = inp[i];
  513. /* Apply pre-emphasis */
  514. inp[i] = SHL32(x, SIG_SHIFT) - m;
  515. m = SHR32(MULT16_16(coef0, x), 15-SIG_SHIFT);
  516. }
  517. }
  518. *mem = m;
  519. }
  520. static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
  521. {
  522. int i;
  523. opus_val32 L1;
  524. L1 = 0;
  525. for (i=0;i<N;i++)
  526. L1 += EXTEND32(ABS16(tmp[i]));
  527. /* When in doubt, prefer good freq resolution */
  528. L1 = MAC16_32_Q15(L1, LM*bias, L1);
  529. return L1;
  530. }
  531. static int tf_analysis(const CELTMode *m, int len, int isTransient,
  532. int *tf_res, int lambda, celt_norm *X, int N0, int LM,
  533. opus_val16 tf_estimate, int tf_chan, int *importance)
  534. {
  535. int i;
  536. VARDECL(int, metric);
  537. int cost0;
  538. int cost1;
  539. VARDECL(int, path0);
  540. VARDECL(int, path1);
  541. VARDECL(celt_norm, tmp);
  542. VARDECL(celt_norm, tmp_1);
  543. int sel;
  544. int selcost[2];
  545. int tf_select=0;
  546. opus_val16 bias;
  547. SAVE_STACK;
  548. bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
  549. /*printf("%f ", bias);*/
  550. ALLOC(metric, len, int);
  551. ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
  552. ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
  553. ALLOC(path0, len, int);
  554. ALLOC(path1, len, int);
  555. for (i=0;i<len;i++)
  556. {
  557. int k, N;
  558. int narrow;
  559. opus_val32 L1, best_L1;
  560. int best_level=0;
  561. N = (m->eBands[i+1]-m->eBands[i])<<LM;
  562. /* band is too narrow to be split down to LM=-1 */
  563. narrow = (m->eBands[i+1]-m->eBands[i])==1;
  564. OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N);
  565. /* Just add the right channel if we're in stereo */
  566. /*if (C==2)
  567. for (j=0;j<N;j++)
  568. tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
  569. L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
  570. best_L1 = L1;
  571. /* Check the -1 case for transients */
  572. if (isTransient && !narrow)
  573. {
  574. OPUS_COPY(tmp_1, tmp, N);
  575. haar1(tmp_1, N>>LM, 1<<LM);
  576. L1 = l1_metric(tmp_1, N, LM+1, bias);
  577. if (L1<best_L1)
  578. {
  579. best_L1 = L1;
  580. best_level = -1;
  581. }
  582. }
  583. /*printf ("%f ", L1);*/
  584. for (k=0;k<LM+!(isTransient||narrow);k++)
  585. {
  586. int B;
  587. if (isTransient)
  588. B = (LM-k-1);
  589. else
  590. B = k+1;
  591. haar1(tmp, N>>k, 1<<k);
  592. L1 = l1_metric(tmp, N, B, bias);
  593. if (L1 < best_L1)
  594. {
  595. best_L1 = L1;
  596. best_level = k+1;
  597. }
  598. }
  599. /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
  600. /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
  601. if (isTransient)
  602. metric[i] = 2*best_level;
  603. else
  604. metric[i] = -2*best_level;
  605. /* For bands that can't be split to -1, set the metric to the half-way point to avoid
  606. biasing the decision */
  607. if (narrow && (metric[i]==0 || metric[i]==-2*LM))
  608. metric[i]-=1;
  609. /*printf("%d ", metric[i]/2 + (!isTransient)*LM);*/
  610. }
  611. /*printf("\n");*/
  612. /* Search for the optimal tf resolution, including tf_select */
  613. tf_select = 0;
  614. for (sel=0;sel<2;sel++)
  615. {
  616. cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
  617. cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+1]) + (isTransient ? 0 : lambda);
  618. for (i=1;i<len;i++)
  619. {
  620. int curr0, curr1;
  621. curr0 = IMIN(cost0, cost1 + lambda);
  622. curr1 = IMIN(cost0 + lambda, cost1);
  623. cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
  624. cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
  625. }
  626. cost0 = IMIN(cost0, cost1);
  627. selcost[sel]=cost0;
  628. }
  629. /* For now, we're conservative and only allow tf_select=1 for transients.
  630. * If tests confirm it's useful for non-transients, we could allow it. */
  631. if (selcost[1]<selcost[0] && isTransient)
  632. tf_select=1;
  633. cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
  634. cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]) + (isTransient ? 0 : lambda);
  635. /* Viterbi forward pass */
  636. for (i=1;i<len;i++)
  637. {
  638. int curr0, curr1;
  639. int from0, from1;
  640. from0 = cost0;
  641. from1 = cost1 + lambda;
  642. if (from0 < from1)
  643. {
  644. curr0 = from0;
  645. path0[i]= 0;
  646. } else {
  647. curr0 = from1;
  648. path0[i]= 1;
  649. }
  650. from0 = cost0 + lambda;
  651. from1 = cost1;
  652. if (from0 < from1)
  653. {
  654. curr1 = from0;
  655. path1[i]= 0;
  656. } else {
  657. curr1 = from1;
  658. path1[i]= 1;
  659. }
  660. cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
  661. cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
  662. }
  663. tf_res[len-1] = cost0 < cost1 ? 0 : 1;
  664. /* Viterbi backward pass to check the decisions */
  665. for (i=len-2;i>=0;i--)
  666. {
  667. if (tf_res[i+1] == 1)
  668. tf_res[i] = path1[i+1];
  669. else
  670. tf_res[i] = path0[i+1];
  671. }
  672. /*printf("%d %f\n", *tf_sum, tf_estimate);*/
  673. RESTORE_STACK;
  674. #ifdef FUZZING
  675. tf_select = rand()&0x1;
  676. tf_res[0] = rand()&0x1;
  677. for (i=1;i<len;i++)
  678. tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
  679. #endif
  680. return tf_select;
  681. }
  682. static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
  683. {
  684. int curr, i;
  685. int tf_select_rsv;
  686. int tf_changed;
  687. int logp;
  688. opus_uint32 budget;
  689. opus_uint32 tell;
  690. budget = enc->storage*8;
  691. tell = ec_tell(enc);
  692. logp = isTransient ? 2 : 4;
  693. /* Reserve space to code the tf_select decision. */
  694. tf_select_rsv = LM>0 && tell+logp+1 <= budget;
  695. budget -= tf_select_rsv;
  696. curr = tf_changed = 0;
  697. for (i=start;i<end;i++)
  698. {
  699. if (tell+logp<=budget)
  700. {
  701. ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
  702. tell = ec_tell(enc);
  703. curr = tf_res[i];
  704. tf_changed |= curr;
  705. }
  706. else
  707. tf_res[i] = curr;
  708. logp = isTransient ? 4 : 5;
  709. }
  710. /* Only code tf_select if it would actually make a difference. */
  711. if (tf_select_rsv &&
  712. tf_select_table[LM][4*isTransient+0+tf_changed]!=
  713. tf_select_table[LM][4*isTransient+2+tf_changed])
  714. ec_enc_bit_logp(enc, tf_select, 1);
  715. else
  716. tf_select = 0;
  717. for (i=start;i<end;i++)
  718. tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
  719. /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
  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. AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
  724. int intensity, opus_val16 surround_trim, opus_int32 equiv_rate, int arch)
  725. {
  726. int i;
  727. opus_val32 diff=0;
  728. int c;
  729. int trim_index;
  730. opus_val16 trim = QCONST16(5.f, 8);
  731. opus_val16 logXC, logXC2;
  732. /* At low bitrate, reducing the trim seems to help. At higher bitrates, it's less
  733. clear what's best, so we're keeping it as it was before, at least for now. */
  734. if (equiv_rate < 64000) {
  735. trim = QCONST16(4.f, 8);
  736. } else if (equiv_rate < 80000) {
  737. opus_int32 frac = (equiv_rate-64000) >> 10;
  738. trim = QCONST16(4.f, 8) + QCONST16(1.f/16.f, 8)*frac;
  739. }
  740. if (C==2)
  741. {
  742. opus_val16 sum = 0; /* Q10 */
  743. opus_val16 minXC; /* Q10 */
  744. /* Compute inter-channel correlation for low frequencies */
  745. for (i=0;i<8;i++)
  746. {
  747. opus_val32 partial;
  748. partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
  749. (m->eBands[i+1]-m->eBands[i])<<LM, arch);
  750. sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
  751. }
  752. sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
  753. sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
  754. minXC = sum;
  755. for (i=8;i<intensity;i++)
  756. {
  757. opus_val32 partial;
  758. partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
  759. (m->eBands[i+1]-m->eBands[i])<<LM, arch);
  760. minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
  761. }
  762. minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
  763. /*printf ("%f\n", sum);*/
  764. /* mid-side savings estimations based on the LF average*/
  765. logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
  766. /* mid-side savings estimations based on min correlation */
  767. logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
  768. #ifdef FIXED_POINT
  769. /* Compensate for Q20 vs Q14 input and convert output to Q8 */
  770. logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
  771. logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
  772. #endif
  773. trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
  774. *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
  775. }
  776. /* Estimate spectral tilt */
  777. c=0; do {
  778. for (i=0;i<end-1;i++)
  779. {
  780. diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end);
  781. }
  782. } while (++c<C);
  783. diff /= C*(end-1);
  784. /*printf("%f\n", diff);*/
  785. trim -= MAX32(-QCONST16(2.f, 8), MIN32(QCONST16(2.f, 8), SHR32(diff+QCONST16(1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
  786. trim -= SHR16(surround_trim, DB_SHIFT-8);
  787. trim -= 2*SHR16(tf_estimate, 14-8);
  788. #ifndef DISABLE_FLOAT_API
  789. if (analysis->valid)
  790. {
  791. trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
  792. (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
  793. }
  794. #else
  795. (void)analysis;
  796. #endif
  797. #ifdef FIXED_POINT
  798. trim_index = PSHR32(trim, 8);
  799. #else
  800. trim_index = (int)floor(.5f+trim);
  801. #endif
  802. trim_index = IMAX(0, IMIN(10, trim_index));
  803. /*printf("%d\n", trim_index);*/
  804. #ifdef FUZZING
  805. trim_index = rand()%11;
  806. #endif
  807. return trim_index;
  808. }
  809. static int stereo_analysis(const CELTMode *m, const celt_norm *X,
  810. int LM, int N0)
  811. {
  812. int i;
  813. int thetas;
  814. opus_val32 sumLR = EPSILON, sumMS = EPSILON;
  815. /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
  816. for (i=0;i<13;i++)
  817. {
  818. int j;
  819. for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
  820. {
  821. opus_val32 L, R, M, S;
  822. /* We cast to 32-bit first because of the -32768 case */
  823. L = EXTEND32(X[j]);
  824. R = EXTEND32(X[N0+j]);
  825. M = ADD32(L, R);
  826. S = SUB32(L, R);
  827. sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
  828. sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
  829. }
  830. }
  831. sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
  832. thetas = 13;
  833. /* We don't need thetas for lower bands with LM<=1 */
  834. if (LM<=1)
  835. thetas -= 8;
  836. return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
  837. > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
  838. }
  839. #define MSWAP(a,b) do {opus_val16 tmp = a;a=b;b=tmp;} while(0)
  840. static opus_val16 median_of_5(const opus_val16 *x)
  841. {
  842. opus_val16 t0, t1, t2, t3, t4;
  843. t2 = x[2];
  844. if (x[0] > x[1])
  845. {
  846. t0 = x[1];
  847. t1 = x[0];
  848. } else {
  849. t0 = x[0];
  850. t1 = x[1];
  851. }
  852. if (x[3] > x[4])
  853. {
  854. t3 = x[4];
  855. t4 = x[3];
  856. } else {
  857. t3 = x[3];
  858. t4 = x[4];
  859. }
  860. if (t0 > t3)
  861. {
  862. MSWAP(t0, t3);
  863. MSWAP(t1, t4);
  864. }
  865. if (t2 > t1)
  866. {
  867. if (t1 < t3)
  868. return MIN16(t2, t3);
  869. else
  870. return MIN16(t4, t1);
  871. } else {
  872. if (t2 < t3)
  873. return MIN16(t1, t3);
  874. else
  875. return MIN16(t2, t4);
  876. }
  877. }
  878. static opus_val16 median_of_3(const opus_val16 *x)
  879. {
  880. opus_val16 t0, t1, t2;
  881. if (x[0] > x[1])
  882. {
  883. t0 = x[1];
  884. t1 = x[0];
  885. } else {
  886. t0 = x[0];
  887. t1 = x[1];
  888. }
  889. t2 = x[2];
  890. if (t1 < t2)
  891. return t1;
  892. else if (t0 < t2)
  893. return t2;
  894. else
  895. return t0;
  896. }
  897. static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 *bandLogE2,
  898. int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN,
  899. int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM,
  900. int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_dynalloc,
  901. AnalysisInfo *analysis, int *importance, int *spread_weight)
  902. {
  903. int i, c;
  904. opus_int32 tot_boost=0;
  905. opus_val16 maxDepth;
  906. VARDECL(opus_val16, follower);
  907. VARDECL(opus_val16, noise_floor);
  908. SAVE_STACK;
  909. ALLOC(follower, C*nbEBands, opus_val16);
  910. ALLOC(noise_floor, C*nbEBands, opus_val16);
  911. OPUS_CLEAR(offsets, nbEBands);
  912. /* Dynamic allocation code */
  913. maxDepth=-QCONST16(31.9f, DB_SHIFT);
  914. for (i=0;i<end;i++)
  915. {
  916. /* Noise floor must take into account eMeans, the depth, the width of the bands
  917. and the preemphasis filter (approx. square of bark band ID) */
  918. noise_floor[i] = MULT16_16(QCONST16(0.0625f, DB_SHIFT),logN[i])
  919. +QCONST16(.5f,DB_SHIFT)+SHL16(9-lsb_depth,DB_SHIFT)-SHL16(eMeans[i],6)
  920. +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5));
  921. }
  922. c=0;do
  923. {
  924. for (i=0;i<end;i++)
  925. maxDepth = MAX16(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
  926. } while (++c<C);
  927. {
  928. /* Compute a really simple masking model to avoid taking into account completely masked
  929. bands when computing the spreading decision. */
  930. VARDECL(opus_val16, mask);
  931. VARDECL(opus_val16, sig);
  932. ALLOC(mask, nbEBands, opus_val16);
  933. ALLOC(sig, nbEBands, opus_val16);
  934. for (i=0;i<end;i++)
  935. mask[i] = bandLogE[i]-noise_floor[i];
  936. if (C==2)
  937. {
  938. for (i=0;i<end;i++)
  939. mask[i] = MAX16(mask[i], bandLogE[nbEBands+i]-noise_floor[i]);
  940. }
  941. OPUS_COPY(sig, mask, end);
  942. for (i=1;i<end;i++)
  943. mask[i] = MAX16(mask[i], mask[i-1] - QCONST16(2.f, DB_SHIFT));
  944. for (i=end-2;i>=0;i--)
  945. mask[i] = MAX16(mask[i], mask[i+1] - QCONST16(3.f, DB_SHIFT));
  946. for (i=0;i<end;i++)
  947. {
  948. /* Compute SMR: Mask is never more than 72 dB below the peak and never below the noise floor.*/
  949. opus_val16 smr = sig[i]-MAX16(MAX16(0, maxDepth-QCONST16(12.f, DB_SHIFT)), mask[i]);
  950. /* Clamp SMR to make sure we're not shifting by something negative or too large. */
  951. #ifdef FIXED_POINT
  952. /* FIXME: Use PSHR16() instead */
  953. int shift = -PSHR32(MAX16(-QCONST16(5.f, DB_SHIFT), MIN16(0, smr)), DB_SHIFT);
  954. #else
  955. int shift = IMIN(5, IMAX(0, -(int)floor(.5f + smr)));
  956. #endif
  957. spread_weight[i] = 32 >> shift;
  958. }
  959. /*for (i=0;i<end;i++)
  960. printf("%d ", spread_weight[i]);
  961. printf("\n");*/
  962. }
  963. /* Make sure that dynamic allocation can't make us bust the budget */
  964. if (effectiveBytes > 50 && LM>=1 && !lfe)
  965. {
  966. int last=0;
  967. c=0;do
  968. {
  969. opus_val16 offset;
  970. opus_val16 tmp;
  971. opus_val16 *f;
  972. f = &follower[c*nbEBands];
  973. f[0] = bandLogE2[c*nbEBands];
  974. for (i=1;i<end;i++)
  975. {
  976. /* The last band to be at least 3 dB higher than the previous one
  977. is the last we'll consider. Otherwise, we run into problems on
  978. bandlimited signals. */
  979. if (bandLogE2[c*nbEBands+i] > bandLogE2[c*nbEBands+i-1]+QCONST16(.5f,DB_SHIFT))
  980. last=i;
  981. f[i] = MIN16(f[i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*nbEBands+i]);
  982. }
  983. for (i=last-1;i>=0;i--)
  984. f[i] = MIN16(f[i], MIN16(f[i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*nbEBands+i]));
  985. /* Combine with a median filter to avoid dynalloc triggering unnecessarily.
  986. The "offset" value controls how conservative we are -- a higher offset
  987. reduces the impact of the median filter and makes dynalloc use more bits. */
  988. offset = QCONST16(1.f, DB_SHIFT);
  989. for (i=2;i<end-2;i++)
  990. f[i] = MAX16(f[i], median_of_5(&bandLogE2[c*nbEBands+i-2])-offset);
  991. tmp = median_of_3(&bandLogE2[c*nbEBands])-offset;
  992. f[0] = MAX16(f[0], tmp);
  993. f[1] = MAX16(f[1], tmp);
  994. tmp = median_of_3(&bandLogE2[c*nbEBands+end-3])-offset;
  995. f[end-2] = MAX16(f[end-2], tmp);
  996. f[end-1] = MAX16(f[end-1], tmp);
  997. for (i=0;i<end;i++)
  998. f[i] = MAX16(f[i], noise_floor[i]);
  999. } while (++c<C);
  1000. if (C==2)
  1001. {
  1002. for (i=start;i<end;i++)
  1003. {
  1004. /* Consider 24 dB "cross-talk" */
  1005. follower[nbEBands+i] = MAX16(follower[nbEBands+i], follower[ i]-QCONST16(4.f,DB_SHIFT));
  1006. follower[ i] = MAX16(follower[ i], follower[nbEBands+i]-QCONST16(4.f,DB_SHIFT));
  1007. follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
  1008. }
  1009. } else {
  1010. for (i=start;i<end;i++)
  1011. {
  1012. follower[i] = MAX16(0, bandLogE[i]-follower[i]);
  1013. }
  1014. }
  1015. for (i=start;i<end;i++)
  1016. follower[i] = MAX16(follower[i], surround_dynalloc[i]);
  1017. for (i=start;i<end;i++)
  1018. {
  1019. #ifdef FIXED_POINT
  1020. importance[i] = PSHR32(13*celt_exp2(MIN16(follower[i], QCONST16(4.f, DB_SHIFT))), 16);
  1021. #else
  1022. importance[i] = (int)floor(.5f+13*celt_exp2(MIN16(follower[i], QCONST16(4.f, DB_SHIFT))));
  1023. #endif
  1024. }
  1025. /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
  1026. if ((!vbr || constrained_vbr)&&!isTransient)
  1027. {
  1028. for (i=start;i<end;i++)
  1029. follower[i] = HALF16(follower[i]);
  1030. }
  1031. for (i=start;i<end;i++)
  1032. {
  1033. if (i<8)
  1034. follower[i] *= 2;
  1035. if (i>=12)
  1036. follower[i] = HALF16(follower[i]);
  1037. }
  1038. #ifdef DISABLE_FLOAT_API
  1039. (void)analysis;
  1040. #else
  1041. if (analysis->valid)
  1042. {
  1043. for (i=start;i<IMIN(LEAK_BANDS, end);i++)
  1044. follower[i] = follower[i] + QCONST16(1.f/64.f, DB_SHIFT)*analysis->leak_boost[i];
  1045. }
  1046. #endif
  1047. for (i=start;i<end;i++)
  1048. {
  1049. int width;
  1050. int boost;
  1051. int boost_bits;
  1052. follower[i] = MIN16(follower[i], QCONST16(4, DB_SHIFT));
  1053. width = C*(eBands[i+1]-eBands[i])<<LM;
  1054. if (width<6)
  1055. {
  1056. boost = (int)SHR32(EXTEND32(follower[i]),DB_SHIFT);
  1057. boost_bits = boost*width<<BITRES;
  1058. } else if (width > 48) {
  1059. boost = (int)SHR32(EXTEND32(follower[i])*8,DB_SHIFT);
  1060. boost_bits = (boost*width<<BITRES)/8;
  1061. } else {
  1062. boost = (int)SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT);
  1063. boost_bits = boost*6<<BITRES;
  1064. }
  1065. /* For CBR and non-transient CVBR frames, limit dynalloc to 2/3 of the bits */
  1066. if ((!vbr || (constrained_vbr&&!isTransient))
  1067. && (tot_boost+boost_bits)>>BITRES>>3 > 2*effectiveBytes/3)
  1068. {
  1069. opus_int32 cap = ((2*effectiveBytes/3)<<BITRES<<3);
  1070. offsets[i] = cap-tot_boost;
  1071. tot_boost = cap;
  1072. break;
  1073. } else {
  1074. offsets[i] = boost;
  1075. tot_boost += boost_bits;
  1076. }
  1077. }
  1078. } else {
  1079. for (i=start;i<end;i++)
  1080. importance[i] = 13;
  1081. }
  1082. *tot_boost_ = tot_boost;
  1083. RESTORE_STACK;
  1084. return maxDepth;
  1085. }
  1086. static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N,
  1087. int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, int nbAvailableBytes, AnalysisInfo *analysis)
  1088. {
  1089. int c;
  1090. VARDECL(celt_sig, _pre);
  1091. celt_sig *pre[2];
  1092. const CELTMode *mode;
  1093. int pitch_index;
  1094. opus_val16 gain1;
  1095. opus_val16 pf_threshold;
  1096. int pf_on;
  1097. int qg;
  1098. int overlap;
  1099. SAVE_STACK;
  1100. mode = st->mode;
  1101. overlap = mode->overlap;
  1102. ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
  1103. pre[0] = _pre;
  1104. pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
  1105. c=0; do {
  1106. OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
  1107. OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+overlap)+overlap, N);
  1108. } while (++c<CC);
  1109. if (enabled)
  1110. {
  1111. VARDECL(opus_val16, pitch_buf);
  1112. ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
  1113. pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC, st->arch);
  1114. /* Don't search for the fir last 1.5 octave of the range because
  1115. there's too many false-positives due to short-term correlation */
  1116. pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
  1117. COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index,
  1118. st->arch);
  1119. pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
  1120. gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
  1121. N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
  1122. if (pitch_index > COMBFILTER_MAXPERIOD-2)
  1123. pitch_index = COMBFILTER_MAXPERIOD-2;
  1124. gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
  1125. /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
  1126. if (st->loss_rate>2)
  1127. gain1 = HALF32(gain1);
  1128. if (st->loss_rate>4)
  1129. gain1 = HALF32(gain1);
  1130. if (st->loss_rate>8)
  1131. gain1 = 0;
  1132. } else {
  1133. gain1 = 0;
  1134. pitch_index = COMBFILTER_MINPERIOD;
  1135. }
  1136. #ifndef DISABLE_FLOAT_API
  1137. if (analysis->valid)
  1138. gain1 = (opus_val16)(gain1 * analysis->max_pitch_ratio);
  1139. #else
  1140. (void)analysis;
  1141. #endif
  1142. /* Gain threshold for enabling the prefilter/postfilter */
  1143. pf_threshold = QCONST16(.2f,15);
  1144. /* Adjusting the threshold based on rate and continuity */
  1145. if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
  1146. pf_threshold += QCONST16(.2f,15);
  1147. if (nbAvailableBytes<25)
  1148. pf_threshold += QCONST16(.1f,15);
  1149. if (nbAvailableBytes<35)
  1150. pf_threshold += QCONST16(.1f,15);
  1151. if (st->prefilter_gain > QCONST16(.4f,15))
  1152. pf_threshold -= QCONST16(.1f,15);
  1153. if (st->prefilter_gain > QCONST16(.55f,15))
  1154. pf_threshold -= QCONST16(.1f,15);
  1155. /* Hard threshold at 0.2 */
  1156. pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
  1157. if (gain1<pf_threshold)
  1158. {
  1159. gain1 = 0;
  1160. pf_on = 0;
  1161. qg = 0;
  1162. } else {
  1163. /*This block is not gated by a total bits check only because
  1164. of the nbAvailableBytes check above.*/
  1165. if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
  1166. gain1=st->prefilter_gain;
  1167. #ifdef FIXED_POINT
  1168. qg = ((gain1+1536)>>10)/3-1;
  1169. #else
  1170. qg = (int)floor(.5f+gain1*32/3)-1;
  1171. #endif
  1172. qg = IMAX(0, IMIN(7, qg));
  1173. gain1 = QCONST16(0.09375f,15)*(qg+1);
  1174. pf_on = 1;
  1175. }
  1176. /*printf("%d %f\n", pitch_index, gain1);*/
  1177. c=0; do {
  1178. int offset = mode->shortMdctSize-overlap;
  1179. st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
  1180. OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap);
  1181. if (offset)
  1182. comb_filter(in+c*(N+overlap)+overlap, pre[c]+COMBFILTER_MAXPERIOD,
  1183. st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
  1184. st->prefilter_tapset, st->prefilter_tapset, NULL, 0, st->arch);
  1185. comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
  1186. st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
  1187. st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
  1188. OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap);
  1189. if (N>COMBFILTER_MAXPERIOD)
  1190. {
  1191. OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
  1192. } else {
  1193. OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
  1194. OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
  1195. }
  1196. } while (++c<CC);
  1197. RESTORE_STACK;
  1198. *gain = gain1;
  1199. *pitch = pitch_index;
  1200. *qgain = qg;
  1201. return pf_on;
  1202. }
  1203. static int compute_vbr(const CELTMode *mode, AnalysisInfo *analysis, opus_int32 base_target,
  1204. int LM, opus_int32 bitrate, int lastCodedBands, int C, int intensity,
  1205. int constrained_vbr, opus_val16 stereo_saving, int tot_boost,
  1206. opus_val16 tf_estimate, int pitch_change, opus_val16 maxDepth,
  1207. int lfe, int has_surround_mask, opus_val16 surround_masking,
  1208. opus_val16 temporal_vbr)
  1209. {
  1210. /* The target rate in 8th bits per frame */
  1211. opus_int32 target;
  1212. int coded_bins;
  1213. int coded_bands;
  1214. opus_val16 tf_calibration;
  1215. int nbEBands;
  1216. const opus_int16 *eBands;
  1217. nbEBands = mode->nbEBands;
  1218. eBands = mode->eBands;
  1219. coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
  1220. coded_bins = eBands[coded_bands]<<LM;
  1221. if (C==2)
  1222. coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM;
  1223. target = base_target;
  1224. /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
  1225. #ifndef DISABLE_FLOAT_API
  1226. if (analysis->valid && analysis->activity<.4)
  1227. target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity));
  1228. #endif
  1229. /* Stereo savings */
  1230. if (C==2)
  1231. {
  1232. int coded_stereo_bands;
  1233. int coded_stereo_dof;
  1234. opus_val16 max_frac;
  1235. coded_stereo_bands = IMIN(intensity, coded_bands);
  1236. coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
  1237. /* Maximum fraction of the bits we can save if the signal is mono. */
  1238. max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
  1239. stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
  1240. /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
  1241. target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
  1242. SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
  1243. }
  1244. /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
  1245. target += tot_boost-(19<<LM);
  1246. /* Apply transient boost, compensating for average boost. */
  1247. tf_calibration = QCONST16(0.044f,14);
  1248. target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1);
  1249. #ifndef DISABLE_FLOAT_API
  1250. /* Apply tonality boost */
  1251. if (analysis->valid && !lfe)
  1252. {
  1253. opus_int32 tonal_target;
  1254. float tonal;
  1255. /* Tonality boost (compensating for the average). */
  1256. tonal = MAX16(0.f,analysis->tonality-.15f)-0.12f;
  1257. tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
  1258. if (pitch_change)
  1259. tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f);
  1260. /*printf("%f %f ", analysis->tonality, tonal);*/
  1261. target = tonal_target;
  1262. }
  1263. #else
  1264. (void)analysis;
  1265. (void)pitch_change;
  1266. #endif
  1267. if (has_surround_mask&&!lfe)
  1268. {
  1269. opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(surround_masking,coded_bins<<BITRES), DB_SHIFT);
  1270. /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
  1271. target = IMAX(target/4, surround_target);
  1272. }
  1273. {
  1274. opus_int32 floor_depth;
  1275. int bins;
  1276. bins = eBands[nbEBands-2]<<LM;
  1277. /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
  1278. floor_depth = (opus_int32)SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_SHIFT);
  1279. floor_depth = IMAX(floor_depth, target>>2);
  1280. target = IMIN(target, floor_depth);
  1281. /*printf("%f %d\n", maxDepth, floor_depth);*/
  1282. }
  1283. /* Make VBR less aggressive for constrained VBR because we can't keep a higher bitrate
  1284. for long. Needs tuning. */
  1285. if ((!has_surround_mask||lfe) && constrained_vbr)
  1286. {
  1287. target = base_target + (opus_int32)MULT16_32_Q15(QCONST16(0.67f, 15), target-base_target);
  1288. }
  1289. if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
  1290. {
  1291. opus_val16 amount;
  1292. opus_val16 tvbr_factor;
  1293. amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
  1294. tvbr_factor = SHR32(MULT16_16(temporal_vbr, amount), DB_SHIFT);
  1295. target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
  1296. }
  1297. /* Don't allow more than doubling the rate */
  1298. target = IMIN(2*base_target, target);
  1299. return target;
  1300. }
  1301. int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
  1302. {
  1303. int i, c, N;
  1304. opus_int32 bits;
  1305. ec_enc _enc;
  1306. VARDECL(celt_sig, in);
  1307. VARDECL(celt_sig, freq);
  1308. VARDECL(celt_norm, X);
  1309. VARDECL(celt_ener, bandE);
  1310. VARDECL(opus_val16, bandLogE);
  1311. VARDECL(opus_val16, bandLogE2);
  1312. VARDECL(int, fine_quant);
  1313. VARDECL(opus_val16, error);
  1314. VARDECL(int, pulses);
  1315. VARDECL(int, cap);
  1316. VARDECL(int, offsets);
  1317. VARDECL(int, importance);
  1318. VARDECL(int, spread_weight);
  1319. VARDECL(int, fine_priority);
  1320. VARDECL(int, tf_res);
  1321. VARDECL(unsigned char, collapse_masks);
  1322. celt_sig *prefilter_mem;
  1323. opus_val16 *oldBandE, *oldLogE, *oldLogE2, *energyError;
  1324. int shortBlocks=0;
  1325. int isTransient=0;
  1326. const int CC = st->channels;
  1327. const int C = st->stream_channels;
  1328. int LM, M;
  1329. int tf_select;
  1330. int nbFilledBytes, nbAvailableBytes;
  1331. int start;
  1332. int end;
  1333. int effEnd;
  1334. int codedBands;
  1335. int alloc_trim;
  1336. int pitch_index=COMBFILTER_MINPERIOD;
  1337. opus_val16 gain1 = 0;
  1338. int dual_stereo=0;
  1339. int effectiveBytes;
  1340. int dynalloc_logp;
  1341. opus_int32 vbr_rate;
  1342. opus_int32 total_bits;
  1343. opus_int32 total_boost;
  1344. opus_int32 balance;
  1345. opus_int32 tell;
  1346. opus_int32 tell0_frac;
  1347. int prefilter_tapset=0;
  1348. int pf_on;
  1349. int anti_collapse_rsv;
  1350. int anti_collapse_on=0;
  1351. int silence=0;
  1352. int tf_chan = 0;
  1353. opus_val16 tf_estimate;
  1354. int pitch_change=0;
  1355. opus_int32 tot_boost;
  1356. opus_val32 sample_max;
  1357. opus_val16 maxDepth;
  1358. const OpusCustomMode *mode;
  1359. int nbEBands;
  1360. int overlap;
  1361. const opus_int16 *eBands;
  1362. int secondMdct;
  1363. int signalBandwidth;
  1364. int transient_got_disabled=0;
  1365. opus_val16 surround_masking=0;
  1366. opus_val16 temporal_vbr=0;
  1367. opus_val16 surround_trim = 0;
  1368. opus_int32 equiv_rate;
  1369. int hybrid;
  1370. int weak_transient = 0;
  1371. int enable_tf_analysis;
  1372. VARDECL(opus_val16, surround_dynalloc);
  1373. ALLOC_STACK;
  1374. mode = st->mode;
  1375. nbEBands = mode->nbEBands;
  1376. overlap = mode->overlap;
  1377. eBands = mode->eBands;
  1378. start = st->start;
  1379. end = st->end;
  1380. hybrid = start != 0;
  1381. tf_estimate = 0;
  1382. if (nbCompressedBytes<2 || pcm==NULL)
  1383. {
  1384. RESTORE_STACK;
  1385. return OPUS_BAD_ARG;
  1386. }
  1387. frame_size *= st->upsample;
  1388. for (LM=0;LM<=mode->maxLM;LM++)
  1389. if (mode->shortMdctSize<<LM==frame_size)
  1390. break;
  1391. if (LM>mode->maxLM)
  1392. {
  1393. RESTORE_STACK;
  1394. return OPUS_BAD_ARG;
  1395. }
  1396. M=1<<LM;
  1397. N = M*mode->shortMdctSize;
  1398. prefilter_mem = st->in_mem+CC*(overlap);
  1399. oldBandE = (opus_val16*)(st->in_mem+CC*(overlap+COMBFILTER_MAXPERIOD));
  1400. oldLogE = oldBandE + CC*nbEBands;
  1401. oldLogE2 = oldLogE + CC*nbEBands;
  1402. energyError = oldLogE2 + CC*nbEBands;
  1403. if (enc==NULL)
  1404. {
  1405. tell0_frac=tell=1;
  1406. nbFilledBytes=0;
  1407. } else {
  1408. tell0_frac=ec_tell_frac(enc);
  1409. tell=ec_tell(enc);
  1410. nbFilledBytes=(tell+4)>>3;
  1411. }
  1412. #ifdef CUSTOM_MODES
  1413. if (st->signalling && enc==NULL)
  1414. {
  1415. int tmp = (mode->effEBands-end)>>1;
  1416. end = st->end = IMAX(1, mode->effEBands-tmp);
  1417. compressed[0] = tmp<<5;
  1418. compressed[0] |= LM<<3;
  1419. compressed[0] |= (C==2)<<2;
  1420. /* Convert "standard mode" to Opus header */
  1421. if (mode->Fs==48000 && mode->shortMdctSize==120)
  1422. {
  1423. int c0 = toOpus(compressed[0]);
  1424. if (c0<0)
  1425. {
  1426. RESTORE_STACK;
  1427. return OPUS_BAD_ARG;
  1428. }
  1429. compressed[0] = c0;
  1430. }
  1431. compressed++;
  1432. nbCompressedBytes--;
  1433. }
  1434. #else
  1435. celt_assert(st->signalling==0);
  1436. #endif
  1437. /* Can't produce more than 1275 output bytes */
  1438. nbCompressedBytes = IMIN(nbCompressedBytes,1275);
  1439. nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
  1440. if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
  1441. {
  1442. opus_int32 den=mode->Fs>>BITRES;
  1443. vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
  1444. #ifdef CUSTOM_MODES
  1445. if (st->signalling)
  1446. vbr_rate -= 8<<BITRES;
  1447. #endif
  1448. effectiveBytes = vbr_rate>>(3+BITRES);
  1449. } else {
  1450. opus_int32 tmp;
  1451. vbr_rate = 0;
  1452. tmp = st->bitrate*frame_size;
  1453. if (tell>1)
  1454. tmp += tell;
  1455. if (st->bitrate!=OPUS_BITRATE_MAX)
  1456. nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
  1457. (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
  1458. effectiveBytes = nbCompressedBytes - nbFilledBytes;
  1459. }
  1460. equiv_rate = ((opus_int32)nbCompressedBytes*8*50 >> (3-LM)) - (40*C+20)*((400>>LM) - 50);
  1461. if (st->bitrate != OPUS_BITRATE_MAX)
  1462. equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
  1463. if (enc==NULL)
  1464. {
  1465. ec_enc_init(&_enc, compressed, nbCompressedBytes);
  1466. enc = &_enc;
  1467. }
  1468. if (vbr_rate>0)
  1469. {
  1470. /* Computes the max bit-rate allowed in VBR mode to avoid violating the
  1471. target rate and buffering.
  1472. We must do this up front so that bust-prevention logic triggers
  1473. correctly if we don't have enough bits. */
  1474. if (st->constrained_vbr)
  1475. {
  1476. opus_int32 vbr_bound;
  1477. opus_int32 max_allowed;
  1478. /* We could use any multiple of vbr_rate as bound (depending on the
  1479. delay).
  1480. This is clamped to ensure we use at least two bytes if the encoder
  1481. was entirely empty, but to allow 0 in hybrid mode. */
  1482. vbr_bound = vbr_rate;
  1483. max_allowed = IMIN(IMAX(tell==1?2:0,
  1484. (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
  1485. nbAvailableBytes);
  1486. if(max_allowed < nbAvailableBytes)
  1487. {
  1488. nbCompressedBytes = nbFilledBytes+max_allowed;
  1489. nbAvailableBytes = max_allowed;
  1490. ec_enc_shrink(enc, nbCompressedBytes);
  1491. }
  1492. }
  1493. }
  1494. total_bits = nbCompressedBytes*8;
  1495. effEnd = end;
  1496. if (effEnd > mode->effEBands)
  1497. effEnd = mode->effEBands;
  1498. ALLOC(in, CC*(N+overlap), celt_sig);
  1499. sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsample));
  1500. st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
  1501. sample_max=MAX32(sample_max, st->overlap_max);
  1502. #ifdef FIXED_POINT
  1503. silence = (sample_max==0);
  1504. #else
  1505. silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
  1506. #endif
  1507. #ifdef FUZZING
  1508. if ((rand()&0x3F)==0)
  1509. silence = 1;
  1510. #endif
  1511. if (tell==1)
  1512. ec_enc_bit_logp(enc, silence, 15);
  1513. else
  1514. silence=0;
  1515. if (silence)
  1516. {
  1517. /*In VBR mode there is no need to send more than the minimum. */
  1518. if (vbr_rate>0)
  1519. {
  1520. effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
  1521. total_bits=nbCompressedBytes*8;
  1522. nbAvailableBytes=2;
  1523. ec_enc_shrink(enc, nbCompressedBytes);
  1524. }
  1525. /* Pretend we've filled all the remaining bits with zeros
  1526. (that's what the initialiser did anyway) */
  1527. tell = nbCompressedBytes*8;
  1528. enc->nbits_total+=tell-ec_tell(enc);
  1529. }
  1530. c=0; do {
  1531. int need_clip=0;
  1532. #ifndef FIXED_POINT
  1533. need_clip = st->clip && sample_max>65536.f;
  1534. #endif
  1535. celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
  1536. mode->preemph, st->preemph_memE+c, need_clip);
  1537. } while (++c<CC);
  1538. /* Find pitch period and gain */
  1539. {
  1540. int enabled;
  1541. int qg;
  1542. enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && !st->disable_pf
  1543. && st->complexity >= 5;
  1544. prefilter_tapset = st->tapset_decision;
  1545. pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pitch_index, &gain1, &qg, enabled, nbAvailableBytes, &st->analysis);
  1546. if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
  1547. && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
  1548. pitch_change = 1;
  1549. if (pf_on==0)
  1550. {
  1551. if(!hybrid && tell+16<=total_bits)
  1552. ec_enc_bit_logp(enc, 0, 1);
  1553. } else {
  1554. /*This block is not gated by a total bits check only because
  1555. of the nbAvailableBytes check above.*/
  1556. int octave;
  1557. ec_enc_bit_logp(enc, 1, 1);
  1558. pitch_index += 1;
  1559. octave = EC_ILOG(pitch_index)-5;
  1560. ec_enc_uint(enc, octave, 6);
  1561. ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
  1562. pitch_index -= 1;
  1563. ec_enc_bits(enc, qg, 3);
  1564. ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
  1565. }
  1566. }
  1567. isTransient = 0;
  1568. shortBlocks = 0;
  1569. if (st->complexity >= 1 && !st->lfe)
  1570. {
  1571. /* Reduces the likelihood of energy instability on fricatives at low bitrate
  1572. in hybrid mode. It seems like we still want to have real transients on vowels
  1573. though (small SILK quantization offset value). */
  1574. int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.signalType != 2;
  1575. isTransient = transient_analysis(in, N+overlap, CC,
  1576. &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient);
  1577. }
  1578. if (LM>0 && ec_tell(enc)+3<=total_bits)
  1579. {
  1580. if (isTransient)
  1581. shortBlocks = M;
  1582. } else {
  1583. isTransient = 0;
  1584. transient_got_disabled=1;
  1585. }
  1586. ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
  1587. ALLOC(bandE,nbEBands*CC, celt_ener);
  1588. ALLOC(bandLogE,nbEBands*CC, opus_val16);
  1589. secondMdct = shortBlocks && st->complexity>=8;
  1590. ALLOC(bandLogE2, C*nbEBands, opus_val16);
  1591. if (secondMdct)
  1592. {
  1593. compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
  1594. compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
  1595. amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
  1596. for (i=0;i<C*nbEBands;i++)
  1597. bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
  1598. }
  1599. compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
  1600. /* This should catch any NaN in the CELT input. Since we're not supposed to see any (they're filtered
  1601. at the Opus layer), just abort. */
  1602. celt_assert(!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N])));
  1603. if (CC==2&&C==1)
  1604. tf_chan = 0;
  1605. compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
  1606. if (st->lfe)
  1607. {
  1608. for (i=2;i<end;i++)
  1609. {
  1610. bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
  1611. bandE[i] = MAX32(bandE[i], EPSILON);
  1612. }
  1613. }
  1614. amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
  1615. ALLOC(surround_dynalloc, C*nbEBands, opus_val16);
  1616. OPUS_CLEAR(surround_dynalloc, end);
  1617. /* This computes how much masking takes place between surround channels */
  1618. if (!hybrid&&st->energy_mask&&!st->lfe)
  1619. {
  1620. int mask_end;
  1621. int midband;
  1622. int count_dynalloc;
  1623. opus_val32 mask_avg=0;
  1624. opus_val32 diff=0;
  1625. int count=0;
  1626. mask_end = IMAX(2,st->lastCodedBands);
  1627. for (c=0;c<C;c++)
  1628. {
  1629. for(i=0;i<mask_end;i++)
  1630. {
  1631. opus_val16 mask;
  1632. mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i],
  1633. QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
  1634. if (mask > 0)
  1635. mask = HALF16(mask);
  1636. mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]);
  1637. count += eBands[i+1]-eBands[i];
  1638. diff += MULT16_16(mask, 1+2*i-mask_end);
  1639. }
  1640. }
  1641. celt_assert(count>0);
  1642. mask_avg = DIV32_16(mask_avg,count);
  1643. mask_avg += QCONST16(.2f, DB_SHIFT);
  1644. diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end);
  1645. /* Again, being conservative */
  1646. diff = HALF32(diff);
  1647. diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_SHIFT));
  1648. /* Find the band that's in the middle of the coded spectrum */
  1649. for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
  1650. count_dynalloc=0;
  1651. for(i=0;i<mask_end;i++)
  1652. {
  1653. opus_val32 lin;
  1654. opus_val16 unmask;
  1655. lin = mask_avg + diff*(i-midband);
  1656. if (C==2)
  1657. unmask = MAX16(st->energy_mask[i], st->energy_mask[nbEBands+i]);
  1658. else
  1659. unmask = st->energy_mask[i];
  1660. unmask = MIN16(unmask, QCONST16(.0f, DB_SHIFT));
  1661. unmask -= lin;
  1662. if (unmask > QCONST16(.25f, DB_SHIFT))
  1663. {
  1664. surround_dynalloc[i] = unmask - QCONST16(.25f, DB_SHIFT);
  1665. count_dynalloc++;
  1666. }
  1667. }
  1668. if (count_dynalloc>=3)
  1669. {
  1670. /* If we need dynalloc in many bands, it's probably because our
  1671. initial masking rate was too low. */
  1672. mask_avg += QCONST16(.25f, DB_SHIFT);
  1673. if (mask_avg>0)
  1674. {
  1675. /* Something went really wrong in the original calculations,
  1676. disabling masking. */
  1677. mask_avg = 0;
  1678. diff = 0;
  1679. OPUS_CLEAR(surround_dynalloc, mask_end);
  1680. } else {
  1681. for(i=0;i<mask_end;i++)
  1682. surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25f, DB_SHIFT));
  1683. }
  1684. }
  1685. mask_avg += QCONST16(.2f, DB_SHIFT);
  1686. /* Convert to 1/64th units used for the trim */
  1687. surround_trim = 64*diff;
  1688. /*printf("%d %d ", mask_avg, surround_trim);*/
  1689. surround_masking = mask_avg;
  1690. }
  1691. /* Temporal VBR (but not for LFE) */
  1692. if (!st->lfe)
  1693. {
  1694. opus_val16 follow=-QCONST16(10.0f,DB_SHIFT);
  1695. opus_val32 frame_avg=0;
  1696. opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0;
  1697. for(i=start;i<end;i++)
  1698. {
  1699. follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset);
  1700. if (C==2)
  1701. follow = MAX16(follow, bandLogE[i+nbEBands]-offset);
  1702. frame_avg += follow;
  1703. }
  1704. frame_avg /= (end-start);
  1705. temporal_vbr = SUB16(frame_avg,st->spec_avg);
  1706. temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHIFT), temporal_vbr));
  1707. st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr);
  1708. }
  1709. /*for (i=0;i<21;i++)
  1710. printf("%f ", bandLogE[i]);
  1711. printf("\n");*/
  1712. if (!secondMdct)
  1713. {
  1714. OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
  1715. }
  1716. /* Last chance to catch any transient we might have missed in the
  1717. time-domain analysis */
  1718. if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
  1719. {
  1720. if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
  1721. {
  1722. isTransient = 1;
  1723. shortBlocks = M;
  1724. compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
  1725. compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
  1726. amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
  1727. /* Compensate for the scaling of short vs long mdcts */
  1728. for (i=0;i<C*nbEBands;i++)
  1729. bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
  1730. tf_estimate = QCONST16(.2f,14);
  1731. }
  1732. }
  1733. if (LM>0 && ec_tell(enc)+3<=total_bits)
  1734. ec_enc_bit_logp(enc, isTransient, 3);
  1735. ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
  1736. /* Band normalisation */
  1737. normalise_bands(mode, freq, X, bandE, effEnd, C, M);
  1738. enable_tf_analysis = effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe;
  1739. ALLOC(offsets, nbEBands, int);
  1740. ALLOC(importance, nbEBands, int);
  1741. ALLOC(spread_weight, nbEBands, int);
  1742. maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, start, end, C, offsets,
  1743. st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
  1744. eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis, importance, spread_weight);
  1745. ALLOC(tf_res, nbEBands, int);
  1746. /* Disable variable tf resolution for hybrid and at very low bitrate */
  1747. if (enable_tf_analysis)
  1748. {
  1749. int lambda;
  1750. lambda = IMAX(80, 20480/effectiveBytes + 2);
  1751. tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan, importance);
  1752. for (i=effEnd;i<end;i++)
  1753. tf_res[i] = tf_res[effEnd-1];
  1754. } else if (hybrid && weak_transient)
  1755. {
  1756. /* For weak transients, we rely on the fact that improving time resolution using
  1757. TF on a long window is imperfect and will not result in an energy collapse at
  1758. low bitrate. */
  1759. for (i=0;i<end;i++)
  1760. tf_res[i] = 1;
  1761. tf_select=0;
  1762. } else if (hybrid && effectiveBytes<15 && st->silk_info.signalType != 2)
  1763. {
  1764. /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
  1765. for (i=0;i<end;i++)
  1766. tf_res[i] = 0;
  1767. tf_select=isTransient;
  1768. } else {
  1769. for (i=0;i<end;i++)
  1770. tf_res[i] = isTransient;
  1771. tf_select=0;
  1772. }
  1773. ALLOC(error, C*nbEBands, opus_val16);
  1774. c=0;
  1775. do {
  1776. for (i=start;i<end;i++)
  1777. {
  1778. /* When the energy is stable, slightly bias energy quantization towards
  1779. the previous error to make the gain more stable (a constant offset is
  1780. better than fluctuations). */
  1781. if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands])) < QCONST16(2.f, DB_SHIFT))
  1782. {
  1783. bandLogE[i+c*nbEBands] -= MULT16_16_Q15(energyError[i+c*nbEBands], QCONST16(0.25f, 15));
  1784. }
  1785. }
  1786. } while (++c < C);
  1787. quant_coarse_energy(mode, start, end, effEnd, bandLogE,
  1788. oldBandE, total_bits, error, enc,
  1789. C, LM, nbAvailableBytes, st->force_intra,
  1790. &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
  1791. tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
  1792. if (ec_tell(enc)+4<=total_bits)
  1793. {
  1794. if (st->lfe)
  1795. {
  1796. st->tapset_decision = 0;
  1797. st->spread_decision = SPREAD_NORMAL;
  1798. } else if (hybrid)
  1799. {
  1800. if (st->complexity == 0)
  1801. st->spread_decision = SPREAD_NONE;
  1802. else if (isTransient)
  1803. st->spread_decision = SPREAD_NORMAL;
  1804. else
  1805. st->spread_decision = SPREAD_AGGRESSIVE;
  1806. } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
  1807. {
  1808. if (st->complexity == 0)
  1809. st->spread_decision = SPREAD_NONE;
  1810. else
  1811. st->spread_decision = SPREAD_NORMAL;
  1812. } else {
  1813. /* Disable new spreading+tapset estimator until we can show it works
  1814. better than the old one. So far it seems like spreading_decision()
  1815. works best. */
  1816. #if 0
  1817. if (st->analysis.valid)
  1818. {
  1819. static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
  1820. static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
  1821. static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
  1822. static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
  1823. st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
  1824. st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
  1825. } else
  1826. #endif
  1827. {
  1828. st->spread_decision = spreading_decision(mode, X,
  1829. &st->tonal_average, st->spread_decision, &st->hf_average,
  1830. &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M, spread_weight);
  1831. }
  1832. /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
  1833. /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
  1834. }
  1835. ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
  1836. }
  1837. /* For LFE, everything interesting is in the first band */
  1838. if (st->lfe)
  1839. offsets[0] = IMIN(8, effectiveBytes/3);
  1840. ALLOC(cap, nbEBands, int);
  1841. init_caps(mode,cap,LM,C);
  1842. dynalloc_logp = 6;
  1843. total_bits<<=BITRES;
  1844. total_boost = 0;
  1845. tell = ec_tell_frac(enc);
  1846. for (i=start;i<end;i++)
  1847. {
  1848. int width, quanta;
  1849. int dynalloc_loop_logp;
  1850. int boost;
  1851. int j;
  1852. width = C*(eBands[i+1]-eBands[i])<<LM;
  1853. /* quanta is 6 bits, but no more than 1 bit/sample
  1854. and no less than 1/8 bit/sample */
  1855. quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
  1856. dynalloc_loop_logp = dynalloc_logp;
  1857. boost = 0;
  1858. for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
  1859. && boost < cap[i]; j++)
  1860. {
  1861. int flag;
  1862. flag = j<offsets[i];
  1863. ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
  1864. tell = ec_tell_frac(enc);
  1865. if (!flag)
  1866. break;
  1867. boost += quanta;
  1868. total_boost += quanta;
  1869. dynalloc_loop_logp = 1;
  1870. }
  1871. /* Making dynalloc more likely */
  1872. if (j)
  1873. dynalloc_logp = IMAX(2, dynalloc_logp-1);
  1874. offsets[i] = boost;
  1875. }
  1876. if (C==2)
  1877. {
  1878. static const opus_val16 intensity_thresholds[21]=
  1879. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
  1880. { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
  1881. static const opus_val16 intensity_histeresis[21]=
  1882. { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8};
  1883. /* Always use MS for 2.5 ms frames until we can do a better analysis */
  1884. if (LM!=0)
  1885. dual_stereo = stereo_analysis(mode, X, LM, N);
  1886. st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
  1887. intensity_thresholds, intensity_histeresis, 21, st->intensity);
  1888. st->intensity = IMIN(end,IMAX(start, st->intensity));
  1889. }
  1890. alloc_trim = 5;
  1891. if (tell+(6<<BITRES) <= total_bits - total_boost)
  1892. {
  1893. if (start > 0 || st->lfe)
  1894. {
  1895. st->stereo_saving = 0;
  1896. alloc_trim = 5;
  1897. } else {
  1898. alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
  1899. end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
  1900. st->intensity, surround_trim, equiv_rate, st->arch);
  1901. }
  1902. ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
  1903. tell = ec_tell_frac(enc);
  1904. }
  1905. /* Variable bitrate */
  1906. if (vbr_rate>0)
  1907. {
  1908. opus_val16 alpha;
  1909. opus_int32 delta;
  1910. /* The target rate in 8th bits per frame */
  1911. opus_int32 target, base_target;
  1912. opus_int32 min_allowed;
  1913. int lm_diff = mode->maxLM - LM;
  1914. /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
  1915. The CELT allocator will just not be able to use more than that anyway. */
  1916. nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
  1917. if (!hybrid)
  1918. {
  1919. base_target = vbr_rate - ((40*C+20)<<BITRES);
  1920. } else {
  1921. base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES));
  1922. }
  1923. if (st->constrained_vbr)
  1924. base_target += (st->vbr_offset>>lm_diff);
  1925. if (!hybrid)
  1926. {
  1927. target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
  1928. st->lastCodedBands, C, st->intensity, st->constrained_vbr,
  1929. st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
  1930. st->lfe, st->energy_mask!=NULL, surround_masking,
  1931. temporal_vbr);
  1932. } else {
  1933. target = base_target;
  1934. /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
  1935. if (st->silk_info.offset < 100) target += 12 << BITRES >> (3-LM);
  1936. if (st->silk_info.offset > 100) target -= 18 << BITRES >> (3-LM);
  1937. /* Boosting bitrate on transients and vowels with significant temporal
  1938. spikes. */
  1939. target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES));
  1940. /* If we have a strong transient, let's make sure it has enough bits to code
  1941. the first two bands, so that it can use folding rather than noise. */
  1942. if (tf_estimate > QCONST16(.7f,14))
  1943. target = IMAX(target, 50<<BITRES);
  1944. }
  1945. /* The current offset is removed from the target and the space used
  1946. so far is added*/
  1947. target=target+tell;
  1948. /* In VBR mode the frame size must not be reduced so much that it would
  1949. result in the encoder running out of bits.
  1950. The margin of 2 bytes ensures that none of the bust-prevention logic
  1951. in the decoder will have triggered so far. */
  1952. min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2;
  1953. /* Take into account the 37 bits we need to have left in the packet to
  1954. signal a redundant frame in hybrid mode. Creating a shorter packet would
  1955. create an entropy coder desync. */
  1956. if (hybrid)
  1957. min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3));
  1958. nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
  1959. nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
  1960. nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
  1961. /* By how much did we "miss" the target on that frame */
  1962. delta = target - vbr_rate;
  1963. target=nbAvailableBytes<<(BITRES+3);
  1964. /*If the frame is silent we don't adjust our drift, otherwise
  1965. the encoder will shoot to very high rates after hitting a
  1966. span of silence, but we do allow the bitres to refill.
  1967. This means that we'll undershoot our target in CVBR/VBR modes
  1968. on files with lots of silence. */
  1969. if(silence)
  1970. {
  1971. nbAvailableBytes = 2;
  1972. target = 2*8<<BITRES;
  1973. delta = 0;
  1974. }
  1975. if (st->vbr_count < 970)
  1976. {
  1977. st->vbr_count++;
  1978. alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
  1979. } else
  1980. alpha = QCONST16(.001f,15);
  1981. /* How many bits have we used in excess of what we're allowed */
  1982. if (st->constrained_vbr)
  1983. st->vbr_reservoir += target - vbr_rate;
  1984. /*printf ("%d\n", st->vbr_reservoir);*/
  1985. /* Compute the offset we need to apply in order to reach the target */
  1986. if (st->constrained_vbr)
  1987. {
  1988. st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
  1989. st->vbr_offset = -st->vbr_drift;
  1990. }
  1991. /*printf ("%d\n", st->vbr_drift);*/
  1992. if (st->constrained_vbr && st->vbr_reservoir < 0)
  1993. {
  1994. /* We're under the min value -- increase rate */
  1995. int adjust = (-st->vbr_reservoir)/(8<<BITRES);
  1996. /* Unless we're just coding silence */
  1997. nbAvailableBytes += silence?0:adjust;
  1998. st->vbr_reservoir = 0;
  1999. /*printf ("+%d\n", adjust);*/
  2000. }
  2001. nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
  2002. /*printf("%d\n", nbCompressedBytes*50*8);*/
  2003. /* This moves the raw bits to take into account the new compressed size */
  2004. ec_enc_shrink(enc, nbCompressedBytes);
  2005. }
  2006. /* Bit allocation */
  2007. ALLOC(fine_quant, nbEBands, int);
  2008. ALLOC(pulses, nbEBands, int);
  2009. ALLOC(fine_priority, nbEBands, int);
  2010. /* bits = packet size - where we are - safety*/
  2011. bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
  2012. anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
  2013. bits -= anti_collapse_rsv;
  2014. signalBandwidth = end-1;
  2015. #ifndef DISABLE_FLOAT_API
  2016. if (st->analysis.valid)
  2017. {
  2018. int min_bandwidth;
  2019. if (equiv_rate < (opus_int32)32000*C)
  2020. min_bandwidth = 13;
  2021. else if (equiv_rate < (opus_int32)48000*C)
  2022. min_bandwidth = 16;
  2023. else if (equiv_rate < (opus_int32)60000*C)
  2024. min_bandwidth = 18;
  2025. else if (equiv_rate < (opus_int32)80000*C)
  2026. min_bandwidth = 19;
  2027. else
  2028. min_bandwidth = 20;
  2029. signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
  2030. }
  2031. #endif
  2032. if (st->lfe)
  2033. signalBandwidth = 1;
  2034. codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
  2035. alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
  2036. fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
  2037. if (st->lastCodedBands)
  2038. st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
  2039. else
  2040. st->lastCodedBands = codedBands;
  2041. quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, enc, C);
  2042. /* Residual quantisation */
  2043. ALLOC(collapse_masks, C*nbEBands, unsigned char);
  2044. quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
  2045. bandE, pulses, shortBlocks, st->spread_decision,
  2046. dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
  2047. balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv);
  2048. if (anti_collapse_rsv > 0)
  2049. {
  2050. anti_collapse_on = st->consec_transient<2;
  2051. #ifdef FUZZING
  2052. anti_collapse_on = rand()&0x1;
  2053. #endif
  2054. ec_enc_bits(enc, anti_collapse_on, 1);
  2055. }
  2056. quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
  2057. OPUS_CLEAR(energyError, nbEBands*CC);
  2058. c=0;
  2059. do {
  2060. for (i=start;i<end;i++)
  2061. {
  2062. energyError[i+c*nbEBands] = MAX16(-QCONST16(0.5f, 15), MIN16(QCONST16(0.5f, 15), error[i+c*nbEBands]));
  2063. }
  2064. } while (++c < C);
  2065. if (silence)
  2066. {
  2067. for (i=0;i<C*nbEBands;i++)
  2068. oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
  2069. }
  2070. #ifdef RESYNTH
  2071. /* Re-synthesis of the coded audio if required */
  2072. {
  2073. celt_sig *out_mem[2];
  2074. if (anti_collapse_on)
  2075. {
  2076. anti_collapse(mode, X, collapse_masks, LM, C, N,
  2077. start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
  2078. }
  2079. c=0; do {
  2080. OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2);
  2081. } while (++c<CC);
  2082. c=0; do {
  2083. out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N;
  2084. } while (++c<CC);
  2085. celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
  2086. C, CC, isTransient, LM, st->upsample, silence, st->arch);
  2087. c=0; do {
  2088. st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
  2089. st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
  2090. comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
  2091. st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
  2092. mode->window, overlap);
  2093. if (LM!=0)
  2094. comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
  2095. st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
  2096. mode->window, overlap);
  2097. } while (++c<CC);
  2098. /* We reuse freq[] as scratch space for the de-emphasis */
  2099. deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD);
  2100. st->prefilter_period_old = st->prefilter_period;
  2101. st->prefilter_gain_old = st->prefilter_gain;
  2102. st->prefilter_tapset_old = st->prefilter_tapset;
  2103. }
  2104. #endif
  2105. st->prefilter_period = pitch_index;
  2106. st->prefilter_gain = gain1;
  2107. st->prefilter_tapset = prefilter_tapset;
  2108. #ifdef RESYNTH
  2109. if (LM!=0)
  2110. {
  2111. st->prefilter_period_old = st->prefilter_period;
  2112. st->prefilter_gain_old = st->prefilter_gain;
  2113. st->prefilter_tapset_old = st->prefilter_tapset;
  2114. }
  2115. #endif
  2116. if (CC==2&&C==1) {
  2117. OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
  2118. }
  2119. if (!isTransient)
  2120. {
  2121. OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
  2122. OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
  2123. } else {
  2124. for (i=0;i<CC*nbEBands;i++)
  2125. oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
  2126. }
  2127. /* In case start or end were to change */
  2128. c=0; do
  2129. {
  2130. for (i=0;i<start;i++)
  2131. {
  2132. oldBandE[c*nbEBands+i]=0;
  2133. oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
  2134. }
  2135. for (i=end;i<nbEBands;i++)
  2136. {
  2137. oldBandE[c*nbEBands+i]=0;
  2138. oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
  2139. }
  2140. } while (++c<CC);
  2141. if (isTransient || transient_got_disabled)
  2142. st->consec_transient++;
  2143. else
  2144. st->consec_transient=0;
  2145. st->rng = enc->rng;
  2146. /* If there's any room left (can only happen for very high rates),
  2147. it's already filled with zeros */
  2148. ec_enc_done(enc);
  2149. #ifdef CUSTOM_MODES
  2150. if (st->signalling)
  2151. nbCompressedBytes++;
  2152. #endif
  2153. RESTORE_STACK;
  2154. if (ec_get_error(enc))
  2155. return OPUS_INTERNAL_ERROR;
  2156. else
  2157. return nbCompressedBytes;
  2158. }
  2159. #ifdef CUSTOM_MODES
  2160. #ifdef FIXED_POINT
  2161. int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  2162. {
  2163. return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
  2164. }
  2165. #ifndef DISABLE_FLOAT_API
  2166. int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  2167. {
  2168. int j, ret, C, N;
  2169. VARDECL(opus_int16, in);
  2170. ALLOC_STACK;
  2171. if (pcm==NULL)
  2172. return OPUS_BAD_ARG;
  2173. C = st->channels;
  2174. N = frame_size;
  2175. ALLOC(in, C*N, opus_int16);
  2176. for (j=0;j<C*N;j++)
  2177. in[j] = FLOAT2INT16(pcm[j]);
  2178. ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
  2179. #ifdef RESYNTH
  2180. for (j=0;j<C*N;j++)
  2181. ((float*)pcm)[j]=in[j]*(1.f/32768.f);
  2182. #endif
  2183. RESTORE_STACK;
  2184. return ret;
  2185. }
  2186. #endif /* DISABLE_FLOAT_API */
  2187. #else
  2188. int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  2189. {
  2190. int j, ret, C, N;
  2191. VARDECL(celt_sig, in);
  2192. ALLOC_STACK;
  2193. if (pcm==NULL)
  2194. return OPUS_BAD_ARG;
  2195. C=st->channels;
  2196. N=frame_size;
  2197. ALLOC(in, C*N, celt_sig);
  2198. for (j=0;j<C*N;j++) {
  2199. in[j] = SCALEOUT(pcm[j]);
  2200. }
  2201. ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
  2202. #ifdef RESYNTH
  2203. for (j=0;j<C*N;j++)
  2204. ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
  2205. #endif
  2206. RESTORE_STACK;
  2207. return ret;
  2208. }
  2209. int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
  2210. {
  2211. return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
  2212. }
  2213. #endif
  2214. #endif /* CUSTOM_MODES */
  2215. int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
  2216. {
  2217. va_list ap;
  2218. va_start(ap, request);
  2219. switch (request)
  2220. {
  2221. case OPUS_SET_COMPLEXITY_REQUEST:
  2222. {
  2223. int value = va_arg(ap, opus_int32);
  2224. if (value<0 || value>10)
  2225. goto bad_arg;
  2226. st->complexity = value;
  2227. }
  2228. break;
  2229. case CELT_SET_START_BAND_REQUEST:
  2230. {
  2231. opus_int32 value = va_arg(ap, opus_int32);
  2232. if (value<0 || value>=st->mode->nbEBands)
  2233. goto bad_arg;
  2234. st->start = value;
  2235. }
  2236. break;
  2237. case CELT_SET_END_BAND_REQUEST:
  2238. {
  2239. opus_int32 value = va_arg(ap, opus_int32);
  2240. if (value<1 || value>st->mode->nbEBands)
  2241. goto bad_arg;
  2242. st->end = value;
  2243. }
  2244. break;
  2245. case CELT_SET_PREDICTION_REQUEST:
  2246. {
  2247. int value = va_arg(ap, opus_int32);
  2248. if (value<0 || value>2)
  2249. goto bad_arg;
  2250. st->disable_pf = value<=1;
  2251. st->force_intra = value==0;
  2252. }
  2253. break;
  2254. case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
  2255. {
  2256. int value = va_arg(ap, opus_int32);
  2257. if (value<0 || value>100)
  2258. goto bad_arg;
  2259. st->loss_rate = value;
  2260. }
  2261. break;
  2262. case OPUS_SET_VBR_CONSTRAINT_REQUEST:
  2263. {
  2264. opus_int32 value = va_arg(ap, opus_int32);
  2265. st->constrained_vbr = value;
  2266. }
  2267. break;
  2268. case OPUS_SET_VBR_REQUEST:
  2269. {
  2270. opus_int32 value = va_arg(ap, opus_int32);
  2271. st->vbr = value;
  2272. }
  2273. break;
  2274. case OPUS_SET_BITRATE_REQUEST:
  2275. {
  2276. opus_int32 value = va_arg(ap, opus_int32);
  2277. if (value<=500 && value!=OPUS_BITRATE_MAX)
  2278. goto bad_arg;
  2279. value = IMIN(value, 260000*st->channels);
  2280. st->bitrate = value;
  2281. }
  2282. break;
  2283. case CELT_SET_CHANNELS_REQUEST:
  2284. {
  2285. opus_int32 value = va_arg(ap, opus_int32);
  2286. if (value<1 || value>2)
  2287. goto bad_arg;
  2288. st->stream_channels = value;
  2289. }
  2290. break;
  2291. case OPUS_SET_LSB_DEPTH_REQUEST:
  2292. {
  2293. opus_int32 value = va_arg(ap, opus_int32);
  2294. if (value<8 || value>24)
  2295. goto bad_arg;
  2296. st->lsb_depth=value;
  2297. }
  2298. break;
  2299. case OPUS_GET_LSB_DEPTH_REQUEST:
  2300. {
  2301. opus_int32 *value = va_arg(ap, opus_int32*);
  2302. *value=st->lsb_depth;
  2303. }
  2304. break;
  2305. case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
  2306. {
  2307. opus_int32 value = va_arg(ap, opus_int32);
  2308. if(value<0 || value>1)
  2309. {
  2310. goto bad_arg;
  2311. }
  2312. st->disable_inv = value;
  2313. }
  2314. break;
  2315. case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
  2316. {
  2317. opus_int32 *value = va_arg(ap, opus_int32*);
  2318. if (!value)
  2319. {
  2320. goto bad_arg;
  2321. }
  2322. *value = st->disable_inv;
  2323. }
  2324. break;
  2325. case OPUS_RESET_STATE:
  2326. {
  2327. int i;
  2328. opus_val16 *oldBandE, *oldLogE, *oldLogE2;
  2329. oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->mode->overlap+COMBFILTER_MAXPERIOD));
  2330. oldLogE = oldBandE + st->channels*st->mode->nbEBands;
  2331. oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
  2332. OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
  2333. opus_custom_encoder_get_size(st->mode, st->channels)-
  2334. ((char*)&st->ENCODER_RESET_START - (char*)st));
  2335. for (i=0;i<st->channels*st->mode->nbEBands;i++)
  2336. oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
  2337. st->vbr_offset = 0;
  2338. st->delayedIntra = 1;
  2339. st->spread_decision = SPREAD_NORMAL;
  2340. st->tonal_average = 256;
  2341. st->hf_average = 0;
  2342. st->tapset_decision = 0;
  2343. }
  2344. break;
  2345. #ifdef CUSTOM_MODES
  2346. case CELT_SET_INPUT_CLIPPING_REQUEST:
  2347. {
  2348. opus_int32 value = va_arg(ap, opus_int32);
  2349. st->clip = value;
  2350. }
  2351. break;
  2352. #endif
  2353. case CELT_SET_SIGNALLING_REQUEST:
  2354. {
  2355. opus_int32 value = va_arg(ap, opus_int32);
  2356. st->signalling = value;
  2357. }
  2358. break;
  2359. case CELT_SET_ANALYSIS_REQUEST:
  2360. {
  2361. AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
  2362. if (info)
  2363. OPUS_COPY(&st->analysis, info, 1);
  2364. }
  2365. break;
  2366. case CELT_SET_SILK_INFO_REQUEST:
  2367. {
  2368. SILKInfo *info = va_arg(ap, SILKInfo *);
  2369. if (info)
  2370. OPUS_COPY(&st->silk_info, info, 1);
  2371. }
  2372. break;
  2373. case CELT_GET_MODE_REQUEST:
  2374. {
  2375. const CELTMode ** value = va_arg(ap, const CELTMode**);
  2376. if (value==0)
  2377. goto bad_arg;
  2378. *value=st->mode;
  2379. }
  2380. break;
  2381. case OPUS_GET_FINAL_RANGE_REQUEST:
  2382. {
  2383. opus_uint32 * value = va_arg(ap, opus_uint32 *);
  2384. if (value==0)
  2385. goto bad_arg;
  2386. *value=st->rng;
  2387. }
  2388. break;
  2389. case OPUS_SET_LFE_REQUEST:
  2390. {
  2391. opus_int32 value = va_arg(ap, opus_int32);
  2392. st->lfe = value;
  2393. }
  2394. break;
  2395. case OPUS_SET_ENERGY_MASK_REQUEST:
  2396. {
  2397. opus_val16 *value = va_arg(ap, opus_val16*);
  2398. st->energy_mask = value;
  2399. }
  2400. break;
  2401. default:
  2402. goto bad_request;
  2403. }
  2404. va_end(ap);
  2405. return OPUS_OK;
  2406. bad_arg:
  2407. va_end(ap);
  2408. return OPUS_BAD_ARG;
  2409. bad_request:
  2410. va_end(ap);
  2411. return OPUS_UNIMPLEMENTED;
  2412. }