2
0

opusenc_psy.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Opus encoder
  3. * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "opusenc_psy.h"
  22. #include "opus_pvq.h"
  23. #include "opustab.h"
  24. #include "mdct15.h"
  25. #include "libavutil/qsort.h"
  26. static float pvq_band_cost(CeltPVQ *pvq, CeltFrame *f, OpusRangeCoder *rc, int band,
  27. float *bits, float lambda)
  28. {
  29. int i, b = 0;
  30. uint32_t cm[2] = { (1 << f->blocks) - 1, (1 << f->blocks) - 1 };
  31. const int band_size = ff_celt_freq_range[band] << f->size;
  32. float buf[176 * 2], lowband_scratch[176], norm1[176], norm2[176];
  33. float dist, cost, err_x = 0.0f, err_y = 0.0f;
  34. float *X = buf;
  35. float *X_orig = f->block[0].coeffs + (ff_celt_freq_bands[band] << f->size);
  36. float *Y = (f->channels == 2) ? &buf[176] : NULL;
  37. float *Y_orig = f->block[1].coeffs + (ff_celt_freq_bands[band] << f->size);
  38. OPUS_RC_CHECKPOINT_SPAWN(rc);
  39. memcpy(X, X_orig, band_size*sizeof(float));
  40. if (Y)
  41. memcpy(Y, Y_orig, band_size*sizeof(float));
  42. f->remaining2 = ((f->framebits << 3) - f->anticollapse_needed) - opus_rc_tell_frac(rc) - 1;
  43. if (band <= f->coded_bands - 1) {
  44. int curr_balance = f->remaining / FFMIN(3, f->coded_bands - band);
  45. b = av_clip_uintp2(FFMIN(f->remaining2 + 1, f->pulses[band] + curr_balance), 14);
  46. }
  47. if (f->dual_stereo) {
  48. pvq->quant_band(pvq, f, rc, band, X, NULL, band_size, b / 2, f->blocks, NULL,
  49. f->size, norm1, 0, 1.0f, lowband_scratch, cm[0]);
  50. pvq->quant_band(pvq, f, rc, band, Y, NULL, band_size, b / 2, f->blocks, NULL,
  51. f->size, norm2, 0, 1.0f, lowband_scratch, cm[1]);
  52. } else {
  53. pvq->quant_band(pvq, f, rc, band, X, Y, band_size, b, f->blocks, NULL, f->size,
  54. norm1, 0, 1.0f, lowband_scratch, cm[0] | cm[1]);
  55. }
  56. for (i = 0; i < band_size; i++) {
  57. err_x += (X[i] - X_orig[i])*(X[i] - X_orig[i]);
  58. if (Y)
  59. err_y += (Y[i] - Y_orig[i])*(Y[i] - Y_orig[i]);
  60. }
  61. dist = sqrtf(err_x) + sqrtf(err_y);
  62. cost = OPUS_RC_CHECKPOINT_BITS(rc)/8.0f;
  63. *bits += cost;
  64. OPUS_RC_CHECKPOINT_ROLLBACK(rc);
  65. return lambda*dist*cost;
  66. }
  67. /* Populate metrics without taking into consideration neighbouring steps */
  68. static void step_collect_psy_metrics(OpusPsyContext *s, int index)
  69. {
  70. int silence = 0, ch, i, j;
  71. OpusPsyStep *st = s->steps[index];
  72. st->index = index;
  73. for (ch = 0; ch < s->avctx->channels; ch++) {
  74. const int lap_size = (1 << s->bsize_analysis);
  75. for (i = 1; i <= FFMIN(lap_size, index); i++) {
  76. const int offset = i*120;
  77. AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index - i);
  78. memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
  79. }
  80. for (i = 0; i < lap_size; i++) {
  81. const int offset = i*120 + lap_size;
  82. AVFrame *cur = ff_bufqueue_peek(s->bufqueue, index + i);
  83. memcpy(&s->scratch[offset], cur->extended_data[ch], cur->nb_samples*sizeof(float));
  84. }
  85. s->dsp->vector_fmul(s->scratch, s->scratch, s->window[s->bsize_analysis],
  86. (OPUS_BLOCK_SIZE(s->bsize_analysis) << 1));
  87. s->mdct[s->bsize_analysis]->mdct(s->mdct[s->bsize_analysis], st->coeffs[ch], s->scratch, 1);
  88. for (i = 0; i < CELT_MAX_BANDS; i++)
  89. st->bands[ch][i] = &st->coeffs[ch][ff_celt_freq_bands[i] << s->bsize_analysis];
  90. }
  91. for (ch = 0; ch < s->avctx->channels; ch++) {
  92. for (i = 0; i < CELT_MAX_BANDS; i++) {
  93. float avg_c_s, energy = 0.0f, dist_dev = 0.0f;
  94. const int range = ff_celt_freq_range[i] << s->bsize_analysis;
  95. const float *coeffs = st->bands[ch][i];
  96. for (j = 0; j < range; j++)
  97. energy += coeffs[j]*coeffs[j];
  98. st->energy[ch][i] += sqrtf(energy);
  99. silence |= !!st->energy[ch][i];
  100. avg_c_s = energy / range;
  101. for (j = 0; j < range; j++) {
  102. const float c_s = coeffs[j]*coeffs[j];
  103. dist_dev += (avg_c_s - c_s)*(avg_c_s - c_s);
  104. }
  105. st->tone[ch][i] += sqrtf(dist_dev);
  106. }
  107. }
  108. st->silence = !silence;
  109. if (s->avctx->channels > 1) {
  110. for (i = 0; i < CELT_MAX_BANDS; i++) {
  111. float incompat = 0.0f;
  112. const float *coeffs1 = st->bands[0][i];
  113. const float *coeffs2 = st->bands[1][i];
  114. const int range = ff_celt_freq_range[i] << s->bsize_analysis;
  115. for (j = 0; j < range; j++)
  116. incompat += (coeffs1[j] - coeffs2[j])*(coeffs1[j] - coeffs2[j]);
  117. st->stereo[i] = sqrtf(incompat);
  118. }
  119. }
  120. for (ch = 0; ch < s->avctx->channels; ch++) {
  121. for (i = 0; i < CELT_MAX_BANDS; i++) {
  122. OpusBandExcitation *ex = &s->ex[ch][i];
  123. float bp_e = bessel_filter(&s->bfilter_lo[ch][i], st->energy[ch][i]);
  124. bp_e = bessel_filter(&s->bfilter_hi[ch][i], bp_e);
  125. bp_e *= bp_e;
  126. if (bp_e > ex->excitation) {
  127. st->change_amp[ch][i] = bp_e - ex->excitation;
  128. st->total_change += st->change_amp[ch][i];
  129. ex->excitation = ex->excitation_init = bp_e;
  130. ex->excitation_dist = 0.0f;
  131. }
  132. if (ex->excitation > 0.0f) {
  133. ex->excitation -= av_clipf((1/expf(ex->excitation_dist)), ex->excitation_init/20, ex->excitation_init/1.09);
  134. ex->excitation = FFMAX(ex->excitation, 0.0f);
  135. ex->excitation_dist += 1.0f;
  136. }
  137. }
  138. }
  139. }
  140. static void search_for_change_points(OpusPsyContext *s, float tgt_change,
  141. int offset_s, int offset_e, int resolution,
  142. int level)
  143. {
  144. int i;
  145. float c_change = 0.0f;
  146. if ((offset_e - offset_s) <= resolution)
  147. return;
  148. for (i = offset_s; i < offset_e; i++) {
  149. c_change += s->steps[i]->total_change;
  150. if (c_change > tgt_change)
  151. break;
  152. }
  153. if (i == offset_e)
  154. return;
  155. search_for_change_points(s, tgt_change / 2.0f, offset_s, i + 0, resolution, level + 1);
  156. s->inflection_points[s->inflection_points_count++] = i;
  157. search_for_change_points(s, tgt_change / 2.0f, i + 1, offset_e, resolution, level + 1);
  158. }
  159. static int flush_silent_frames(OpusPsyContext *s)
  160. {
  161. int fsize, silent_frames;
  162. for (silent_frames = 0; silent_frames < s->buffered_steps; silent_frames++)
  163. if (!s->steps[silent_frames]->silence)
  164. break;
  165. if (--silent_frames < 0)
  166. return 0;
  167. for (fsize = CELT_BLOCK_960; fsize > CELT_BLOCK_120; fsize--) {
  168. if ((1 << fsize) > silent_frames)
  169. continue;
  170. s->p.frames = FFMIN(silent_frames / (1 << fsize), 48 >> fsize);
  171. s->p.framesize = fsize;
  172. return 1;
  173. }
  174. return 0;
  175. }
  176. /* Main function which decides frame size and frames per current packet */
  177. static void psy_output_groups(OpusPsyContext *s)
  178. {
  179. int max_delay_samples = (s->options->max_delay_ms*s->avctx->sample_rate)/1000;
  180. int max_bsize = FFMIN(OPUS_SAMPLES_TO_BLOCK_SIZE(max_delay_samples), CELT_BLOCK_960);
  181. /* These don't change for now */
  182. s->p.mode = OPUS_MODE_CELT;
  183. s->p.bandwidth = OPUS_BANDWIDTH_FULLBAND;
  184. /* Flush silent frames ASAP */
  185. if (s->steps[0]->silence && flush_silent_frames(s))
  186. return;
  187. s->p.framesize = FFMIN(max_bsize, CELT_BLOCK_960);
  188. s->p.frames = 1;
  189. }
  190. int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
  191. {
  192. int i;
  193. float total_energy_change = 0.0f;
  194. if (s->buffered_steps < s->max_steps && !s->eof) {
  195. const int awin = (1 << s->bsize_analysis);
  196. if (++s->steps_to_process >= awin) {
  197. step_collect_psy_metrics(s, s->buffered_steps - awin + 1);
  198. s->steps_to_process = 0;
  199. }
  200. if ((++s->buffered_steps) < s->max_steps)
  201. return 1;
  202. }
  203. for (i = 0; i < s->buffered_steps; i++)
  204. total_energy_change += s->steps[i]->total_change;
  205. search_for_change_points(s, total_energy_change / 2.0f, 0,
  206. s->buffered_steps, 1, 0);
  207. psy_output_groups(s);
  208. p->frames = s->p.frames;
  209. p->framesize = s->p.framesize;
  210. p->mode = s->p.mode;
  211. p->bandwidth = s->p.bandwidth;
  212. return 0;
  213. }
  214. void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
  215. {
  216. int i, neighbouring_points = 0, start_offset = 0;
  217. int radius = (1 << s->p.framesize), step_offset = radius*index;
  218. int silence = 1;
  219. f->start_band = (s->p.mode == OPUS_MODE_HYBRID) ? 17 : 0;
  220. f->end_band = ff_celt_band_end[s->p.bandwidth];
  221. f->channels = s->avctx->channels;
  222. f->size = s->p.framesize;
  223. for (i = 0; i < (1 << f->size); i++)
  224. silence &= s->steps[index*(1 << f->size) + i]->silence;
  225. f->silence = silence;
  226. if (f->silence) {
  227. f->framebits = 0; /* Otherwise the silence flag eats up 16(!) bits */
  228. return;
  229. }
  230. for (i = 0; i < s->inflection_points_count; i++) {
  231. if (s->inflection_points[i] >= step_offset) {
  232. start_offset = i;
  233. break;
  234. }
  235. }
  236. for (i = start_offset; i < FFMIN(radius, s->inflection_points_count - start_offset); i++) {
  237. if (s->inflection_points[i] < (step_offset + radius)) {
  238. neighbouring_points++;
  239. }
  240. }
  241. /* Transient flagging */
  242. f->transient = neighbouring_points > 0;
  243. f->blocks = f->transient ? OPUS_BLOCK_SIZE(s->p.framesize)/CELT_OVERLAP : 1;
  244. /* Some sane defaults */
  245. f->pfilter = 0;
  246. f->pf_gain = 0.5f;
  247. f->pf_octave = 2;
  248. f->pf_period = 1;
  249. f->pf_tapset = 2;
  250. /* More sane defaults */
  251. f->tf_select = 0;
  252. f->anticollapse = 1;
  253. f->alloc_trim = 5;
  254. f->skip_band_floor = f->end_band;
  255. f->intensity_stereo = f->end_band;
  256. f->dual_stereo = 0;
  257. f->spread = CELT_SPREAD_NORMAL;
  258. memset(f->tf_change, 0, sizeof(int)*CELT_MAX_BANDS);
  259. memset(f->alloc_boost, 0, sizeof(int)*CELT_MAX_BANDS);
  260. }
  261. static void celt_gauge_psy_weight(OpusPsyContext *s, OpusPsyStep **start,
  262. CeltFrame *f_out)
  263. {
  264. int i, f, ch;
  265. int frame_size = OPUS_BLOCK_SIZE(s->p.framesize);
  266. float rate, frame_bits = 0;
  267. /* Used for the global ROTATE flag */
  268. float tonal = 0.0f;
  269. /* Pseudo-weights */
  270. float band_score[CELT_MAX_BANDS] = { 0 };
  271. float max_score = 1.0f;
  272. /* Pass one - one loop around each band, computing unquant stuff */
  273. for (i = 0; i < CELT_MAX_BANDS; i++) {
  274. float weight = 0.0f;
  275. float tonal_contrib = 0.0f;
  276. for (f = 0; f < (1 << s->p.framesize); f++) {
  277. weight = start[f]->stereo[i];
  278. for (ch = 0; ch < s->avctx->channels; ch++) {
  279. weight += start[f]->change_amp[ch][i] + start[f]->tone[ch][i] + start[f]->energy[ch][i];
  280. tonal_contrib += start[f]->tone[ch][i];
  281. }
  282. }
  283. tonal += tonal_contrib;
  284. band_score[i] = weight;
  285. }
  286. tonal /= (float)CELT_MAX_BANDS;
  287. for (i = 0; i < CELT_MAX_BANDS; i++) {
  288. if (band_score[i] > max_score)
  289. max_score = band_score[i];
  290. }
  291. for (i = 0; i < CELT_MAX_BANDS; i++) {
  292. f_out->alloc_boost[i] = (int)((band_score[i]/max_score)*3.0f);
  293. frame_bits += band_score[i]*8.0f;
  294. }
  295. tonal /= 1333136.0f;
  296. f_out->spread = av_clip_uintp2(lrintf(tonal), 2);
  297. rate = ((float)s->avctx->bit_rate) + frame_bits*frame_size*16;
  298. rate *= s->lambda;
  299. rate /= s->avctx->sample_rate/frame_size;
  300. f_out->framebits = lrintf(rate);
  301. f_out->framebits = FFMIN(f_out->framebits, OPUS_MAX_PACKET_SIZE*8);
  302. f_out->framebits = FFALIGN(f_out->framebits, 8);
  303. }
  304. static int bands_dist(OpusPsyContext *s, CeltFrame *f, float *total_dist)
  305. {
  306. int i, tdist = 0.0f;
  307. OpusRangeCoder dump;
  308. ff_opus_rc_enc_init(&dump);
  309. ff_celt_bitalloc(f, &dump, 1);
  310. for (i = 0; i < CELT_MAX_BANDS; i++) {
  311. float bits = 0.0f;
  312. float dist = pvq_band_cost(f->pvq, f, &dump, i, &bits, s->lambda);
  313. tdist += dist;
  314. }
  315. *total_dist = tdist;
  316. return 0;
  317. }
  318. static void celt_search_for_dual_stereo(OpusPsyContext *s, CeltFrame *f)
  319. {
  320. float td1, td2;
  321. f->dual_stereo = 0;
  322. if (s->avctx->channels < 2)
  323. return;
  324. bands_dist(s, f, &td1);
  325. f->dual_stereo = 1;
  326. bands_dist(s, f, &td2);
  327. f->dual_stereo = td2 < td1;
  328. s->dual_stereo_used += td2 < td1;
  329. }
  330. static void celt_search_for_intensity(OpusPsyContext *s, CeltFrame *f)
  331. {
  332. int i, best_band = CELT_MAX_BANDS - 1;
  333. float dist, best_dist = FLT_MAX;
  334. /* TODO: fix, make some heuristic up here using the lambda value */
  335. float end_band = 0;
  336. if (s->avctx->channels < 2)
  337. return;
  338. for (i = f->end_band; i >= end_band; i--) {
  339. f->intensity_stereo = i;
  340. bands_dist(s, f, &dist);
  341. if (best_dist > dist) {
  342. best_dist = dist;
  343. best_band = i;
  344. }
  345. }
  346. f->intensity_stereo = best_band;
  347. s->avg_is_band = (s->avg_is_band + f->intensity_stereo)/2.0f;
  348. }
  349. static int celt_search_for_tf(OpusPsyContext *s, OpusPsyStep **start, CeltFrame *f)
  350. {
  351. int i, j, k, cway, config[2][CELT_MAX_BANDS] = { { 0 } };
  352. float score[2] = { 0 };
  353. for (cway = 0; cway < 2; cway++) {
  354. int mag[2];
  355. int base = f->transient ? 120 : 960;
  356. for (i = 0; i < 2; i++) {
  357. int c = ff_celt_tf_select[f->size][f->transient][cway][i];
  358. mag[i] = c < 0 ? base >> FFABS(c) : base << FFABS(c);
  359. }
  360. for (i = 0; i < CELT_MAX_BANDS; i++) {
  361. float iscore0 = 0.0f;
  362. float iscore1 = 0.0f;
  363. for (j = 0; j < (1 << f->size); j++) {
  364. for (k = 0; k < s->avctx->channels; k++) {
  365. iscore0 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[0];
  366. iscore1 += start[j]->tone[k][i]*start[j]->change_amp[k][i]/mag[1];
  367. }
  368. }
  369. config[cway][i] = FFABS(iscore0 - 1.0f) < FFABS(iscore1 - 1.0f);
  370. score[cway] += config[cway][i] ? iscore1 : iscore0;
  371. }
  372. }
  373. f->tf_select = score[0] < score[1];
  374. memcpy(f->tf_change, config[f->tf_select], sizeof(int)*CELT_MAX_BANDS);
  375. return 0;
  376. }
  377. int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
  378. {
  379. int start_transient_flag = f->transient;
  380. OpusPsyStep **start = &s->steps[index * (1 << s->p.framesize)];
  381. if (f->silence)
  382. return 0;
  383. celt_gauge_psy_weight(s, start, f);
  384. celt_search_for_intensity(s, f);
  385. celt_search_for_dual_stereo(s, f);
  386. celt_search_for_tf(s, start, f);
  387. if (f->transient != start_transient_flag) {
  388. f->blocks = f->transient ? OPUS_BLOCK_SIZE(s->p.framesize)/CELT_OVERLAP : 1;
  389. s->redo_analysis = 1;
  390. return 1;
  391. }
  392. s->redo_analysis = 0;
  393. return 0;
  394. }
  395. void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f, OpusRangeCoder *rc)
  396. {
  397. int i, frame_size = OPUS_BLOCK_SIZE(s->p.framesize);
  398. int steps_out = s->p.frames*(frame_size/120);
  399. void *tmp[FF_BUFQUEUE_SIZE];
  400. float ideal_fbits;
  401. for (i = 0; i < steps_out; i++)
  402. memset(s->steps[i], 0, sizeof(OpusPsyStep));
  403. for (i = 0; i < s->max_steps; i++)
  404. tmp[i] = s->steps[i];
  405. for (i = 0; i < s->max_steps; i++) {
  406. const int i_new = i - steps_out;
  407. s->steps[i_new < 0 ? s->max_steps + i_new : i_new] = tmp[i];
  408. }
  409. for (i = steps_out; i < s->buffered_steps; i++)
  410. s->steps[i]->index -= steps_out;
  411. ideal_fbits = s->avctx->bit_rate/(s->avctx->sample_rate/frame_size);
  412. for (i = 0; i < s->p.frames; i++) {
  413. s->avg_is_band += f[i].intensity_stereo;
  414. s->lambda *= ideal_fbits / f[i].framebits;
  415. }
  416. s->avg_is_band /= (s->p.frames + 1);
  417. s->cs_num = 0;
  418. s->steps_to_process = 0;
  419. s->buffered_steps -= steps_out;
  420. s->total_packets_out += s->p.frames;
  421. s->inflection_points_count = 0;
  422. }
  423. av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx,
  424. struct FFBufQueue *bufqueue, OpusEncOptions *options)
  425. {
  426. int i, ch, ret;
  427. s->redo_analysis = 0;
  428. s->lambda = 1.0f;
  429. s->options = options;
  430. s->avctx = avctx;
  431. s->bufqueue = bufqueue;
  432. s->max_steps = ceilf(s->options->max_delay_ms/2.5f);
  433. s->bsize_analysis = CELT_BLOCK_960;
  434. s->avg_is_band = CELT_MAX_BANDS - 1;
  435. s->inflection_points_count = 0;
  436. s->inflection_points = av_mallocz(sizeof(*s->inflection_points)*s->max_steps);
  437. if (!s->inflection_points) {
  438. ret = AVERROR(ENOMEM);
  439. goto fail;
  440. }
  441. s->dsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  442. if (!s->dsp) {
  443. ret = AVERROR(ENOMEM);
  444. goto fail;
  445. }
  446. for (ch = 0; ch < s->avctx->channels; ch++) {
  447. for (i = 0; i < CELT_MAX_BANDS; i++) {
  448. bessel_init(&s->bfilter_hi[ch][i], 1.0f, 19.0f, 100.0f, 1);
  449. bessel_init(&s->bfilter_lo[ch][i], 1.0f, 20.0f, 100.0f, 0);
  450. }
  451. }
  452. for (i = 0; i < s->max_steps; i++) {
  453. s->steps[i] = av_mallocz(sizeof(OpusPsyStep));
  454. if (!s->steps[i]) {
  455. ret = AVERROR(ENOMEM);
  456. goto fail;
  457. }
  458. }
  459. for (i = 0; i < CELT_BLOCK_NB; i++) {
  460. float tmp;
  461. const int len = OPUS_BLOCK_SIZE(i);
  462. s->window[i] = av_malloc(2*len*sizeof(float));
  463. if (!s->window[i]) {
  464. ret = AVERROR(ENOMEM);
  465. goto fail;
  466. }
  467. generate_window_func(s->window[i], 2*len, WFUNC_SINE, &tmp);
  468. if ((ret = ff_mdct15_init(&s->mdct[i], 0, i + 3, 68 << (CELT_BLOCK_NB - 1 - i))))
  469. goto fail;
  470. }
  471. return 0;
  472. fail:
  473. av_freep(&s->inflection_points);
  474. av_freep(&s->dsp);
  475. for (i = 0; i < CELT_BLOCK_NB; i++) {
  476. ff_mdct15_uninit(&s->mdct[i]);
  477. av_freep(&s->window[i]);
  478. }
  479. for (i = 0; i < s->max_steps; i++)
  480. av_freep(&s->steps[i]);
  481. return ret;
  482. }
  483. void ff_opus_psy_signal_eof(OpusPsyContext *s)
  484. {
  485. s->eof = 1;
  486. }
  487. av_cold int ff_opus_psy_end(OpusPsyContext *s)
  488. {
  489. int i;
  490. av_freep(&s->inflection_points);
  491. av_freep(&s->dsp);
  492. for (i = 0; i < CELT_BLOCK_NB; i++) {
  493. ff_mdct15_uninit(&s->mdct[i]);
  494. av_freep(&s->window[i]);
  495. }
  496. for (i = 0; i < s->max_steps; i++)
  497. av_freep(&s->steps[i]);
  498. av_log(s->avctx, AV_LOG_INFO, "Average Intensity Stereo band: %0.1f\n", s->avg_is_band);
  499. av_log(s->avctx, AV_LOG_INFO, "Dual Stereo used: %0.2f%%\n", ((float)s->dual_stereo_used/s->total_packets_out)*100.0f);
  500. return 0;
  501. }