LCOV - code coverage report
Current view: top level - libavcodec - opusenc_psy.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 363 0.0 %
Date: 2017-12-16 21:16:39 Functions: 0 17 0.0 %

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

Generated by: LCOV version 1.13