LCOV - code coverage report
Current view: top level - libavcodec - wavpackenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 792 1763 44.9 %
Date: 2017-12-15 18:13:28 Functions: 25 46 54.3 %

          Line data    Source code
       1             : /*
       2             :  * WavPack lossless audio encoder
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU Lesser General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2.1 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #define BITSTREAM_WRITER_LE
      22             : 
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "libavutil/opt.h"
      25             : #include "avcodec.h"
      26             : #include "internal.h"
      27             : #include "put_bits.h"
      28             : #include "bytestream.h"
      29             : #include "wavpackenc.h"
      30             : #include "wavpack.h"
      31             : 
      32             : #define UPDATE_WEIGHT(weight, delta, source, result) \
      33             :     if ((source) && (result)) { \
      34             :         int32_t s = (int32_t) ((source) ^ (result)) >> 31; \
      35             :         weight = ((delta) ^ s) + ((weight) - s); \
      36             :     }
      37             : 
      38             : #define APPLY_WEIGHT_F(weight, sample) ((((((sample) & 0xffff) * (weight)) >> 9) + \
      39             :     ((((sample) & ~0xffff) >> 9) * (weight)) + 1) >> 1)
      40             : 
      41             : #define APPLY_WEIGHT_I(weight, sample) (((weight) * (sample) + 512) >> 10)
      42             : 
      43             : #define APPLY_WEIGHT(weight, sample) ((sample) != (short) (sample) ? \
      44             :     APPLY_WEIGHT_F(weight, sample) : APPLY_WEIGHT_I (weight, sample))
      45             : 
      46             : #define CLEAR(destin) memset(&destin, 0, sizeof(destin));
      47             : 
      48             : #define SHIFT_LSB       13
      49             : #define SHIFT_MASK      (0x1FU << SHIFT_LSB)
      50             : 
      51             : #define MAG_LSB         18
      52             : #define MAG_MASK        (0x1FU << MAG_LSB)
      53             : 
      54             : #define SRATE_LSB       23
      55             : #define SRATE_MASK      (0xFU << SRATE_LSB)
      56             : 
      57             : #define EXTRA_TRY_DELTAS     1
      58             : #define EXTRA_ADJUST_DELTAS  2
      59             : #define EXTRA_SORT_FIRST     4
      60             : #define EXTRA_BRANCHES       8
      61             : #define EXTRA_SORT_LAST     16
      62             : 
      63             : typedef struct WavPackExtraInfo {
      64             :     struct Decorr dps[MAX_TERMS];
      65             :     int nterms, log_limit, gt16bit;
      66             :     uint32_t best_bits;
      67             : } WavPackExtraInfo;
      68             : 
      69             : typedef struct WavPackWords {
      70             :     int pend_data, holding_one, zeros_acc;
      71             :     int holding_zero, pend_count;
      72             :     WvChannel c[2];
      73             : } WavPackWords;
      74             : 
      75             : typedef struct WavPackEncodeContext {
      76             :     AVClass *class;
      77             :     AVCodecContext *avctx;
      78             :     PutBitContext pb;
      79             :     int block_samples;
      80             :     int buffer_size;
      81             :     int sample_index;
      82             :     int stereo, stereo_in;
      83             :     int ch_offset;
      84             : 
      85             :     int32_t *samples[2];
      86             :     int samples_size[2];
      87             : 
      88             :     int32_t *sampleptrs[MAX_TERMS+2][2];
      89             :     int sampleptrs_size[MAX_TERMS+2][2];
      90             : 
      91             :     int32_t *temp_buffer[2][2];
      92             :     int temp_buffer_size[2][2];
      93             : 
      94             :     int32_t *best_buffer[2];
      95             :     int best_buffer_size[2];
      96             : 
      97             :     int32_t *js_left, *js_right;
      98             :     int js_left_size, js_right_size;
      99             : 
     100             :     int32_t *orig_l, *orig_r;
     101             :     int orig_l_size, orig_r_size;
     102             : 
     103             :     unsigned extra_flags;
     104             :     int optimize_mono;
     105             :     int decorr_filter;
     106             :     int joint;
     107             :     int num_branches;
     108             : 
     109             :     uint32_t flags;
     110             :     uint32_t crc_x;
     111             :     WavPackWords w;
     112             : 
     113             :     uint8_t int32_sent_bits, int32_zeros, int32_ones, int32_dups;
     114             :     uint8_t float_flags, float_shift, float_max_exp, max_exp;
     115             :     int32_t shifted_ones, shifted_zeros, shifted_both;
     116             :     int32_t false_zeros, neg_zeros, ordata;
     117             : 
     118             :     int num_terms, shift, joint_stereo, false_stereo;
     119             :     int num_decorrs, num_passes, best_decorr, mask_decorr;
     120             :     struct Decorr decorr_passes[MAX_TERMS];
     121             :     const WavPackDecorrSpec *decorr_specs;
     122             :     float delta_decay;
     123             : } WavPackEncodeContext;
     124             : 
     125           2 : static av_cold int wavpack_encode_init(AVCodecContext *avctx)
     126             : {
     127           2 :     WavPackEncodeContext *s = avctx->priv_data;
     128             : 
     129           2 :     s->avctx = avctx;
     130             : 
     131           2 :     if (avctx->channels > 255) {
     132           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d\n", avctx->channels);
     133           0 :         return AVERROR(EINVAL);
     134             :     }
     135             : 
     136           2 :     if (!avctx->frame_size) {
     137             :         int block_samples;
     138           2 :         if (!(avctx->sample_rate & 1))
     139           2 :             block_samples = avctx->sample_rate / 2;
     140             :         else
     141           0 :             block_samples = avctx->sample_rate;
     142             : 
     143           4 :         while (block_samples * avctx->channels > WV_MAX_SAMPLES)
     144           0 :             block_samples /= 2;
     145             : 
     146           5 :         while (block_samples * avctx->channels < 40000)
     147           1 :             block_samples *= 2;
     148           2 :         avctx->frame_size = block_samples;
     149           0 :     } else if (avctx->frame_size && (avctx->frame_size < 128 ||
     150           0 :                               avctx->frame_size > WV_MAX_SAMPLES)) {
     151           0 :         av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n", avctx->frame_size);
     152           0 :         return AVERROR(EINVAL);
     153             :     }
     154             : 
     155           2 :     if (avctx->compression_level != FF_COMPRESSION_DEFAULT) {
     156           1 :         if (avctx->compression_level >= 3) {
     157           0 :             s->decorr_filter = 3;
     158           0 :             s->num_passes = 9;
     159           0 :             if      (avctx->compression_level >= 8) {
     160           0 :                 s->num_branches = 4;
     161           0 :                 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_SORT_LAST|EXTRA_BRANCHES;
     162           0 :             } else if (avctx->compression_level >= 7) {
     163           0 :                 s->num_branches = 3;
     164           0 :                 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
     165           0 :             } else if (avctx->compression_level >= 6) {
     166           0 :                 s->num_branches = 2;
     167           0 :                 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
     168           0 :             } else if (avctx->compression_level >= 5) {
     169           0 :                 s->num_branches = 1;
     170           0 :                 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
     171           0 :             } else if (avctx->compression_level >= 4) {
     172           0 :                 s->num_branches = 1;
     173           0 :                 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_BRANCHES;
     174             :             }
     175           1 :         } else if (avctx->compression_level == 2) {
     176           0 :             s->decorr_filter = 2;
     177           0 :             s->num_passes = 4;
     178           1 :         } else if (avctx->compression_level == 1) {
     179           1 :             s->decorr_filter = 1;
     180           1 :             s->num_passes = 2;
     181           0 :         } else if (avctx->compression_level < 1) {
     182           0 :             s->decorr_filter = 0;
     183           0 :             s->num_passes = 0;
     184             :         }
     185             :     }
     186             : 
     187           2 :     s->num_decorrs = decorr_filter_sizes[s->decorr_filter];
     188           2 :     s->decorr_specs = decorr_filters[s->decorr_filter];
     189             : 
     190           2 :     s->delta_decay = 2.0;
     191             : 
     192           2 :     return 0;
     193             : }
     194             : 
     195           0 : static void shift_mono(int32_t *samples, int nb_samples, int shift)
     196             : {
     197             :     int i;
     198           0 :     for (i = 0; i < nb_samples; i++)
     199           0 :         samples[i] >>= shift;
     200           0 : }
     201             : 
     202           0 : static void shift_stereo(int32_t *left, int32_t *right,
     203             :                          int nb_samples, int shift)
     204             : {
     205             :     int i;
     206           0 :     for (i = 0; i < nb_samples; i++) {
     207           0 :         left [i] >>= shift;
     208           0 :         right[i] >>= shift;
     209             :     }
     210           0 : }
     211             : 
     212             : #define FLOAT_SHIFT_ONES 1
     213             : #define FLOAT_SHIFT_SAME 2
     214             : #define FLOAT_SHIFT_SENT 4
     215             : #define FLOAT_ZEROS_SENT 8
     216             : #define FLOAT_NEG_ZEROS  0x10
     217             : #define FLOAT_EXCEPTIONS 0x20
     218             : 
     219             : #define get_mantissa(f)     ((f) & 0x7fffff)
     220             : #define get_exponent(f)     (((f) >> 23) & 0xff)
     221             : #define get_sign(f)         (((f) >> 31) & 0x1)
     222             : 
     223           0 : static void process_float(WavPackEncodeContext *s, int32_t *sample)
     224             : {
     225           0 :     int32_t shift_count, value, f = *sample;
     226             : 
     227           0 :     if (get_exponent(f) == 255) {
     228           0 :         s->float_flags |= FLOAT_EXCEPTIONS;
     229           0 :         value = 0x1000000;
     230           0 :         shift_count = 0;
     231           0 :     } else if (get_exponent(f)) {
     232           0 :         shift_count = s->max_exp - get_exponent(f);
     233           0 :         value = 0x800000 + get_mantissa(f);
     234             :     } else {
     235           0 :         shift_count = s->max_exp ? s->max_exp - 1 : 0;
     236           0 :         value = get_mantissa(f);
     237             :     }
     238             : 
     239           0 :     if (shift_count < 25)
     240           0 :         value >>= shift_count;
     241             :     else
     242           0 :         value = 0;
     243             : 
     244           0 :     if (!value) {
     245           0 :         if (get_exponent(f) || get_mantissa(f))
     246           0 :             s->false_zeros++;
     247           0 :         else if (get_sign(f))
     248           0 :             s->neg_zeros++;
     249           0 :     } else if (shift_count) {
     250           0 :         int32_t mask = (1 << shift_count) - 1;
     251             : 
     252           0 :         if (!(get_mantissa(f) & mask))
     253           0 :             s->shifted_zeros++;
     254           0 :         else if ((get_mantissa(f) & mask) == mask)
     255           0 :             s->shifted_ones++;
     256             :         else
     257           0 :             s->shifted_both++;
     258             :     }
     259             : 
     260           0 :     s->ordata |= value;
     261           0 :     *sample = get_sign(f) ? -value : value;
     262           0 : }
     263             : 
     264           0 : static int scan_float(WavPackEncodeContext *s,
     265             :                       int32_t *samples_l, int32_t *samples_r,
     266             :                       int nb_samples)
     267             : {
     268           0 :     uint32_t crc = 0xffffffffu;
     269             :     int i;
     270             : 
     271           0 :     s->shifted_ones = s->shifted_zeros = s->shifted_both = s->ordata = 0;
     272           0 :     s->float_shift = s->float_flags = 0;
     273           0 :     s->false_zeros = s->neg_zeros = 0;
     274           0 :     s->max_exp = 0;
     275             : 
     276           0 :     if (s->flags & WV_MONO_DATA) {
     277           0 :         for (i = 0; i < nb_samples; i++) {
     278           0 :             int32_t f = samples_l[i];
     279           0 :             crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
     280             : 
     281           0 :             if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
     282           0 :                 s->max_exp = get_exponent(f);
     283             :         }
     284             :     } else {
     285           0 :         for (i = 0; i < nb_samples; i++) {
     286             :             int32_t f;
     287             : 
     288           0 :             f = samples_l[i];
     289           0 :             crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
     290           0 :             if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
     291           0 :                 s->max_exp = get_exponent(f);
     292             : 
     293           0 :             f = samples_r[i];
     294           0 :             crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
     295             : 
     296           0 :             if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
     297           0 :                 s->max_exp = get_exponent(f);
     298             :         }
     299             :     }
     300             : 
     301           0 :     s->crc_x = crc;
     302             : 
     303           0 :     if (s->flags & WV_MONO_DATA) {
     304           0 :         for (i = 0; i < nb_samples; i++)
     305           0 :             process_float(s, &samples_l[i]);
     306             :     } else {
     307           0 :         for (i = 0; i < nb_samples; i++) {
     308           0 :             process_float(s, &samples_l[i]);
     309           0 :             process_float(s, &samples_r[i]);
     310             :         }
     311             :     }
     312             : 
     313           0 :     s->float_max_exp = s->max_exp;
     314             : 
     315           0 :     if (s->shifted_both)
     316           0 :         s->float_flags |= FLOAT_SHIFT_SENT;
     317           0 :     else if (s->shifted_ones && !s->shifted_zeros)
     318           0 :         s->float_flags |= FLOAT_SHIFT_ONES;
     319           0 :     else if (s->shifted_ones && s->shifted_zeros)
     320           0 :         s->float_flags |= FLOAT_SHIFT_SAME;
     321           0 :     else if (s->ordata && !(s->ordata & 1)) {
     322             :         do {
     323           0 :             s->float_shift++;
     324           0 :             s->ordata >>= 1;
     325           0 :         } while (!(s->ordata & 1));
     326             : 
     327           0 :         if (s->flags & WV_MONO_DATA)
     328           0 :             shift_mono(samples_l, nb_samples, s->float_shift);
     329             :         else
     330           0 :             shift_stereo(samples_l, samples_r, nb_samples, s->float_shift);
     331             :     }
     332             : 
     333           0 :     s->flags &= ~MAG_MASK;
     334             : 
     335           0 :     while (s->ordata) {
     336           0 :         s->flags += 1 << MAG_LSB;
     337           0 :         s->ordata >>= 1;
     338             :     }
     339             : 
     340           0 :     if (s->false_zeros || s->neg_zeros)
     341           0 :         s->float_flags |= FLOAT_ZEROS_SENT;
     342             : 
     343           0 :     if (s->neg_zeros)
     344           0 :         s->float_flags |= FLOAT_NEG_ZEROS;
     345             : 
     346           0 :     return s->float_flags & (FLOAT_EXCEPTIONS | FLOAT_ZEROS_SENT |
     347             :                              FLOAT_SHIFT_SENT | FLOAT_SHIFT_SAME);
     348             : }
     349             : 
     350          13 : static void scan_int23(WavPackEncodeContext *s,
     351             :                        int32_t *samples_l, int32_t *samples_r,
     352             :                        int nb_samples)
     353             : {
     354          13 :     uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
     355          13 :     int i, total_shift = 0;
     356             : 
     357          13 :     s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
     358             : 
     359          13 :     if (s->flags & WV_MONO_DATA) {
     360           9 :         for (i = 0; i < nb_samples; i++) {
     361           9 :             int32_t M = samples_l[i];
     362             : 
     363           9 :             magdata |= (M < 0) ? ~M : M;
     364           9 :             xordata |= M ^ -(M & 1);
     365           9 :             anddata &= M;
     366           9 :             ordata  |= M;
     367             : 
     368           9 :             if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
     369           1 :                 return;
     370             :         }
     371             :     } else {
     372          41 :         for (i = 0; i < nb_samples; i++) {
     373          41 :             int32_t L = samples_l[i];
     374          41 :             int32_t R = samples_r[i];
     375             : 
     376          41 :             magdata |= (L < 0) ? ~L : L;
     377          41 :             magdata |= (R < 0) ? ~R : R;
     378          41 :             xordata |= L ^ -(L & 1);
     379          41 :             xordata |= R ^ -(R & 1);
     380          41 :             anddata &= L & R;
     381          41 :             ordata  |= L | R;
     382             : 
     383          41 :             if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
     384          12 :                 return;
     385             :         }
     386             :     }
     387             : 
     388           0 :     s->flags &= ~MAG_MASK;
     389             : 
     390           0 :     while (magdata) {
     391           0 :         s->flags += 1 << MAG_LSB;
     392           0 :         magdata >>= 1;
     393             :     }
     394             : 
     395           0 :     if (!(s->flags & MAG_MASK))
     396           0 :         return;
     397             : 
     398           0 :     if (!(ordata & 1)) {
     399             :         do {
     400           0 :             s->flags -= 1 << MAG_LSB;
     401           0 :             s->int32_zeros++;
     402           0 :             total_shift++;
     403           0 :             ordata >>= 1;
     404           0 :         } while (!(ordata & 1));
     405           0 :     } else if (anddata & 1) {
     406             :         do {
     407           0 :             s->flags -= 1 << MAG_LSB;
     408           0 :             s->int32_ones++;
     409           0 :             total_shift++;
     410           0 :             anddata >>= 1;
     411           0 :         } while (anddata & 1);
     412           0 :     } else if (!(xordata & 2)) {
     413             :         do {
     414           0 :             s->flags -= 1 << MAG_LSB;
     415           0 :             s->int32_dups++;
     416           0 :             total_shift++;
     417           0 :             xordata >>= 1;
     418           0 :         } while (!(xordata & 2));
     419             :     }
     420             : 
     421           0 :     if (total_shift) {
     422           0 :         s->flags |= WV_INT32_DATA;
     423             : 
     424           0 :         if (s->flags & WV_MONO_DATA)
     425           0 :             shift_mono(samples_l, nb_samples, total_shift);
     426             :         else
     427           0 :             shift_stereo(samples_l, samples_r, nb_samples, total_shift);
     428             :     }
     429             : }
     430             : 
     431           0 : static int scan_int32(WavPackEncodeContext *s,
     432             :                       int32_t *samples_l, int32_t *samples_r,
     433             :                       int nb_samples)
     434             : {
     435           0 :     uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
     436           0 :     uint32_t crc = 0xffffffffu;
     437           0 :     int i, total_shift = 0;
     438             : 
     439           0 :     s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
     440             : 
     441           0 :     if (s->flags & WV_MONO_DATA) {
     442           0 :         for (i = 0; i < nb_samples; i++) {
     443           0 :             int32_t M = samples_l[i];
     444             : 
     445           0 :             crc = crc * 9 + (M & 0xffff) * 3 + ((M >> 16) & 0xffff);
     446           0 :             magdata |= (M < 0) ? ~M : M;
     447           0 :             xordata |= M ^ -(M & 1);
     448           0 :             anddata &= M;
     449           0 :             ordata  |= M;
     450             :         }
     451             :     } else {
     452           0 :         for (i = 0; i < nb_samples; i++) {
     453           0 :             int32_t L = samples_l[i];
     454           0 :             int32_t R = samples_r[i];
     455             : 
     456           0 :             crc = crc * 9 + (L & 0xffff) * 3 + ((L >> 16) & 0xffff);
     457           0 :             crc = crc * 9 + (R & 0xffff) * 3 + ((R >> 16) & 0xffff);
     458           0 :             magdata |= (L < 0) ? ~L : L;
     459           0 :             magdata |= (R < 0) ? ~R : R;
     460           0 :             xordata |= L ^ -(L & 1);
     461           0 :             xordata |= R ^ -(R & 1);
     462           0 :             anddata &= L & R;
     463           0 :             ordata  |= L | R;
     464             :         }
     465             :     }
     466             : 
     467           0 :     s->crc_x = crc;
     468           0 :     s->flags &= ~MAG_MASK;
     469             : 
     470           0 :     while (magdata) {
     471           0 :         s->flags += 1 << MAG_LSB;
     472           0 :         magdata >>= 1;
     473             :     }
     474             : 
     475           0 :     if (!((s->flags & MAG_MASK) >> MAG_LSB)) {
     476           0 :         s->flags &= ~WV_INT32_DATA;
     477           0 :         return 0;
     478             :     }
     479             : 
     480           0 :     if (!(ordata & 1))
     481             :         do {
     482           0 :             s->flags -= 1 << MAG_LSB;
     483           0 :             s->int32_zeros++;
     484           0 :             total_shift++;
     485           0 :             ordata >>= 1;
     486           0 :         } while (!(ordata & 1));
     487           0 :     else if (anddata & 1)
     488             :         do {
     489           0 :             s->flags -= 1 << MAG_LSB;
     490           0 :             s->int32_ones++;
     491           0 :             total_shift++;
     492           0 :             anddata >>= 1;
     493           0 :         } while (anddata & 1);
     494           0 :     else if (!(xordata & 2))
     495             :         do {
     496           0 :             s->flags -= 1 << MAG_LSB;
     497           0 :             s->int32_dups++;
     498           0 :             total_shift++;
     499           0 :             xordata >>= 1;
     500           0 :         } while (!(xordata & 2));
     501             : 
     502           0 :     if (((s->flags & MAG_MASK) >> MAG_LSB) > 23) {
     503           0 :         s->int32_sent_bits = (uint8_t)(((s->flags & MAG_MASK) >> MAG_LSB) - 23);
     504           0 :         total_shift += s->int32_sent_bits;
     505           0 :         s->flags &= ~MAG_MASK;
     506           0 :         s->flags += 23 << MAG_LSB;
     507             :     }
     508             : 
     509           0 :     if (total_shift) {
     510           0 :         s->flags |= WV_INT32_DATA;
     511             : 
     512           0 :         if (s->flags & WV_MONO_DATA)
     513           0 :             shift_mono(samples_l, nb_samples, total_shift);
     514             :         else
     515           0 :             shift_stereo(samples_l, samples_r, nb_samples, total_shift);
     516             :     }
     517             : 
     518           0 :     return s->int32_sent_bits;
     519             : }
     520             : 
     521         624 : static int8_t store_weight(int weight)
     522             : {
     523         624 :     weight = av_clip(weight, -1024, 1024);
     524         624 :     if (weight > 0)
     525         267 :         weight -= (weight + 64) >> 7;
     526             : 
     527         624 :     return (weight + 4) >> 3;
     528             : }
     529             : 
     530         606 : static int restore_weight(int8_t weight)
     531             : {
     532             :     int result;
     533             : 
     534         606 :     if ((result = (int) weight << 3) > 0)
     535         252 :         result += (result + 64) >> 7;
     536             : 
     537         606 :     return result;
     538             : }
     539             : 
     540        3922 : static int log2s(int32_t value)
     541             : {
     542        3922 :     return (value < 0) ? -wp_log2(-value) : wp_log2(value);
     543             : }
     544             : 
     545           4 : static void decorr_mono(int32_t *in_samples, int32_t *out_samples,
     546             :                         int nb_samples, struct Decorr *dpp, int dir)
     547             : {
     548           4 :     int m = 0, i;
     549             : 
     550           4 :     dpp->sumA = 0;
     551             : 
     552           4 :     if (dir < 0) {
     553           2 :         out_samples += (nb_samples - 1);
     554           2 :         in_samples  += (nb_samples - 1);
     555             :     }
     556             : 
     557           4 :     dpp->weightA = restore_weight(store_weight(dpp->weightA));
     558             : 
     559          36 :     for (i = 0; i < MAX_TERM; i++)
     560          32 :         dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
     561             : 
     562           4 :     if (dpp->value > MAX_TERM) {
     563       92304 :         while (nb_samples--) {
     564             :             int32_t left, sam_A;
     565             : 
     566       92296 :             sam_A = ((3 - (dpp->value & 1)) * dpp->samplesA[0] - dpp->samplesA[1]) >> !(dpp->value & 1);
     567             : 
     568       92296 :             dpp->samplesA[1] = dpp->samplesA[0];
     569       92296 :             dpp->samplesA[0] = left = in_samples[0];
     570             : 
     571       92296 :             left -= APPLY_WEIGHT(dpp->weightA, sam_A);
     572       92296 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
     573       92296 :             dpp->sumA += dpp->weightA;
     574       92296 :             out_samples[0] = left;
     575       92296 :             in_samples += dir;
     576       92296 :             out_samples += dir;
     577             :         }
     578           0 :     } else if (dpp->value > 0) {
     579           0 :         while (nb_samples--) {
     580           0 :             int k = (m + dpp->value) & (MAX_TERM - 1);
     581             :             int32_t left, sam_A;
     582             : 
     583           0 :             sam_A = dpp->samplesA[m];
     584           0 :             dpp->samplesA[k] = left = in_samples[0];
     585           0 :             m = (m + 1) & (MAX_TERM - 1);
     586             : 
     587           0 :             left -= APPLY_WEIGHT(dpp->weightA, sam_A);
     588           0 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
     589           0 :             dpp->sumA += dpp->weightA;
     590           0 :             out_samples[0] = left;
     591           0 :             in_samples += dir;
     592           0 :             out_samples += dir;
     593             :         }
     594             :     }
     595             : 
     596           4 :     if (m && dpp->value > 0 && dpp->value <= MAX_TERM) {
     597             :         int32_t temp_A[MAX_TERM];
     598             : 
     599           0 :         memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
     600             : 
     601           0 :         for (i = 0; i < MAX_TERM; i++) {
     602           0 :             dpp->samplesA[i] = temp_A[m];
     603           0 :             m = (m + 1) & (MAX_TERM - 1);
     604             :         }
     605             :     }
     606           4 : }
     607             : 
     608           1 : static void reverse_mono_decorr(struct Decorr *dpp)
     609             : {
     610           1 :     if (dpp->value > MAX_TERM) {
     611             :         int32_t sam_A;
     612             : 
     613           1 :         if (dpp->value & 1)
     614           0 :             sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
     615             :         else
     616           1 :             sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
     617             : 
     618           1 :         dpp->samplesA[1] = dpp->samplesA[0];
     619           1 :         dpp->samplesA[0] = sam_A;
     620             : 
     621           1 :         if (dpp->value & 1)
     622           0 :             sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
     623             :         else
     624           1 :             sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
     625             : 
     626           1 :         dpp->samplesA[1] = sam_A;
     627           0 :     } else if (dpp->value > 1) {
     628             :         int i, j, k;
     629             : 
     630           0 :         for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
     631           0 :             i &= (MAX_TERM - 1);
     632           0 :             j &= (MAX_TERM - 1);
     633           0 :             dpp->samplesA[i] ^= dpp->samplesA[j];
     634           0 :             dpp->samplesA[j] ^= dpp->samplesA[i];
     635           0 :             dpp->samplesA[i] ^= dpp->samplesA[j];
     636             :         }
     637             :     }
     638           1 : }
     639             : 
     640     1102500 : static uint32_t log2sample(uint32_t v, int limit, uint32_t *result)
     641             : {
     642             :     uint32_t dbits;
     643             : 
     644     1102500 :     if ((v += v >> 9) < (1 << 8)) {
     645      959899 :         dbits = nbits_table[v];
     646      959899 :         *result += (dbits << 8) + wp_log2_table[(v << (9 - dbits)) & 0xff];
     647             :     } else {
     648      142601 :         if (v < (1 << 16))
     649      142601 :             dbits = nbits_table[v >> 8] + 8;
     650           0 :         else if (v < (1 << 24))
     651           0 :             dbits = nbits_table[v >> 16] + 16;
     652             :         else
     653           0 :             dbits = nbits_table[v >> 24] + 24;
     654             : 
     655      142601 :         *result += dbits = (dbits << 8) + wp_log2_table[(v >> (dbits - 9)) & 0xff];
     656             : 
     657      142601 :         if (limit && dbits >= limit)
     658           0 :             return 1;
     659             :     }
     660             : 
     661     1102500 :     return 0;
     662             : }
     663             : 
     664           1 : static uint32_t log2mono(int32_t *samples, int nb_samples, int limit)
     665             : {
     666           1 :     uint32_t result = 0;
     667       44102 :     while (nb_samples--) {
     668       44100 :         if (log2sample(abs(*samples++), limit, &result))
     669           0 :             return UINT32_MAX;
     670             :     }
     671           1 :     return result;
     672             : }
     673             : 
     674          24 : static uint32_t log2stereo(int32_t *samples_l, int32_t *samples_r,
     675             :                            int nb_samples, int limit)
     676             : {
     677          24 :     uint32_t result = 0;
     678      529248 :     while (nb_samples--) {
     679     1058400 :         if (log2sample(abs(*samples_l++), limit, &result) ||
     680      529200 :             log2sample(abs(*samples_r++), limit, &result))
     681           0 :             return UINT32_MAX;
     682             :     }
     683          24 :     return result;
     684             : }
     685             : 
     686           0 : static void decorr_mono_buffer(int32_t *samples, int32_t *outsamples,
     687             :                                int nb_samples, struct Decorr *dpp,
     688             :                                int tindex)
     689             : {
     690           0 :     struct Decorr dp, *dppi = dpp + tindex;
     691           0 :     int delta = dppi->delta, pre_delta, term = dppi->value;
     692             : 
     693           0 :     if (delta == 7)
     694           0 :         pre_delta = 7;
     695           0 :     else if (delta < 2)
     696           0 :         pre_delta = 3;
     697             :     else
     698           0 :         pre_delta = delta + 1;
     699             : 
     700           0 :     CLEAR(dp);
     701           0 :     dp.value = term;
     702           0 :     dp.delta = pre_delta;
     703           0 :     decorr_mono(samples, outsamples, FFMIN(2048, nb_samples), &dp, -1);
     704           0 :     dp.delta = delta;
     705             : 
     706           0 :     if (tindex == 0)
     707           0 :         reverse_mono_decorr(&dp);
     708             :     else
     709           0 :         CLEAR(dp.samplesA);
     710             : 
     711           0 :     memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
     712           0 :     dppi->weightA = dp.weightA;
     713             : 
     714           0 :     if (delta == 0) {
     715           0 :         dp.delta = 1;
     716           0 :         decorr_mono(samples, outsamples, nb_samples, &dp, 1);
     717           0 :         dp.delta = 0;
     718           0 :         memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
     719           0 :         dppi->weightA = dp.weightA = dp.sumA / nb_samples;
     720             :     }
     721             : 
     722           0 :     decorr_mono(samples, outsamples, nb_samples, &dp, 1);
     723           0 : }
     724             : 
     725           0 : static void recurse_mono(WavPackEncodeContext *s, WavPackExtraInfo *info,
     726             :                          int depth, int delta, uint32_t input_bits)
     727             : {
     728           0 :     int term, branches = s->num_branches - depth;
     729             :     int32_t *samples, *outsamples;
     730             :     uint32_t term_bits[22], bits;
     731             : 
     732           0 :     if (branches < 1 || depth + 1 == info->nterms)
     733           0 :         branches = 1;
     734             : 
     735           0 :     CLEAR(term_bits);
     736           0 :     samples = s->sampleptrs[depth][0];
     737           0 :     outsamples = s->sampleptrs[depth + 1][0];
     738             : 
     739           0 :     for (term = 1; term <= 18; term++) {
     740           0 :         if (term == 17 && branches == 1 && depth + 1 < info->nterms)
     741           0 :             continue;
     742             : 
     743           0 :         if (term > 8 && term < 17)
     744           0 :             continue;
     745             : 
     746           0 :         if (!s->extra_flags && (term > 4 && term < 17))
     747           0 :             continue;
     748             : 
     749           0 :         info->dps[depth].value = term;
     750           0 :         info->dps[depth].delta = delta;
     751           0 :         decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
     752           0 :         bits = log2mono(outsamples, s->block_samples, info->log_limit);
     753             : 
     754           0 :         if (bits < info->best_bits) {
     755           0 :             info->best_bits = bits;
     756           0 :             CLEAR(s->decorr_passes);
     757           0 :             memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
     758           0 :             memcpy(s->sampleptrs[info->nterms + 1][0],
     759           0 :                    s->sampleptrs[depth + 1][0], s->block_samples * 4);
     760             :         }
     761             : 
     762           0 :         term_bits[term + 3] = bits;
     763             :     }
     764             : 
     765           0 :     while (depth + 1 < info->nterms && branches--) {
     766           0 :         uint32_t local_best_bits = input_bits;
     767           0 :         int best_term = 0, i;
     768             : 
     769           0 :         for (i = 0; i < 22; i++)
     770           0 :             if (term_bits[i] && term_bits[i] < local_best_bits) {
     771           0 :                 local_best_bits = term_bits[i];
     772           0 :                 best_term = i - 3;
     773             :             }
     774             : 
     775           0 :         if (!best_term)
     776           0 :             break;
     777             : 
     778           0 :         term_bits[best_term + 3] = 0;
     779             : 
     780           0 :         info->dps[depth].value = best_term;
     781           0 :         info->dps[depth].delta = delta;
     782           0 :         decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
     783             : 
     784           0 :         recurse_mono(s, info, depth + 1, delta, local_best_bits);
     785             :     }
     786           0 : }
     787             : 
     788           0 : static void sort_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
     789             : {
     790           0 :     int reversed = 1;
     791             :     uint32_t bits;
     792             : 
     793           0 :     while (reversed) {
     794             :         int ri, i;
     795             : 
     796           0 :         memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
     797           0 :         reversed = 0;
     798             : 
     799           0 :         for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
     800             : 
     801           0 :             if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
     802             :                 break;
     803             : 
     804           0 :             if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
     805           0 :                 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
     806           0 :                                    s->block_samples, info->dps, ri);
     807           0 :                 continue;
     808             :             }
     809             : 
     810           0 :             info->dps[ri  ] = s->decorr_passes[ri+1];
     811           0 :             info->dps[ri+1] = s->decorr_passes[ri  ];
     812             : 
     813           0 :             for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
     814           0 :                 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
     815           0 :                                    s->block_samples, info->dps, i);
     816             : 
     817           0 :             bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
     818           0 :             if (bits < info->best_bits) {
     819           0 :                 reversed = 1;
     820           0 :                 info->best_bits = bits;
     821           0 :                 CLEAR(s->decorr_passes);
     822           0 :                 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
     823           0 :                 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
     824           0 :                        s->block_samples * 4);
     825             :             } else {
     826           0 :                 info->dps[ri  ] = s->decorr_passes[ri];
     827           0 :                 info->dps[ri+1] = s->decorr_passes[ri+1];
     828           0 :                 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
     829           0 :                                    s->block_samples, info->dps, ri);
     830             :             }
     831             :         }
     832             :     }
     833           0 : }
     834             : 
     835           0 : static void delta_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
     836             : {
     837           0 :     int lower = 0, delta, d;
     838             :     uint32_t bits;
     839             : 
     840           0 :     if (!s->decorr_passes[0].value)
     841           0 :         return;
     842           0 :     delta = s->decorr_passes[0].delta;
     843             : 
     844           0 :     for (d = delta - 1; d >= 0; d--) {
     845             :         int i;
     846             : 
     847           0 :         for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
     848           0 :             info->dps[i].value = s->decorr_passes[i].value;
     849           0 :             info->dps[i].delta = d;
     850           0 :             decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
     851           0 :                                s->block_samples, info->dps, i);
     852             :         }
     853             : 
     854           0 :         bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
     855           0 :         if (bits >= info->best_bits)
     856           0 :             break;
     857             : 
     858           0 :         lower = 1;
     859           0 :         info->best_bits = bits;
     860           0 :         CLEAR(s->decorr_passes);
     861           0 :         memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
     862           0 :         memcpy(s->sampleptrs[info->nterms + 1][0],  s->sampleptrs[i][0],
     863           0 :                s->block_samples * 4);
     864             :     }
     865             : 
     866           0 :     for (d = delta + 1; !lower && d <= 7; d++) {
     867             :         int i;
     868             : 
     869           0 :         for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
     870           0 :             info->dps[i].value = s->decorr_passes[i].value;
     871           0 :             info->dps[i].delta = d;
     872           0 :             decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
     873           0 :                                s->block_samples, info->dps, i);
     874             :         }
     875             : 
     876           0 :         bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
     877           0 :         if (bits >= info->best_bits)
     878           0 :             break;
     879             : 
     880           0 :         info->best_bits = bits;
     881           0 :         CLEAR(s->decorr_passes);
     882           0 :         memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
     883           0 :         memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
     884           0 :                s->block_samples * 4);
     885             :     }
     886             : }
     887             : 
     888           0 : static int allocate_buffers2(WavPackEncodeContext *s, int nterms)
     889             : {
     890             :     int i;
     891             : 
     892           0 :     for (i = 0; i < nterms + 2; i++) {
     893           0 :         av_fast_padded_malloc(&s->sampleptrs[i][0], &s->sampleptrs_size[i][0],
     894           0 :                               s->block_samples * 4);
     895           0 :         if (!s->sampleptrs[i][0])
     896           0 :             return AVERROR(ENOMEM);
     897           0 :         if (!(s->flags & WV_MONO_DATA)) {
     898           0 :             av_fast_padded_malloc(&s->sampleptrs[i][1], &s->sampleptrs_size[i][1],
     899           0 :                                   s->block_samples * 4);
     900           0 :             if (!s->sampleptrs[i][1])
     901           0 :                 return AVERROR(ENOMEM);
     902             :         }
     903             :     }
     904             : 
     905           0 :     return 0;
     906             : }
     907             : 
     908          13 : static int allocate_buffers(WavPackEncodeContext *s)
     909             : {
     910             :     int i;
     911             : 
     912          39 :     for (i = 0; i < 2; i++) {
     913          26 :         av_fast_padded_malloc(&s->best_buffer[0], &s->best_buffer_size[0],
     914          26 :                               s->block_samples * 4);
     915          26 :         if (!s->best_buffer[0])
     916           0 :             return AVERROR(ENOMEM);
     917             : 
     918          26 :         av_fast_padded_malloc(&s->temp_buffer[i][0], &s->temp_buffer_size[i][0],
     919          26 :                               s->block_samples * 4);
     920          26 :         if (!s->temp_buffer[i][0])
     921           0 :             return AVERROR(ENOMEM);
     922          26 :         if (!(s->flags & WV_MONO_DATA)) {
     923          24 :             av_fast_padded_malloc(&s->best_buffer[1], &s->best_buffer_size[1],
     924          24 :                                   s->block_samples * 4);
     925          24 :             if (!s->best_buffer[1])
     926           0 :                 return AVERROR(ENOMEM);
     927             : 
     928          24 :             av_fast_padded_malloc(&s->temp_buffer[i][1], &s->temp_buffer_size[i][1],
     929          24 :                                   s->block_samples * 4);
     930          24 :             if (!s->temp_buffer[i][1])
     931           0 :                 return AVERROR(ENOMEM);
     932             :         }
     933             :     }
     934             : 
     935          13 :     return 0;
     936             : }
     937             : 
     938           0 : static void analyze_mono(WavPackEncodeContext *s, int32_t *samples, int do_samples)
     939             : {
     940             :     WavPackExtraInfo info;
     941             :     int i;
     942             : 
     943           0 :     info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
     944           0 :     info.log_limit = FFMIN(6912, info.log_limit);
     945             : 
     946           0 :     info.nterms = s->num_terms;
     947             : 
     948           0 :     if (allocate_buffers2(s, s->num_terms))
     949           0 :         return;
     950             : 
     951           0 :     memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
     952           0 :     memcpy(s->sampleptrs[0][0], samples, s->block_samples * 4);
     953             : 
     954           0 :     for (i = 0; i < info.nterms && info.dps[i].value; i++)
     955           0 :         decorr_mono(s->sampleptrs[i][0], s->sampleptrs[i + 1][0],
     956           0 :                     s->block_samples, info.dps + i, 1);
     957             : 
     958           0 :     info.best_bits = log2mono(s->sampleptrs[info.nterms][0], s->block_samples, 0) * 1;
     959           0 :     memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
     960             : 
     961           0 :     if (s->extra_flags & EXTRA_BRANCHES)
     962           0 :         recurse_mono(s, &info, 0, (int) floor(s->delta_decay + 0.5),
     963             :                      log2mono(s->sampleptrs[0][0], s->block_samples, 0));
     964             : 
     965           0 :     if (s->extra_flags & EXTRA_SORT_FIRST)
     966           0 :         sort_mono(s, &info);
     967             : 
     968           0 :     if (s->extra_flags & EXTRA_TRY_DELTAS) {
     969           0 :         delta_mono(s, &info);
     970             : 
     971           0 :         if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
     972           0 :             s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
     973             :         else
     974           0 :             s->delta_decay = 2.0;
     975             :     }
     976             : 
     977           0 :     if (s->extra_flags & EXTRA_SORT_LAST)
     978           0 :         sort_mono(s, &info);
     979             : 
     980           0 :     if (do_samples)
     981           0 :         memcpy(samples, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
     982             : 
     983           0 :     for (i = 0; i < info.nterms; i++)
     984           0 :         if (!s->decorr_passes[i].value)
     985           0 :             break;
     986             : 
     987           0 :     s->num_terms = i;
     988             : }
     989             : 
     990           3 : static void scan_word(WavPackEncodeContext *s, WvChannel *c,
     991             :                       int32_t *samples, int nb_samples, int dir)
     992             : {
     993           3 :     if (dir < 0)
     994           3 :         samples += nb_samples - 1;
     995             : 
     996       88206 :     while (nb_samples--) {
     997       88200 :         uint32_t low, value = labs(samples[0]);
     998             : 
     999       88200 :         if (value < GET_MED(0)) {
    1000       68946 :             DEC_MED(0);
    1001             :         } else {
    1002       19254 :             low = GET_MED(0);
    1003       19254 :             INC_MED(0);
    1004             : 
    1005       19254 :             if (value - low < GET_MED(1)) {
    1006       13482 :                 DEC_MED(1);
    1007             :             } else {
    1008        5772 :                 low += GET_MED(1);
    1009        5772 :                 INC_MED(1);
    1010             : 
    1011        5772 :                 if (value - low < GET_MED(2)) {
    1012        3975 :                     DEC_MED(2);
    1013             :                 } else {
    1014        1797 :                     INC_MED(2);
    1015             :                 }
    1016             :             }
    1017             :         }
    1018       88200 :         samples += dir;
    1019             :     }
    1020           3 : }
    1021             : 
    1022           1 : static int wv_mono(WavPackEncodeContext *s, int32_t *samples,
    1023             :                    int no_history, int do_samples)
    1024             : {
    1025           1 :     struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
    1026           1 :     int nb_samples = s->block_samples;
    1027           1 :     int buf_size = sizeof(int32_t) * nb_samples;
    1028           1 :     uint32_t best_size = UINT32_MAX, size;
    1029             :     int log_limit, pi, i, ret;
    1030             : 
    1031           1 :     for (i = 0; i < nb_samples; i++)
    1032           1 :         if (samples[i])
    1033           1 :             break;
    1034             : 
    1035           1 :     if (i == nb_samples) {
    1036           0 :         CLEAR(s->decorr_passes);
    1037           0 :         CLEAR(s->w);
    1038           0 :         s->num_terms = 0;
    1039           0 :         return 0;
    1040             :     }
    1041             : 
    1042           1 :     log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
    1043           1 :     log_limit = FFMIN(6912, log_limit);
    1044             : 
    1045           1 :     if ((ret = allocate_buffers(s)) < 0)
    1046           0 :         return ret;
    1047             : 
    1048           1 :     if (no_history || s->num_passes >= 7)
    1049           1 :         s->best_decorr = s->mask_decorr = 0;
    1050             : 
    1051           3 :     for (pi = 0; pi < s->num_passes;) {
    1052             :         const WavPackDecorrSpec *wpds;
    1053             :         int nterms, c, j;
    1054             : 
    1055           1 :         if (!pi) {
    1056           1 :             c = s->best_decorr;
    1057             :         } else {
    1058           0 :             if (s->mask_decorr == 0)
    1059           0 :                 c = 0;
    1060             :             else
    1061           0 :                 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
    1062             : 
    1063           0 :             if (c == s->best_decorr) {
    1064           0 :                 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
    1065           0 :                 continue;
    1066             :             }
    1067             :         }
    1068             : 
    1069           1 :         wpds = &s->decorr_specs[c];
    1070           1 :         nterms = decorr_filter_nterms[s->decorr_filter];
    1071             : 
    1072             :         while (1) {
    1073           1 :         memcpy(s->temp_buffer[0][0], samples, buf_size);
    1074           1 :         CLEAR(save_decorr_passes);
    1075             : 
    1076           3 :         for (j = 0; j < nterms; j++) {
    1077           2 :             CLEAR(temp_decorr_pass);
    1078           2 :             temp_decorr_pass.delta = wpds->delta;
    1079           2 :             temp_decorr_pass.value = wpds->terms[j];
    1080             : 
    1081           2 :             if (temp_decorr_pass.value < 0)
    1082           0 :                 temp_decorr_pass.value = 1;
    1083             : 
    1084           2 :             decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
    1085             :                         FFMIN(nb_samples, 2048), &temp_decorr_pass, -1);
    1086             : 
    1087           2 :             if (j) {
    1088           1 :                 CLEAR(temp_decorr_pass.samplesA);
    1089             :             } else {
    1090           1 :                 reverse_mono_decorr(&temp_decorr_pass);
    1091             :             }
    1092             : 
    1093           2 :             memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
    1094           2 :             decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
    1095             :                         nb_samples, &temp_decorr_pass, 1);
    1096             :         }
    1097             : 
    1098           1 :         size = log2mono(s->temp_buffer[j&1][0], nb_samples, log_limit);
    1099           1 :         if (size != UINT32_MAX || !nterms)
    1100             :             break;
    1101           0 :         nterms >>= 1;
    1102             :         }
    1103             : 
    1104           1 :         if (size < best_size) {
    1105           1 :             memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
    1106           1 :             memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
    1107           1 :             s->num_terms = nterms;
    1108           1 :             s->best_decorr = c;
    1109           1 :             best_size = size;
    1110             :         }
    1111             : 
    1112           1 :         if (pi++)
    1113           0 :             s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
    1114             :     }
    1115             : 
    1116           1 :     if (s->extra_flags)
    1117           0 :         analyze_mono(s, samples, do_samples);
    1118           1 :     else if (do_samples)
    1119           0 :         memcpy(samples, s->best_buffer[0], buf_size);
    1120             : 
    1121           1 :     if (no_history || s->extra_flags) {
    1122           1 :         CLEAR(s->w);
    1123           1 :         scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
    1124             :     }
    1125           1 :     return 0;
    1126             : }
    1127             : 
    1128         120 : static void decorr_stereo(int32_t *in_left, int32_t *in_right,
    1129             :                           int32_t *out_left, int32_t *out_right,
    1130             :                           int nb_samples, struct Decorr *dpp, int dir)
    1131             : {
    1132         120 :     int m = 0, i;
    1133             : 
    1134         120 :     dpp->sumA = dpp->sumB = 0;
    1135             : 
    1136         120 :     if (dir < 0) {
    1137         120 :         out_left  += nb_samples - 1;
    1138         120 :         out_right += nb_samples - 1;
    1139         120 :         in_left   += nb_samples - 1;
    1140         120 :         in_right  += nb_samples - 1;
    1141             :     }
    1142             : 
    1143         120 :     dpp->weightA = restore_weight(store_weight(dpp->weightA));
    1144         120 :     dpp->weightB = restore_weight(store_weight(dpp->weightB));
    1145             : 
    1146        1080 :     for (i = 0; i < MAX_TERM; i++) {
    1147         960 :         dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
    1148         960 :         dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
    1149             :     }
    1150             : 
    1151         120 :     switch (dpp->value) {
    1152          21 :     case 2:
    1153       43050 :         while (nb_samples--) {
    1154             :             int32_t sam, tmp;
    1155             : 
    1156       43008 :             sam = dpp->samplesA[0];
    1157       43008 :             dpp->samplesA[0] = dpp->samplesA[1];
    1158       43008 :             out_left[0] = tmp = (dpp->samplesA[1] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
    1159       43008 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1160       43008 :             dpp->sumA += dpp->weightA;
    1161             : 
    1162       43008 :             sam = dpp->samplesB[0];
    1163       43008 :             dpp->samplesB[0] = dpp->samplesB[1];
    1164       43008 :             out_right[0] = tmp = (dpp->samplesB[1] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
    1165       43008 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1166       43008 :             dpp->sumB += dpp->weightB;
    1167             : 
    1168       43008 :             in_left   += dir;
    1169       43008 :             out_left  += dir;
    1170       43008 :             in_right  += dir;
    1171       43008 :             out_right += dir;
    1172             :         }
    1173          21 :         break;
    1174          40 :     case 17:
    1175       82000 :         while (nb_samples--) {
    1176             :             int32_t sam, tmp;
    1177             : 
    1178       81920 :             sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
    1179       81920 :             dpp->samplesA[1] = dpp->samplesA[0];
    1180       81920 :             out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
    1181       81920 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1182       81920 :             dpp->sumA += dpp->weightA;
    1183             : 
    1184       81920 :             sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
    1185       81920 :             dpp->samplesB[1] = dpp->samplesB[0];
    1186       81920 :             out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT (dpp->weightB, sam);
    1187       81920 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1188       81920 :             dpp->sumB += dpp->weightB;
    1189             : 
    1190       81920 :             in_left   += dir;
    1191       81920 :             out_left  += dir;
    1192       81920 :             in_right  += dir;
    1193       81920 :             out_right += dir;
    1194             :         }
    1195          40 :         break;
    1196          25 :     case 18:
    1197       51250 :         while (nb_samples--) {
    1198             :             int32_t sam, tmp;
    1199             : 
    1200       51200 :             sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
    1201       51200 :             dpp->samplesA[1] = dpp->samplesA[0];
    1202       51200 :             out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
    1203       51200 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1204       51200 :             dpp->sumA += dpp->weightA;
    1205             : 
    1206       51200 :             sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
    1207       51200 :             dpp->samplesB[1] = dpp->samplesB[0];
    1208       51200 :             out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
    1209       51200 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1210       51200 :             dpp->sumB += dpp->weightB;
    1211             : 
    1212       51200 :             in_left   += dir;
    1213       51200 :             out_left  += dir;
    1214       51200 :             in_right  += dir;
    1215       51200 :             out_right += dir;
    1216             :         }
    1217          25 :         break;
    1218          17 :     default: {
    1219          17 :         int k = dpp->value & (MAX_TERM - 1);
    1220             : 
    1221       34850 :         while (nb_samples--) {
    1222             :             int32_t sam, tmp;
    1223             : 
    1224       34816 :             sam = dpp->samplesA[m];
    1225       34816 :             out_left[0] = tmp = (dpp->samplesA[k] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
    1226       34816 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1227       34816 :             dpp->sumA += dpp->weightA;
    1228             : 
    1229       34816 :             sam = dpp->samplesB[m];
    1230       34816 :             out_right[0] = tmp = (dpp->samplesB[k] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
    1231       34816 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1232       34816 :             dpp->sumB += dpp->weightB;
    1233             : 
    1234       34816 :             in_left   += dir;
    1235       34816 :             out_left  += dir;
    1236       34816 :             in_right  += dir;
    1237       34816 :             out_right += dir;
    1238       34816 :             m = (m + 1) & (MAX_TERM - 1);
    1239       34816 :             k = (k + 1) & (MAX_TERM - 1);
    1240             :         }
    1241             : 
    1242          17 :         if (m) {
    1243             :             int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
    1244             :             int k;
    1245             : 
    1246           0 :             memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
    1247           0 :             memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
    1248             : 
    1249           0 :             for (k = 0; k < MAX_TERM; k++) {
    1250           0 :                 dpp->samplesA[k] = temp_A[m];
    1251           0 :                 dpp->samplesB[k] = temp_B[m];
    1252           0 :                 m = (m + 1) & (MAX_TERM - 1);
    1253             :             }
    1254             :         }
    1255          17 :         break;
    1256             :         }
    1257           3 :     case -1:
    1258        6150 :         while (nb_samples--) {
    1259             :             int32_t sam_A, sam_B, tmp;
    1260             : 
    1261        6144 :             sam_A = dpp->samplesA[0];
    1262        6144 :             out_left[0] = tmp = (sam_B = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
    1263        6144 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    1264        6144 :             dpp->sumA += dpp->weightA;
    1265             : 
    1266        6144 :             out_right[0] = tmp = (dpp->samplesA[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
    1267        6144 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    1268        6144 :             dpp->sumB += dpp->weightB;
    1269             : 
    1270        6144 :             in_left   += dir;
    1271        6144 :             out_left  += dir;
    1272        6144 :             in_right  += dir;
    1273        6144 :             out_right += dir;
    1274             :         }
    1275           3 :         break;
    1276          13 :     case -2:
    1277       26650 :         while (nb_samples--) {
    1278             :             int32_t sam_A, sam_B, tmp;
    1279             : 
    1280       26624 :             sam_B = dpp->samplesB[0];
    1281       26624 :             out_right[0] = tmp = (sam_A = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
    1282       26624 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    1283       26624 :             dpp->sumB += dpp->weightB;
    1284             : 
    1285       26624 :             out_left[0] = tmp = (dpp->samplesB[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
    1286       26624 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    1287       26624 :             dpp->sumA += dpp->weightA;
    1288             : 
    1289       26624 :             in_left   += dir;
    1290       26624 :             out_left  += dir;
    1291       26624 :             in_right  += dir;
    1292       26624 :             out_right += dir;
    1293             :         }
    1294          13 :         break;
    1295           1 :     case -3:
    1296        2050 :         while (nb_samples--) {
    1297             :             int32_t sam_A, sam_B, tmp;
    1298             : 
    1299        2048 :             sam_A = dpp->samplesA[0];
    1300        2048 :             sam_B = dpp->samplesB[0];
    1301             : 
    1302        2048 :             dpp->samplesA[0] = tmp = in_right[0];
    1303        2048 :             out_right[0] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
    1304        2048 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    1305        2048 :             dpp->sumB += dpp->weightB;
    1306             : 
    1307        2048 :             dpp->samplesB[0] = tmp = in_left[0];
    1308        2048 :             out_left[0] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
    1309        2048 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    1310        2048 :             dpp->sumA += dpp->weightA;
    1311             : 
    1312        2048 :             in_left   += dir;
    1313        2048 :             out_left  += dir;
    1314        2048 :             in_right  += dir;
    1315        2048 :             out_right += dir;
    1316             :         }
    1317           1 :         break;
    1318             :     }
    1319         120 : }
    1320             : 
    1321          24 : static void reverse_decorr(struct Decorr *dpp)
    1322             : {
    1323          24 :     if (dpp->value > MAX_TERM) {
    1324             :         int32_t sam_A, sam_B;
    1325             : 
    1326          23 :         if (dpp->value & 1) {
    1327          12 :             sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
    1328          12 :             sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
    1329             :         } else {
    1330          11 :             sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
    1331          11 :             sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
    1332             :         }
    1333             : 
    1334          23 :         dpp->samplesA[1] = dpp->samplesA[0];
    1335          23 :         dpp->samplesB[1] = dpp->samplesB[0];
    1336          23 :         dpp->samplesA[0] = sam_A;
    1337          23 :         dpp->samplesB[0] = sam_B;
    1338             : 
    1339          23 :         if (dpp->value & 1) {
    1340          12 :             sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
    1341          12 :             sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
    1342             :         } else {
    1343          11 :             sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
    1344          11 :             sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
    1345             :         }
    1346             : 
    1347          23 :         dpp->samplesA[1] = sam_A;
    1348          23 :         dpp->samplesB[1] = sam_B;
    1349           1 :     } else if (dpp->value > 1) {
    1350             :         int i, j, k;
    1351             : 
    1352           2 :         for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
    1353           1 :             i &= (MAX_TERM - 1);
    1354           1 :             j &= (MAX_TERM - 1);
    1355           1 :             dpp->samplesA[i] ^= dpp->samplesA[j];
    1356           1 :             dpp->samplesA[j] ^= dpp->samplesA[i];
    1357           1 :             dpp->samplesA[i] ^= dpp->samplesA[j];
    1358           1 :             dpp->samplesB[i] ^= dpp->samplesB[j];
    1359           1 :             dpp->samplesB[j] ^= dpp->samplesB[i];
    1360           1 :             dpp->samplesB[i] ^= dpp->samplesB[j];
    1361             :         }
    1362             :     }
    1363          24 : }
    1364             : 
    1365         120 : static void decorr_stereo_quick(int32_t *in_left,  int32_t *in_right,
    1366             :                                 int32_t *out_left, int32_t *out_right,
    1367             :                                 int nb_samples, struct Decorr *dpp)
    1368             : {
    1369         120 :     int m = 0, i;
    1370             : 
    1371         120 :     dpp->weightA = restore_weight(store_weight(dpp->weightA));
    1372         120 :     dpp->weightB = restore_weight(store_weight(dpp->weightB));
    1373             : 
    1374        1080 :     for (i = 0; i < MAX_TERM; i++) {
    1375         960 :         dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
    1376         960 :         dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
    1377             :     }
    1378             : 
    1379         120 :     switch (dpp->value) {
    1380          21 :     case 2:
    1381      463071 :         for (i = 0; i < nb_samples; i++) {
    1382             :             int32_t sam, tmp;
    1383             : 
    1384      463050 :             sam = dpp->samplesA[0];
    1385      463050 :             dpp->samplesA[0] = dpp->samplesA[1];
    1386      463050 :             out_left[i] = tmp = (dpp->samplesA[1] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
    1387      463050 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1388             : 
    1389      463050 :             sam = dpp->samplesB[0];
    1390      463050 :             dpp->samplesB[0] = dpp->samplesB[1];
    1391      463050 :             out_right[i] = tmp = (dpp->samplesB[1] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
    1392      463050 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1393             :         }
    1394          21 :         break;
    1395          40 :     case 17:
    1396      882040 :         for (i = 0; i < nb_samples; i++) {
    1397             :             int32_t sam, tmp;
    1398             : 
    1399      882000 :             sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
    1400      882000 :             dpp->samplesA[1] = dpp->samplesA[0];
    1401      882000 :             out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
    1402      882000 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1403             : 
    1404      882000 :             sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
    1405      882000 :             dpp->samplesB[1] = dpp->samplesB[0];
    1406      882000 :             out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
    1407      882000 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1408             :         }
    1409          40 :         break;
    1410          25 :     case 18:
    1411      551275 :         for (i = 0; i < nb_samples; i++) {
    1412             :             int32_t sam, tmp;
    1413             : 
    1414      551250 :             sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
    1415      551250 :             dpp->samplesA[1] = dpp->samplesA[0];
    1416      551250 :             out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
    1417      551250 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1418             : 
    1419      551250 :             sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
    1420      551250 :             dpp->samplesB[1] = dpp->samplesB[0];
    1421      551250 :             out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
    1422      551250 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1423             :         }
    1424          25 :         break;
    1425          17 :     default: {
    1426          17 :         int k = dpp->value & (MAX_TERM - 1);
    1427             : 
    1428      374867 :         for (i = 0; i < nb_samples; i++) {
    1429             :             int32_t sam, tmp;
    1430             : 
    1431      374850 :             sam = dpp->samplesA[m];
    1432      374850 :             out_left[i] = tmp = (dpp->samplesA[k] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
    1433      374850 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    1434             : 
    1435      374850 :             sam = dpp->samplesB[m];
    1436      374850 :             out_right[i] = tmp = (dpp->samplesB[k] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
    1437      374850 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    1438             : 
    1439      374850 :             m = (m + 1) & (MAX_TERM - 1);
    1440      374850 :             k = (k + 1) & (MAX_TERM - 1);
    1441             :         }
    1442             : 
    1443          17 :         if (m) {
    1444             :             int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
    1445             :             int k;
    1446             : 
    1447          17 :             memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
    1448          17 :             memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
    1449             : 
    1450         153 :             for (k = 0; k < MAX_TERM; k++) {
    1451         136 :                 dpp->samplesA[k] = temp_A[m];
    1452         136 :                 dpp->samplesB[k] = temp_B[m];
    1453         136 :                 m = (m + 1) & (MAX_TERM - 1);
    1454             :             }
    1455             :         }
    1456          17 :         break;
    1457             :     }
    1458           3 :     case -1:
    1459       66153 :         for (i = 0; i < nb_samples; i++) {
    1460             :             int32_t sam_A, sam_B, tmp;
    1461             : 
    1462       66150 :             sam_A = dpp->samplesA[0];
    1463       66150 :             out_left[i] = tmp = (sam_B = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
    1464       66150 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    1465             : 
    1466       66150 :             out_right[i] = tmp = (dpp->samplesA[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
    1467       66150 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    1468             :         }
    1469           3 :         break;
    1470          13 :     case -2:
    1471      286663 :         for (i = 0; i < nb_samples; i++) {
    1472             :             int32_t sam_A, sam_B, tmp;
    1473             : 
    1474      286650 :             sam_B = dpp->samplesB[0];
    1475      286650 :             out_right[i] = tmp = (sam_A = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
    1476      286650 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    1477             : 
    1478      286650 :             out_left[i] = tmp = (dpp->samplesB[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
    1479      286650 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    1480             :         }
    1481          13 :         break;
    1482           1 :     case -3:
    1483       22051 :         for (i = 0; i < nb_samples; i++) {
    1484             :             int32_t sam_A, sam_B, tmp;
    1485             : 
    1486       22050 :             sam_A = dpp->samplesA[0];
    1487       22050 :             sam_B = dpp->samplesB[0];
    1488             : 
    1489       22050 :             dpp->samplesA[0] = tmp = in_right[i];
    1490       22050 :             out_right[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
    1491       22050 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    1492             : 
    1493       22050 :             dpp->samplesB[0] = tmp = in_left[i];
    1494       22050 :             out_left[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
    1495       22050 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    1496             :         }
    1497           1 :         break;
    1498             :     }
    1499         120 : }
    1500             : 
    1501           0 : static void decorr_stereo_buffer(WavPackExtraInfo *info,
    1502             :                                  int32_t *in_left,  int32_t *in_right,
    1503             :                                  int32_t *out_left, int32_t *out_right,
    1504             :                                  int nb_samples, int tindex)
    1505             : {
    1506           0 :     struct Decorr dp = {0}, *dppi = info->dps + tindex;
    1507           0 :     int delta = dppi->delta, pre_delta;
    1508           0 :     int term = dppi->value;
    1509             : 
    1510           0 :     if (delta == 7)
    1511           0 :         pre_delta = 7;
    1512           0 :     else if (delta < 2)
    1513           0 :         pre_delta = 3;
    1514             :     else
    1515           0 :         pre_delta = delta + 1;
    1516             : 
    1517           0 :     dp.value = term;
    1518           0 :     dp.delta = pre_delta;
    1519           0 :     decorr_stereo(in_left, in_right, out_left, out_right,
    1520             :                   FFMIN(2048, nb_samples), &dp, -1);
    1521           0 :     dp.delta = delta;
    1522             : 
    1523           0 :     if (tindex == 0) {
    1524           0 :         reverse_decorr(&dp);
    1525             :     } else {
    1526           0 :         CLEAR(dp.samplesA);
    1527           0 :         CLEAR(dp.samplesB);
    1528             :     }
    1529             : 
    1530           0 :     memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
    1531           0 :     memcpy(dppi->samplesB, dp.samplesB, sizeof(dp.samplesB));
    1532           0 :     dppi->weightA = dp.weightA;
    1533           0 :     dppi->weightB = dp.weightB;
    1534             : 
    1535           0 :     if (delta == 0) {
    1536           0 :         dp.delta = 1;
    1537           0 :         decorr_stereo(in_left, in_right, out_left, out_right, nb_samples, &dp, 1);
    1538           0 :         dp.delta = 0;
    1539           0 :         memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
    1540           0 :         memcpy(dp.samplesB, dppi->samplesB, sizeof(dp.samplesB));
    1541           0 :         dppi->weightA = dp.weightA = dp.sumA / nb_samples;
    1542           0 :         dppi->weightB = dp.weightB = dp.sumB / nb_samples;
    1543             :     }
    1544             : 
    1545           0 :     if (info->gt16bit)
    1546           0 :         decorr_stereo(in_left, in_right, out_left, out_right,
    1547             :                            nb_samples, &dp, 1);
    1548             :     else
    1549           0 :         decorr_stereo_quick(in_left, in_right, out_left, out_right,
    1550             :                             nb_samples, &dp);
    1551           0 : }
    1552             : 
    1553           0 : static void sort_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
    1554             : {
    1555           0 :     int reversed = 1;
    1556             :     uint32_t bits;
    1557             : 
    1558           0 :     while (reversed) {
    1559             :         int ri, i;
    1560             : 
    1561           0 :         memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
    1562           0 :         reversed = 0;
    1563             : 
    1564           0 :         for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
    1565             : 
    1566           0 :             if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
    1567             :                 break;
    1568             : 
    1569           0 :             if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
    1570           0 :                 decorr_stereo_buffer(info,
    1571             :                                      s->sampleptrs[ri  ][0], s->sampleptrs[ri  ][1],
    1572           0 :                                      s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
    1573             :                                      s->block_samples, ri);
    1574           0 :                 continue;
    1575             :             }
    1576             : 
    1577           0 :             info->dps[ri  ] = s->decorr_passes[ri+1];
    1578           0 :             info->dps[ri+1] = s->decorr_passes[ri  ];
    1579             : 
    1580           0 :             for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
    1581           0 :                 decorr_stereo_buffer(info,
    1582             :                                      s->sampleptrs[i  ][0], s->sampleptrs[i  ][1],
    1583           0 :                                      s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
    1584             :                                      s->block_samples, i);
    1585             : 
    1586           0 :             bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
    1587             :                               s->block_samples, info->log_limit);
    1588             : 
    1589           0 :             if (bits < info->best_bits) {
    1590           0 :                 reversed = 1;
    1591           0 :                 info->best_bits = bits;
    1592           0 :                 CLEAR(s->decorr_passes);
    1593           0 :                 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
    1594           0 :                 memcpy(s->sampleptrs[info->nterms + 1][0],
    1595           0 :                        s->sampleptrs[i][0], s->block_samples * 4);
    1596           0 :                 memcpy(s->sampleptrs[info->nterms + 1][1],
    1597           0 :                        s->sampleptrs[i][1], s->block_samples * 4);
    1598             :             } else {
    1599           0 :                 info->dps[ri  ] = s->decorr_passes[ri  ];
    1600           0 :                 info->dps[ri+1] = s->decorr_passes[ri+1];
    1601           0 :                 decorr_stereo_buffer(info,
    1602             :                                      s->sampleptrs[ri  ][0], s->sampleptrs[ri  ][1],
    1603           0 :                                      s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
    1604             :                                      s->block_samples, ri);
    1605             :             }
    1606             :         }
    1607             :     }
    1608           0 : }
    1609             : 
    1610           0 : static void delta_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
    1611             : {
    1612           0 :     int lower = 0, delta, d, i;
    1613             :     uint32_t bits;
    1614             : 
    1615           0 :     if (!s->decorr_passes[0].value)
    1616           0 :         return;
    1617           0 :     delta = s->decorr_passes[0].delta;
    1618             : 
    1619           0 :     for (d = delta - 1; d >= 0; d--) {
    1620           0 :         for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
    1621           0 :             info->dps[i].value = s->decorr_passes[i].value;
    1622           0 :             info->dps[i].delta = d;
    1623           0 :             decorr_stereo_buffer(info,
    1624             :                                  s->sampleptrs[i  ][0], s->sampleptrs[i  ][1],
    1625           0 :                                  s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
    1626             :                                  s->block_samples, i);
    1627             :         }
    1628             : 
    1629           0 :         bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
    1630             :                           s->block_samples, info->log_limit);
    1631           0 :         if (bits >= info->best_bits)
    1632           0 :             break;
    1633           0 :         lower = 1;
    1634           0 :         info->best_bits = bits;
    1635           0 :         CLEAR(s->decorr_passes);
    1636           0 :         memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
    1637           0 :         memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
    1638           0 :                s->block_samples * 4);
    1639           0 :         memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[i][1],
    1640           0 :                s->block_samples * 4);
    1641             :     }
    1642             : 
    1643           0 :     for (d = delta + 1; !lower && d <= 7; d++) {
    1644           0 :         for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
    1645           0 :             info->dps[i].value = s->decorr_passes[i].value;
    1646           0 :             info->dps[i].delta = d;
    1647           0 :             decorr_stereo_buffer(info,
    1648             :                                  s->sampleptrs[i  ][0], s->sampleptrs[i  ][1],
    1649           0 :                                  s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
    1650             :                                  s->block_samples, i);
    1651             :         }
    1652             : 
    1653           0 :         bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
    1654             :                           s->block_samples, info->log_limit);
    1655             : 
    1656           0 :         if (bits < info->best_bits) {
    1657           0 :             info->best_bits = bits;
    1658           0 :             CLEAR(s->decorr_passes);
    1659           0 :             memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
    1660           0 :             memcpy(s->sampleptrs[info->nterms + 1][0],
    1661           0 :                    s->sampleptrs[i][0], s->block_samples * 4);
    1662           0 :             memcpy(s->sampleptrs[info->nterms + 1][1],
    1663           0 :                    s->sampleptrs[i][1], s->block_samples * 4);
    1664             :         }
    1665             :         else
    1666           0 :             break;
    1667             :     }
    1668             : }
    1669             : 
    1670           0 : static void recurse_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info,
    1671             :                            int depth, int delta, uint32_t input_bits)
    1672             : {
    1673           0 :     int term, branches = s->num_branches - depth;
    1674             :     int32_t *in_left, *in_right, *out_left, *out_right;
    1675             :     uint32_t term_bits[22], bits;
    1676             : 
    1677           0 :     if (branches < 1 || depth + 1 == info->nterms)
    1678           0 :         branches = 1;
    1679             : 
    1680           0 :     CLEAR(term_bits);
    1681           0 :     in_left   = s->sampleptrs[depth    ][0];
    1682           0 :     in_right  = s->sampleptrs[depth    ][1];
    1683           0 :     out_left  = s->sampleptrs[depth + 1][0];
    1684           0 :     out_right = s->sampleptrs[depth + 1][1];
    1685             : 
    1686           0 :     for (term = -3; term <= 18; term++) {
    1687           0 :         if (!term || (term > 8 && term < 17))
    1688           0 :             continue;
    1689             : 
    1690           0 :         if (term == 17 && branches == 1 && depth + 1 < info->nterms)
    1691           0 :             continue;
    1692             : 
    1693           0 :         if (term == -1 || term == -2)
    1694           0 :             if (!(s->flags & WV_CROSS_DECORR))
    1695           0 :                 continue;
    1696             : 
    1697           0 :         if (!s->extra_flags && (term > 4 && term < 17))
    1698           0 :             continue;
    1699             : 
    1700           0 :         info->dps[depth].value = term;
    1701           0 :         info->dps[depth].delta = delta;
    1702           0 :         decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
    1703             :                              s->block_samples, depth);
    1704           0 :         bits = log2stereo(out_left, out_right, s->block_samples, info->log_limit);
    1705             : 
    1706           0 :         if (bits < info->best_bits) {
    1707           0 :             info->best_bits = bits;
    1708           0 :             CLEAR(s->decorr_passes);
    1709           0 :             memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
    1710           0 :             memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[depth + 1][0],
    1711           0 :                    s->block_samples * 4);
    1712           0 :             memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[depth + 1][1],
    1713           0 :                    s->block_samples * 4);
    1714             :         }
    1715             : 
    1716           0 :         term_bits[term + 3] = bits;
    1717             :     }
    1718             : 
    1719           0 :     while (depth + 1 < info->nterms && branches--) {
    1720           0 :         uint32_t local_best_bits = input_bits;
    1721           0 :         int best_term = 0, i;
    1722             : 
    1723           0 :         for (i = 0; i < 22; i++)
    1724           0 :             if (term_bits[i] && term_bits[i] < local_best_bits) {
    1725           0 :                 local_best_bits = term_bits[i];
    1726           0 :                 best_term = i - 3;
    1727             :             }
    1728             : 
    1729           0 :         if (!best_term)
    1730           0 :             break;
    1731             : 
    1732           0 :         term_bits[best_term + 3] = 0;
    1733             : 
    1734           0 :         info->dps[depth].value = best_term;
    1735           0 :         info->dps[depth].delta = delta;
    1736           0 :         decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
    1737             :                              s->block_samples, depth);
    1738             : 
    1739           0 :         recurse_stereo(s, info, depth + 1, delta, local_best_bits);
    1740             :     }
    1741           0 : }
    1742             : 
    1743           0 : static void analyze_stereo(WavPackEncodeContext *s,
    1744             :                            int32_t *in_left, int32_t *in_right,
    1745             :                            int do_samples)
    1746             : {
    1747             :     WavPackExtraInfo info;
    1748             :     int i;
    1749             : 
    1750           0 :     info.gt16bit = ((s->flags & MAG_MASK) >> MAG_LSB) >= 16;
    1751             : 
    1752           0 :     info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
    1753           0 :     info.log_limit = FFMIN(6912, info.log_limit);
    1754             : 
    1755           0 :     info.nterms = s->num_terms;
    1756             : 
    1757           0 :     if (allocate_buffers2(s, s->num_terms))
    1758           0 :         return;
    1759             : 
    1760           0 :     memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
    1761           0 :     memcpy(s->sampleptrs[0][0], in_left,  s->block_samples * 4);
    1762           0 :     memcpy(s->sampleptrs[0][1], in_right, s->block_samples * 4);
    1763             : 
    1764           0 :     for (i = 0; i < info.nterms && info.dps[i].value; i++)
    1765           0 :         if (info.gt16bit)
    1766           0 :             decorr_stereo(s->sampleptrs[i    ][0], s->sampleptrs[i    ][1],
    1767           0 :                           s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
    1768           0 :                           s->block_samples, info.dps + i, 1);
    1769             :         else
    1770           0 :             decorr_stereo_quick(s->sampleptrs[i    ][0], s->sampleptrs[i    ][1],
    1771           0 :                                 s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
    1772           0 :                                 s->block_samples, info.dps + i);
    1773             : 
    1774           0 :     info.best_bits = log2stereo(s->sampleptrs[info.nterms][0], s->sampleptrs[info.nterms][1],
    1775             :                                 s->block_samples, 0);
    1776             : 
    1777           0 :     memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
    1778           0 :     memcpy(s->sampleptrs[info.nterms + 1][1], s->sampleptrs[i][1], s->block_samples * 4);
    1779             : 
    1780           0 :     if (s->extra_flags & EXTRA_BRANCHES)
    1781           0 :         recurse_stereo(s, &info, 0, (int) floor(s->delta_decay + 0.5),
    1782             :                        log2stereo(s->sampleptrs[0][0], s->sampleptrs[0][1],
    1783             :                                   s->block_samples, 0));
    1784             : 
    1785           0 :     if (s->extra_flags & EXTRA_SORT_FIRST)
    1786           0 :         sort_stereo(s, &info);
    1787             : 
    1788           0 :     if (s->extra_flags & EXTRA_TRY_DELTAS) {
    1789           0 :         delta_stereo(s, &info);
    1790             : 
    1791           0 :         if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
    1792           0 :             s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
    1793             :         else
    1794           0 :             s->delta_decay = 2.0;
    1795             :     }
    1796             : 
    1797           0 :     if (s->extra_flags & EXTRA_SORT_LAST)
    1798           0 :         sort_stereo(s, &info);
    1799             : 
    1800           0 :     if (do_samples) {
    1801           0 :         memcpy(in_left,  s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
    1802           0 :         memcpy(in_right, s->sampleptrs[info.nterms + 1][1], s->block_samples * 4);
    1803             :     }
    1804             : 
    1805           0 :     for (i = 0; i < info.nterms; i++)
    1806           0 :         if (!s->decorr_passes[i].value)
    1807           0 :             break;
    1808             : 
    1809           0 :     s->num_terms = i;
    1810             : }
    1811             : 
    1812          12 : static int wv_stereo(WavPackEncodeContext *s,
    1813             :                      int32_t *samples_l, int32_t *samples_r,
    1814             :                      int no_history, int do_samples)
    1815             : {
    1816          12 :     struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
    1817          12 :     int nb_samples = s->block_samples, ret;
    1818          12 :     int buf_size = sizeof(int32_t) * nb_samples;
    1819          12 :     int log_limit, force_js = 0, force_ts = 0, got_js = 0, pi, i;
    1820          12 :     uint32_t best_size = UINT32_MAX, size;
    1821             : 
    1822          12 :     for (i = 0; i < nb_samples; i++)
    1823          12 :         if (samples_l[i] || samples_r[i])
    1824             :             break;
    1825             : 
    1826          12 :     if (i == nb_samples) {
    1827           0 :         s->flags &= ~((uint32_t) WV_JOINT_STEREO);
    1828           0 :         CLEAR(s->decorr_passes);
    1829           0 :         CLEAR(s->w);
    1830           0 :         s->num_terms = 0;
    1831           0 :         return 0;
    1832             :     }
    1833             : 
    1834          12 :     log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
    1835          12 :     log_limit = FFMIN(6912, log_limit);
    1836             : 
    1837          12 :     if (s->joint != -1) {
    1838           0 :         force_js =  s->joint;
    1839           0 :         force_ts = !s->joint;
    1840             :     }
    1841             : 
    1842          12 :     if ((ret = allocate_buffers(s)) < 0)
    1843           0 :         return ret;
    1844             : 
    1845          12 :     if (no_history || s->num_passes >= 7)
    1846           1 :         s->best_decorr = s->mask_decorr = 0;
    1847             : 
    1848          49 :     for (pi = 0; pi < s->num_passes;) {
    1849             :         const WavPackDecorrSpec *wpds;
    1850             :         int nterms, c, j;
    1851             : 
    1852          25 :         if (!pi)
    1853          12 :             c = s->best_decorr;
    1854             :         else {
    1855          13 :             if (s->mask_decorr == 0)
    1856           2 :                 c = 0;
    1857             :             else
    1858          11 :                 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
    1859             : 
    1860          13 :             if (c == s->best_decorr) {
    1861           1 :                 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
    1862           1 :                 continue;
    1863             :             }
    1864             :         }
    1865             : 
    1866          24 :         wpds = &s->decorr_specs[c];
    1867          24 :         nterms = decorr_filter_nterms[s->decorr_filter];
    1868             : 
    1869             :         while (1) {
    1870          24 :             if (force_js || (wpds->joint_stereo && !force_ts)) {
    1871           3 :                 if (!got_js) {
    1872           3 :                     av_fast_padded_malloc(&s->js_left,  &s->js_left_size,  buf_size);
    1873           3 :                     av_fast_padded_malloc(&s->js_right, &s->js_right_size, buf_size);
    1874           3 :                     memcpy(s->js_left,  samples_l, buf_size);
    1875           3 :                     memcpy(s->js_right, samples_r, buf_size);
    1876             : 
    1877       66153 :                     for (i = 0; i < nb_samples; i++)
    1878       66150 :                         s->js_right[i] += ((s->js_left[i] -= s->js_right[i]) >> 1);
    1879           3 :                     got_js = 1;
    1880             :                 }
    1881             : 
    1882           3 :                 memcpy(s->temp_buffer[0][0], s->js_left,  buf_size);
    1883           3 :                 memcpy(s->temp_buffer[0][1], s->js_right, buf_size);
    1884             :             } else {
    1885          21 :                 memcpy(s->temp_buffer[0][0], samples_l, buf_size);
    1886          21 :                 memcpy(s->temp_buffer[0][1], samples_r, buf_size);
    1887             :             }
    1888             : 
    1889          24 :             CLEAR(save_decorr_passes);
    1890             : 
    1891         144 :             for (j = 0; j < nterms; j++) {
    1892         120 :                 CLEAR(temp_decorr_pass);
    1893         120 :                 temp_decorr_pass.delta = wpds->delta;
    1894         120 :                 temp_decorr_pass.value = wpds->terms[j];
    1895             : 
    1896         120 :                 if (temp_decorr_pass.value < 0 && !(s->flags & WV_CROSS_DECORR))
    1897           0 :                     temp_decorr_pass.value = -3;
    1898             : 
    1899         360 :                 decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
    1900         240 :                               s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
    1901             :                               FFMIN(2048, nb_samples), &temp_decorr_pass, -1);
    1902             : 
    1903         120 :                 if (j) {
    1904          96 :                     CLEAR(temp_decorr_pass.samplesA);
    1905          96 :                     CLEAR(temp_decorr_pass.samplesB);
    1906             :                 } else {
    1907          24 :                     reverse_decorr(&temp_decorr_pass);
    1908             :                 }
    1909             : 
    1910         120 :                 memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
    1911             : 
    1912         120 :                 if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16)
    1913           0 :                     decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
    1914           0 :                                   s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
    1915             :                                   nb_samples, &temp_decorr_pass, 1);
    1916             :                 else
    1917         240 :                     decorr_stereo_quick(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
    1918         240 :                                         s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
    1919             :                                         nb_samples, &temp_decorr_pass);
    1920             :             }
    1921             : 
    1922          24 :             size = log2stereo(s->temp_buffer[j&1][0], s->temp_buffer[j&1][1],
    1923             :                               nb_samples, log_limit);
    1924          24 :             if (size != UINT32_MAX || !nterms)
    1925             :                 break;
    1926           0 :             nterms >>= 1;
    1927             :         }
    1928             : 
    1929          24 :         if (size < best_size) {
    1930          18 :             memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
    1931          18 :             memcpy(s->best_buffer[1], s->temp_buffer[j&1][1], buf_size);
    1932          18 :             memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
    1933          18 :             s->num_terms = nterms;
    1934          18 :             s->best_decorr = c;
    1935          18 :             best_size = size;
    1936             :         }
    1937             : 
    1938          24 :         if (pi++)
    1939          12 :             s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
    1940             :     }
    1941             : 
    1942          12 :     if (force_js || (s->decorr_specs[s->best_decorr].joint_stereo && !force_ts))
    1943           0 :         s->flags |= WV_JOINT_STEREO;
    1944             :     else
    1945          12 :         s->flags &= ~((uint32_t) WV_JOINT_STEREO);
    1946             : 
    1947          12 :     if (s->extra_flags) {
    1948           0 :         if (s->flags & WV_JOINT_STEREO) {
    1949           0 :             analyze_stereo(s, s->js_left, s->js_right, do_samples);
    1950             : 
    1951           0 :             if (do_samples) {
    1952           0 :                 memcpy(samples_l, s->js_left,  buf_size);
    1953           0 :                 memcpy(samples_r, s->js_right, buf_size);
    1954             :             }
    1955             :         } else
    1956           0 :             analyze_stereo(s, samples_l, samples_r, do_samples);
    1957          12 :     } else if (do_samples) {
    1958          12 :         memcpy(samples_l, s->best_buffer[0], buf_size);
    1959          12 :         memcpy(samples_r, s->best_buffer[1], buf_size);
    1960             :     }
    1961             : 
    1962          23 :     if (s->extra_flags || no_history ||
    1963          11 :         s->joint_stereo != s->decorr_specs[s->best_decorr].joint_stereo) {
    1964           1 :         s->joint_stereo = s->decorr_specs[s->best_decorr].joint_stereo;
    1965           1 :         CLEAR(s->w);
    1966           1 :         scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
    1967           1 :         scan_word(s, &s->w.c[1], s->best_buffer[1], nb_samples, -1);
    1968             :     }
    1969          12 :     return 0;
    1970             : }
    1971             : 
    1972             : #define count_bits(av) ( \
    1973             :  (av) < (1 << 8) ? nbits_table[av] : \
    1974             :   ( \
    1975             :    (av) < (1 << 16) ? nbits_table[(av) >> 8] + 8 : \
    1976             :    ((av) < (1 << 24) ? nbits_table[(av) >> 16] + 16 : nbits_table[(av) >> 24] + 24) \
    1977             :   ) \
    1978             : )
    1979             : 
    1980      573304 : static void encode_flush(WavPackEncodeContext *s)
    1981             : {
    1982      573304 :     WavPackWords *w = &s->w;
    1983      573304 :     PutBitContext *pb = &s->pb;
    1984             : 
    1985      573304 :     if (w->zeros_acc) {
    1986           0 :         int cbits = count_bits(w->zeros_acc);
    1987             : 
    1988             :         do {
    1989           0 :             if (cbits > 31) {
    1990           0 :                 put_bits(pb, 31, 0x7FFFFFFF);
    1991           0 :                 cbits -= 31;
    1992             :             } else {
    1993           0 :                 put_bits(pb, cbits, (1 << cbits) - 1);
    1994           0 :                 cbits = 0;
    1995             :             }
    1996           0 :         } while (cbits);
    1997             : 
    1998           0 :         put_bits(pb, 1, 0);
    1999             : 
    2000           0 :         while (w->zeros_acc > 1) {
    2001           0 :             put_bits(pb, 1, w->zeros_acc & 1);
    2002           0 :             w->zeros_acc >>= 1;
    2003             :         }
    2004             : 
    2005           0 :         w->zeros_acc = 0;
    2006             :     }
    2007             : 
    2008      573304 :     if (w->holding_one) {
    2009      125252 :         if (w->holding_one >= 16) {
    2010             :             int cbits;
    2011             : 
    2012          81 :             put_bits(pb, 16, (1 << 16) - 1);
    2013          81 :             put_bits(pb, 1, 0);
    2014          81 :             w->holding_one -= 16;
    2015          81 :             cbits = count_bits(w->holding_one);
    2016             : 
    2017             :             do {
    2018          81 :                 if (cbits > 31) {
    2019           0 :                     put_bits(pb, 31, 0x7FFFFFFF);
    2020           0 :                     cbits -= 31;
    2021             :                 } else {
    2022          81 :                     put_bits(pb, cbits, (1 << cbits) - 1);
    2023          81 :                     cbits = 0;
    2024             :                 }
    2025          81 :             } while (cbits);
    2026             : 
    2027          81 :             put_bits(pb, 1, 0);
    2028             : 
    2029         533 :             while (w->holding_one > 1) {
    2030         371 :                 put_bits(pb, 1, w->holding_one & 1);
    2031         371 :                 w->holding_one >>= 1;
    2032             :             }
    2033             : 
    2034          81 :             w->holding_zero = 0;
    2035             :         } else {
    2036      125171 :             put_bits(pb, w->holding_one, (1 << w->holding_one) - 1);
    2037             :         }
    2038             : 
    2039      125252 :         w->holding_one = 0;
    2040             :     }
    2041             : 
    2042      573304 :     if (w->holding_zero) {
    2043      307729 :         put_bits(pb, 1, 0);
    2044      307729 :         w->holding_zero = 0;
    2045             :     }
    2046             : 
    2047      573304 :     if (w->pend_count) {
    2048      573300 :         put_bits(pb, w->pend_count, w->pend_data);
    2049      573300 :         w->pend_data = w->pend_count = 0;
    2050             :     }
    2051      573304 : }
    2052             : 
    2053      573300 : static void wavpack_encode_sample(WavPackEncodeContext *s, WvChannel *c, int32_t sample)
    2054             : {
    2055      573300 :     WavPackWords *w = &s->w;
    2056             :     uint32_t ones_count, low, high;
    2057      573300 :     int sign = sample < 0;
    2058             : 
    2059      573300 :     if (s->w.c[0].median[0] < 2 && !s->w.holding_zero && s->w.c[1].median[0] < 2) {
    2060           0 :         if (w->zeros_acc) {
    2061           0 :             if (sample)
    2062           0 :                 encode_flush(s);
    2063             :             else {
    2064           0 :                 w->zeros_acc++;
    2065           0 :                 return;
    2066             :             }
    2067           0 :         } else if (sample) {
    2068           0 :             put_bits(&s->pb, 1, 0);
    2069             :         } else {
    2070           0 :             CLEAR(s->w.c[0].median);
    2071           0 :             CLEAR(s->w.c[1].median);
    2072           0 :             w->zeros_acc = 1;
    2073           0 :             return;
    2074             :         }
    2075             :     }
    2076             : 
    2077      573300 :     if (sign)
    2078      203451 :         sample = ~sample;
    2079             : 
    2080      573300 :     if (sample < (int32_t) GET_MED(0)) {
    2081      447048 :         ones_count = low = 0;
    2082      447048 :         high = GET_MED(0) - 1;
    2083      447048 :         DEC_MED(0);
    2084             :     } else {
    2085      126252 :         low = GET_MED(0);
    2086      126252 :         INC_MED(0);
    2087             : 
    2088      126252 :         if (sample - low < GET_MED(1)) {
    2089       89886 :             ones_count = 1;
    2090       89886 :             high = low + GET_MED(1) - 1;
    2091       89886 :             DEC_MED(1);
    2092             :         } else {
    2093       36366 :             low += GET_MED(1);
    2094       36366 :             INC_MED(1);
    2095             : 
    2096       36366 :             if (sample - low < GET_MED(2)) {
    2097       25626 :                 ones_count = 2;
    2098       25626 :                 high = low + GET_MED(2) - 1;
    2099       25626 :                 DEC_MED(2);
    2100             :             } else {
    2101       10740 :                 ones_count = 2 + (sample - low) / GET_MED(2);
    2102       10740 :                 low += (ones_count - 2) * GET_MED(2);
    2103       10740 :                 high = low + GET_MED(2) - 1;
    2104       10740 :                 INC_MED(2);
    2105             :             }
    2106             :         }
    2107             :     }
    2108             : 
    2109      573300 :     if (w->holding_zero) {
    2110      307801 :         if (ones_count)
    2111       42311 :             w->holding_one++;
    2112             : 
    2113      307801 :         encode_flush(s);
    2114             : 
    2115      307801 :         if (ones_count) {
    2116       42311 :             w->holding_zero = 1;
    2117       42311 :             ones_count--;
    2118             :         } else
    2119      265490 :             w->holding_zero = 0;
    2120             :     } else
    2121      265499 :         w->holding_zero = 1;
    2122             : 
    2123      573300 :     w->holding_one = ones_count * 2;
    2124             : 
    2125      573300 :     if (high != low) {
    2126      417675 :         uint32_t maxcode = high - low, code = sample - low;
    2127      417675 :         int bitcount = count_bits(maxcode);
    2128      417675 :         uint32_t extras = (1 << bitcount) - maxcode - 1;
    2129             : 
    2130      417675 :         if (code < extras) {
    2131      151217 :             w->pend_data |= code << w->pend_count;
    2132      151217 :             w->pend_count += bitcount - 1;
    2133             :         } else {
    2134      266458 :             w->pend_data |= ((code + extras) >> 1) << w->pend_count;
    2135      266458 :             w->pend_count += bitcount - 1;
    2136      266458 :             w->pend_data |= ((code + extras) & 1) << w->pend_count++;
    2137             :         }
    2138             :     }
    2139             : 
    2140      573300 :     w->pend_data |= ((int32_t) sign << w->pend_count++);
    2141             : 
    2142      573300 :     if (!w->holding_zero)
    2143      265490 :         encode_flush(s);
    2144             : }
    2145             : 
    2146           0 : static void pack_int32(WavPackEncodeContext *s,
    2147             :                        int32_t *samples_l, int32_t *samples_r,
    2148             :                        int nb_samples)
    2149             : {
    2150           0 :     const int sent_bits = s->int32_sent_bits;
    2151           0 :     PutBitContext *pb = &s->pb;
    2152             :     int i, pre_shift;
    2153             : 
    2154           0 :     pre_shift = s->int32_zeros + s->int32_ones + s->int32_dups;
    2155             : 
    2156           0 :     if (!sent_bits)
    2157           0 :         return;
    2158             : 
    2159           0 :     if (s->flags & WV_MONO_DATA) {
    2160           0 :         for (i = 0; i < nb_samples; i++) {
    2161           0 :             put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
    2162             :         }
    2163             :     } else {
    2164           0 :         for (i = 0; i < nb_samples; i++) {
    2165           0 :             put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
    2166           0 :             put_sbits(pb, sent_bits, samples_r[i] >> pre_shift);
    2167             :         }
    2168             :     }
    2169             : }
    2170             : 
    2171           0 : static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
    2172             : {
    2173           0 :     const int max_exp = s->float_max_exp;
    2174           0 :     PutBitContext *pb = &s->pb;
    2175             :     int32_t value, shift_count;
    2176             : 
    2177           0 :     if (get_exponent(*sample) == 255) {
    2178           0 :         if (get_mantissa(*sample)) {
    2179           0 :             put_bits(pb, 1, 1);
    2180           0 :             put_bits(pb, 23, get_mantissa(*sample));
    2181             :         } else {
    2182           0 :             put_bits(pb, 1, 0);
    2183             :         }
    2184             : 
    2185           0 :         value = 0x1000000;
    2186           0 :         shift_count = 0;
    2187           0 :     } else if (get_exponent(*sample)) {
    2188           0 :         shift_count = max_exp - get_exponent(*sample);
    2189           0 :         value = 0x800000 + get_mantissa(*sample);
    2190             :     } else {
    2191           0 :         shift_count = max_exp ? max_exp - 1 : 0;
    2192           0 :         value = get_mantissa(*sample);
    2193             :     }
    2194             : 
    2195           0 :     if (shift_count < 25)
    2196           0 :         value >>= shift_count;
    2197             :     else
    2198           0 :         value = 0;
    2199             : 
    2200           0 :     if (!value) {
    2201           0 :         if (s->float_flags & FLOAT_ZEROS_SENT) {
    2202           0 :             if (get_exponent(*sample) || get_mantissa(*sample)) {
    2203           0 :                 put_bits(pb, 1, 1);
    2204           0 :                 put_bits(pb, 23, get_mantissa(*sample));
    2205             : 
    2206           0 :                 if (max_exp >= 25)
    2207           0 :                     put_bits(pb, 8, get_exponent(*sample));
    2208             : 
    2209           0 :                 put_bits(pb, 1, get_sign(*sample));
    2210             :             } else {
    2211           0 :                 put_bits(pb, 1, 0);
    2212             : 
    2213           0 :                 if (s->float_flags & FLOAT_NEG_ZEROS)
    2214           0 :                     put_bits(pb, 1, get_sign(*sample));
    2215             :             }
    2216             :         }
    2217           0 :     } else if (shift_count) {
    2218           0 :         if (s->float_flags & FLOAT_SHIFT_SENT) {
    2219           0 :             put_sbits(pb, shift_count, get_mantissa(*sample));
    2220           0 :         } else if (s->float_flags & FLOAT_SHIFT_SAME) {
    2221           0 :             put_bits(pb, 1, get_mantissa(*sample) & 1);
    2222             :         }
    2223             :     }
    2224           0 : }
    2225             : 
    2226           0 : static void pack_float(WavPackEncodeContext *s,
    2227             :                        int32_t *samples_l, int32_t *samples_r,
    2228             :                        int nb_samples)
    2229             : {
    2230             :     int i;
    2231             : 
    2232           0 :     if (s->flags & WV_MONO_DATA) {
    2233           0 :         for (i = 0; i < nb_samples; i++)
    2234           0 :             pack_float_sample(s, &samples_l[i]);
    2235             :     } else {
    2236           0 :         for (i = 0; i < nb_samples; i++) {
    2237           0 :             pack_float_sample(s, &samples_l[i]);
    2238           0 :             pack_float_sample(s, &samples_r[i]);
    2239             :         }
    2240             :     }
    2241           0 : }
    2242             : 
    2243           0 : static void decorr_stereo_pass2(struct Decorr *dpp,
    2244             :                                 int32_t *samples_l, int32_t *samples_r,
    2245             :                                 int nb_samples)
    2246             : {
    2247             :     int i, m, k;
    2248             : 
    2249           0 :     switch (dpp->value) {
    2250           0 :     case 17:
    2251           0 :         for (i = 0; i < nb_samples; i++) {
    2252             :             int32_t sam, tmp;
    2253             : 
    2254           0 :             sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
    2255           0 :             dpp->samplesA[1] = dpp->samplesA[0];
    2256           0 :             samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
    2257           0 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    2258             : 
    2259           0 :             sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
    2260           0 :             dpp->samplesB[1] = dpp->samplesB[0];
    2261           0 :             samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
    2262           0 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    2263             :         }
    2264           0 :         break;
    2265           0 :     case 18:
    2266           0 :         for (i = 0; i < nb_samples; i++) {
    2267             :             int32_t sam, tmp;
    2268             : 
    2269           0 :             sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
    2270           0 :             dpp->samplesA[1] = dpp->samplesA[0];
    2271           0 :             samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
    2272           0 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    2273             : 
    2274           0 :             sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
    2275           0 :             dpp->samplesB[1] = dpp->samplesB[0];
    2276           0 :             samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
    2277           0 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    2278             :         }
    2279           0 :         break;
    2280           0 :     default:
    2281           0 :         for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
    2282             :             int32_t sam, tmp;
    2283             : 
    2284           0 :             sam = dpp->samplesA[m];
    2285           0 :             samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
    2286           0 :             UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
    2287             : 
    2288           0 :             sam = dpp->samplesB[m];
    2289           0 :             samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
    2290           0 :             UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
    2291             : 
    2292           0 :             m = (m + 1) & (MAX_TERM - 1);
    2293           0 :             k = (k + 1) & (MAX_TERM - 1);
    2294             :         }
    2295           0 :         if (m) {
    2296             :             int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
    2297             : 
    2298           0 :             memcpy(temp_A, dpp->samplesA, sizeof (dpp->samplesA));
    2299           0 :             memcpy(temp_B, dpp->samplesB, sizeof (dpp->samplesB));
    2300             : 
    2301           0 :             for (k = 0; k < MAX_TERM; k++) {
    2302           0 :                 dpp->samplesA[k] = temp_A[m];
    2303           0 :                 dpp->samplesB[k] = temp_B[m];
    2304           0 :                 m = (m + 1) & (MAX_TERM - 1);
    2305             :             }
    2306             :         }
    2307           0 :         break;
    2308           0 :     case -1:
    2309           0 :         for (i = 0; i < nb_samples; i++) {
    2310             :             int32_t sam_A, sam_B, tmp;
    2311             : 
    2312           0 :             sam_A = dpp->samplesA[0];
    2313           0 :             samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
    2314           0 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    2315             : 
    2316           0 :             samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
    2317           0 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    2318             :         }
    2319           0 :         break;
    2320           0 :     case -2:
    2321           0 :         for (i = 0; i < nb_samples; i++) {
    2322             :             int32_t sam_A, sam_B, tmp;
    2323             : 
    2324           0 :             sam_B = dpp->samplesB[0];
    2325           0 :             samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
    2326           0 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    2327             : 
    2328           0 :             samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
    2329           0 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    2330             :         }
    2331           0 :         break;
    2332           0 :     case -3:
    2333           0 :         for (i = 0; i < nb_samples; i++) {
    2334             :             int32_t sam_A, sam_B, tmp;
    2335             : 
    2336           0 :             sam_A = dpp->samplesA[0];
    2337           0 :             sam_B = dpp->samplesB[0];
    2338             : 
    2339           0 :             dpp->samplesA[0] = tmp = samples_r[i];
    2340           0 :             samples_r[i] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
    2341           0 :             UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
    2342             : 
    2343           0 :             dpp->samplesB[0] = tmp = samples_l[i];
    2344           0 :             samples_l[i] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
    2345           0 :             UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
    2346             :         }
    2347           0 :         break;
    2348             :     }
    2349           0 : }
    2350             : 
    2351             : #define update_weight_d2(weight, delta, source, result) \
    2352             :     if (source && result) \
    2353             :         weight -= (((source ^ result) >> 29) & 4) - 2;
    2354             : 
    2355             : #define update_weight_clip_d2(weight, delta, source, result) \
    2356             :     if (source && result) { \
    2357             :         const int32_t s = (source ^ result) >> 31; \
    2358             :         if ((weight = (weight ^ s) + (2 - s)) > 1024) weight = 1024; \
    2359             :         weight = (weight ^ s) - s; \
    2360             :     }
    2361             : 
    2362           0 : static void decorr_stereo_pass_id2(struct Decorr *dpp,
    2363             :                                    int32_t *samples_l, int32_t *samples_r,
    2364             :                                    int nb_samples)
    2365             : {
    2366             :     int i, m, k;
    2367             : 
    2368           0 :     switch (dpp->value) {
    2369           0 :     case 17:
    2370           0 :         for (i = 0; i < nb_samples; i++) {
    2371             :             int32_t sam, tmp;
    2372             : 
    2373           0 :             sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
    2374           0 :             dpp->samplesA[1] = dpp->samplesA[0];
    2375           0 :             samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
    2376           0 :             update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
    2377             : 
    2378           0 :             sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
    2379           0 :             dpp->samplesB[1] = dpp->samplesB[0];
    2380           0 :             samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
    2381           0 :             update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
    2382             :         }
    2383           0 :         break;
    2384           0 :     case 18:
    2385           0 :         for (i = 0; i < nb_samples; i++) {
    2386             :             int32_t sam, tmp;
    2387             : 
    2388           0 :             sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
    2389           0 :             dpp->samplesA[1] = dpp->samplesA[0];
    2390           0 :             samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
    2391           0 :             update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
    2392             : 
    2393           0 :             sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
    2394           0 :             dpp->samplesB[1] = dpp->samplesB[0];
    2395           0 :             samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
    2396           0 :             update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
    2397             :         }
    2398           0 :         break;
    2399           0 :     default:
    2400           0 :         for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
    2401             :             int32_t sam, tmp;
    2402             : 
    2403           0 :             sam = dpp->samplesA[m];
    2404           0 :             samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
    2405           0 :             update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
    2406             : 
    2407           0 :             sam = dpp->samplesB[m];
    2408           0 :             samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
    2409           0 :             update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
    2410             : 
    2411           0 :             m = (m + 1) & (MAX_TERM - 1);
    2412           0 :             k = (k + 1) & (MAX_TERM - 1);
    2413             :         }
    2414             : 
    2415           0 :         if (m) {
    2416             :             int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
    2417             : 
    2418           0 :             memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
    2419           0 :             memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
    2420             : 
    2421           0 :             for (k = 0; k < MAX_TERM; k++) {
    2422           0 :                 dpp->samplesA[k] = temp_A[m];
    2423           0 :                 dpp->samplesB[k] = temp_B[m];
    2424           0 :                 m = (m + 1) & (MAX_TERM - 1);
    2425             :             }
    2426             :         }
    2427           0 :         break;
    2428           0 :     case -1:
    2429           0 :         for (i = 0; i < nb_samples; i++) {
    2430             :             int32_t sam_A, sam_B, tmp;
    2431             : 
    2432           0 :             sam_A = dpp->samplesA[0];
    2433           0 :             samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
    2434           0 :             update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
    2435             : 
    2436           0 :             samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
    2437           0 :             update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
    2438             :         }
    2439           0 :         break;
    2440           0 :     case -2:
    2441           0 :         for (i = 0; i < nb_samples; i++) {
    2442             :             int32_t sam_A, sam_B, tmp;
    2443             : 
    2444           0 :             sam_B = dpp->samplesB[0];
    2445           0 :             samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
    2446           0 :             update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
    2447             : 
    2448           0 :             samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
    2449           0 :             update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
    2450             :         }
    2451           0 :         break;
    2452           0 :     case -3:
    2453           0 :         for (i = 0; i < nb_samples; i++) {
    2454             :             int32_t sam_A, sam_B, tmp;
    2455             : 
    2456           0 :             sam_A = dpp->samplesA[0];
    2457           0 :             sam_B = dpp->samplesB[0];
    2458             : 
    2459           0 :             dpp->samplesA[0] = tmp = samples_r[i];
    2460           0 :             samples_r[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
    2461           0 :             update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
    2462             : 
    2463           0 :             dpp->samplesB[0] = tmp = samples_l[i];
    2464           0 :             samples_l[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
    2465           0 :             update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
    2466             :         }
    2467           0 :         break;
    2468             :     }
    2469           0 : }
    2470             : 
    2471          26 : static void put_metadata_block(PutByteContext *pb, int flags, int size)
    2472             : {
    2473          26 :     if (size & 1)
    2474          12 :         flags |= WP_IDF_ODD;
    2475             : 
    2476          26 :     bytestream2_put_byte(pb, flags);
    2477          26 :     bytestream2_put_byte(pb, (size + 1) >> 1);
    2478          26 : }
    2479             : 
    2480          13 : static int wavpack_encode_block(WavPackEncodeContext *s,
    2481             :                                 int32_t *samples_l, int32_t *samples_r,
    2482             :                                 uint8_t *out, int out_size)
    2483             : {
    2484          13 :     int block_size, start, end, data_size, tcount, temp, m = 0;
    2485          13 :     int i, j, ret = 0, got_extra = 0, nb_samples = s->block_samples;
    2486          13 :     uint32_t crc = 0xffffffffu;
    2487             :     struct Decorr *dpp;
    2488             :     PutByteContext pb;
    2489             : 
    2490          13 :     if (s->flags & WV_MONO_DATA) {
    2491           1 :         CLEAR(s->w);
    2492             :     }
    2493          13 :     if (!(s->flags & WV_MONO) && s->optimize_mono) {
    2494           0 :         int32_t lor = 0, diff = 0;
    2495             : 
    2496           0 :         for (i = 0; i < nb_samples; i++) {
    2497           0 :             lor  |= samples_l[i] | samples_r[i];
    2498           0 :             diff |= samples_l[i] - samples_r[i];
    2499             : 
    2500           0 :             if (lor && diff)
    2501           0 :                 break;
    2502             :         }
    2503             : 
    2504           0 :         if (i == nb_samples && lor && !diff) {
    2505           0 :             s->flags &= ~(WV_JOINT_STEREO | WV_CROSS_DECORR);
    2506           0 :             s->flags |= WV_FALSE_STEREO;
    2507             : 
    2508           0 :             if (!s->false_stereo) {
    2509           0 :                 s->false_stereo = 1;
    2510           0 :                 s->num_terms = 0;
    2511           0 :                 CLEAR(s->w);
    2512             :             }
    2513           0 :         } else if (s->false_stereo) {
    2514           0 :             s->false_stereo = 0;
    2515           0 :             s->num_terms = 0;
    2516           0 :             CLEAR(s->w);
    2517             :         }
    2518             :     }
    2519             : 
    2520          13 :     if (s->flags & SHIFT_MASK) {
    2521           0 :         int shift = (s->flags & SHIFT_MASK) >> SHIFT_LSB;
    2522           0 :         int mag = (s->flags & MAG_MASK) >> MAG_LSB;
    2523             : 
    2524           0 :         if (s->flags & WV_MONO_DATA)
    2525           0 :             shift_mono(samples_l, nb_samples, shift);
    2526             :         else
    2527           0 :             shift_stereo(samples_l, samples_r, nb_samples, shift);
    2528             : 
    2529           0 :         if ((mag -= shift) < 0)
    2530           0 :             s->flags &= ~MAG_MASK;
    2531             :         else
    2532           0 :             s->flags -= (1 << MAG_LSB) * shift;
    2533             :     }
    2534             : 
    2535          13 :     if ((s->flags & WV_FLOAT_DATA) || (s->flags & MAG_MASK) >> MAG_LSB >= 24) {
    2536           0 :         av_fast_padded_malloc(&s->orig_l, &s->orig_l_size, sizeof(int32_t) * nb_samples);
    2537           0 :         memcpy(s->orig_l, samples_l, sizeof(int32_t) * nb_samples);
    2538           0 :         if (!(s->flags & WV_MONO_DATA)) {
    2539           0 :             av_fast_padded_malloc(&s->orig_r, &s->orig_r_size, sizeof(int32_t) * nb_samples);
    2540           0 :             memcpy(s->orig_r, samples_r, sizeof(int32_t) * nb_samples);
    2541             :         }
    2542             : 
    2543           0 :         if (s->flags & WV_FLOAT_DATA)
    2544           0 :             got_extra = scan_float(s, samples_l, samples_r, nb_samples);
    2545             :         else
    2546           0 :             got_extra = scan_int32(s, samples_l, samples_r, nb_samples);
    2547           0 :         s->num_terms = 0;
    2548             :     } else {
    2549          13 :         scan_int23(s, samples_l, samples_r, nb_samples);
    2550          13 :         if (s->shift != s->int32_zeros + s->int32_ones + s->int32_dups) {
    2551           0 :             s->shift = s->int32_zeros + s->int32_ones + s->int32_dups;
    2552           0 :             s->num_terms = 0;
    2553             :         }
    2554             :     }
    2555             : 
    2556          13 :     if (!s->num_passes && !s->num_terms) {
    2557           1 :         s->num_passes = 1;
    2558             : 
    2559           1 :         if (s->flags & WV_MONO_DATA)
    2560           1 :             ret = wv_mono(s, samples_l, 1, 0);
    2561             :         else
    2562           0 :             ret = wv_stereo(s, samples_l, samples_r, 1, 0);
    2563             : 
    2564           1 :         s->num_passes = 0;
    2565             :     }
    2566          13 :     if (s->flags & WV_MONO_DATA) {
    2567       44101 :         for (i = 0; i < nb_samples; i++)
    2568       44100 :             crc += (crc << 1) + samples_l[i];
    2569             : 
    2570           1 :         if (s->num_passes)
    2571           0 :             ret = wv_mono(s, samples_l, !s->num_terms, 1);
    2572             :     } else {
    2573      264612 :         for (i = 0; i < nb_samples; i++)
    2574      264600 :             crc += (crc << 3) + (samples_l[i] << 1) + samples_l[i] + samples_r[i];
    2575             : 
    2576          12 :         if (s->num_passes)
    2577          12 :             ret = wv_stereo(s, samples_l, samples_r, !s->num_terms, 1);
    2578             :     }
    2579          13 :     if (ret < 0)
    2580           0 :         return ret;
    2581             : 
    2582          13 :     if (!s->ch_offset)
    2583          13 :         s->flags |= WV_INITIAL_BLOCK;
    2584             : 
    2585          13 :     s->ch_offset += 1 + !(s->flags & WV_MONO);
    2586             : 
    2587          13 :     if (s->ch_offset == s->avctx->channels)
    2588          13 :         s->flags |= WV_FINAL_BLOCK;
    2589             : 
    2590          13 :     bytestream2_init_writer(&pb, out, out_size);
    2591          13 :     bytestream2_put_le32(&pb, MKTAG('w', 'v', 'p', 'k'));
    2592          13 :     bytestream2_put_le32(&pb, 0);
    2593          13 :     bytestream2_put_le16(&pb, 0x410);
    2594          13 :     bytestream2_put_le16(&pb, 0);
    2595          13 :     bytestream2_put_le32(&pb, 0);
    2596          13 :     bytestream2_put_le32(&pb, s->sample_index);
    2597          13 :     bytestream2_put_le32(&pb, nb_samples);
    2598          13 :     bytestream2_put_le32(&pb, s->flags);
    2599          13 :     bytestream2_put_le32(&pb, crc);
    2600             : 
    2601          26 :     if (s->flags & WV_INITIAL_BLOCK &&
    2602          25 :         s->avctx->channel_layout != AV_CH_LAYOUT_MONO &&
    2603          12 :         s->avctx->channel_layout != AV_CH_LAYOUT_STEREO) {
    2604           0 :         put_metadata_block(&pb, WP_ID_CHANINFO, 5);
    2605           0 :         bytestream2_put_byte(&pb, s->avctx->channels);
    2606           0 :         bytestream2_put_le32(&pb, s->avctx->channel_layout);
    2607           0 :         bytestream2_put_byte(&pb, 0);
    2608             :     }
    2609             : 
    2610          13 :     if ((s->flags & SRATE_MASK) == SRATE_MASK) {
    2611           0 :         put_metadata_block(&pb, WP_ID_SAMPLE_RATE, 3);
    2612           0 :         bytestream2_put_le24(&pb, s->avctx->sample_rate);
    2613           0 :         bytestream2_put_byte(&pb, 0);
    2614             :     }
    2615             : 
    2616          13 :     put_metadata_block(&pb, WP_ID_DECTERMS, s->num_terms);
    2617          75 :     for (i = 0; i < s->num_terms; i++) {
    2618          62 :         struct Decorr *dpp = &s->decorr_passes[i];
    2619          62 :         bytestream2_put_byte(&pb, ((dpp->value + 5) & 0x1f) | ((dpp->delta << 5) & 0xe0));
    2620             :     }
    2621          13 :     if (s->num_terms & 1)
    2622          12 :         bytestream2_put_byte(&pb, 0);
    2623             : 
    2624             : #define WRITE_DECWEIGHT(type) do {            \
    2625             :         temp = store_weight(type);    \
    2626             :         bytestream2_put_byte(&pb, temp);      \
    2627             :         type = restore_weight(temp);  \
    2628             :     } while (0)
    2629             : 
    2630          13 :     bytestream2_put_byte(&pb, WP_ID_DECWEIGHTS);
    2631          13 :     bytestream2_put_byte(&pb, 0);
    2632          13 :     start = bytestream2_tell_p(&pb);
    2633          13 :     for (i = s->num_terms - 1; i >= 0; --i) {
    2634          13 :         struct Decorr *dpp = &s->decorr_passes[i];
    2635             : 
    2636          18 :         if (store_weight(dpp->weightA) ||
    2637          10 :             (!(s->flags & WV_MONO_DATA) && store_weight(dpp->weightB)))
    2638             :                 break;
    2639             :     }
    2640          13 :     tcount = i + 1;
    2641          75 :     for (i = 0; i < s->num_terms; i++) {
    2642          62 :         struct Decorr *dpp = &s->decorr_passes[i];
    2643          62 :         if (i < tcount) {
    2644          62 :             WRITE_DECWEIGHT(dpp->weightA);
    2645          62 :             if (!(s->flags & WV_MONO_DATA))
    2646          60 :                 WRITE_DECWEIGHT(dpp->weightB);
    2647             :         } else {
    2648           0 :             dpp->weightA = dpp->weightB = 0;
    2649             :         }
    2650             :     }
    2651          13 :     end = bytestream2_tell_p(&pb);
    2652          13 :     out[start - 2] = WP_ID_DECWEIGHTS | (((end - start) & 1) ? WP_IDF_ODD: 0);
    2653          13 :     out[start - 1] = (end - start + 1) >> 1;
    2654          13 :     if ((end - start) & 1)
    2655           0 :         bytestream2_put_byte(&pb, 0);
    2656             : 
    2657             : #define WRITE_DECSAMPLE(type) do {        \
    2658             :         temp = log2s(type);               \
    2659             :         type = wp_exp2(temp);             \
    2660             :         bytestream2_put_le16(&pb, temp);  \
    2661             :     } while (0)
    2662             : 
    2663          13 :     bytestream2_put_byte(&pb, WP_ID_DECSAMPLES);
    2664          13 :     bytestream2_put_byte(&pb, 0);
    2665          13 :     start = bytestream2_tell_p(&pb);
    2666          75 :     for (i = 0; i < s->num_terms; i++) {
    2667          62 :         struct Decorr *dpp = &s->decorr_passes[i];
    2668          62 :         if (i == 0) {
    2669          13 :             if (dpp->value > MAX_TERM) {
    2670          13 :                 WRITE_DECSAMPLE(dpp->samplesA[0]);
    2671          13 :                 WRITE_DECSAMPLE(dpp->samplesA[1]);
    2672          13 :                 if (!(s->flags & WV_MONO_DATA)) {
    2673          12 :                     WRITE_DECSAMPLE(dpp->samplesB[0]);
    2674          12 :                     WRITE_DECSAMPLE(dpp->samplesB[1]);
    2675             :                 }
    2676           0 :             } else if (dpp->value < 0) {
    2677           0 :                 WRITE_DECSAMPLE(dpp->samplesA[0]);
    2678           0 :                 WRITE_DECSAMPLE(dpp->samplesB[0]);
    2679             :             } else {
    2680           0 :                 for (j = 0; j < dpp->value; j++) {
    2681           0 :                     WRITE_DECSAMPLE(dpp->samplesA[j]);
    2682           0 :                     if (!(s->flags & WV_MONO_DATA))
    2683           0 :                         WRITE_DECSAMPLE(dpp->samplesB[j]);
    2684             :                 }
    2685             :             }
    2686             :         } else {
    2687          49 :             CLEAR(dpp->samplesA);
    2688          49 :             CLEAR(dpp->samplesB);
    2689             :         }
    2690             :     }
    2691          13 :     end = bytestream2_tell_p(&pb);
    2692          13 :     out[start - 1] = (end - start) >> 1;
    2693             : 
    2694             : #define WRITE_CHAN_ENTROPY(chan) do {               \
    2695             :         for (i = 0; i < 3; i++) {                   \
    2696             :             temp = wp_log2(s->w.c[chan].median[i]); \
    2697             :             bytestream2_put_le16(&pb, temp);        \
    2698             :             s->w.c[chan].median[i] = wp_exp2(temp); \
    2699             :         }                                           \
    2700             :     } while (0)
    2701             : 
    2702          13 :     put_metadata_block(&pb, WP_ID_ENTROPY, 6 * (1 + (!(s->flags & WV_MONO_DATA))));
    2703          13 :     WRITE_CHAN_ENTROPY(0);
    2704          13 :     if (!(s->flags & WV_MONO_DATA))
    2705          12 :         WRITE_CHAN_ENTROPY(1);
    2706             : 
    2707          13 :     if (s->flags & WV_FLOAT_DATA) {
    2708           0 :         put_metadata_block(&pb, WP_ID_FLOATINFO, 4);
    2709           0 :         bytestream2_put_byte(&pb, s->float_flags);
    2710           0 :         bytestream2_put_byte(&pb, s->float_shift);
    2711           0 :         bytestream2_put_byte(&pb, s->float_max_exp);
    2712           0 :         bytestream2_put_byte(&pb, 127);
    2713             :     }
    2714             : 
    2715          13 :     if (s->flags & WV_INT32_DATA) {
    2716           0 :         put_metadata_block(&pb, WP_ID_INT32INFO, 4);
    2717           0 :         bytestream2_put_byte(&pb, s->int32_sent_bits);
    2718           0 :         bytestream2_put_byte(&pb, s->int32_zeros);
    2719           0 :         bytestream2_put_byte(&pb, s->int32_ones);
    2720           0 :         bytestream2_put_byte(&pb, s->int32_dups);
    2721             :     }
    2722             : 
    2723          13 :     if (s->flags & WV_MONO_DATA && !s->num_passes) {
    2724       44101 :         for (i = 0; i < nb_samples; i++) {
    2725       44100 :             int32_t code = samples_l[i];
    2726             : 
    2727      132300 :             for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++) {
    2728             :                 int32_t sam;
    2729             : 
    2730       88200 :                 if (dpp->value > MAX_TERM) {
    2731       88200 :                     if (dpp->value & 1)
    2732       44100 :                         sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
    2733             :                     else
    2734       44100 :                         sam = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
    2735             : 
    2736       88200 :                     dpp->samplesA[1] = dpp->samplesA[0];
    2737       88200 :                     dpp->samplesA[0] = code;
    2738             :                 } else {
    2739           0 :                     sam = dpp->samplesA[m];
    2740           0 :                     dpp->samplesA[(m + dpp->value) & (MAX_TERM - 1)] = code;
    2741             :                 }
    2742             : 
    2743       88200 :                 code -= APPLY_WEIGHT(dpp->weightA, sam);
    2744       88200 :                 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, code);
    2745             :             }
    2746             : 
    2747       44100 :             m = (m + 1) & (MAX_TERM - 1);
    2748       44100 :             samples_l[i] = code;
    2749             :         }
    2750           2 :         if (m) {
    2751           3 :             for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++)
    2752           2 :                 if (dpp->value > 0 && dpp->value <= MAX_TERM) {
    2753             :                 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
    2754             :                 int k;
    2755             : 
    2756           0 :                 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
    2757           0 :                 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
    2758             : 
    2759           0 :                 for (k = 0; k < MAX_TERM; k++) {
    2760           0 :                     dpp->samplesA[k] = temp_A[m];
    2761           0 :                     dpp->samplesB[k] = temp_B[m];
    2762           0 :                     m = (m + 1) & (MAX_TERM - 1);
    2763             :                 }
    2764             :             }
    2765             :         }
    2766          12 :     } else if (!s->num_passes) {
    2767           0 :         if (s->flags & WV_JOINT_STEREO) {
    2768           0 :             for (i = 0; i < nb_samples; i++)
    2769           0 :                 samples_r[i] += ((samples_l[i] -= samples_r[i]) >> 1);
    2770             :         }
    2771             : 
    2772           0 :         for (i = 0; i < s->num_terms; i++) {
    2773           0 :             struct Decorr *dpp = &s->decorr_passes[i];
    2774           0 :             if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16 || dpp->delta != 2)
    2775           0 :                 decorr_stereo_pass2(dpp, samples_l, samples_r, nb_samples);
    2776             :             else
    2777           0 :                 decorr_stereo_pass_id2(dpp, samples_l, samples_r, nb_samples);
    2778             :         }
    2779             :     }
    2780             : 
    2781          13 :     bytestream2_put_byte(&pb, WP_ID_DATA | WP_IDF_LONG);
    2782          13 :     init_put_bits(&s->pb, pb.buffer + 3, bytestream2_get_bytes_left_p(&pb));
    2783          13 :     if (s->flags & WV_MONO_DATA) {
    2784       44101 :         for (i = 0; i < nb_samples; i++)
    2785       44100 :             wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
    2786             :     } else {
    2787      264612 :         for (i = 0; i < nb_samples; i++) {
    2788      264600 :             wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
    2789      264600 :             wavpack_encode_sample(s, &s->w.c[1], s->samples[1][i]);
    2790             :         }
    2791             :     }
    2792          13 :     encode_flush(s);
    2793          13 :     flush_put_bits(&s->pb);
    2794          13 :     data_size = put_bits_count(&s->pb) >> 3;
    2795          13 :     bytestream2_put_le24(&pb, (data_size + 1) >> 1);
    2796          13 :     bytestream2_skip_p(&pb, data_size);
    2797          13 :     if (data_size & 1)
    2798           7 :         bytestream2_put_byte(&pb, 0);
    2799             : 
    2800          13 :     if (got_extra) {
    2801           0 :         bytestream2_put_byte(&pb, WP_ID_EXTRABITS | WP_IDF_LONG);
    2802           0 :         init_put_bits(&s->pb, pb.buffer + 7, bytestream2_get_bytes_left_p(&pb));
    2803           0 :         if (s->flags & WV_FLOAT_DATA)
    2804           0 :             pack_float(s, s->orig_l, s->orig_r, nb_samples);
    2805             :         else
    2806           0 :             pack_int32(s, s->orig_l, s->orig_r, nb_samples);
    2807           0 :         flush_put_bits(&s->pb);
    2808           0 :         data_size = put_bits_count(&s->pb) >> 3;
    2809           0 :         bytestream2_put_le24(&pb, (data_size + 5) >> 1);
    2810           0 :         bytestream2_put_le32(&pb, s->crc_x);
    2811           0 :         bytestream2_skip_p(&pb, data_size);
    2812           0 :         if (data_size & 1)
    2813           0 :             bytestream2_put_byte(&pb, 0);
    2814             :     }
    2815             : 
    2816          13 :     block_size = bytestream2_tell_p(&pb);
    2817          13 :     AV_WL32(out + 4, block_size - 8);
    2818             : 
    2819          13 :     av_assert0(!bytestream2_get_eof(&pb));
    2820             : 
    2821          13 :     return block_size;
    2822             : }
    2823             : 
    2824          25 : static void fill_buffer(WavPackEncodeContext *s,
    2825             :                         const int8_t *src, int32_t *dst,
    2826             :                         int nb_samples)
    2827             : {
    2828             :     int i;
    2829             : 
    2830             : #define COPY_SAMPLES(type, offset, shift) do {            \
    2831             :         const type *sptr = (const type *)src;             \
    2832             :         for (i = 0; i < nb_samples; i++)                  \
    2833             :             dst[i] = (sptr[i] - offset) >> shift;         \
    2834             :     } while (0)
    2835             : 
    2836          25 :     switch (s->avctx->sample_fmt) {
    2837           0 :     case AV_SAMPLE_FMT_U8P:
    2838           0 :         COPY_SAMPLES(int8_t, 0x80, 0);
    2839           0 :         break;
    2840          25 :     case AV_SAMPLE_FMT_S16P:
    2841          25 :         COPY_SAMPLES(int16_t, 0, 0);
    2842          25 :         break;
    2843           0 :     case AV_SAMPLE_FMT_S32P:
    2844           0 :         if (s->avctx->bits_per_raw_sample <= 24) {
    2845           0 :             COPY_SAMPLES(int32_t, 0, 8);
    2846           0 :             break;
    2847             :         }
    2848             :     case AV_SAMPLE_FMT_FLTP:
    2849           0 :         memcpy(dst, src, nb_samples * 4);
    2850             :     }
    2851          25 : }
    2852             : 
    2853          13 : static void set_samplerate(WavPackEncodeContext *s)
    2854             : {
    2855             :     int i;
    2856             : 
    2857         130 :     for (i = 0; i < 15; i++) {
    2858         130 :         if (wv_rates[i] == s->avctx->sample_rate)
    2859          13 :             break;
    2860             :     }
    2861             : 
    2862          13 :     s->flags = i << SRATE_LSB;
    2863          13 : }
    2864             : 
    2865          13 : static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
    2866             :                                 const AVFrame *frame, int *got_packet_ptr)
    2867             : {
    2868          13 :     WavPackEncodeContext *s = avctx->priv_data;
    2869             :     int buf_size, ret;
    2870             :     uint8_t *buf;
    2871             : 
    2872          13 :     s->block_samples = frame->nb_samples;
    2873          13 :     av_fast_padded_malloc(&s->samples[0], &s->samples_size[0],
    2874          13 :                           sizeof(int32_t) * s->block_samples);
    2875          13 :     if (!s->samples[0])
    2876           0 :         return AVERROR(ENOMEM);
    2877          13 :     if (avctx->channels > 1) {
    2878          12 :         av_fast_padded_malloc(&s->samples[1], &s->samples_size[1],
    2879          12 :                               sizeof(int32_t) * s->block_samples);
    2880          12 :         if (!s->samples[1])
    2881           0 :             return AVERROR(ENOMEM);
    2882             :     }
    2883             : 
    2884          39 :     buf_size = s->block_samples * avctx->channels * 8
    2885          26 :              + 200 * avctx->channels /* for headers */;
    2886          13 :     if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
    2887           0 :         return ret;
    2888          13 :     buf = avpkt->data;
    2889             : 
    2890          39 :     for (s->ch_offset = 0; s->ch_offset < avctx->channels;) {
    2891          13 :         set_samplerate(s);
    2892             : 
    2893          13 :         switch (s->avctx->sample_fmt) {
    2894          13 :         case AV_SAMPLE_FMT_S16P: s->flags |= 1; break;
    2895           0 :         case AV_SAMPLE_FMT_S32P: s->flags |= 3 - (s->avctx->bits_per_raw_sample <= 24); break;
    2896           0 :         case AV_SAMPLE_FMT_FLTP: s->flags |= 3 | WV_FLOAT_DATA;
    2897             :         }
    2898             : 
    2899          13 :         fill_buffer(s, frame->extended_data[s->ch_offset], s->samples[0], s->block_samples);
    2900          13 :         if (avctx->channels - s->ch_offset == 1) {
    2901           1 :             s->flags |= WV_MONO;
    2902             :         } else {
    2903          12 :             s->flags |= WV_CROSS_DECORR;
    2904          12 :             fill_buffer(s, frame->extended_data[s->ch_offset + 1], s->samples[1], s->block_samples);
    2905             :         }
    2906             : 
    2907          13 :         s->flags += (1 << MAG_LSB) * ((s->flags & 3) * 8 + 7);
    2908             : 
    2909          13 :         if ((ret = wavpack_encode_block(s, s->samples[0], s->samples[1],
    2910             :                                         buf, buf_size)) < 0)
    2911           0 :             return ret;
    2912             : 
    2913          13 :         buf      += ret;
    2914          13 :         buf_size -= ret;
    2915             :     }
    2916          13 :     s->sample_index += frame->nb_samples;
    2917             : 
    2918          13 :     avpkt->pts      = frame->pts;
    2919          13 :     avpkt->size     = buf - avpkt->data;
    2920          13 :     avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
    2921          13 :     *got_packet_ptr = 1;
    2922          13 :     return 0;
    2923             : }
    2924             : 
    2925           2 : static av_cold int wavpack_encode_close(AVCodecContext *avctx)
    2926             : {
    2927           2 :     WavPackEncodeContext *s = avctx->priv_data;
    2928             :     int i;
    2929             : 
    2930          38 :     for (i = 0; i < MAX_TERMS + 2; i++) {
    2931          36 :         av_freep(&s->sampleptrs[i][0]);
    2932          36 :         av_freep(&s->sampleptrs[i][1]);
    2933          36 :         s->sampleptrs_size[i][0] = s->sampleptrs_size[i][1] = 0;
    2934             :     }
    2935             : 
    2936           6 :     for (i = 0; i < 2; i++) {
    2937           4 :         av_freep(&s->samples[i]);
    2938           4 :         s->samples_size[i] = 0;
    2939             : 
    2940           4 :         av_freep(&s->best_buffer[i]);
    2941           4 :         s->best_buffer_size[i] = 0;
    2942             : 
    2943           4 :         av_freep(&s->temp_buffer[i][0]);
    2944           4 :         av_freep(&s->temp_buffer[i][1]);
    2945           4 :         s->temp_buffer_size[i][0] = s->temp_buffer_size[i][1] = 0;
    2946             :     }
    2947             : 
    2948           2 :     av_freep(&s->js_left);
    2949           2 :     av_freep(&s->js_right);
    2950           2 :     s->js_left_size = s->js_right_size = 0;
    2951             : 
    2952           2 :     av_freep(&s->orig_l);
    2953           2 :     av_freep(&s->orig_r);
    2954           2 :     s->orig_l_size = s->orig_r_size = 0;
    2955             : 
    2956           2 :     return 0;
    2957             : }
    2958             : 
    2959             : #define OFFSET(x) offsetof(WavPackEncodeContext, x)
    2960             : #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
    2961             : static const AVOption options[] = {
    2962             :     { "joint_stereo",  "", OFFSET(joint), AV_OPT_TYPE_BOOL, {.i64=-1}, -1, 1, FLAGS },
    2963             :     { "optimize_mono", "", OFFSET(optimize_mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
    2964             :     { NULL },
    2965             : };
    2966             : 
    2967             : static const AVClass wavpack_encoder_class = {
    2968             :     .class_name = "WavPack encoder",
    2969             :     .item_name  = av_default_item_name,
    2970             :     .option     = options,
    2971             :     .version    = LIBAVUTIL_VERSION_INT,
    2972             : };
    2973             : 
    2974             : AVCodec ff_wavpack_encoder = {
    2975             :     .name           = "wavpack",
    2976             :     .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
    2977             :     .type           = AVMEDIA_TYPE_AUDIO,
    2978             :     .id             = AV_CODEC_ID_WAVPACK,
    2979             :     .priv_data_size = sizeof(WavPackEncodeContext),
    2980             :     .priv_class     = &wavpack_encoder_class,
    2981             :     .init           = wavpack_encode_init,
    2982             :     .encode2        = wavpack_encode_frame,
    2983             :     .close          = wavpack_encode_close,
    2984             :     .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME,
    2985             :     .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8P,
    2986             :                                                      AV_SAMPLE_FMT_S16P,
    2987             :                                                      AV_SAMPLE_FMT_S32P,
    2988             :                                                      AV_SAMPLE_FMT_FLTP,
    2989             :                                                      AV_SAMPLE_FMT_NONE },
    2990             : };

Generated by: LCOV version 1.13