GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/wavpackenc.c Lines: 791 1746 45.3 %
Date: 2020-08-14 10:39:37 Branches: 551 1441 38.2 %

Line Branch Exec Source
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
        av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d\n", avctx->channels);
133
        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
            block_samples = avctx->sample_rate;
142
143
2
        while (block_samples * avctx->channels > WV_MAX_SAMPLES)
144
            block_samples /= 2;
145
146
3
        while (block_samples * avctx->channels < 40000)
147
1
            block_samples *= 2;
148
2
        avctx->frame_size = block_samples;
149
    } else if (avctx->frame_size && (avctx->frame_size < 128 ||
150
                              avctx->frame_size > WV_MAX_SAMPLES)) {
151
        av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n", avctx->frame_size);
152
        return AVERROR(EINVAL);
153
    }
154
155
2
    if (avctx->compression_level != FF_COMPRESSION_DEFAULT) {
156
1
        if (avctx->compression_level >= 3) {
157
            s->decorr_filter = 3;
158
            s->num_passes = 9;
159
            if      (avctx->compression_level >= 8) {
160
                s->num_branches = 4;
161
                s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_SORT_LAST|EXTRA_BRANCHES;
162
            } else if (avctx->compression_level >= 7) {
163
                s->num_branches = 3;
164
                s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
165
            } else if (avctx->compression_level >= 6) {
166
                s->num_branches = 2;
167
                s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
168
            } else if (avctx->compression_level >= 5) {
169
                s->num_branches = 1;
170
                s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
171
            } else if (avctx->compression_level >= 4) {
172
                s->num_branches = 1;
173
                s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_BRANCHES;
174
            }
175
1
        } else if (avctx->compression_level == 2) {
176
            s->decorr_filter = 2;
177
            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
        } else if (avctx->compression_level < 1) {
182
            s->decorr_filter = 0;
183
            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
static void shift_mono(int32_t *samples, int nb_samples, int shift)
196
{
197
    int i;
198
    for (i = 0; i < nb_samples; i++)
199
        samples[i] >>= shift;
200
}
201
202
static void shift_stereo(int32_t *left, int32_t *right,
203
                         int nb_samples, int shift)
204
{
205
    int i;
206
    for (i = 0; i < nb_samples; i++) {
207
        left [i] >>= shift;
208
        right[i] >>= shift;
209
    }
210
}
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
static void process_float(WavPackEncodeContext *s, int32_t *sample)
224
{
225
    int32_t shift_count, value, f = *sample;
226
227
    if (get_exponent(f) == 255) {
228
        s->float_flags |= FLOAT_EXCEPTIONS;
229
        value = 0x1000000;
230
        shift_count = 0;
231
    } else if (get_exponent(f)) {
232
        shift_count = s->max_exp - get_exponent(f);
233
        value = 0x800000 + get_mantissa(f);
234
    } else {
235
        shift_count = s->max_exp ? s->max_exp - 1 : 0;
236
        value = get_mantissa(f);
237
    }
238
239
    if (shift_count < 25)
240
        value >>= shift_count;
241
    else
242
        value = 0;
243
244
    if (!value) {
245
        if (get_exponent(f) || get_mantissa(f))
246
            s->false_zeros++;
247
        else if (get_sign(f))
248
            s->neg_zeros++;
249
    } else if (shift_count) {
250
        int32_t mask = (1 << shift_count) - 1;
251
252
        if (!(get_mantissa(f) & mask))
253
            s->shifted_zeros++;
254
        else if ((get_mantissa(f) & mask) == mask)
255
            s->shifted_ones++;
256
        else
257
            s->shifted_both++;
258
    }
259
260
    s->ordata |= value;
261
    *sample = get_sign(f) ? -value : value;
262
}
263
264
static int scan_float(WavPackEncodeContext *s,
265
                      int32_t *samples_l, int32_t *samples_r,
266
                      int nb_samples)
267
{
268
    uint32_t crc = 0xffffffffu;
269
    int i;
270
271
    s->shifted_ones = s->shifted_zeros = s->shifted_both = s->ordata = 0;
272
    s->float_shift = s->float_flags = 0;
273
    s->false_zeros = s->neg_zeros = 0;
274
    s->max_exp = 0;
275
276
    if (s->flags & WV_MONO_DATA) {
277
        for (i = 0; i < nb_samples; i++) {
278
            int32_t f = samples_l[i];
279
            crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
280
281
            if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
282
                s->max_exp = get_exponent(f);
283
        }
284
    } else {
285
        for (i = 0; i < nb_samples; i++) {
286
            int32_t f;
287
288
            f = samples_l[i];
289
            crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
290
            if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
291
                s->max_exp = get_exponent(f);
292
293
            f = samples_r[i];
294
            crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
295
296
            if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
297
                s->max_exp = get_exponent(f);
298
        }
299
    }
300
301
    s->crc_x = crc;
302
303
    if (s->flags & WV_MONO_DATA) {
304
        for (i = 0; i < nb_samples; i++)
305
            process_float(s, &samples_l[i]);
306
    } else {
307
        for (i = 0; i < nb_samples; i++) {
308
            process_float(s, &samples_l[i]);
309
            process_float(s, &samples_r[i]);
310
        }
311
    }
312
313
    s->float_max_exp = s->max_exp;
314
315
    if (s->shifted_both)
316
        s->float_flags |= FLOAT_SHIFT_SENT;
317
    else if (s->shifted_ones && !s->shifted_zeros)
318
        s->float_flags |= FLOAT_SHIFT_ONES;
319
    else if (s->shifted_ones && s->shifted_zeros)
320
        s->float_flags |= FLOAT_SHIFT_SAME;
321
    else if (s->ordata && !(s->ordata & 1)) {
322
        do {
323
            s->float_shift++;
324
            s->ordata >>= 1;
325
        } while (!(s->ordata & 1));
326
327
        if (s->flags & WV_MONO_DATA)
328
            shift_mono(samples_l, nb_samples, s->float_shift);
329
        else
330
            shift_stereo(samples_l, samples_r, nb_samples, s->float_shift);
331
    }
332
333
    s->flags &= ~MAG_MASK;
334
335
    while (s->ordata) {
336
        s->flags += 1 << MAG_LSB;
337
        s->ordata >>= 1;
338
    }
339
340
    if (s->false_zeros || s->neg_zeros)
341
        s->float_flags |= FLOAT_ZEROS_SENT;
342
343
    if (s->neg_zeros)
344
        s->float_flags |= FLOAT_NEG_ZEROS;
345
346
    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
    s->flags &= ~MAG_MASK;
389
390
    while (magdata) {
391
        s->flags += 1 << MAG_LSB;
392
        magdata >>= 1;
393
    }
394
395
    if (!(s->flags & MAG_MASK))
396
        return;
397
398
    if (!(ordata & 1)) {
399
        do {
400
            s->flags -= 1 << MAG_LSB;
401
            s->int32_zeros++;
402
            total_shift++;
403
            ordata >>= 1;
404
        } while (!(ordata & 1));
405
    } else if (anddata & 1) {
406
        do {
407
            s->flags -= 1 << MAG_LSB;
408
            s->int32_ones++;
409
            total_shift++;
410
            anddata >>= 1;
411
        } while (anddata & 1);
412
    } else if (!(xordata & 2)) {
413
        do {
414
            s->flags -= 1 << MAG_LSB;
415
            s->int32_dups++;
416
            total_shift++;
417
            xordata >>= 1;
418
        } while (!(xordata & 2));
419
    }
420
421
    if (total_shift) {
422
        s->flags |= WV_INT32_DATA;
423
424
        if (s->flags & WV_MONO_DATA)
425
            shift_mono(samples_l, nb_samples, total_shift);
426
        else
427
            shift_stereo(samples_l, samples_r, nb_samples, total_shift);
428
    }
429
}
430
431
static int scan_int32(WavPackEncodeContext *s,
432
                      int32_t *samples_l, int32_t *samples_r,
433
                      int nb_samples)
434
{
435
    uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
436
    uint32_t crc = 0xffffffffu;
437
    int i, total_shift = 0;
438
439
    s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
440
441
    if (s->flags & WV_MONO_DATA) {
442
        for (i = 0; i < nb_samples; i++) {
443
            int32_t M = samples_l[i];
444
445
            crc = crc * 9 + (M & 0xffff) * 3 + ((M >> 16) & 0xffff);
446
            magdata |= (M < 0) ? ~M : M;
447
            xordata |= M ^ -(M & 1);
448
            anddata &= M;
449
            ordata  |= M;
450
        }
451
    } else {
452
        for (i = 0; i < nb_samples; i++) {
453
            int32_t L = samples_l[i];
454
            int32_t R = samples_r[i];
455
456
            crc = crc * 9 + (L & 0xffff) * 3 + ((L >> 16) & 0xffff);
457
            crc = crc * 9 + (R & 0xffff) * 3 + ((R >> 16) & 0xffff);
458
            magdata |= (L < 0) ? ~L : L;
459
            magdata |= (R < 0) ? ~R : R;
460
            xordata |= L ^ -(L & 1);
461
            xordata |= R ^ -(R & 1);
462
            anddata &= L & R;
463
            ordata  |= L | R;
464
        }
465
    }
466
467
    s->crc_x = crc;
468
    s->flags &= ~MAG_MASK;
469
470
    while (magdata) {
471
        s->flags += 1 << MAG_LSB;
472
        magdata >>= 1;
473
    }
474
475
    if (!((s->flags & MAG_MASK) >> MAG_LSB)) {
476
        s->flags &= ~WV_INT32_DATA;
477
        return 0;
478
    }
479
480
    if (!(ordata & 1))
481
        do {
482
            s->flags -= 1 << MAG_LSB;
483
            s->int32_zeros++;
484
            total_shift++;
485
            ordata >>= 1;
486
        } while (!(ordata & 1));
487
    else if (anddata & 1)
488
        do {
489
            s->flags -= 1 << MAG_LSB;
490
            s->int32_ones++;
491
            total_shift++;
492
            anddata >>= 1;
493
        } while (anddata & 1);
494
    else if (!(xordata & 2))
495
        do {
496
            s->flags -= 1 << MAG_LSB;
497
            s->int32_dups++;
498
            total_shift++;
499
            xordata >>= 1;
500
        } while (!(xordata & 2));
501
502
    if (((s->flags & MAG_MASK) >> MAG_LSB) > 23) {
503
        s->int32_sent_bits = (uint8_t)(((s->flags & MAG_MASK) >> MAG_LSB) - 23);
504
        total_shift += s->int32_sent_bits;
505
        s->flags &= ~MAG_MASK;
506
        s->flags += 23 << MAG_LSB;
507
    }
508
509
    if (total_shift) {
510
        s->flags |= WV_INT32_DATA;
511
512
        if (s->flags & WV_MONO_DATA)
513
            shift_mono(samples_l, nb_samples, total_shift);
514
        else
515
            shift_stereo(samples_l, samples_r, nb_samples, total_shift);
516
    }
517
518
    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
606
    int result = 8 * weight;
533
534
606
    if (result > 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
92300
        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
    } else if (dpp->value > 0) {
579
        while (nb_samples--) {
580
            int k = (m + dpp->value) & (MAX_TERM - 1);
581
            int32_t left, sam_A;
582
583
            sam_A = dpp->samplesA[m];
584
            dpp->samplesA[k] = left = in_samples[0];
585
            m = (m + 1) & (MAX_TERM - 1);
586
587
            left -= APPLY_WEIGHT(dpp->weightA, sam_A);
588
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
589
            dpp->sumA += dpp->weightA;
590
            out_samples[0] = left;
591
            in_samples += dir;
592
            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
        memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
600
601
        for (i = 0; i < MAX_TERM; i++) {
602
            dpp->samplesA[i] = temp_A[m];
603
            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
            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
            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
    } else if (dpp->value > 1) {
628
        int i, j, k;
629
630
        for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
631
            i &= (MAX_TERM - 1);
632
            j &= (MAX_TERM - 1);
633
            dpp->samplesA[i] ^= dpp->samplesA[j];
634
            dpp->samplesA[j] ^= dpp->samplesA[i];
635
            dpp->samplesA[i] ^= dpp->samplesA[j];
636
        }
637
    }
638
1
}
639
640
#define count_bits(av) ((av) ? 32 - ff_clz(av) : 0)
641
642
1102500
static uint32_t log2sample(uint32_t v, int limit, uint32_t *result)
643
{
644
1102500
    uint32_t dbits = count_bits(v);
645
646
1102500
    if ((v += v >> 9) < (1 << 8)) {
647
959899
        *result += (dbits << 8) + wp_log2_table[(v << (9 - dbits)) & 0xff];
648
    } else {
649
142601
        *result += dbits = (dbits << 8) + wp_log2_table[(v >> (dbits - 9)) & 0xff];
650
651

142601
        if (limit && dbits >= limit)
652
            return 1;
653
    }
654
655
1102500
    return 0;
656
}
657
658
1
static uint32_t log2mono(int32_t *samples, int nb_samples, int limit)
659
{
660
1
    uint32_t result = 0;
661
44101
    while (nb_samples--) {
662
44100
        if (log2sample(abs(*samples++), limit, &result))
663
            return UINT32_MAX;
664
    }
665
1
    return result;
666
}
667
668
24
static uint32_t log2stereo(int32_t *samples_l, int32_t *samples_r,
669
                           int nb_samples, int limit)
670
{
671
24
    uint32_t result = 0;
672
529224
    while (nb_samples--) {
673

1058400
        if (log2sample(abs(*samples_l++), limit, &result) ||
674
529200
            log2sample(abs(*samples_r++), limit, &result))
675
            return UINT32_MAX;
676
    }
677
24
    return result;
678
}
679
680
static void decorr_mono_buffer(int32_t *samples, int32_t *outsamples,
681
                               int nb_samples, struct Decorr *dpp,
682
                               int tindex)
683
{
684
    struct Decorr dp, *dppi = dpp + tindex;
685
    int delta = dppi->delta, pre_delta, term = dppi->value;
686
687
    if (delta == 7)
688
        pre_delta = 7;
689
    else if (delta < 2)
690
        pre_delta = 3;
691
    else
692
        pre_delta = delta + 1;
693
694
    CLEAR(dp);
695
    dp.value = term;
696
    dp.delta = pre_delta;
697
    decorr_mono(samples, outsamples, FFMIN(2048, nb_samples), &dp, -1);
698
    dp.delta = delta;
699
700
    if (tindex == 0)
701
        reverse_mono_decorr(&dp);
702
    else
703
        CLEAR(dp.samplesA);
704
705
    memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
706
    dppi->weightA = dp.weightA;
707
708
    if (delta == 0) {
709
        dp.delta = 1;
710
        decorr_mono(samples, outsamples, nb_samples, &dp, 1);
711
        dp.delta = 0;
712
        memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
713
        dppi->weightA = dp.weightA = dp.sumA / nb_samples;
714
    }
715
716
    decorr_mono(samples, outsamples, nb_samples, &dp, 1);
717
}
718
719
static void recurse_mono(WavPackEncodeContext *s, WavPackExtraInfo *info,
720
                         int depth, int delta, uint32_t input_bits)
721
{
722
    int term, branches = s->num_branches - depth;
723
    int32_t *samples, *outsamples;
724
    uint32_t term_bits[22], bits;
725
726
    if (branches < 1 || depth + 1 == info->nterms)
727
        branches = 1;
728
729
    CLEAR(term_bits);
730
    samples = s->sampleptrs[depth][0];
731
    outsamples = s->sampleptrs[depth + 1][0];
732
733
    for (term = 1; term <= 18; term++) {
734
        if (term == 17 && branches == 1 && depth + 1 < info->nterms)
735
            continue;
736
737
        if (term > 8 && term < 17)
738
            continue;
739
740
        if (!s->extra_flags && (term > 4 && term < 17))
741
            continue;
742
743
        info->dps[depth].value = term;
744
        info->dps[depth].delta = delta;
745
        decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
746
        bits = log2mono(outsamples, s->block_samples, info->log_limit);
747
748
        if (bits < info->best_bits) {
749
            info->best_bits = bits;
750
            CLEAR(s->decorr_passes);
751
            memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
752
            memcpy(s->sampleptrs[info->nterms + 1][0],
753
                   s->sampleptrs[depth + 1][0], s->block_samples * 4);
754
        }
755
756
        term_bits[term + 3] = bits;
757
    }
758
759
    while (depth + 1 < info->nterms && branches--) {
760
        uint32_t local_best_bits = input_bits;
761
        int best_term = 0, i;
762
763
        for (i = 0; i < 22; i++)
764
            if (term_bits[i] && term_bits[i] < local_best_bits) {
765
                local_best_bits = term_bits[i];
766
                best_term = i - 3;
767
            }
768
769
        if (!best_term)
770
            break;
771
772
        term_bits[best_term + 3] = 0;
773
774
        info->dps[depth].value = best_term;
775
        info->dps[depth].delta = delta;
776
        decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
777
778
        recurse_mono(s, info, depth + 1, delta, local_best_bits);
779
    }
780
}
781
782
static void sort_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
783
{
784
    int reversed = 1;
785
    uint32_t bits;
786
787
    while (reversed) {
788
        int ri, i;
789
790
        memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
791
        reversed = 0;
792
793
        for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
794
795
            if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
796
                break;
797
798
            if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
799
                decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
800
                                   s->block_samples, info->dps, ri);
801
                continue;
802
            }
803
804
            info->dps[ri  ] = s->decorr_passes[ri+1];
805
            info->dps[ri+1] = s->decorr_passes[ri  ];
806
807
            for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
808
                decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
809
                                   s->block_samples, info->dps, i);
810
811
            bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
812
            if (bits < info->best_bits) {
813
                reversed = 1;
814
                info->best_bits = bits;
815
                CLEAR(s->decorr_passes);
816
                memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
817
                memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
818
                       s->block_samples * 4);
819
            } else {
820
                info->dps[ri  ] = s->decorr_passes[ri];
821
                info->dps[ri+1] = s->decorr_passes[ri+1];
822
                decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
823
                                   s->block_samples, info->dps, ri);
824
            }
825
        }
826
    }
827
}
828
829
static void delta_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
830
{
831
    int lower = 0, delta, d;
832
    uint32_t bits;
833
834
    if (!s->decorr_passes[0].value)
835
        return;
836
    delta = s->decorr_passes[0].delta;
837
838
    for (d = delta - 1; d >= 0; d--) {
839
        int i;
840
841
        for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
842
            info->dps[i].value = s->decorr_passes[i].value;
843
            info->dps[i].delta = d;
844
            decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
845
                               s->block_samples, info->dps, i);
846
        }
847
848
        bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
849
        if (bits >= info->best_bits)
850
            break;
851
852
        lower = 1;
853
        info->best_bits = bits;
854
        CLEAR(s->decorr_passes);
855
        memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
856
        memcpy(s->sampleptrs[info->nterms + 1][0],  s->sampleptrs[i][0],
857
               s->block_samples * 4);
858
    }
859
860
    for (d = delta + 1; !lower && d <= 7; d++) {
861
        int i;
862
863
        for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
864
            info->dps[i].value = s->decorr_passes[i].value;
865
            info->dps[i].delta = d;
866
            decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
867
                               s->block_samples, info->dps, i);
868
        }
869
870
        bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
871
        if (bits >= info->best_bits)
872
            break;
873
874
        info->best_bits = bits;
875
        CLEAR(s->decorr_passes);
876
        memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
877
        memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
878
               s->block_samples * 4);
879
    }
880
}
881
882
static int allocate_buffers2(WavPackEncodeContext *s, int nterms)
883
{
884
    int i;
885
886
    for (i = 0; i < nterms + 2; i++) {
887
        av_fast_padded_malloc(&s->sampleptrs[i][0], &s->sampleptrs_size[i][0],
888
                              s->block_samples * 4);
889
        if (!s->sampleptrs[i][0])
890
            return AVERROR(ENOMEM);
891
        if (!(s->flags & WV_MONO_DATA)) {
892
            av_fast_padded_malloc(&s->sampleptrs[i][1], &s->sampleptrs_size[i][1],
893
                                  s->block_samples * 4);
894
            if (!s->sampleptrs[i][1])
895
                return AVERROR(ENOMEM);
896
        }
897
    }
898
899
    return 0;
900
}
901
902
13
static int allocate_buffers(WavPackEncodeContext *s)
903
{
904
    int i;
905
906
39
    for (i = 0; i < 2; i++) {
907
26
        av_fast_padded_malloc(&s->best_buffer[0], &s->best_buffer_size[0],
908
26
                              s->block_samples * 4);
909
26
        if (!s->best_buffer[0])
910
            return AVERROR(ENOMEM);
911
912
26
        av_fast_padded_malloc(&s->temp_buffer[i][0], &s->temp_buffer_size[i][0],
913
26
                              s->block_samples * 4);
914
26
        if (!s->temp_buffer[i][0])
915
            return AVERROR(ENOMEM);
916
26
        if (!(s->flags & WV_MONO_DATA)) {
917
24
            av_fast_padded_malloc(&s->best_buffer[1], &s->best_buffer_size[1],
918
24
                                  s->block_samples * 4);
919
24
            if (!s->best_buffer[1])
920
                return AVERROR(ENOMEM);
921
922
24
            av_fast_padded_malloc(&s->temp_buffer[i][1], &s->temp_buffer_size[i][1],
923
24
                                  s->block_samples * 4);
924
24
            if (!s->temp_buffer[i][1])
925
                return AVERROR(ENOMEM);
926
        }
927
    }
928
929
13
    return 0;
930
}
931
932
static void analyze_mono(WavPackEncodeContext *s, int32_t *samples, int do_samples)
933
{
934
    WavPackExtraInfo info;
935
    int i;
936
937
    info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
938
    info.log_limit = FFMIN(6912, info.log_limit);
939
940
    info.nterms = s->num_terms;
941
942
    if (allocate_buffers2(s, s->num_terms))
943
        return;
944
945
    memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
946
    memcpy(s->sampleptrs[0][0], samples, s->block_samples * 4);
947
948
    for (i = 0; i < info.nterms && info.dps[i].value; i++)
949
        decorr_mono(s->sampleptrs[i][0], s->sampleptrs[i + 1][0],
950
                    s->block_samples, info.dps + i, 1);
951
952
    info.best_bits = log2mono(s->sampleptrs[info.nterms][0], s->block_samples, 0) * 1;
953
    memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
954
955
    if (s->extra_flags & EXTRA_BRANCHES)
956
        recurse_mono(s, &info, 0, (int) floor(s->delta_decay + 0.5),
957
                     log2mono(s->sampleptrs[0][0], s->block_samples, 0));
958
959
    if (s->extra_flags & EXTRA_SORT_FIRST)
960
        sort_mono(s, &info);
961
962
    if (s->extra_flags & EXTRA_TRY_DELTAS) {
963
        delta_mono(s, &info);
964
965
        if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
966
            s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
967
        else
968
            s->delta_decay = 2.0;
969
    }
970
971
    if (s->extra_flags & EXTRA_SORT_LAST)
972
        sort_mono(s, &info);
973
974
    if (do_samples)
975
        memcpy(samples, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
976
977
    for (i = 0; i < info.nterms; i++)
978
        if (!s->decorr_passes[i].value)
979
            break;
980
981
    s->num_terms = i;
982
}
983
984
3
static void scan_word(WavPackEncodeContext *s, WvChannel *c,
985
                      int32_t *samples, int nb_samples, int dir)
986
{
987
3
    if (dir < 0)
988
3
        samples += nb_samples - 1;
989
990
88203
    while (nb_samples--) {
991
88200
        uint32_t low, value = labs(samples[0]);
992
993
88200
        if (value < GET_MED(0)) {
994
68946
            DEC_MED(0);
995
        } else {
996
19254
            low = GET_MED(0);
997
19254
            INC_MED(0);
998
999
19254
            if (value - low < GET_MED(1)) {
1000
13482
                DEC_MED(1);
1001
            } else {
1002
5772
                low += GET_MED(1);
1003
5772
                INC_MED(1);
1004
1005
5772
                if (value - low < GET_MED(2)) {
1006
3975
                    DEC_MED(2);
1007
                } else {
1008
1797
                    INC_MED(2);
1009
                }
1010
            }
1011
        }
1012
88200
        samples += dir;
1013
    }
1014
3
}
1015
1016
1
static int wv_mono(WavPackEncodeContext *s, int32_t *samples,
1017
                   int no_history, int do_samples)
1018
{
1019
1
    struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1020
1
    int nb_samples = s->block_samples;
1021
1
    int buf_size = sizeof(int32_t) * nb_samples;
1022
1
    uint32_t best_size = UINT32_MAX, size;
1023
    int log_limit, pi, i, ret;
1024
1025
1
    for (i = 0; i < nb_samples; i++)
1026
1
        if (samples[i])
1027
1
            break;
1028
1029
1
    if (i == nb_samples) {
1030
        CLEAR(s->decorr_passes);
1031
        CLEAR(s->w);
1032
        s->num_terms = 0;
1033
        return 0;
1034
    }
1035
1036
1
    log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1037
1
    log_limit = FFMIN(6912, log_limit);
1038
1039
1
    if ((ret = allocate_buffers(s)) < 0)
1040
        return ret;
1041
1042

1
    if (no_history || s->num_passes >= 7)
1043
1
        s->best_decorr = s->mask_decorr = 0;
1044
1045
2
    for (pi = 0; pi < s->num_passes;) {
1046
        const WavPackDecorrSpec *wpds;
1047
        int nterms, c, j;
1048
1049
1
        if (!pi) {
1050
1
            c = s->best_decorr;
1051
        } else {
1052
            if (s->mask_decorr == 0)
1053
                c = 0;
1054
            else
1055
                c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1056
1057
            if (c == s->best_decorr) {
1058
                s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1059
                continue;
1060
            }
1061
        }
1062
1063
1
        wpds = &s->decorr_specs[c];
1064
1
        nterms = decorr_filter_nterms[s->decorr_filter];
1065
1066
        while (1) {
1067
1
        memcpy(s->temp_buffer[0][0], samples, buf_size);
1068
1
        CLEAR(save_decorr_passes);
1069
1070
3
        for (j = 0; j < nterms; j++) {
1071
2
            CLEAR(temp_decorr_pass);
1072
2
            temp_decorr_pass.delta = wpds->delta;
1073
2
            temp_decorr_pass.value = wpds->terms[j];
1074
1075
2
            if (temp_decorr_pass.value < 0)
1076
                temp_decorr_pass.value = 1;
1077
1078
2
            decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1079
                        FFMIN(nb_samples, 2048), &temp_decorr_pass, -1);
1080
1081
2
            if (j) {
1082
1
                CLEAR(temp_decorr_pass.samplesA);
1083
            } else {
1084
1
                reverse_mono_decorr(&temp_decorr_pass);
1085
            }
1086
1087
2
            memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1088
2
            decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1089
                        nb_samples, &temp_decorr_pass, 1);
1090
        }
1091
1092
1
        size = log2mono(s->temp_buffer[j&1][0], nb_samples, log_limit);
1093

1
        if (size != UINT32_MAX || !nterms)
1094
            break;
1095
        nterms >>= 1;
1096
        }
1097
1098
1
        if (size < best_size) {
1099
1
            memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1100
1
            memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1101
1
            s->num_terms = nterms;
1102
1
            s->best_decorr = c;
1103
1
            best_size = size;
1104
        }
1105
1106
1
        if (pi++)
1107
            s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1108
    }
1109
1110
1
    if (s->extra_flags)
1111
        analyze_mono(s, samples, do_samples);
1112
1
    else if (do_samples)
1113
        memcpy(samples, s->best_buffer[0], buf_size);
1114
1115

1
    if (no_history || s->extra_flags) {
1116
1
        CLEAR(s->w);
1117
1
        scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1118
    }
1119
1
    return 0;
1120
}
1121
1122
120
static void decorr_stereo(int32_t *in_left, int32_t *in_right,
1123
                          int32_t *out_left, int32_t *out_right,
1124
                          int nb_samples, struct Decorr *dpp, int dir)
1125
{
1126
120
    int m = 0, i;
1127
1128
120
    dpp->sumA = dpp->sumB = 0;
1129
1130
120
    if (dir < 0) {
1131
120
        out_left  += nb_samples - 1;
1132
120
        out_right += nb_samples - 1;
1133
120
        in_left   += nb_samples - 1;
1134
120
        in_right  += nb_samples - 1;
1135
    }
1136
1137
120
    dpp->weightA = restore_weight(store_weight(dpp->weightA));
1138
120
    dpp->weightB = restore_weight(store_weight(dpp->weightB));
1139
1140
1080
    for (i = 0; i < MAX_TERM; i++) {
1141
960
        dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1142
960
        dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1143
    }
1144
1145

120
    switch (dpp->value) {
1146
21
    case 2:
1147
43029
        while (nb_samples--) {
1148
            int32_t sam, tmp;
1149
1150
43008
            sam = dpp->samplesA[0];
1151
43008
            dpp->samplesA[0] = dpp->samplesA[1];
1152
43008
            out_left[0] = tmp = (dpp->samplesA[1] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1153

43008
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1154
43008
            dpp->sumA += dpp->weightA;
1155
1156
43008
            sam = dpp->samplesB[0];
1157
43008
            dpp->samplesB[0] = dpp->samplesB[1];
1158
43008
            out_right[0] = tmp = (dpp->samplesB[1] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1159

43008
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1160
43008
            dpp->sumB += dpp->weightB;
1161
1162
43008
            in_left   += dir;
1163
43008
            out_left  += dir;
1164
43008
            in_right  += dir;
1165
43008
            out_right += dir;
1166
        }
1167
21
        break;
1168
40
    case 17:
1169
81960
        while (nb_samples--) {
1170
            int32_t sam, tmp;
1171
1172
81920
            sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1173
81920
            dpp->samplesA[1] = dpp->samplesA[0];
1174
81920
            out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1175

81920
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1176
81920
            dpp->sumA += dpp->weightA;
1177
1178
81920
            sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1179
81920
            dpp->samplesB[1] = dpp->samplesB[0];
1180
81920
            out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT (dpp->weightB, sam);
1181

81920
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1182
81920
            dpp->sumB += dpp->weightB;
1183
1184
81920
            in_left   += dir;
1185
81920
            out_left  += dir;
1186
81920
            in_right  += dir;
1187
81920
            out_right += dir;
1188
        }
1189
40
        break;
1190
25
    case 18:
1191
51225
        while (nb_samples--) {
1192
            int32_t sam, tmp;
1193
1194
51200
            sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1195
51200
            dpp->samplesA[1] = dpp->samplesA[0];
1196
51200
            out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1197

51200
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1198
51200
            dpp->sumA += dpp->weightA;
1199
1200
51200
            sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1201
51200
            dpp->samplesB[1] = dpp->samplesB[0];
1202
51200
            out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1203

51200
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1204
51200
            dpp->sumB += dpp->weightB;
1205
1206
51200
            in_left   += dir;
1207
51200
            out_left  += dir;
1208
51200
            in_right  += dir;
1209
51200
            out_right += dir;
1210
        }
1211
25
        break;
1212
17
    default: {
1213
17
        int k = dpp->value & (MAX_TERM - 1);
1214
1215
34833
        while (nb_samples--) {
1216
            int32_t sam, tmp;
1217
1218
34816
            sam = dpp->samplesA[m];
1219
34816
            out_left[0] = tmp = (dpp->samplesA[k] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1220

34816
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1221
34816
            dpp->sumA += dpp->weightA;
1222
1223
34816
            sam = dpp->samplesB[m];
1224
34816
            out_right[0] = tmp = (dpp->samplesB[k] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1225

34816
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1226
34816
            dpp->sumB += dpp->weightB;
1227
1228
34816
            in_left   += dir;
1229
34816
            out_left  += dir;
1230
34816
            in_right  += dir;
1231
34816
            out_right += dir;
1232
34816
            m = (m + 1) & (MAX_TERM - 1);
1233
34816
            k = (k + 1) & (MAX_TERM - 1);
1234
        }
1235
1236
17
        if (m) {
1237
            int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1238
            int k;
1239
1240
            memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1241
            memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1242
1243
            for (k = 0; k < MAX_TERM; k++) {
1244
                dpp->samplesA[k] = temp_A[m];
1245
                dpp->samplesB[k] = temp_B[m];
1246
                m = (m + 1) & (MAX_TERM - 1);
1247
            }
1248
        }
1249
17
        break;
1250
        }
1251
3
    case -1:
1252
6147
        while (nb_samples--) {
1253
            int32_t sam_A, sam_B, tmp;
1254
1255
6144
            sam_A = dpp->samplesA[0];
1256
6144
            out_left[0] = tmp = (sam_B = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1257


6144
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1258
6144
            dpp->sumA += dpp->weightA;
1259
1260
6144
            out_right[0] = tmp = (dpp->samplesA[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1261


6144
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1262
6144
            dpp->sumB += dpp->weightB;
1263
1264
6144
            in_left   += dir;
1265
6144
            out_left  += dir;
1266
6144
            in_right  += dir;
1267
6144
            out_right += dir;
1268
        }
1269
3
        break;
1270
13
    case -2:
1271
26637
        while (nb_samples--) {
1272
            int32_t sam_A, sam_B, tmp;
1273
1274
26624
            sam_B = dpp->samplesB[0];
1275
26624
            out_right[0] = tmp = (sam_A = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1276


26624
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1277
26624
            dpp->sumB += dpp->weightB;
1278
1279
26624
            out_left[0] = tmp = (dpp->samplesB[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1280


26624
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1281
26624
            dpp->sumA += dpp->weightA;
1282
1283
26624
            in_left   += dir;
1284
26624
            out_left  += dir;
1285
26624
            in_right  += dir;
1286
26624
            out_right += dir;
1287
        }
1288
13
        break;
1289
1
    case -3:
1290
2049
        while (nb_samples--) {
1291
            int32_t sam_A, sam_B, tmp;
1292
1293
2048
            sam_A = dpp->samplesA[0];
1294
2048
            sam_B = dpp->samplesB[0];
1295
1296
2048
            dpp->samplesA[0] = tmp = in_right[0];
1297
2048
            out_right[0] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
1298


2048
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1299
2048
            dpp->sumB += dpp->weightB;
1300
1301
2048
            dpp->samplesB[0] = tmp = in_left[0];
1302
2048
            out_left[0] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
1303


2048
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1304
2048
            dpp->sumA += dpp->weightA;
1305
1306
2048
            in_left   += dir;
1307
2048
            out_left  += dir;
1308
2048
            in_right  += dir;
1309
2048
            out_right += dir;
1310
        }
1311
1
        break;
1312
    }
1313
120
}
1314
1315
24
static void reverse_decorr(struct Decorr *dpp)
1316
{
1317
24
    if (dpp->value > MAX_TERM) {
1318
        int32_t sam_A, sam_B;
1319
1320
23
        if (dpp->value & 1) {
1321
12
            sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1322
12
            sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1323
        } else {
1324
11
            sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1325
11
            sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1326
        }
1327
1328
23
        dpp->samplesA[1] = dpp->samplesA[0];
1329
23
        dpp->samplesB[1] = dpp->samplesB[0];
1330
23
        dpp->samplesA[0] = sam_A;
1331
23
        dpp->samplesB[0] = sam_B;
1332
1333
23
        if (dpp->value & 1) {
1334
12
            sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1335
12
            sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1336
        } else {
1337
11
            sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1338
11
            sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1339
        }
1340
1341
23
        dpp->samplesA[1] = sam_A;
1342
23
        dpp->samplesB[1] = sam_B;
1343
1
    } else if (dpp->value > 1) {
1344
        int i, j, k;
1345
1346
2
        for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
1347
1
            i &= (MAX_TERM - 1);
1348
1
            j &= (MAX_TERM - 1);
1349
1
            dpp->samplesA[i] ^= dpp->samplesA[j];
1350
1
            dpp->samplesA[j] ^= dpp->samplesA[i];
1351
1
            dpp->samplesA[i] ^= dpp->samplesA[j];
1352
1
            dpp->samplesB[i] ^= dpp->samplesB[j];
1353
1
            dpp->samplesB[j] ^= dpp->samplesB[i];
1354
1
            dpp->samplesB[i] ^= dpp->samplesB[j];
1355
        }
1356
    }
1357
24
}
1358
1359
120
static void decorr_stereo_quick(int32_t *in_left,  int32_t *in_right,
1360
                                int32_t *out_left, int32_t *out_right,
1361
                                int nb_samples, struct Decorr *dpp)
1362
{
1363
120
    int m = 0, i;
1364
1365
120
    dpp->weightA = restore_weight(store_weight(dpp->weightA));
1366
120
    dpp->weightB = restore_weight(store_weight(dpp->weightB));
1367
1368
1080
    for (i = 0; i < MAX_TERM; i++) {
1369
960
        dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1370
960
        dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1371
    }
1372
1373

120
    switch (dpp->value) {
1374
21
    case 2:
1375
463071
        for (i = 0; i < nb_samples; i++) {
1376
            int32_t sam, tmp;
1377
1378
463050
            sam = dpp->samplesA[0];
1379
463050
            dpp->samplesA[0] = dpp->samplesA[1];
1380
463050
            out_left[i] = tmp = (dpp->samplesA[1] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1381

463050
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1382
1383
463050
            sam = dpp->samplesB[0];
1384
463050
            dpp->samplesB[0] = dpp->samplesB[1];
1385
463050
            out_right[i] = tmp = (dpp->samplesB[1] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1386

463050
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1387
        }
1388
21
        break;
1389
40
    case 17:
1390
882040
        for (i = 0; i < nb_samples; i++) {
1391
            int32_t sam, tmp;
1392
1393
882000
            sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1394
882000
            dpp->samplesA[1] = dpp->samplesA[0];
1395
882000
            out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1396

882000
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1397
1398
882000
            sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1399
882000
            dpp->samplesB[1] = dpp->samplesB[0];
1400
882000
            out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1401

882000
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1402
        }
1403
40
        break;
1404
25
    case 18:
1405
551275
        for (i = 0; i < nb_samples; i++) {
1406
            int32_t sam, tmp;
1407
1408
551250
            sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1409
551250
            dpp->samplesA[1] = dpp->samplesA[0];
1410
551250
            out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1411

551250
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1412
1413
551250
            sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1414
551250
            dpp->samplesB[1] = dpp->samplesB[0];
1415
551250
            out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1416

551250
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1417
        }
1418
25
        break;
1419
17
    default: {
1420
17
        int k = dpp->value & (MAX_TERM - 1);
1421
1422
374867
        for (i = 0; i < nb_samples; i++) {
1423
            int32_t sam, tmp;
1424
1425
374850
            sam = dpp->samplesA[m];
1426
374850
            out_left[i] = tmp = (dpp->samplesA[k] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1427

374850
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1428
1429
374850
            sam = dpp->samplesB[m];
1430
374850
            out_right[i] = tmp = (dpp->samplesB[k] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1431

374850
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1432
1433
374850
            m = (m + 1) & (MAX_TERM - 1);
1434
374850
            k = (k + 1) & (MAX_TERM - 1);
1435
        }
1436
1437
17
        if (m) {
1438
            int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1439
            int k;
1440
1441
17
            memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1442
17
            memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1443
1444
153
            for (k = 0; k < MAX_TERM; k++) {
1445
136
                dpp->samplesA[k] = temp_A[m];
1446
136
                dpp->samplesB[k] = temp_B[m];
1447
136
                m = (m + 1) & (MAX_TERM - 1);
1448
            }
1449
        }
1450
17
        break;
1451
    }
1452
3
    case -1:
1453
66153
        for (i = 0; i < nb_samples; i++) {
1454
            int32_t sam_A, sam_B, tmp;
1455
1456
66150
            sam_A = dpp->samplesA[0];
1457
66150
            out_left[i] = tmp = (sam_B = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1458


66150
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1459
1460
66150
            out_right[i] = tmp = (dpp->samplesA[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1461


66150
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1462
        }
1463
3
        break;
1464
13
    case -2:
1465
286663
        for (i = 0; i < nb_samples; i++) {
1466
            int32_t sam_A, sam_B, tmp;
1467
1468
286650
            sam_B = dpp->samplesB[0];
1469
286650
            out_right[i] = tmp = (sam_A = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1470


286650
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1471
1472
286650
            out_left[i] = tmp = (dpp->samplesB[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1473


286650
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1474
        }
1475
13
        break;
1476
1
    case -3:
1477
22051
        for (i = 0; i < nb_samples; i++) {
1478
            int32_t sam_A, sam_B, tmp;
1479
1480
22050
            sam_A = dpp->samplesA[0];
1481
22050
            sam_B = dpp->samplesB[0];
1482
1483
22050
            dpp->samplesA[0] = tmp = in_right[i];
1484
22050
            out_right[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
1485


22050
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1486
1487
22050
            dpp->samplesB[0] = tmp = in_left[i];
1488
22050
            out_left[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
1489


22050
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1490
        }
1491
1
        break;
1492
    }
1493
120
}
1494
1495
static void decorr_stereo_buffer(WavPackExtraInfo *info,
1496
                                 int32_t *in_left,  int32_t *in_right,
1497
                                 int32_t *out_left, int32_t *out_right,
1498
                                 int nb_samples, int tindex)
1499
{
1500
    struct Decorr dp = {0}, *dppi = info->dps + tindex;
1501
    int delta = dppi->delta, pre_delta;
1502
    int term = dppi->value;
1503
1504
    if (delta == 7)
1505
        pre_delta = 7;
1506
    else if (delta < 2)
1507
        pre_delta = 3;
1508
    else
1509
        pre_delta = delta + 1;
1510
1511
    dp.value = term;
1512
    dp.delta = pre_delta;
1513
    decorr_stereo(in_left, in_right, out_left, out_right,
1514
                  FFMIN(2048, nb_samples), &dp, -1);
1515
    dp.delta = delta;
1516
1517
    if (tindex == 0) {
1518
        reverse_decorr(&dp);
1519
    } else {
1520
        CLEAR(dp.samplesA);
1521
        CLEAR(dp.samplesB);
1522
    }
1523
1524
    memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
1525
    memcpy(dppi->samplesB, dp.samplesB, sizeof(dp.samplesB));
1526
    dppi->weightA = dp.weightA;
1527
    dppi->weightB = dp.weightB;
1528
1529
    if (delta == 0) {
1530
        dp.delta = 1;
1531
        decorr_stereo(in_left, in_right, out_left, out_right, nb_samples, &dp, 1);
1532
        dp.delta = 0;
1533
        memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
1534
        memcpy(dp.samplesB, dppi->samplesB, sizeof(dp.samplesB));
1535
        dppi->weightA = dp.weightA = dp.sumA / nb_samples;
1536
        dppi->weightB = dp.weightB = dp.sumB / nb_samples;
1537
    }
1538
1539
    if (info->gt16bit)
1540
        decorr_stereo(in_left, in_right, out_left, out_right,
1541
                           nb_samples, &dp, 1);
1542
    else
1543
        decorr_stereo_quick(in_left, in_right, out_left, out_right,
1544
                            nb_samples, &dp);
1545
}
1546
1547
static void sort_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1548
{
1549
    int reversed = 1;
1550
    uint32_t bits;
1551
1552
    while (reversed) {
1553
        int ri, i;
1554
1555
        memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
1556
        reversed = 0;
1557
1558
        for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
1559
1560
            if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
1561
                break;
1562
1563
            if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
1564
                decorr_stereo_buffer(info,
1565
                                     s->sampleptrs[ri  ][0], s->sampleptrs[ri  ][1],
1566
                                     s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1567
                                     s->block_samples, ri);
1568
                continue;
1569
            }
1570
1571
            info->dps[ri  ] = s->decorr_passes[ri+1];
1572
            info->dps[ri+1] = s->decorr_passes[ri  ];
1573
1574
            for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
1575
                decorr_stereo_buffer(info,
1576
                                     s->sampleptrs[i  ][0], s->sampleptrs[i  ][1],
1577
                                     s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1578
                                     s->block_samples, i);
1579
1580
            bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1581
                              s->block_samples, info->log_limit);
1582
1583
            if (bits < info->best_bits) {
1584
                reversed = 1;
1585
                info->best_bits = bits;
1586
                CLEAR(s->decorr_passes);
1587
                memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1588
                memcpy(s->sampleptrs[info->nterms + 1][0],
1589
                       s->sampleptrs[i][0], s->block_samples * 4);
1590
                memcpy(s->sampleptrs[info->nterms + 1][1],
1591
                       s->sampleptrs[i][1], s->block_samples * 4);
1592
            } else {
1593
                info->dps[ri  ] = s->decorr_passes[ri  ];
1594
                info->dps[ri+1] = s->decorr_passes[ri+1];
1595
                decorr_stereo_buffer(info,
1596
                                     s->sampleptrs[ri  ][0], s->sampleptrs[ri  ][1],
1597
                                     s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1598
                                     s->block_samples, ri);
1599
            }
1600
        }
1601
    }
1602
}
1603
1604
static void delta_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1605
{
1606
    int lower = 0, delta, d, i;
1607
    uint32_t bits;
1608
1609
    if (!s->decorr_passes[0].value)
1610
        return;
1611
    delta = s->decorr_passes[0].delta;
1612
1613
    for (d = delta - 1; d >= 0; d--) {
1614
        for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1615
            info->dps[i].value = s->decorr_passes[i].value;
1616
            info->dps[i].delta = d;
1617
            decorr_stereo_buffer(info,
1618
                                 s->sampleptrs[i  ][0], s->sampleptrs[i  ][1],
1619
                                 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1620
                                 s->block_samples, i);
1621
        }
1622
1623
        bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1624
                          s->block_samples, info->log_limit);
1625
        if (bits >= info->best_bits)
1626
            break;
1627
        lower = 1;
1628
        info->best_bits = bits;
1629
        CLEAR(s->decorr_passes);
1630
        memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1631
        memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
1632
               s->block_samples * 4);
1633
        memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[i][1],
1634
               s->block_samples * 4);
1635
    }
1636
1637
    for (d = delta + 1; !lower && d <= 7; d++) {
1638
        for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1639
            info->dps[i].value = s->decorr_passes[i].value;
1640
            info->dps[i].delta = d;
1641
            decorr_stereo_buffer(info,
1642
                                 s->sampleptrs[i  ][0], s->sampleptrs[i  ][1],
1643
                                 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1644
                                 s->block_samples, i);
1645
        }
1646
1647
        bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1648
                          s->block_samples, info->log_limit);
1649
1650
        if (bits < info->best_bits) {
1651
            info->best_bits = bits;
1652
            CLEAR(s->decorr_passes);
1653
            memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1654
            memcpy(s->sampleptrs[info->nterms + 1][0],
1655
                   s->sampleptrs[i][0], s->block_samples * 4);
1656
            memcpy(s->sampleptrs[info->nterms + 1][1],
1657
                   s->sampleptrs[i][1], s->block_samples * 4);
1658
        }
1659
        else
1660
            break;
1661
    }
1662
}
1663
1664
static void recurse_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info,
1665
                           int depth, int delta, uint32_t input_bits)
1666
{
1667
    int term, branches = s->num_branches - depth;
1668
    int32_t *in_left, *in_right, *out_left, *out_right;
1669
    uint32_t term_bits[22], bits;
1670
1671
    if (branches < 1 || depth + 1 == info->nterms)
1672
        branches = 1;
1673
1674
    CLEAR(term_bits);
1675
    in_left   = s->sampleptrs[depth    ][0];
1676
    in_right  = s->sampleptrs[depth    ][1];
1677
    out_left  = s->sampleptrs[depth + 1][0];
1678
    out_right = s->sampleptrs[depth + 1][1];
1679
1680
    for (term = -3; term <= 18; term++) {
1681
        if (!term || (term > 8 && term < 17))
1682
            continue;
1683
1684
        if (term == 17 && branches == 1 && depth + 1 < info->nterms)
1685
            continue;
1686
1687
        if (term == -1 || term == -2)
1688
            if (!(s->flags & WV_CROSS_DECORR))
1689
                continue;
1690
1691
        if (!s->extra_flags && (term > 4 && term < 17))
1692
            continue;
1693
1694
        info->dps[depth].value = term;
1695
        info->dps[depth].delta = delta;
1696
        decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1697
                             s->block_samples, depth);
1698
        bits = log2stereo(out_left, out_right, s->block_samples, info->log_limit);
1699
1700
        if (bits < info->best_bits) {
1701
            info->best_bits = bits;
1702
            CLEAR(s->decorr_passes);
1703
            memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
1704
            memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[depth + 1][0],
1705
                   s->block_samples * 4);
1706
            memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[depth + 1][1],
1707
                   s->block_samples * 4);
1708
        }
1709
1710
        term_bits[term + 3] = bits;
1711
    }
1712
1713
    while (depth + 1 < info->nterms && branches--) {
1714
        uint32_t local_best_bits = input_bits;
1715
        int best_term = 0, i;
1716
1717
        for (i = 0; i < 22; i++)
1718
            if (term_bits[i] && term_bits[i] < local_best_bits) {
1719
                local_best_bits = term_bits[i];
1720
                best_term = i - 3;
1721
            }
1722
1723
        if (!best_term)
1724
            break;
1725
1726
        term_bits[best_term + 3] = 0;
1727
1728
        info->dps[depth].value = best_term;
1729
        info->dps[depth].delta = delta;
1730
        decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1731
                             s->block_samples, depth);
1732
1733
        recurse_stereo(s, info, depth + 1, delta, local_best_bits);
1734
    }
1735
}
1736
1737
static void analyze_stereo(WavPackEncodeContext *s,
1738
                           int32_t *in_left, int32_t *in_right,
1739
                           int do_samples)
1740
{
1741
    WavPackExtraInfo info;
1742
    int i;
1743
1744
    info.gt16bit = ((s->flags & MAG_MASK) >> MAG_LSB) >= 16;
1745
1746
    info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1747
    info.log_limit = FFMIN(6912, info.log_limit);
1748
1749
    info.nterms = s->num_terms;
1750
1751
    if (allocate_buffers2(s, s->num_terms))
1752
        return;
1753
1754
    memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
1755
    memcpy(s->sampleptrs[0][0], in_left,  s->block_samples * 4);
1756
    memcpy(s->sampleptrs[0][1], in_right, s->block_samples * 4);
1757
1758
    for (i = 0; i < info.nterms && info.dps[i].value; i++)
1759
        if (info.gt16bit)
1760
            decorr_stereo(s->sampleptrs[i    ][0], s->sampleptrs[i    ][1],
1761
                          s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1762
                          s->block_samples, info.dps + i, 1);
1763
        else
1764
            decorr_stereo_quick(s->sampleptrs[i    ][0], s->sampleptrs[i    ][1],
1765
                                s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1766
                                s->block_samples, info.dps + i);
1767
1768
    info.best_bits = log2stereo(s->sampleptrs[info.nterms][0], s->sampleptrs[info.nterms][1],
1769
                                s->block_samples, 0);
1770
1771
    memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
1772
    memcpy(s->sampleptrs[info.nterms + 1][1], s->sampleptrs[i][1], s->block_samples * 4);
1773
1774
    if (s->extra_flags & EXTRA_BRANCHES)
1775
        recurse_stereo(s, &info, 0, (int) floor(s->delta_decay + 0.5),
1776
                       log2stereo(s->sampleptrs[0][0], s->sampleptrs[0][1],
1777
                                  s->block_samples, 0));
1778
1779
    if (s->extra_flags & EXTRA_SORT_FIRST)
1780
        sort_stereo(s, &info);
1781
1782
    if (s->extra_flags & EXTRA_TRY_DELTAS) {
1783
        delta_stereo(s, &info);
1784
1785
        if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
1786
            s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
1787
        else
1788
            s->delta_decay = 2.0;
1789
    }
1790
1791
    if (s->extra_flags & EXTRA_SORT_LAST)
1792
        sort_stereo(s, &info);
1793
1794
    if (do_samples) {
1795
        memcpy(in_left,  s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
1796
        memcpy(in_right, s->sampleptrs[info.nterms + 1][1], s->block_samples * 4);
1797
    }
1798
1799
    for (i = 0; i < info.nterms; i++)
1800
        if (!s->decorr_passes[i].value)
1801
            break;
1802
1803
    s->num_terms = i;
1804
}
1805
1806
12
static int wv_stereo(WavPackEncodeContext *s,
1807
                     int32_t *samples_l, int32_t *samples_r,
1808
                     int no_history, int do_samples)
1809
{
1810
12
    struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1811
12
    int nb_samples = s->block_samples, ret;
1812
12
    int buf_size = sizeof(int32_t) * nb_samples;
1813
12
    int log_limit, force_js = 0, force_ts = 0, got_js = 0, pi, i;
1814
12
    uint32_t best_size = UINT32_MAX, size;
1815
1816
12
    for (i = 0; i < nb_samples; i++)
1817

12
        if (samples_l[i] || samples_r[i])
1818
            break;
1819
1820
12
    if (i == nb_samples) {
1821
        s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1822
        CLEAR(s->decorr_passes);
1823
        CLEAR(s->w);
1824
        s->num_terms = 0;
1825
        return 0;
1826
    }
1827
1828
12
    log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1829
12
    log_limit = FFMIN(6912, log_limit);
1830
1831
12
    if (s->joint != -1) {
1832
        force_js =  s->joint;
1833
        force_ts = !s->joint;
1834
    }
1835
1836
12
    if ((ret = allocate_buffers(s)) < 0)
1837
        return ret;
1838
1839

12
    if (no_history || s->num_passes >= 7)
1840
1
        s->best_decorr = s->mask_decorr = 0;
1841
1842
37
    for (pi = 0; pi < s->num_passes;) {
1843
        const WavPackDecorrSpec *wpds;
1844
        int nterms, c, j;
1845
1846
25
        if (!pi)
1847
12
            c = s->best_decorr;
1848
        else {
1849
13
            if (s->mask_decorr == 0)
1850
2
                c = 0;
1851
            else
1852
11
                c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1853
1854
13
            if (c == s->best_decorr) {
1855
1
                s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1856
1
                continue;
1857
            }
1858
        }
1859
1860
24
        wpds = &s->decorr_specs[c];
1861
24
        nterms = decorr_filter_nterms[s->decorr_filter];
1862
1863
        while (1) {
1864

24
            if (force_js || (wpds->joint_stereo && !force_ts)) {
1865
3
                if (!got_js) {
1866
3
                    av_fast_padded_malloc(&s->js_left,  &s->js_left_size,  buf_size);
1867
3
                    av_fast_padded_malloc(&s->js_right, &s->js_right_size, buf_size);
1868
3
                    memcpy(s->js_left,  samples_l, buf_size);
1869
3
                    memcpy(s->js_right, samples_r, buf_size);
1870
1871
66153
                    for (i = 0; i < nb_samples; i++)
1872
66150
                        s->js_right[i] += ((s->js_left[i] -= s->js_right[i]) >> 1);
1873
3
                    got_js = 1;
1874
                }
1875
1876
3
                memcpy(s->temp_buffer[0][0], s->js_left,  buf_size);
1877
3
                memcpy(s->temp_buffer[0][1], s->js_right, buf_size);
1878
            } else {
1879
21
                memcpy(s->temp_buffer[0][0], samples_l, buf_size);
1880
21
                memcpy(s->temp_buffer[0][1], samples_r, buf_size);
1881
            }
1882
1883
24
            CLEAR(save_decorr_passes);
1884
1885
144
            for (j = 0; j < nterms; j++) {
1886
120
                CLEAR(temp_decorr_pass);
1887
120
                temp_decorr_pass.delta = wpds->delta;
1888
120
                temp_decorr_pass.value = wpds->terms[j];
1889
1890

120
                if (temp_decorr_pass.value < 0 && !(s->flags & WV_CROSS_DECORR))
1891
                    temp_decorr_pass.value = -3;
1892
1893
120
                decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1894
120
                              s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1895
                              FFMIN(2048, nb_samples), &temp_decorr_pass, -1);
1896
1897
120
                if (j) {
1898
96
                    CLEAR(temp_decorr_pass.samplesA);
1899
96
                    CLEAR(temp_decorr_pass.samplesB);
1900
                } else {
1901
24
                    reverse_decorr(&temp_decorr_pass);
1902
                }
1903
1904
120
                memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1905
1906
120
                if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16)
1907
                    decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1908
                                  s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1909
                                  nb_samples, &temp_decorr_pass, 1);
1910
                else
1911
120
                    decorr_stereo_quick(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1912
120
                                        s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1913
                                        nb_samples, &temp_decorr_pass);
1914
            }
1915
1916
24
            size = log2stereo(s->temp_buffer[j&1][0], s->temp_buffer[j&1][1],
1917
                              nb_samples, log_limit);
1918

24
            if (size != UINT32_MAX || !nterms)
1919
                break;
1920
            nterms >>= 1;
1921
        }
1922
1923
24
        if (size < best_size) {
1924
18
            memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1925
18
            memcpy(s->best_buffer[1], s->temp_buffer[j&1][1], buf_size);
1926
18
            memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1927
18
            s->num_terms = nterms;
1928
18
            s->best_decorr = c;
1929
18
            best_size = size;
1930
        }
1931
1932
24
        if (pi++)
1933
12
            s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1934
    }
1935
1936

12
    if (force_js || (s->decorr_specs[s->best_decorr].joint_stereo && !force_ts))
1937
        s->flags |= WV_JOINT_STEREO;
1938
    else
1939
12
        s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1940
1941
12
    if (s->extra_flags) {
1942
        if (s->flags & WV_JOINT_STEREO) {
1943
            analyze_stereo(s, s->js_left, s->js_right, do_samples);
1944
1945
            if (do_samples) {
1946
                memcpy(samples_l, s->js_left,  buf_size);
1947
                memcpy(samples_r, s->js_right, buf_size);
1948
            }
1949
        } else
1950
            analyze_stereo(s, samples_l, samples_r, do_samples);
1951
12
    } else if (do_samples) {
1952
12
        memcpy(samples_l, s->best_buffer[0], buf_size);
1953
12
        memcpy(samples_r, s->best_buffer[1], buf_size);
1954
    }
1955
1956

12
    if (s->extra_flags || no_history ||
1957
11
        s->joint_stereo != s->decorr_specs[s->best_decorr].joint_stereo) {
1958
1
        s->joint_stereo = s->decorr_specs[s->best_decorr].joint_stereo;
1959
1
        CLEAR(s->w);
1960
1
        scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1961
1
        scan_word(s, &s->w.c[1], s->best_buffer[1], nb_samples, -1);
1962
    }
1963
12
    return 0;
1964
}
1965
1966
573304
static void encode_flush(WavPackEncodeContext *s)
1967
{
1968
573304
    WavPackWords *w = &s->w;
1969
573304
    PutBitContext *pb = &s->pb;
1970
1971
573304
    if (w->zeros_acc) {
1972
        int cbits = count_bits(w->zeros_acc);
1973
1974
        do {
1975
            if (cbits > 31) {
1976
                put_bits(pb, 31, 0x7FFFFFFF);
1977
                cbits -= 31;
1978
            } else {
1979
                put_bits(pb, cbits, (1 << cbits) - 1);
1980
                cbits = 0;
1981
            }
1982
        } while (cbits);
1983
1984
        put_bits(pb, 1, 0);
1985
1986
        while (w->zeros_acc > 1) {
1987
            put_bits(pb, 1, w->zeros_acc & 1);
1988
            w->zeros_acc >>= 1;
1989
        }
1990
1991
        w->zeros_acc = 0;
1992
    }
1993
1994
573304
    if (w->holding_one) {
1995
125252
        if (w->holding_one >= 16) {
1996
            int cbits;
1997
1998
81
            put_bits(pb, 16, (1 << 16) - 1);
1999
81
            put_bits(pb, 1, 0);
2000
81
            w->holding_one -= 16;
2001
81
            cbits = count_bits(w->holding_one);
2002
2003
            do {
2004
81
                if (cbits > 31) {
2005
                    put_bits(pb, 31, 0x7FFFFFFF);
2006
                    cbits -= 31;
2007
                } else {
2008
81
                    put_bits(pb, cbits, (1 << cbits) - 1);
2009
81
                    cbits = 0;
2010
                }
2011
81
            } while (cbits);
2012
2013
81
            put_bits(pb, 1, 0);
2014
2015
452
            while (w->holding_one > 1) {
2016
371
                put_bits(pb, 1, w->holding_one & 1);
2017
371
                w->holding_one >>= 1;
2018
            }
2019
2020
81
            w->holding_zero = 0;
2021
        } else {
2022
125171
            put_bits(pb, w->holding_one, (1 << w->holding_one) - 1);
2023
        }
2024
2025
125252
        w->holding_one = 0;
2026
    }
2027
2028
573304
    if (w->holding_zero) {
2029
307729
        put_bits(pb, 1, 0);
2030
307729
        w->holding_zero = 0;
2031
    }
2032
2033
573304
    if (w->pend_count) {
2034
573300
        put_bits(pb, w->pend_count, w->pend_data);
2035
573300
        w->pend_data = w->pend_count = 0;
2036
    }
2037
573304
}
2038
2039
573300
static void wavpack_encode_sample(WavPackEncodeContext *s, WvChannel *c, int32_t sample)
2040
{
2041
573300
    WavPackWords *w = &s->w;
2042
    uint32_t ones_count, low, high;
2043
573300
    int sign = sample < 0;
2044
2045

573300
    if (s->w.c[0].median[0] < 2 && !s->w.holding_zero && s->w.c[1].median[0] < 2) {
2046
        if (w->zeros_acc) {
2047
            if (sample)
2048
                encode_flush(s);
2049
            else {
2050
                w->zeros_acc++;
2051
                return;
2052
            }
2053
        } else if (sample) {
2054
            put_bits(&s->pb, 1, 0);
2055
        } else {
2056
            CLEAR(s->w.c[0].median);
2057
            CLEAR(s->w.c[1].median);
2058
            w->zeros_acc = 1;
2059
            return;
2060
        }
2061
    }
2062
2063
573300
    if (sign)
2064
203451
        sample = ~sample;
2065
2066
573300
    if (sample < (int32_t) GET_MED(0)) {
2067
447048
        ones_count = low = 0;
2068
447048
        high = GET_MED(0) - 1;
2069
447048
        DEC_MED(0);
2070
    } else {
2071
126252
        low = GET_MED(0);
2072
126252
        INC_MED(0);
2073
2074
126252
        if (sample - low < GET_MED(1)) {
2075
89886
            ones_count = 1;
2076
89886
            high = low + GET_MED(1) - 1;
2077
89886
            DEC_MED(1);
2078
        } else {
2079
36366
            low += GET_MED(1);
2080
36366
            INC_MED(1);
2081
2082
36366
            if (sample - low < GET_MED(2)) {
2083
25626
                ones_count = 2;
2084
25626
                high = low + GET_MED(2) - 1;
2085
25626
                DEC_MED(2);
2086
            } else {
2087
10740
                ones_count = 2 + (sample - low) / GET_MED(2);
2088
10740
                low += (ones_count - 2) * GET_MED(2);
2089
10740
                high = low + GET_MED(2) - 1;
2090
10740
                INC_MED(2);
2091
            }
2092
        }
2093
    }
2094
2095
573300
    if (w->holding_zero) {
2096
307801
        if (ones_count)
2097
42311
            w->holding_one++;
2098
2099
307801
        encode_flush(s);
2100
2101
307801
        if (ones_count) {
2102
42311
            w->holding_zero = 1;
2103
42311
            ones_count--;
2104
        } else
2105
265490
            w->holding_zero = 0;
2106
    } else
2107
265499
        w->holding_zero = 1;
2108
2109
573300
    w->holding_one = ones_count * 2;
2110
2111
573300
    if (high != low) {
2112
417675
        uint32_t maxcode = high - low, code = sample - low;
2113
417675
        int bitcount = count_bits(maxcode);
2114
417675
        uint32_t extras = (1 << bitcount) - maxcode - 1;
2115
2116
417675
        if (code < extras) {
2117
151217
            w->pend_data |= code << w->pend_count;
2118
151217
            w->pend_count += bitcount - 1;
2119
        } else {
2120
266458
            w->pend_data |= ((code + extras) >> 1) << w->pend_count;
2121
266458
            w->pend_count += bitcount - 1;
2122
266458
            w->pend_data |= ((code + extras) & 1) << w->pend_count++;
2123
        }
2124
    }
2125
2126
573300
    w->pend_data |= ((int32_t) sign << w->pend_count++);
2127
2128
573300
    if (!w->holding_zero)
2129
265490
        encode_flush(s);
2130
}
2131
2132
static void pack_int32(WavPackEncodeContext *s,
2133
                       int32_t *samples_l, int32_t *samples_r,
2134
                       int nb_samples)
2135
{
2136
    const int sent_bits = s->int32_sent_bits;
2137
    PutBitContext *pb = &s->pb;
2138
    int i, pre_shift;
2139
2140
    pre_shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2141
2142
    if (!sent_bits)
2143
        return;
2144
2145
    if (s->flags & WV_MONO_DATA) {
2146
        for (i = 0; i < nb_samples; i++) {
2147
            put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2148
        }
2149
    } else {
2150
        for (i = 0; i < nb_samples; i++) {
2151
            put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2152
            put_sbits(pb, sent_bits, samples_r[i] >> pre_shift);
2153
        }
2154
    }
2155
}
2156
2157
static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
2158
{
2159
    const int max_exp = s->float_max_exp;
2160
    PutBitContext *pb = &s->pb;
2161
    int32_t value, shift_count;
2162
2163
    if (get_exponent(*sample) == 255) {
2164
        if (get_mantissa(*sample)) {
2165
            put_bits(pb, 1, 1);
2166
            put_bits(pb, 23, get_mantissa(*sample));
2167
        } else {
2168
            put_bits(pb, 1, 0);
2169
        }
2170
2171
        value = 0x1000000;
2172
        shift_count = 0;
2173
    } else if (get_exponent(*sample)) {
2174
        shift_count = max_exp - get_exponent(*sample);
2175
        value = 0x800000 + get_mantissa(*sample);
2176
    } else {
2177
        shift_count = max_exp ? max_exp - 1 : 0;
2178
        value = get_mantissa(*sample);
2179
    }
2180
2181
    if (shift_count < 25)
2182
        value >>= shift_count;
2183
    else
2184
        value = 0;
2185
2186
    if (!value) {
2187
        if (s->float_flags & FLOAT_ZEROS_SENT) {
2188
            if (get_exponent(*sample) || get_mantissa(*sample)) {
2189
                put_bits(pb, 1, 1);
2190
                put_bits(pb, 23, get_mantissa(*sample));
2191
2192
                if (max_exp >= 25)
2193
                    put_bits(pb, 8, get_exponent(*sample));
2194
2195
                put_bits(pb, 1, get_sign(*sample));
2196
            } else {
2197
                put_bits(pb, 1, 0);
2198
2199
                if (s->float_flags & FLOAT_NEG_ZEROS)
2200
                    put_bits(pb, 1, get_sign(*sample));
2201
            }
2202
        }
2203
    } else if (shift_count) {
2204
        if (s->float_flags & FLOAT_SHIFT_SENT) {
2205
            put_sbits(pb, shift_count, get_mantissa(*sample));
2206
        } else if (s->float_flags & FLOAT_SHIFT_SAME) {
2207
            put_bits(pb, 1, get_mantissa(*sample) & 1);
2208
        }
2209
    }
2210
}
2211
2212
static void pack_float(WavPackEncodeContext *s,
2213
                       int32_t *samples_l, int32_t *samples_r,
2214
                       int nb_samples)
2215
{
2216
    int i;
2217
2218
    if (s->flags & WV_MONO_DATA) {
2219
        for (i = 0; i < nb_samples; i++)
2220
            pack_float_sample(s, &samples_l[i]);
2221
    } else {
2222
        for (i = 0; i < nb_samples; i++) {
2223
            pack_float_sample(s, &samples_l[i]);
2224
            pack_float_sample(s, &samples_r[i]);
2225
        }
2226
    }
2227
}
2228
2229
static void decorr_stereo_pass2(struct Decorr *dpp,
2230
                                int32_t *samples_l, int32_t *samples_r,
2231
                                int nb_samples)
2232
{
2233
    int i, m, k;
2234
2235
    switch (dpp->value) {
2236
    case 17:
2237
        for (i = 0; i < nb_samples; i++) {
2238
            int32_t sam, tmp;
2239
2240
            sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2241
            dpp->samplesA[1] = dpp->samplesA[0];
2242
            samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2243
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2244
2245
            sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2246
            dpp->samplesB[1] = dpp->samplesB[0];
2247
            samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2248
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2249
        }
2250
        break;
2251
    case 18:
2252
        for (i = 0; i < nb_samples; i++) {
2253
            int32_t sam, tmp;
2254
2255
            sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2256
            dpp->samplesA[1] = dpp->samplesA[0];
2257
            samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2258
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2259
2260
            sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2261
            dpp->samplesB[1] = dpp->samplesB[0];
2262
            samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2263
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2264
        }
2265
        break;
2266
    default:
2267
        for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2268
            int32_t sam, tmp;
2269
2270
            sam = dpp->samplesA[m];
2271
            samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2272
            UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2273
2274
            sam = dpp->samplesB[m];
2275
            samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2276
            UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2277
2278
            m = (m + 1) & (MAX_TERM - 1);
2279
            k = (k + 1) & (MAX_TERM - 1);
2280
        }
2281
        if (m) {
2282
            int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2283
2284
            memcpy(temp_A, dpp->samplesA, sizeof (dpp->samplesA));
2285
            memcpy(temp_B, dpp->samplesB, sizeof (dpp->samplesB));
2286
2287
            for (k = 0; k < MAX_TERM; k++) {
2288
                dpp->samplesA[k] = temp_A[m];
2289
                dpp->samplesB[k] = temp_B[m];
2290
                m = (m + 1) & (MAX_TERM - 1);
2291
            }
2292
        }
2293
        break;
2294
    case -1:
2295
        for (i = 0; i < nb_samples; i++) {
2296
            int32_t sam_A, sam_B, tmp;
2297
2298
            sam_A = dpp->samplesA[0];
2299
            samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2300
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2301
2302
            samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2303
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2304
        }
2305
        break;
2306
    case -2:
2307
        for (i = 0; i < nb_samples; i++) {
2308
            int32_t sam_A, sam_B, tmp;
2309
2310
            sam_B = dpp->samplesB[0];
2311
            samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2312
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2313
2314
            samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2315
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2316
        }
2317
        break;
2318
    case -3:
2319
        for (i = 0; i < nb_samples; i++) {
2320
            int32_t sam_A, sam_B, tmp;
2321
2322
            sam_A = dpp->samplesA[0];
2323
            sam_B = dpp->samplesB[0];
2324
2325
            dpp->samplesA[0] = tmp = samples_r[i];
2326
            samples_r[i] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
2327
            UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2328
2329
            dpp->samplesB[0] = tmp = samples_l[i];
2330
            samples_l[i] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
2331
            UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2332
        }
2333
        break;
2334
    }
2335
}
2336
2337
#define update_weight_d2(weight, delta, source, result) \
2338
    if (source && result) \
2339
        weight -= (((source ^ result) >> 29) & 4) - 2;
2340
2341
#define update_weight_clip_d2(weight, delta, source, result) \
2342
    if (source && result) { \
2343
        const int32_t s = (source ^ result) >> 31; \
2344
        if ((weight = (weight ^ s) + (2 - s)) > 1024) weight = 1024; \
2345
        weight = (weight ^ s) - s; \
2346
    }
2347
2348
static void decorr_stereo_pass_id2(struct Decorr *dpp,
2349
                                   int32_t *samples_l, int32_t *samples_r,
2350
                                   int nb_samples)
2351
{
2352
    int i, m, k;
2353
2354
    switch (dpp->value) {
2355
    case 17:
2356
        for (i = 0; i < nb_samples; i++) {
2357
            int32_t sam, tmp;
2358
2359
            sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2360
            dpp->samplesA[1] = dpp->samplesA[0];
2361
            samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2362
            update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2363
2364
            sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2365
            dpp->samplesB[1] = dpp->samplesB[0];
2366
            samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2367
            update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2368
        }
2369
        break;
2370
    case 18:
2371
        for (i = 0; i < nb_samples; i++) {
2372
            int32_t sam, tmp;
2373
2374
            sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2375
            dpp->samplesA[1] = dpp->samplesA[0];
2376
            samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2377
            update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2378
2379
            sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2380
            dpp->samplesB[1] = dpp->samplesB[0];
2381
            samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2382
            update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2383
        }
2384
        break;
2385
    default:
2386
        for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2387
            int32_t sam, tmp;
2388
2389
            sam = dpp->samplesA[m];
2390
            samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2391
            update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2392
2393
            sam = dpp->samplesB[m];
2394
            samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2395
            update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2396
2397
            m = (m + 1) & (MAX_TERM - 1);
2398
            k = (k + 1) & (MAX_TERM - 1);
2399
        }
2400
2401
        if (m) {
2402
            int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2403
2404
            memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2405
            memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2406
2407
            for (k = 0; k < MAX_TERM; k++) {
2408
                dpp->samplesA[k] = temp_A[m];
2409
                dpp->samplesB[k] = temp_B[m];
2410
                m = (m + 1) & (MAX_TERM - 1);
2411
            }
2412
        }
2413
        break;
2414
    case -1:
2415
        for (i = 0; i < nb_samples; i++) {
2416
            int32_t sam_A, sam_B, tmp;
2417
2418
            sam_A = dpp->samplesA[0];
2419
            samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2420
            update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2421
2422
            samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2423
            update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2424
        }
2425
        break;
2426
    case -2:
2427
        for (i = 0; i < nb_samples; i++) {
2428
            int32_t sam_A, sam_B, tmp;
2429
2430
            sam_B = dpp->samplesB[0];
2431
            samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2432
            update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2433
2434
            samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2435
            update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2436
        }
2437
        break;
2438
    case -3:
2439
        for (i = 0; i < nb_samples; i++) {
2440
            int32_t sam_A, sam_B, tmp;
2441
2442
            sam_A = dpp->samplesA[0];
2443
            sam_B = dpp->samplesB[0];
2444
2445
            dpp->samplesA[0] = tmp = samples_r[i];
2446
            samples_r[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
2447
            update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2448
2449
            dpp->samplesB[0] = tmp = samples_l[i];
2450
            samples_l[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
2451
            update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2452
        }
2453
        break;
2454
    }
2455
}
2456
2457
26
static void put_metadata_block(PutByteContext *pb, int flags, int size)
2458
{
2459
26
    if (size & 1)
2460
12
        flags |= WP_IDF_ODD;
2461
2462
26
    bytestream2_put_byte(pb, flags);
2463
26
    bytestream2_put_byte(pb, (size + 1) >> 1);
2464
26
}
2465
2466
13
static int wavpack_encode_block(WavPackEncodeContext *s,
2467
                                int32_t *samples_l, int32_t *samples_r,
2468
                                uint8_t *out, int out_size)
2469
{
2470
13
    int block_size, start, end, data_size, tcount, temp, m = 0;
2471
13
    int i, j, ret = 0, got_extra = 0, nb_samples = s->block_samples;
2472
13
    uint32_t crc = 0xffffffffu;
2473
    struct Decorr *dpp;
2474
    PutByteContext pb;
2475
2476
13
    if (s->flags & WV_MONO_DATA) {
2477
1
        CLEAR(s->w);
2478
    }
2479

13
    if (!(s->flags & WV_MONO) && s->optimize_mono) {
2480
        int32_t lor = 0, diff = 0;
2481
2482
        for (i = 0; i < nb_samples; i++) {
2483
            lor  |= samples_l[i] | samples_r[i];
2484
            diff |= samples_l[i] - samples_r[i];
2485
2486
            if (lor && diff)
2487
                break;
2488
        }
2489
2490
        if (i == nb_samples && lor && !diff) {
2491
            s->flags &= ~(WV_JOINT_STEREO | WV_CROSS_DECORR);
2492
            s->flags |= WV_FALSE_STEREO;
2493
2494
            if (!s->false_stereo) {
2495
                s->false_stereo = 1;
2496
                s->num_terms = 0;
2497
                CLEAR(s->w);
2498
            }
2499
        } else if (s->false_stereo) {
2500
            s->false_stereo = 0;
2501
            s->num_terms = 0;
2502
            CLEAR(s->w);
2503
        }
2504
    }
2505
2506
13
    if (s->flags & SHIFT_MASK) {
2507
        int shift = (s->flags & SHIFT_MASK) >> SHIFT_LSB;
2508
        int mag = (s->flags & MAG_MASK) >> MAG_LSB;
2509
2510
        if (s->flags & WV_MONO_DATA)
2511
            shift_mono(samples_l, nb_samples, shift);
2512
        else
2513
            shift_stereo(samples_l, samples_r, nb_samples, shift);
2514
2515
        if ((mag -= shift) < 0)
2516
            s->flags &= ~MAG_MASK;
2517
        else
2518
            s->flags -= (1 << MAG_LSB) * shift;
2519
    }
2520
2521

13
    if ((s->flags & WV_FLOAT_DATA) || (s->flags & MAG_MASK) >> MAG_LSB >= 24) {
2522
        av_fast_padded_malloc(&s->orig_l, &s->orig_l_size, sizeof(int32_t) * nb_samples);
2523
        memcpy(s->orig_l, samples_l, sizeof(int32_t) * nb_samples);
2524
        if (!(s->flags & WV_MONO_DATA)) {
2525
            av_fast_padded_malloc(&s->orig_r, &s->orig_r_size, sizeof(int32_t) * nb_samples);
2526
            memcpy(s->orig_r, samples_r, sizeof(int32_t) * nb_samples);
2527
        }
2528
2529
        if (s->flags & WV_FLOAT_DATA)
2530
            got_extra = scan_float(s, samples_l, samples_r, nb_samples);
2531
        else
2532
            got_extra = scan_int32(s, samples_l, samples_r, nb_samples);
2533
        s->num_terms = 0;
2534
    } else {
2535
13
        scan_int23(s, samples_l, samples_r, nb_samples);
2536
13
        if (s->shift != s->int32_zeros + s->int32_ones + s->int32_dups) {
2537
            s->shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2538
            s->num_terms = 0;
2539
        }
2540
    }
2541
2542

13
    if (!s->num_passes && !s->num_terms) {
2543
1
        s->num_passes = 1;
2544
2545
1
        if (s->flags & WV_MONO_DATA)
2546
1
            ret = wv_mono(s, samples_l, 1, 0);
2547
        else
2548
            ret = wv_stereo(s, samples_l, samples_r, 1, 0);
2549
2550
1
        s->num_passes = 0;
2551
    }
2552
13
    if (s->flags & WV_MONO_DATA) {
2553
44101
        for (i = 0; i < nb_samples; i++)
2554
44100
            crc += (crc << 1) + samples_l[i];
2555
2556
1
        if (s->num_passes)
2557
            ret = wv_mono(s, samples_l, !s->num_terms, 1);
2558
    } else {
2559
264612
        for (i = 0; i < nb_samples; i++)
2560
264600
            crc += (crc << 3) + ((uint32_t)samples_l[i] << 1) + samples_l[i] + samples_r[i];
2561
2562
12
        if (s->num_passes)
2563
12
            ret = wv_stereo(s, samples_l, samples_r, !s->num_terms, 1);
2564
    }
2565
13
    if (ret < 0)
2566
        return ret;
2567
2568
13
    if (!s->ch_offset)
2569
13
        s->flags |= WV_INITIAL_BLOCK;
2570
2571
13
    s->ch_offset += 1 + !(s->flags & WV_MONO);
2572
2573
13
    if (s->ch_offset == s->avctx->channels)
2574
13
        s->flags |= WV_FINAL_BLOCK;
2575
2576
13
    bytestream2_init_writer(&pb, out, out_size);
2577
13
    bytestream2_put_le32(&pb, MKTAG('w', 'v', 'p', 'k'));
2578
13
    bytestream2_put_le32(&pb, 0);
2579
13
    bytestream2_put_le16(&pb, 0x410);
2580
13
    bytestream2_put_le16(&pb, 0);
2581
13
    bytestream2_put_le32(&pb, 0);
2582
13
    bytestream2_put_le32(&pb, s->sample_index);
2583
13
    bytestream2_put_le32(&pb, nb_samples);
2584
13
    bytestream2_put_le32(&pb, s->flags);
2585
13
    bytestream2_put_le32(&pb, crc);
2586
2587
13
    if (s->flags & WV_INITIAL_BLOCK &&
2588
13
        s->avctx->channel_layout != AV_CH_LAYOUT_MONO &&
2589
12
        s->avctx->channel_layout != AV_CH_LAYOUT_STEREO) {
2590
        put_metadata_block(&pb, WP_ID_CHANINFO, 5);
2591
        bytestream2_put_byte(&pb, s->avctx->channels);
2592
        bytestream2_put_le32(&pb, s->avctx->channel_layout);
2593
        bytestream2_put_byte(&pb, 0);
2594
    }
2595
2596
13
    if ((s->flags & SRATE_MASK) == SRATE_MASK) {
2597
        put_metadata_block(&pb, WP_ID_SAMPLE_RATE, 3);
2598
        bytestream2_put_le24(&pb, s->avctx->sample_rate);
2599
        bytestream2_put_byte(&pb, 0);
2600
    }
2601
2602
13
    put_metadata_block(&pb, WP_ID_DECTERMS, s->num_terms);
2603
75
    for (i = 0; i < s->num_terms; i++) {
2604
62
        struct Decorr *dpp = &s->decorr_passes[i];
2605
62
        bytestream2_put_byte(&pb, ((dpp->value + 5) & 0x1f) | ((dpp->delta << 5) & 0xe0));
2606
    }
2607
13
    if (s->num_terms & 1)
2608
12
        bytestream2_put_byte(&pb, 0);
2609
2610
#define WRITE_DECWEIGHT(type) do {            \
2611
        temp = store_weight(type);    \
2612
        bytestream2_put_byte(&pb, temp);      \
2613
        type = restore_weight(temp);  \
2614
    } while (0)
2615
2616
13
    bytestream2_put_byte(&pb, WP_ID_DECWEIGHTS);
2617
13
    bytestream2_put_byte(&pb, 0);
2618
13
    start = bytestream2_tell_p(&pb);
2619
13
    for (i = s->num_terms - 1; i >= 0; --i) {
2620
13
        struct Decorr *dpp = &s->decorr_passes[i];
2621
2622
13
        if (store_weight(dpp->weightA) ||
2623

5
            (!(s->flags & WV_MONO_DATA) && store_weight(dpp->weightB)))
2624
                break;
2625
    }
2626
13
    tcount = i + 1;
2627
75
    for (i = 0; i < s->num_terms; i++) {
2628
62
        struct Decorr *dpp = &s->decorr_passes[i];
2629
62
        if (i < tcount) {
2630
62
            WRITE_DECWEIGHT(dpp->weightA);
2631
62
            if (!(s->flags & WV_MONO_DATA))
2632
60
                WRITE_DECWEIGHT(dpp->weightB);
2633
        } else {
2634
            dpp->weightA = dpp->weightB = 0;
2635
        }
2636
    }
2637
13
    end = bytestream2_tell_p(&pb);
2638
13
    out[start - 2] = WP_ID_DECWEIGHTS | (((end - start) & 1) ? WP_IDF_ODD: 0);
2639
13
    out[start - 1] = (end - start + 1) >> 1;
2640
13
    if ((end - start) & 1)
2641
        bytestream2_put_byte(&pb, 0);
2642
2643
#define WRITE_DECSAMPLE(type) do {        \
2644
        temp = log2s(type);               \
2645
        type = wp_exp2(temp);             \
2646
        bytestream2_put_le16(&pb, temp);  \
2647
    } while (0)
2648
2649
13
    bytestream2_put_byte(&pb, WP_ID_DECSAMPLES);
2650
13
    bytestream2_put_byte(&pb, 0);
2651
13
    start = bytestream2_tell_p(&pb);
2652
75
    for (i = 0; i < s->num_terms; i++) {
2653
62
        struct Decorr *dpp = &s->decorr_passes[i];
2654
62
        if (i == 0) {
2655
13
            if (dpp->value > MAX_TERM) {
2656
13
                WRITE_DECSAMPLE(dpp->samplesA[0]);
2657
13
                WRITE_DECSAMPLE(dpp->samplesA[1]);
2658
13
                if (!(s->flags & WV_MONO_DATA)) {
2659
12
                    WRITE_DECSAMPLE(dpp->samplesB[0]);
2660
12
                    WRITE_DECSAMPLE(dpp->samplesB[1]);
2661
                }
2662
            } else if (dpp->value < 0) {
2663
                WRITE_DECSAMPLE(dpp->samplesA[0]);
2664
                WRITE_DECSAMPLE(dpp->samplesB[0]);
2665
            } else {
2666
                for (j = 0; j < dpp->value; j++) {
2667
                    WRITE_DECSAMPLE(dpp->samplesA[j]);
2668
                    if (!(s->flags & WV_MONO_DATA))
2669
                        WRITE_DECSAMPLE(dpp->samplesB[j]);
2670
                }
2671
            }
2672
        } else {
2673
49
            CLEAR(dpp->samplesA);
2674
49
            CLEAR(dpp->samplesB);
2675
        }
2676
    }
2677
13
    end = bytestream2_tell_p(&pb);
2678
13
    out[start - 1] = (end - start) >> 1;
2679
2680
#define WRITE_CHAN_ENTROPY(chan) do {               \
2681
        for (i = 0; i < 3; i++) {                   \
2682
            temp = wp_log2(s->w.c[chan].median[i]); \
2683
            bytestream2_put_le16(&pb, temp);        \
2684
            s->w.c[chan].median[i] = wp_exp2(temp); \
2685
        }                                           \
2686
    } while (0)
2687
2688
13
    put_metadata_block(&pb, WP_ID_ENTROPY, 6 * (1 + (!(s->flags & WV_MONO_DATA))));
2689
52
    WRITE_CHAN_ENTROPY(0);
2690
13
    if (!(s->flags & WV_MONO_DATA))
2691
48
        WRITE_CHAN_ENTROPY(1);
2692
2693
13
    if (s->flags & WV_FLOAT_DATA) {
2694
        put_metadata_block(&pb, WP_ID_FLOATINFO, 4);
2695
        bytestream2_put_byte(&pb, s->float_flags);
2696
        bytestream2_put_byte(&pb, s->float_shift);
2697
        bytestream2_put_byte(&pb, s->float_max_exp);
2698
        bytestream2_put_byte(&pb, 127);
2699
    }
2700
2701
13
    if (s->flags & WV_INT32_DATA) {
2702
        put_metadata_block(&pb, WP_ID_INT32INFO, 4);
2703
        bytestream2_put_byte(&pb, s->int32_sent_bits);
2704
        bytestream2_put_byte(&pb, s->int32_zeros);
2705
        bytestream2_put_byte(&pb, s->int32_ones);
2706
        bytestream2_put_byte(&pb, s->int32_dups);
2707
    }
2708
2709

13
    if (s->flags & WV_MONO_DATA && !s->num_passes) {
2710
44101
        for (i = 0; i < nb_samples; i++) {
2711
44100
            int32_t code = samples_l[i];
2712
2713
132300
            for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++) {
2714
                int32_t sam;
2715
2716
88200
                if (dpp->value > MAX_TERM) {
2717
88200
                    if (dpp->value & 1)
2718
44100
                        sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2719
                    else
2720
44100
                        sam = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
2721
2722
88200
                    dpp->samplesA[1] = dpp->samplesA[0];
2723
88200
                    dpp->samplesA[0] = code;
2724
                } else {
2725
                    sam = dpp->samplesA[m];
2726
                    dpp->samplesA[(m + dpp->value) & (MAX_TERM - 1)] = code;
2727
                }
2728
2729
88200
                code -= APPLY_WEIGHT(dpp->weightA, sam);
2730

88200
                UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, code);
2731
            }
2732
2733
44100
            m = (m + 1) & (MAX_TERM - 1);
2734
44100
            samples_l[i] = code;
2735
        }
2736
1
        if (m) {
2737
3
            for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++)
2738

2
                if (dpp->value > 0 && dpp->value <= MAX_TERM) {
2739
                int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2740
                int k;
2741
2742
                memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2743
                memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2744
2745
                for (k = 0; k < MAX_TERM; k++) {
2746
                    dpp->samplesA[k] = temp_A[m];
2747
                    dpp->samplesB[k] = temp_B[m];
2748
                    m = (m + 1) & (MAX_TERM - 1);
2749
                }
2750
            }
2751
        }
2752
12
    } else if (!s->num_passes) {
2753
        if (s->flags & WV_JOINT_STEREO) {
2754
            for (i = 0; i < nb_samples; i++)
2755
                samples_r[i] += ((samples_l[i] -= samples_r[i]) >> 1);
2756
        }
2757
2758
        for (i = 0; i < s->num_terms; i++) {
2759
            struct Decorr *dpp = &s->decorr_passes[i];
2760
            if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16 || dpp->delta != 2)
2761
                decorr_stereo_pass2(dpp, samples_l, samples_r, nb_samples);
2762
            else
2763
                decorr_stereo_pass_id2(dpp, samples_l, samples_r, nb_samples);
2764
        }
2765
    }
2766
2767
13
    bytestream2_put_byte(&pb, WP_ID_DATA | WP_IDF_LONG);
2768
13
    init_put_bits(&s->pb, pb.buffer + 3, bytestream2_get_bytes_left_p(&pb));
2769
13
    if (s->flags & WV_MONO_DATA) {
2770
44101
        for (i = 0; i < nb_samples; i++)
2771
44100
            wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2772
    } else {
2773
264612
        for (i = 0; i < nb_samples; i++) {
2774
264600
            wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2775
264600
            wavpack_encode_sample(s, &s->w.c[1], s->samples[1][i]);
2776
        }
2777
    }
2778
13
    encode_flush(s);
2779
13
    flush_put_bits(&s->pb);
2780
13
    data_size = put_bits_count(&s->pb) >> 3;
2781
13
    bytestream2_put_le24(&pb, (data_size + 1) >> 1);
2782
13
    bytestream2_skip_p(&pb, data_size);
2783
13
    if (data_size & 1)
2784
7
        bytestream2_put_byte(&pb, 0);
2785
2786
13
    if (got_extra) {
2787
        bytestream2_put_byte(&pb, WP_ID_EXTRABITS | WP_IDF_LONG);
2788
        init_put_bits(&s->pb, pb.buffer + 7, bytestream2_get_bytes_left_p(&pb));
2789
        if (s->flags & WV_FLOAT_DATA)
2790
            pack_float(s, s->orig_l, s->orig_r, nb_samples);
2791
        else
2792
            pack_int32(s, s->orig_l, s->orig_r, nb_samples);
2793
        flush_put_bits(&s->pb);
2794
        data_size = put_bits_count(&s->pb) >> 3;
2795
        bytestream2_put_le24(&pb, (data_size + 5) >> 1);
2796
        bytestream2_put_le32(&pb, s->crc_x);
2797
        bytestream2_skip_p(&pb, data_size);
2798
        if (data_size & 1)
2799
            bytestream2_put_byte(&pb, 0);
2800
    }
2801
2802
13
    block_size = bytestream2_tell_p(&pb);
2803
13
    AV_WL32(out + 4, block_size - 8);
2804
2805
13
    av_assert0(!bytestream2_get_eof(&pb));
2806
2807
13
    return block_size;
2808
}
2809
2810
25
static void fill_buffer(WavPackEncodeContext *s,
2811
                        const int8_t *src, int32_t *dst,
2812
                        int nb_samples)
2813
{
2814
    int i;
2815
2816
#define COPY_SAMPLES(type, offset, shift) do {            \
2817
        const type *sptr = (const type *)src;             \
2818
        for (i = 0; i < nb_samples; i++)                  \
2819
            dst[i] = (sptr[i] - offset) >> shift;         \
2820
    } while (0)
2821
2822

25
    switch (s->avctx->sample_fmt) {
2823
    case AV_SAMPLE_FMT_U8P:
2824
        COPY_SAMPLES(int8_t, 0x80, 0);
2825
        break;
2826
25
    case AV_SAMPLE_FMT_S16P:
2827
573325
        COPY_SAMPLES(int16_t, 0, 0);
2828
25
        break;
2829
    case AV_SAMPLE_FMT_S32P:
2830
        if (s->avctx->bits_per_raw_sample <= 24) {
2831
            COPY_SAMPLES(int32_t, 0, 8);
2832
            break;
2833
        }
2834
    case AV_SAMPLE_FMT_FLTP:
2835
        memcpy(dst, src, nb_samples * 4);
2836
    }
2837
25
}
2838
2839
13
static void set_samplerate(WavPackEncodeContext *s)
2840
{
2841
    int i;
2842
2843
130
    for (i = 0; i < 15; i++) {
2844
130
        if (wv_rates[i] == s->avctx->sample_rate)
2845
13
            break;
2846
    }
2847
2848
13
    s->flags = i << SRATE_LSB;
2849
13
}
2850
2851
13
static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2852
                                const AVFrame *frame, int *got_packet_ptr)
2853
{
2854
13
    WavPackEncodeContext *s = avctx->priv_data;
2855
    int buf_size, ret;
2856
    uint8_t *buf;
2857
2858
13
    s->block_samples = frame->nb_samples;
2859
13
    av_fast_padded_malloc(&s->samples[0], &s->samples_size[0],
2860
13
                          sizeof(int32_t) * s->block_samples);
2861
13
    if (!s->samples[0])
2862
        return AVERROR(ENOMEM);
2863
13
    if (avctx->channels > 1) {
2864
12
        av_fast_padded_malloc(&s->samples[1], &s->samples_size[1],
2865
12
                              sizeof(int32_t) * s->block_samples);
2866
12
        if (!s->samples[1])
2867
            return AVERROR(ENOMEM);
2868
    }
2869
2870
13
    buf_size = s->block_samples * avctx->channels * 8
2871
13
             + 200 * avctx->channels /* for headers */;
2872
13
    if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
2873
        return ret;
2874
13
    buf = avpkt->data;
2875
2876
26
    for (s->ch_offset = 0; s->ch_offset < avctx->channels;) {
2877
13
        set_samplerate(s);
2878
2879

13
        switch (s->avctx->sample_fmt) {
2880
13
        case AV_SAMPLE_FMT_S16P: s->flags |= 1; break;
2881
        case AV_SAMPLE_FMT_S32P: s->flags |= 3 - (s->avctx->bits_per_raw_sample <= 24); break;
2882
        case AV_SAMPLE_FMT_FLTP: s->flags |= 3 | WV_FLOAT_DATA;
2883
        }
2884
2885
13
        fill_buffer(s, frame->extended_data[s->ch_offset], s->samples[0], s->block_samples);
2886
13
        if (avctx->channels - s->ch_offset == 1) {
2887
1
            s->flags |= WV_MONO;
2888
        } else {
2889
12
            s->flags |= WV_CROSS_DECORR;
2890
12
            fill_buffer(s, frame->extended_data[s->ch_offset + 1], s->samples[1], s->block_samples);
2891
        }
2892
2893
13
        s->flags += (1 << MAG_LSB) * ((s->flags & 3) * 8 + 7);
2894
2895
13
        if ((ret = wavpack_encode_block(s, s->samples[0], s->samples[1],
2896
                                        buf, buf_size)) < 0)
2897
            return ret;
2898
2899
13
        buf      += ret;
2900
13
        buf_size -= ret;
2901
    }
2902
13
    s->sample_index += frame->nb_samples;
2903
2904
13
    avpkt->pts      = frame->pts;
2905
13
    avpkt->size     = buf - avpkt->data;
2906
13
    avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
2907
13
    *got_packet_ptr = 1;
2908
13
    return 0;
2909
}
2910
2911
2
static av_cold int wavpack_encode_close(AVCodecContext *avctx)
2912
{
2913
2
    WavPackEncodeContext *s = avctx->priv_data;
2914
    int i;
2915
2916
38
    for (i = 0; i < MAX_TERMS + 2; i++) {
2917
36
        av_freep(&s->sampleptrs[i][0]);
2918
36
        av_freep(&s->sampleptrs[i][1]);
2919
36
        s->sampleptrs_size[i][0] = s->sampleptrs_size[i][1] = 0;
2920
    }
2921
2922
6
    for (i = 0; i < 2; i++) {
2923
4
        av_freep(&s->samples[i]);
2924
4
        s->samples_size[i] = 0;
2925
2926
4
        av_freep(&s->best_buffer[i]);
2927
4
        s->best_buffer_size[i] = 0;
2928
2929
4
        av_freep(&s->temp_buffer[i][0]);
2930
4
        av_freep(&s->temp_buffer[i][1]);
2931
4
        s->temp_buffer_size[i][0] = s->temp_buffer_size[i][1] = 0;
2932
    }
2933
2934
2
    av_freep(&s->js_left);
2935
2
    av_freep(&s->js_right);
2936
2
    s->js_left_size = s->js_right_size = 0;
2937
2938
2
    av_freep(&s->orig_l);
2939
2
    av_freep(&s->orig_r);
2940
2
    s->orig_l_size = s->orig_r_size = 0;
2941
2942
2
    return 0;
2943
}
2944
2945
#define OFFSET(x) offsetof(WavPackEncodeContext, x)
2946
#define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2947
static const AVOption options[] = {
2948
    { "joint_stereo",  "", OFFSET(joint), AV_OPT_TYPE_BOOL, {.i64=-1}, -1, 1, FLAGS },
2949
    { "optimize_mono", "", OFFSET(optimize_mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
2950
    { NULL },
2951
};
2952
2953
static const AVClass wavpack_encoder_class = {
2954
    .class_name = "WavPack encoder",
2955
    .item_name  = av_default_item_name,
2956
    .option     = options,
2957
    .version    = LIBAVUTIL_VERSION_INT,
2958
};
2959
2960
AVCodec ff_wavpack_encoder = {
2961
    .name           = "wavpack",
2962
    .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
2963
    .type           = AVMEDIA_TYPE_AUDIO,
2964
    .id             = AV_CODEC_ID_WAVPACK,
2965
    .priv_data_size = sizeof(WavPackEncodeContext),
2966
    .priv_class     = &wavpack_encoder_class,
2967
    .init           = wavpack_encode_init,
2968
    .encode2        = wavpack_encode_frame,
2969
    .close          = wavpack_encode_close,
2970
    .capabilities   = AV_CODEC_CAP_SMALL_LAST_FRAME,
2971
    .sample_fmts    = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8P,
2972
                                                     AV_SAMPLE_FMT_S16P,
2973
                                                     AV_SAMPLE_FMT_S32P,
2974
                                                     AV_SAMPLE_FMT_FLTP,
2975
                                                     AV_SAMPLE_FMT_NONE },
2976
};