FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpackenc.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 792 1766 44.8%
Functions: 25 46 54.3%
Branches: 554 1449 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/channel_layout.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/opt.h"
26 #include "avcodec.h"
27 #include "codec_internal.h"
28 #include "encode.h"
29 #include "put_bits.h"
30 #include "bytestream.h"
31 #include "wavpackenc.h"
32 #include "wavpack.h"
33
34 #define UPDATE_WEIGHT(weight, delta, source, result) \
35 if ((source) && (result)) { \
36 int32_t s = (int32_t) ((source) ^ (result)) >> 31; \
37 weight = ((delta) ^ s) + ((weight) - s); \
38 }
39
40 #define APPLY_WEIGHT_F(weight, sample) ((((((sample) & 0xffff) * (weight)) >> 9) + \
41 ((((sample) & ~0xffff) >> 9) * (weight)) + 1) >> 1)
42
43 #define APPLY_WEIGHT_I(weight, sample) (((weight) * (sample) + 512) >> 10)
44
45 #define APPLY_WEIGHT(weight, sample) ((sample) != (short) (sample) ? \
46 APPLY_WEIGHT_F(weight, sample) : APPLY_WEIGHT_I (weight, sample))
47
48 #define CLEAR(destin) memset(&destin, 0, sizeof(destin));
49
50 #define SHIFT_LSB 13
51 #define SHIFT_MASK (0x1FU << SHIFT_LSB)
52
53 #define MAG_LSB 18
54 #define MAG_MASK (0x1FU << MAG_LSB)
55
56 #define SRATE_LSB 23
57 #define SRATE_MASK (0xFU << SRATE_LSB)
58
59 #define EXTRA_TRY_DELTAS 1
60 #define EXTRA_ADJUST_DELTAS 2
61 #define EXTRA_SORT_FIRST 4
62 #define EXTRA_BRANCHES 8
63 #define EXTRA_SORT_LAST 16
64
65 typedef struct WavPackExtraInfo {
66 struct Decorr dps[MAX_TERMS];
67 int nterms, log_limit, gt16bit;
68 uint32_t best_bits;
69 } WavPackExtraInfo;
70
71 typedef struct WavPackWords {
72 int pend_data, holding_one, zeros_acc;
73 int holding_zero, pend_count;
74 WvChannel c[2];
75 } WavPackWords;
76
77 typedef struct WavPackEncodeContext {
78 AVClass *class;
79 AVCodecContext *avctx;
80 PutBitContext pb;
81 int block_samples;
82 int buffer_size;
83 int sample_index;
84 int stereo, stereo_in;
85 int ch_offset;
86
87 int32_t *samples[2];
88 int samples_size[2];
89
90 int32_t *sampleptrs[MAX_TERMS+2][2];
91 int sampleptrs_size[MAX_TERMS+2][2];
92
93 int32_t *temp_buffer[2][2];
94 int temp_buffer_size[2][2];
95
96 int32_t *best_buffer[2];
97 int best_buffer_size[2];
98
99 int32_t *js_left, *js_right;
100 int js_left_size, js_right_size;
101
102 int32_t *orig_l, *orig_r;
103 int orig_l_size, orig_r_size;
104
105 unsigned extra_flags;
106 int optimize_mono;
107 int decorr_filter;
108 int joint;
109 int num_branches;
110
111 uint32_t flags;
112 uint32_t crc_x;
113 WavPackWords w;
114
115 uint8_t int32_sent_bits, int32_zeros, int32_ones, int32_dups;
116 uint8_t float_flags, float_shift, float_max_exp, max_exp;
117 int32_t shifted_ones, shifted_zeros, shifted_both;
118 int32_t false_zeros, neg_zeros, ordata;
119
120 int num_terms, shift, joint_stereo, false_stereo;
121 int num_decorrs, num_passes, best_decorr, mask_decorr;
122 struct Decorr decorr_passes[MAX_TERMS];
123 const WavPackDecorrSpec *decorr_specs;
124 float delta_decay;
125 } WavPackEncodeContext;
126
127 2 static av_cold int wavpack_encode_init(AVCodecContext *avctx)
128 {
129 2 WavPackEncodeContext *s = avctx->priv_data;
130
131 2 s->avctx = avctx;
132
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->ch_layout.nb_channels > 255) {
134 av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d\n", avctx->ch_layout.nb_channels);
135 return AVERROR(EINVAL);
136 }
137
138
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!avctx->frame_size) {
139 int block_samples;
140
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!(avctx->sample_rate & 1))
141 2 block_samples = avctx->sample_rate / 2;
142 else
143 block_samples = avctx->sample_rate;
144
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 while (block_samples * avctx->ch_layout.nb_channels > WV_MAX_SAMPLES)
146 block_samples /= 2;
147
148
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 while (block_samples * avctx->ch_layout.nb_channels < 40000)
149 1 block_samples *= 2;
150 2 avctx->frame_size = block_samples;
151 } else if (avctx->frame_size && (avctx->frame_size < 128 ||
152 avctx->frame_size > WV_MAX_SAMPLES)) {
153 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n", avctx->frame_size);
154 return AVERROR(EINVAL);
155 }
156
157
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (avctx->compression_level != FF_COMPRESSION_DEFAULT) {
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (avctx->compression_level >= 3) {
159 s->decorr_filter = 3;
160 s->num_passes = 9;
161 if (avctx->compression_level >= 8) {
162 s->num_branches = 4;
163 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_SORT_LAST|EXTRA_BRANCHES;
164 } else if (avctx->compression_level >= 7) {
165 s->num_branches = 3;
166 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
167 } else if (avctx->compression_level >= 6) {
168 s->num_branches = 2;
169 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
170 } else if (avctx->compression_level >= 5) {
171 s->num_branches = 1;
172 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
173 } else if (avctx->compression_level >= 4) {
174 s->num_branches = 1;
175 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_BRANCHES;
176 }
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (avctx->compression_level == 2) {
178 s->decorr_filter = 2;
179 s->num_passes = 4;
180
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (avctx->compression_level == 1) {
181 1 s->decorr_filter = 1;
182 1 s->num_passes = 2;
183 } else if (avctx->compression_level < 1) {
184 s->decorr_filter = 0;
185 s->num_passes = 0;
186 }
187 }
188
189 2 s->num_decorrs = decorr_filter_sizes[s->decorr_filter];
190 2 s->decorr_specs = decorr_filters[s->decorr_filter];
191
192 2 s->delta_decay = 2.0;
193
194 2 return 0;
195 }
196
197 static void shift_mono(int32_t *samples, int nb_samples, int shift)
198 {
199 int i;
200 for (i = 0; i < nb_samples; i++)
201 samples[i] >>= shift;
202 }
203
204 static void shift_stereo(int32_t *left, int32_t *right,
205 int nb_samples, int shift)
206 {
207 int i;
208 for (i = 0; i < nb_samples; i++) {
209 left [i] >>= shift;
210 right[i] >>= shift;
211 }
212 }
213
214 #define FLOAT_SHIFT_ONES 1
215 #define FLOAT_SHIFT_SAME 2
216 #define FLOAT_SHIFT_SENT 4
217 #define FLOAT_ZEROS_SENT 8
218 #define FLOAT_NEG_ZEROS 0x10
219 #define FLOAT_EXCEPTIONS 0x20
220
221 #define get_mantissa(f) ((f) & 0x7fffff)
222 #define get_exponent(f) (((f) >> 23) & 0xff)
223 #define get_sign(f) (((f) >> 31) & 0x1)
224
225 static void process_float(WavPackEncodeContext *s, int32_t *sample)
226 {
227 int32_t shift_count, value, f = *sample;
228
229 if (get_exponent(f) == 255) {
230 s->float_flags |= FLOAT_EXCEPTIONS;
231 value = 0x1000000;
232 shift_count = 0;
233 } else if (get_exponent(f)) {
234 shift_count = s->max_exp - get_exponent(f);
235 value = 0x800000 + get_mantissa(f);
236 } else {
237 shift_count = s->max_exp ? s->max_exp - 1 : 0;
238 value = get_mantissa(f);
239 }
240
241 if (shift_count < 25)
242 value >>= shift_count;
243 else
244 value = 0;
245
246 if (!value) {
247 if (get_exponent(f) || get_mantissa(f))
248 s->false_zeros++;
249 else if (get_sign(f))
250 s->neg_zeros++;
251 } else if (shift_count) {
252 int32_t mask = (1 << shift_count) - 1;
253
254 if (!(get_mantissa(f) & mask))
255 s->shifted_zeros++;
256 else if ((get_mantissa(f) & mask) == mask)
257 s->shifted_ones++;
258 else
259 s->shifted_both++;
260 }
261
262 s->ordata |= value;
263 *sample = get_sign(f) ? -value : value;
264 }
265
266 static int scan_float(WavPackEncodeContext *s,
267 int32_t *samples_l, int32_t *samples_r,
268 int nb_samples)
269 {
270 uint32_t crc = 0xffffffffu;
271 int i;
272
273 s->shifted_ones = s->shifted_zeros = s->shifted_both = s->ordata = 0;
274 s->float_shift = s->float_flags = 0;
275 s->false_zeros = s->neg_zeros = 0;
276 s->max_exp = 0;
277
278 if (s->flags & WV_MONO_DATA) {
279 for (i = 0; i < nb_samples; i++) {
280 int32_t f = samples_l[i];
281 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
282
283 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
284 s->max_exp = get_exponent(f);
285 }
286 } else {
287 for (i = 0; i < nb_samples; i++) {
288 int32_t f;
289
290 f = samples_l[i];
291 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
292 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
293 s->max_exp = get_exponent(f);
294
295 f = samples_r[i];
296 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
297
298 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
299 s->max_exp = get_exponent(f);
300 }
301 }
302
303 s->crc_x = crc;
304
305 if (s->flags & WV_MONO_DATA) {
306 for (i = 0; i < nb_samples; i++)
307 process_float(s, &samples_l[i]);
308 } else {
309 for (i = 0; i < nb_samples; i++) {
310 process_float(s, &samples_l[i]);
311 process_float(s, &samples_r[i]);
312 }
313 }
314
315 s->float_max_exp = s->max_exp;
316
317 if (s->shifted_both)
318 s->float_flags |= FLOAT_SHIFT_SENT;
319 else if (s->shifted_ones && !s->shifted_zeros)
320 s->float_flags |= FLOAT_SHIFT_ONES;
321 else if (s->shifted_ones && s->shifted_zeros)
322 s->float_flags |= FLOAT_SHIFT_SAME;
323 else if (s->ordata && !(s->ordata & 1)) {
324 do {
325 s->float_shift++;
326 s->ordata >>= 1;
327 } while (!(s->ordata & 1));
328
329 if (s->flags & WV_MONO_DATA)
330 shift_mono(samples_l, nb_samples, s->float_shift);
331 else
332 shift_stereo(samples_l, samples_r, nb_samples, s->float_shift);
333 }
334
335 s->flags &= ~MAG_MASK;
336
337 while (s->ordata) {
338 s->flags += 1 << MAG_LSB;
339 s->ordata >>= 1;
340 }
341
342 if (s->false_zeros || s->neg_zeros)
343 s->float_flags |= FLOAT_ZEROS_SENT;
344
345 if (s->neg_zeros)
346 s->float_flags |= FLOAT_NEG_ZEROS;
347
348 return s->float_flags & (FLOAT_EXCEPTIONS | FLOAT_ZEROS_SENT |
349 FLOAT_SHIFT_SENT | FLOAT_SHIFT_SAME);
350 }
351
352 13 static void scan_int23(WavPackEncodeContext *s,
353 int32_t *samples_l, int32_t *samples_r,
354 int nb_samples)
355 {
356 13 uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
357 13 int i, total_shift = 0;
358
359 13 s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
360
361
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (s->flags & WV_MONO_DATA) {
362
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 for (i = 0; i < nb_samples; i++) {
363 9 int32_t M = samples_l[i];
364
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 magdata |= (M < 0) ? ~M : M;
366 9 xordata |= M ^ -(M & 1);
367 9 anddata &= M;
368 9 ordata |= M;
369
370
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
9 if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
371 1 return;
372 }
373 } else {
374
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 for (i = 0; i < nb_samples; i++) {
375 41 int32_t L = samples_l[i];
376 41 int32_t R = samples_r[i];
377
378
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 37 times.
41 magdata |= (L < 0) ? ~L : L;
379
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 37 times.
41 magdata |= (R < 0) ? ~R : R;
380 41 xordata |= L ^ -(L & 1);
381 41 xordata |= R ^ -(R & 1);
382 41 anddata &= L & R;
383 41 ordata |= L | R;
384
385
6/6
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 3 times.
41 if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
386 12 return;
387 }
388 }
389
390 s->flags &= ~MAG_MASK;
391
392 while (magdata) {
393 s->flags += 1 << MAG_LSB;
394 magdata >>= 1;
395 }
396
397 if (!(s->flags & MAG_MASK))
398 return;
399
400 if (!(ordata & 1)) {
401 do {
402 s->flags -= 1 << MAG_LSB;
403 s->int32_zeros++;
404 total_shift++;
405 ordata >>= 1;
406 } while (!(ordata & 1));
407 } else if (anddata & 1) {
408 do {
409 s->flags -= 1 << MAG_LSB;
410 s->int32_ones++;
411 total_shift++;
412 anddata >>= 1;
413 } while (anddata & 1);
414 } else if (!(xordata & 2)) {
415 do {
416 s->flags -= 1 << MAG_LSB;
417 s->int32_dups++;
418 total_shift++;
419 xordata >>= 1;
420 } while (!(xordata & 2));
421 }
422
423 if (total_shift) {
424 s->flags |= WV_INT32_DATA;
425
426 if (s->flags & WV_MONO_DATA)
427 shift_mono(samples_l, nb_samples, total_shift);
428 else
429 shift_stereo(samples_l, samples_r, nb_samples, total_shift);
430 }
431 }
432
433 static int scan_int32(WavPackEncodeContext *s,
434 int32_t *samples_l, int32_t *samples_r,
435 int nb_samples)
436 {
437 uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
438 uint32_t crc = 0xffffffffu;
439 int i, total_shift = 0;
440
441 s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
442
443 if (s->flags & WV_MONO_DATA) {
444 for (i = 0; i < nb_samples; i++) {
445 int32_t M = samples_l[i];
446
447 crc = crc * 9 + (M & 0xffff) * 3 + ((M >> 16) & 0xffff);
448 magdata |= (M < 0) ? ~M : M;
449 xordata |= M ^ -(M & 1);
450 anddata &= M;
451 ordata |= M;
452 }
453 } else {
454 for (i = 0; i < nb_samples; i++) {
455 int32_t L = samples_l[i];
456 int32_t R = samples_r[i];
457
458 crc = crc * 9 + (L & 0xffff) * 3 + ((L >> 16) & 0xffff);
459 crc = crc * 9 + (R & 0xffff) * 3 + ((R >> 16) & 0xffff);
460 magdata |= (L < 0) ? ~L : L;
461 magdata |= (R < 0) ? ~R : R;
462 xordata |= L ^ -(L & 1);
463 xordata |= R ^ -(R & 1);
464 anddata &= L & R;
465 ordata |= L | R;
466 }
467 }
468
469 s->crc_x = crc;
470 s->flags &= ~MAG_MASK;
471
472 while (magdata) {
473 s->flags += 1 << MAG_LSB;
474 magdata >>= 1;
475 }
476
477 if (!((s->flags & MAG_MASK) >> MAG_LSB)) {
478 s->flags &= ~WV_INT32_DATA;
479 return 0;
480 }
481
482 if (!(ordata & 1))
483 do {
484 s->flags -= 1 << MAG_LSB;
485 s->int32_zeros++;
486 total_shift++;
487 ordata >>= 1;
488 } while (!(ordata & 1));
489 else if (anddata & 1)
490 do {
491 s->flags -= 1 << MAG_LSB;
492 s->int32_ones++;
493 total_shift++;
494 anddata >>= 1;
495 } while (anddata & 1);
496 else if (!(xordata & 2))
497 do {
498 s->flags -= 1 << MAG_LSB;
499 s->int32_dups++;
500 total_shift++;
501 xordata >>= 1;
502 } while (!(xordata & 2));
503
504 if (((s->flags & MAG_MASK) >> MAG_LSB) > 23) {
505 s->int32_sent_bits = (uint8_t)(((s->flags & MAG_MASK) >> MAG_LSB) - 23);
506 total_shift += s->int32_sent_bits;
507 s->flags &= ~MAG_MASK;
508 s->flags += 23 << MAG_LSB;
509 }
510
511 if (total_shift) {
512 s->flags |= WV_INT32_DATA;
513
514 if (s->flags & WV_MONO_DATA)
515 shift_mono(samples_l, nb_samples, total_shift);
516 else
517 shift_stereo(samples_l, samples_r, nb_samples, total_shift);
518 }
519
520 return s->int32_sent_bits;
521 }
522
523 624 static int8_t store_weight(int weight)
524 {
525 624 weight = av_clip(weight, -1024, 1024);
526
2/2
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 357 times.
624 if (weight > 0)
527 267 weight -= (weight + 64) >> 7;
528
529 624 return (weight + 4) >> 3;
530 }
531
532 606 static int restore_weight(int8_t weight)
533 {
534 606 int result = 8 * weight;
535
536
2/2
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 354 times.
606 if (result > 0)
537 252 result += (result + 64) >> 7;
538
539 606 return result;
540 }
541
542 3922 static int log2s(int32_t value)
543 {
544
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3907 times.
3922 return (value < 0) ? -wp_log2(-value) : wp_log2(value);
545 }
546
547 4 static void decorr_mono(int32_t *in_samples, int32_t *out_samples,
548 int nb_samples, struct Decorr *dpp, int dir)
549 {
550 4 int m = 0, i;
551
552 4 dpp->sumA = 0;
553
554
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (dir < 0) {
555 2 out_samples += (nb_samples - 1);
556 2 in_samples += (nb_samples - 1);
557 }
558
559 4 dpp->weightA = restore_weight(store_weight(dpp->weightA));
560
561
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 for (i = 0; i < MAX_TERM; i++)
562 32 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
563
564
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (dpp->value > MAX_TERM) {
565
2/2
✓ Branch 0 taken 92296 times.
✓ Branch 1 taken 4 times.
92300 while (nb_samples--) {
566 int32_t left, sam_A;
567
568 92296 sam_A = ((3 - (dpp->value & 1)) * dpp->samplesA[0] - dpp->samplesA[1]) >> !(dpp->value & 1);
569
570 92296 dpp->samplesA[1] = dpp->samplesA[0];
571 92296 dpp->samplesA[0] = left = in_samples[0];
572
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 92296 times.
92296 left -= APPLY_WEIGHT(dpp->weightA, sam_A);
574
4/4
✓ Branch 0 taken 92286 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 92236 times.
✓ Branch 3 taken 50 times.
92296 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
575 92296 dpp->sumA += dpp->weightA;
576 92296 out_samples[0] = left;
577 92296 in_samples += dir;
578 92296 out_samples += dir;
579 }
580 } else if (dpp->value > 0) {
581 while (nb_samples--) {
582 int k = (m + dpp->value) & (MAX_TERM - 1);
583 int32_t left, sam_A;
584
585 sam_A = dpp->samplesA[m];
586 dpp->samplesA[k] = left = in_samples[0];
587 m = (m + 1) & (MAX_TERM - 1);
588
589 left -= APPLY_WEIGHT(dpp->weightA, sam_A);
590 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
591 dpp->sumA += dpp->weightA;
592 out_samples[0] = left;
593 in_samples += dir;
594 out_samples += dir;
595 }
596 }
597
598
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 if (m && dpp->value > 0 && dpp->value <= MAX_TERM) {
599 int32_t temp_A[MAX_TERM];
600
601 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
602
603 for (i = 0; i < MAX_TERM; i++) {
604 dpp->samplesA[i] = temp_A[m];
605 m = (m + 1) & (MAX_TERM - 1);
606 }
607 }
608 4 }
609
610 1 static void reverse_mono_decorr(struct Decorr *dpp)
611 {
612
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (dpp->value > MAX_TERM) {
613 int32_t sam_A;
614
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dpp->value & 1)
616 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
617 else
618 1 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
619
620 1 dpp->samplesA[1] = dpp->samplesA[0];
621 1 dpp->samplesA[0] = sam_A;
622
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dpp->value & 1)
624 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
625 else
626 1 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
627
628 1 dpp->samplesA[1] = sam_A;
629 } else if (dpp->value > 1) {
630 int i, j, k;
631
632 for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
633 i &= (MAX_TERM - 1);
634 j &= (MAX_TERM - 1);
635 dpp->samplesA[i] ^= dpp->samplesA[j];
636 dpp->samplesA[j] ^= dpp->samplesA[i];
637 dpp->samplesA[i] ^= dpp->samplesA[j];
638 }
639 }
640 1 }
641
642 #define count_bits(av) ((av) ? 32 - ff_clz(av) : 0)
643
644 1102500 static uint32_t log2sample(uint32_t v, int limit, uint32_t *result)
645 {
646
2/2
✓ Branch 0 taken 773442 times.
✓ Branch 1 taken 329058 times.
1102500 uint32_t dbits = count_bits(v);
647
648
2/2
✓ Branch 0 taken 959899 times.
✓ Branch 1 taken 142601 times.
1102500 if ((v += v >> 9) < (1 << 8)) {
649 959899 *result += (dbits << 8) + ff_wp_log2_table[(v << (9 - dbits)) & 0xff];
650 } else {
651 142601 *result += dbits = (dbits << 8) + ff_wp_log2_table[(v >> (dbits - 9)) & 0xff];
652
653
2/4
✓ Branch 0 taken 142601 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 142601 times.
142601 if (limit && dbits >= limit)
654 return 1;
655 }
656
657 1102500 return 0;
658 }
659
660 1 static uint32_t log2mono(int32_t *samples, int nb_samples, int limit)
661 {
662 1 uint32_t result = 0;
663
2/2
✓ Branch 0 taken 44100 times.
✓ Branch 1 taken 1 times.
44101 while (nb_samples--) {
664
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 44100 times.
44100 if (log2sample(abs(*samples++), limit, &result))
665 return UINT32_MAX;
666 }
667 1 return result;
668 }
669
670 24 static uint32_t log2stereo(int32_t *samples_l, int32_t *samples_r,
671 int nb_samples, int limit)
672 {
673 24 uint32_t result = 0;
674
2/2
✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 24 times.
529224 while (nb_samples--) {
675
2/4
✓ Branch 1 taken 529200 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 529200 times.
1058400 if (log2sample(abs(*samples_l++), limit, &result) ||
676 529200 log2sample(abs(*samples_r++), limit, &result))
677 return UINT32_MAX;
678 }
679 24 return result;
680 }
681
682 static void decorr_mono_buffer(int32_t *samples, int32_t *outsamples,
683 int nb_samples, struct Decorr *dpp,
684 int tindex)
685 {
686 struct Decorr dp, *dppi = dpp + tindex;
687 int delta = dppi->delta, pre_delta, term = dppi->value;
688
689 if (delta == 7)
690 pre_delta = 7;
691 else if (delta < 2)
692 pre_delta = 3;
693 else
694 pre_delta = delta + 1;
695
696 CLEAR(dp);
697 dp.value = term;
698 dp.delta = pre_delta;
699 decorr_mono(samples, outsamples, FFMIN(2048, nb_samples), &dp, -1);
700 dp.delta = delta;
701
702 if (tindex == 0)
703 reverse_mono_decorr(&dp);
704 else
705 CLEAR(dp.samplesA);
706
707 memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
708 dppi->weightA = dp.weightA;
709
710 if (delta == 0) {
711 dp.delta = 1;
712 decorr_mono(samples, outsamples, nb_samples, &dp, 1);
713 dp.delta = 0;
714 memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
715 dppi->weightA = dp.weightA = dp.sumA / nb_samples;
716 }
717
718 decorr_mono(samples, outsamples, nb_samples, &dp, 1);
719 }
720
721 static void recurse_mono(WavPackEncodeContext *s, WavPackExtraInfo *info,
722 int depth, int delta, uint32_t input_bits)
723 {
724 int term, branches = s->num_branches - depth;
725 int32_t *samples, *outsamples;
726 uint32_t term_bits[22], bits;
727
728 if (branches < 1 || depth + 1 == info->nterms)
729 branches = 1;
730
731 CLEAR(term_bits);
732 samples = s->sampleptrs[depth][0];
733 outsamples = s->sampleptrs[depth + 1][0];
734
735 for (term = 1; term <= 18; term++) {
736 if (term == 17 && branches == 1 && depth + 1 < info->nterms)
737 continue;
738
739 if (term > 8 && term < 17)
740 continue;
741
742 if (!s->extra_flags && (term > 4 && term < 17))
743 continue;
744
745 info->dps[depth].value = term;
746 info->dps[depth].delta = delta;
747 decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
748 bits = log2mono(outsamples, s->block_samples, info->log_limit);
749
750 if (bits < info->best_bits) {
751 info->best_bits = bits;
752 CLEAR(s->decorr_passes);
753 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
754 memcpy(s->sampleptrs[info->nterms + 1][0],
755 s->sampleptrs[depth + 1][0], s->block_samples * 4);
756 }
757
758 term_bits[term + 3] = bits;
759 }
760
761 while (depth + 1 < info->nterms && branches--) {
762 uint32_t local_best_bits = input_bits;
763 int best_term = 0, i;
764
765 for (i = 0; i < 22; i++)
766 if (term_bits[i] && term_bits[i] < local_best_bits) {
767 local_best_bits = term_bits[i];
768 best_term = i - 3;
769 }
770
771 if (!best_term)
772 break;
773
774 term_bits[best_term + 3] = 0;
775
776 info->dps[depth].value = best_term;
777 info->dps[depth].delta = delta;
778 decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
779
780 recurse_mono(s, info, depth + 1, delta, local_best_bits);
781 }
782 }
783
784 static void sort_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
785 {
786 int reversed = 1;
787 uint32_t bits;
788
789 while (reversed) {
790 int ri, i;
791
792 memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
793 reversed = 0;
794
795 for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
796
797 if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
798 break;
799
800 if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
801 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
802 s->block_samples, info->dps, ri);
803 continue;
804 }
805
806 info->dps[ri ] = s->decorr_passes[ri+1];
807 info->dps[ri+1] = s->decorr_passes[ri ];
808
809 for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
810 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
811 s->block_samples, info->dps, i);
812
813 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
814 if (bits < info->best_bits) {
815 reversed = 1;
816 info->best_bits = bits;
817 CLEAR(s->decorr_passes);
818 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
819 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
820 s->block_samples * 4);
821 } else {
822 info->dps[ri ] = s->decorr_passes[ri];
823 info->dps[ri+1] = s->decorr_passes[ri+1];
824 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
825 s->block_samples, info->dps, ri);
826 }
827 }
828 }
829 }
830
831 static void delta_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
832 {
833 int lower = 0, delta, d;
834 uint32_t bits;
835
836 if (!s->decorr_passes[0].value)
837 return;
838 delta = s->decorr_passes[0].delta;
839
840 for (d = delta - 1; d >= 0; d--) {
841 int i;
842
843 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
844 info->dps[i].value = s->decorr_passes[i].value;
845 info->dps[i].delta = d;
846 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
847 s->block_samples, info->dps, i);
848 }
849
850 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
851 if (bits >= info->best_bits)
852 break;
853
854 lower = 1;
855 info->best_bits = bits;
856 CLEAR(s->decorr_passes);
857 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
858 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
859 s->block_samples * 4);
860 }
861
862 for (d = delta + 1; !lower && d <= 7; d++) {
863 int i;
864
865 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
866 info->dps[i].value = s->decorr_passes[i].value;
867 info->dps[i].delta = d;
868 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
869 s->block_samples, info->dps, i);
870 }
871
872 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
873 if (bits >= info->best_bits)
874 break;
875
876 info->best_bits = bits;
877 CLEAR(s->decorr_passes);
878 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
879 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
880 s->block_samples * 4);
881 }
882 }
883
884 static int allocate_buffers2(WavPackEncodeContext *s, int nterms)
885 {
886 int i;
887
888 for (i = 0; i < nterms + 2; i++) {
889 av_fast_padded_malloc(&s->sampleptrs[i][0], &s->sampleptrs_size[i][0],
890 s->block_samples * 4);
891 if (!s->sampleptrs[i][0])
892 return AVERROR(ENOMEM);
893 if (!(s->flags & WV_MONO_DATA)) {
894 av_fast_padded_malloc(&s->sampleptrs[i][1], &s->sampleptrs_size[i][1],
895 s->block_samples * 4);
896 if (!s->sampleptrs[i][1])
897 return AVERROR(ENOMEM);
898 }
899 }
900
901 return 0;
902 }
903
904 13 static int allocate_buffers(WavPackEncodeContext *s)
905 {
906 int i;
907
908
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 13 times.
39 for (i = 0; i < 2; i++) {
909 26 av_fast_padded_malloc(&s->best_buffer[0], &s->best_buffer_size[0],
910 26 s->block_samples * 4);
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!s->best_buffer[0])
912 return AVERROR(ENOMEM);
913
914 26 av_fast_padded_malloc(&s->temp_buffer[i][0], &s->temp_buffer_size[i][0],
915 26 s->block_samples * 4);
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (!s->temp_buffer[i][0])
917 return AVERROR(ENOMEM);
918
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2 times.
26 if (!(s->flags & WV_MONO_DATA)) {
919 24 av_fast_padded_malloc(&s->best_buffer[1], &s->best_buffer_size[1],
920 24 s->block_samples * 4);
921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!s->best_buffer[1])
922 return AVERROR(ENOMEM);
923
924 24 av_fast_padded_malloc(&s->temp_buffer[i][1], &s->temp_buffer_size[i][1],
925 24 s->block_samples * 4);
926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!s->temp_buffer[i][1])
927 return AVERROR(ENOMEM);
928 }
929 }
930
931 13 return 0;
932 }
933
934 static void analyze_mono(WavPackEncodeContext *s, int32_t *samples, int do_samples)
935 {
936 WavPackExtraInfo info;
937 int i;
938
939 info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
940 info.log_limit = FFMIN(6912, info.log_limit);
941
942 info.nterms = s->num_terms;
943
944 if (allocate_buffers2(s, s->num_terms))
945 return;
946
947 memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
948 memcpy(s->sampleptrs[0][0], samples, s->block_samples * 4);
949
950 for (i = 0; i < info.nterms && info.dps[i].value; i++)
951 decorr_mono(s->sampleptrs[i][0], s->sampleptrs[i + 1][0],
952 s->block_samples, info.dps + i, 1);
953
954 info.best_bits = log2mono(s->sampleptrs[info.nterms][0], s->block_samples, 0) * 1;
955 memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
956
957 if (s->extra_flags & EXTRA_BRANCHES)
958 recurse_mono(s, &info, 0, (int) floor(s->delta_decay + 0.5),
959 log2mono(s->sampleptrs[0][0], s->block_samples, 0));
960
961 if (s->extra_flags & EXTRA_SORT_FIRST)
962 sort_mono(s, &info);
963
964 if (s->extra_flags & EXTRA_TRY_DELTAS) {
965 delta_mono(s, &info);
966
967 if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
968 s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
969 else
970 s->delta_decay = 2.0;
971 }
972
973 if (s->extra_flags & EXTRA_SORT_LAST)
974 sort_mono(s, &info);
975
976 if (do_samples)
977 memcpy(samples, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
978
979 for (i = 0; i < info.nterms; i++)
980 if (!s->decorr_passes[i].value)
981 break;
982
983 s->num_terms = i;
984 }
985
986 3 static void scan_word(WavPackEncodeContext *s, WvChannel *c,
987 int32_t *samples, int nb_samples, int dir)
988 {
989
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (dir < 0)
990 3 samples += nb_samples - 1;
991
992
2/2
✓ Branch 0 taken 88200 times.
✓ Branch 1 taken 3 times.
88203 while (nb_samples--) {
993 88200 uint32_t low, value = labs(samples[0]);
994
995
2/2
✓ Branch 0 taken 68946 times.
✓ Branch 1 taken 19254 times.
88200 if (value < GET_MED(0)) {
996 68946 DEC_MED(0);
997 } else {
998 19254 low = GET_MED(0);
999 19254 INC_MED(0);
1000
1001
2/2
✓ Branch 0 taken 13482 times.
✓ Branch 1 taken 5772 times.
19254 if (value - low < GET_MED(1)) {
1002 13482 DEC_MED(1);
1003 } else {
1004 5772 low += GET_MED(1);
1005 5772 INC_MED(1);
1006
1007
2/2
✓ Branch 0 taken 3975 times.
✓ Branch 1 taken 1797 times.
5772 if (value - low < GET_MED(2)) {
1008 3975 DEC_MED(2);
1009 } else {
1010 1797 INC_MED(2);
1011 }
1012 }
1013 }
1014 88200 samples += dir;
1015 }
1016 3 }
1017
1018 1 static int wv_mono(WavPackEncodeContext *s, int32_t *samples,
1019 int no_history, int do_samples)
1020 {
1021 1 struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1022 1 int nb_samples = s->block_samples;
1023 1 int buf_size = sizeof(int32_t) * nb_samples;
1024 1 uint32_t best_size = UINT32_MAX, size;
1025 int log_limit, pi, i, ret;
1026
1027
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (i = 0; i < nb_samples; i++)
1028
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (samples[i])
1029 1 break;
1030
1031
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (i == nb_samples) {
1032 CLEAR(s->decorr_passes);
1033 CLEAR(s->w);
1034 s->num_terms = 0;
1035 return 0;
1036 }
1037
1038 1 log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1039 1 log_limit = FFMIN(6912, log_limit);
1040
1041
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = allocate_buffers(s)) < 0)
1042 return ret;
1043
1044
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (no_history || s->num_passes >= 7)
1045 1 s->best_decorr = s->mask_decorr = 0;
1046
1047
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (pi = 0; pi < s->num_passes;) {
1048 const WavPackDecorrSpec *wpds;
1049 int nterms, c, j;
1050
1051
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!pi) {
1052 1 c = s->best_decorr;
1053 } else {
1054 if (s->mask_decorr == 0)
1055 c = 0;
1056 else
1057 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1058
1059 if (c == s->best_decorr) {
1060 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1061 continue;
1062 }
1063 }
1064
1065 1 wpds = &s->decorr_specs[c];
1066 1 nterms = decorr_filter_nterms[s->decorr_filter];
1067
1068 while (1) {
1069 1 memcpy(s->temp_buffer[0][0], samples, buf_size);
1070 1 CLEAR(save_decorr_passes);
1071
1072
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (j = 0; j < nterms; j++) {
1073 2 CLEAR(temp_decorr_pass);
1074 2 temp_decorr_pass.delta = wpds->delta;
1075 2 temp_decorr_pass.value = wpds->terms[j];
1076
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (temp_decorr_pass.value < 0)
1078 temp_decorr_pass.value = 1;
1079
1080 2 decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1081 FFMIN(nb_samples, 2048), &temp_decorr_pass, -1);
1082
1083
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (j) {
1084 1 CLEAR(temp_decorr_pass.samplesA);
1085 } else {
1086 1 reverse_mono_decorr(&temp_decorr_pass);
1087 }
1088
1089 2 memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1090 2 decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1091 nb_samples, &temp_decorr_pass, 1);
1092 }
1093
1094 1 size = log2mono(s->temp_buffer[j&1][0], nb_samples, log_limit);
1095
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (size != UINT32_MAX || !nterms)
1096 break;
1097 nterms >>= 1;
1098 }
1099
1100
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (size < best_size) {
1101 1 memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1102 1 memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1103 1 s->num_terms = nterms;
1104 1 s->best_decorr = c;
1105 1 best_size = size;
1106 }
1107
1108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pi++)
1109 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1110 }
1111
1112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->extra_flags)
1113 analyze_mono(s, samples, do_samples);
1114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 else if (do_samples)
1115 memcpy(samples, s->best_buffer[0], buf_size);
1116
1117
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (no_history || s->extra_flags) {
1118 1 CLEAR(s->w);
1119 1 scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1120 }
1121 1 return 0;
1122 }
1123
1124 120 static void decorr_stereo(int32_t *in_left, int32_t *in_right,
1125 int32_t *out_left, int32_t *out_right,
1126 int nb_samples, struct Decorr *dpp, int dir)
1127 {
1128 120 int m = 0, i;
1129
1130 120 dpp->sumA = dpp->sumB = 0;
1131
1132
1/2
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
120 if (dir < 0) {
1133 120 out_left += nb_samples - 1;
1134 120 out_right += nb_samples - 1;
1135 120 in_left += nb_samples - 1;
1136 120 in_right += nb_samples - 1;
1137 }
1138
1139 120 dpp->weightA = restore_weight(store_weight(dpp->weightA));
1140 120 dpp->weightB = restore_weight(store_weight(dpp->weightB));
1141
1142
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 120 times.
1080 for (i = 0; i < MAX_TERM; i++) {
1143 960 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1144 960 dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1145 }
1146
1147
7/7
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 1 times.
120 switch (dpp->value) {
1148 21 case 2:
1149
2/2
✓ Branch 0 taken 43008 times.
✓ Branch 1 taken 21 times.
43029 while (nb_samples--) {
1150 int32_t sam, tmp;
1151
1152 43008 sam = dpp->samplesA[0];
1153 43008 dpp->samplesA[0] = dpp->samplesA[1];
1154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43008 times.
43008 out_left[0] = tmp = (dpp->samplesA[1] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1155
4/4
✓ Branch 0 taken 24962 times.
✓ Branch 1 taken 18046 times.
✓ Branch 2 taken 23419 times.
✓ Branch 3 taken 1543 times.
43008 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1156 43008 dpp->sumA += dpp->weightA;
1157
1158 43008 sam = dpp->samplesB[0];
1159 43008 dpp->samplesB[0] = dpp->samplesB[1];
1160
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 42976 times.
43008 out_right[0] = tmp = (dpp->samplesB[1] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1161
4/4
✓ Branch 0 taken 36818 times.
✓ Branch 1 taken 6190 times.
✓ Branch 2 taken 34741 times.
✓ Branch 3 taken 2077 times.
43008 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1162 43008 dpp->sumB += dpp->weightB;
1163
1164 43008 in_left += dir;
1165 43008 out_left += dir;
1166 43008 in_right += dir;
1167 43008 out_right += dir;
1168 }
1169 21 break;
1170 40 case 17:
1171
2/2
✓ Branch 0 taken 81920 times.
✓ Branch 1 taken 40 times.
81960 while (nb_samples--) {
1172 int32_t sam, tmp;
1173
1174 81920 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1175 81920 dpp->samplesA[1] = dpp->samplesA[0];
1176
2/2
✓ Branch 0 taken 3023 times.
✓ Branch 1 taken 78897 times.
81920 out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1177
4/4
✓ Branch 0 taken 64957 times.
✓ Branch 1 taken 16963 times.
✓ Branch 2 taken 62571 times.
✓ Branch 3 taken 2386 times.
81920 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1178 81920 dpp->sumA += dpp->weightA;
1179
1180 81920 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1181 81920 dpp->samplesB[1] = dpp->samplesB[0];
1182
2/2
✓ Branch 0 taken 4028 times.
✓ Branch 1 taken 77892 times.
81920 out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT (dpp->weightB, sam);
1183
4/4
✓ Branch 0 taken 80670 times.
✓ Branch 1 taken 1250 times.
✓ Branch 2 taken 76508 times.
✓ Branch 3 taken 4162 times.
81920 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1184 81920 dpp->sumB += dpp->weightB;
1185
1186 81920 in_left += dir;
1187 81920 out_left += dir;
1188 81920 in_right += dir;
1189 81920 out_right += dir;
1190 }
1191 40 break;
1192 25 case 18:
1193
2/2
✓ Branch 0 taken 51200 times.
✓ Branch 1 taken 25 times.
51225 while (nb_samples--) {
1194 int32_t sam, tmp;
1195
1196 51200 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1197 51200 dpp->samplesA[1] = dpp->samplesA[0];
1198
2/2
✓ Branch 0 taken 671 times.
✓ Branch 1 taken 50529 times.
51200 out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1199
4/4
✓ Branch 0 taken 40226 times.
✓ Branch 1 taken 10974 times.
✓ Branch 2 taken 39108 times.
✓ Branch 3 taken 1118 times.
51200 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1200 51200 dpp->sumA += dpp->weightA;
1201
1202 51200 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1203 51200 dpp->samplesB[1] = dpp->samplesB[0];
1204
2/2
✓ Branch 0 taken 671 times.
✓ Branch 1 taken 50529 times.
51200 out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1205
4/4
✓ Branch 0 taken 49966 times.
✓ Branch 1 taken 1234 times.
✓ Branch 2 taken 48092 times.
✓ Branch 3 taken 1874 times.
51200 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1206 51200 dpp->sumB += dpp->weightB;
1207
1208 51200 in_left += dir;
1209 51200 out_left += dir;
1210 51200 in_right += dir;
1211 51200 out_right += dir;
1212 }
1213 25 break;
1214 17 default: {
1215 17 int k = dpp->value & (MAX_TERM - 1);
1216
1217
2/2
✓ Branch 0 taken 34816 times.
✓ Branch 1 taken 17 times.
34833 while (nb_samples--) {
1218 int32_t sam, tmp;
1219
1220 34816 sam = dpp->samplesA[m];
1221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34816 times.
34816 out_left[0] = tmp = (dpp->samplesA[k] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1222
4/4
✓ Branch 0 taken 22208 times.
✓ Branch 1 taken 12608 times.
✓ Branch 2 taken 20184 times.
✓ Branch 3 taken 2024 times.
34816 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1223 34816 dpp->sumA += dpp->weightA;
1224
1225 34816 sam = dpp->samplesB[m];
1226
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 34761 times.
34816 out_right[0] = tmp = (dpp->samplesB[k] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1227
4/4
✓ Branch 0 taken 28242 times.
✓ Branch 1 taken 6574 times.
✓ Branch 2 taken 26274 times.
✓ Branch 3 taken 1968 times.
34816 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1228 34816 dpp->sumB += dpp->weightB;
1229
1230 34816 in_left += dir;
1231 34816 out_left += dir;
1232 34816 in_right += dir;
1233 34816 out_right += dir;
1234 34816 m = (m + 1) & (MAX_TERM - 1);
1235 34816 k = (k + 1) & (MAX_TERM - 1);
1236 }
1237
1238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (m) {
1239 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1240 int k;
1241
1242 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1243 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1244
1245 for (k = 0; k < MAX_TERM; k++) {
1246 dpp->samplesA[k] = temp_A[m];
1247 dpp->samplesB[k] = temp_B[m];
1248 m = (m + 1) & (MAX_TERM - 1);
1249 }
1250 }
1251 17 break;
1252 }
1253 3 case -1:
1254
2/2
✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 3 times.
6147 while (nb_samples--) {
1255 int32_t sam_A, sam_B, tmp;
1256
1257 6144 sam_A = dpp->samplesA[0];
1258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6144 times.
6144 out_left[0] = tmp = (sam_B = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1259
8/10
✓ Branch 0 taken 5896 times.
✓ Branch 1 taken 248 times.
✓ Branch 2 taken 5359 times.
✓ Branch 3 taken 537 times.
✓ Branch 4 taken 2246 times.
✓ Branch 5 taken 3113 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2246 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3113 times.
6144 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1260 6144 dpp->sumA += dpp->weightA;
1261
1262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6144 times.
6144 out_right[0] = tmp = (dpp->samplesA[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1263
8/10
✓ Branch 0 taken 5706 times.
✓ Branch 1 taken 438 times.
✓ Branch 2 taken 2685 times.
✓ Branch 3 taken 3021 times.
✓ Branch 4 taken 750 times.
✓ Branch 5 taken 1935 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 750 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1935 times.
6144 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1264 6144 dpp->sumB += dpp->weightB;
1265
1266 6144 in_left += dir;
1267 6144 out_left += dir;
1268 6144 in_right += dir;
1269 6144 out_right += dir;
1270 }
1271 3 break;
1272 13 case -2:
1273
2/2
✓ Branch 0 taken 26624 times.
✓ Branch 1 taken 13 times.
26637 while (nb_samples--) {
1274 int32_t sam_A, sam_B, tmp;
1275
1276 26624 sam_B = dpp->samplesB[0];
1277
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 26594 times.
26624 out_right[0] = tmp = (sam_A = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1278
8/10
✓ Branch 0 taken 26025 times.
✓ Branch 1 taken 599 times.
✓ Branch 2 taken 25110 times.
✓ Branch 3 taken 915 times.
✓ Branch 4 taken 11467 times.
✓ Branch 5 taken 13643 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 11467 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 13643 times.
26624 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1279 26624 dpp->sumB += dpp->weightB;
1280
1281
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 26594 times.
26624 out_left[0] = tmp = (dpp->samplesB[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1282
9/10
✓ Branch 0 taken 26027 times.
✓ Branch 1 taken 597 times.
✓ Branch 2 taken 16895 times.
✓ Branch 3 taken 9132 times.
✓ Branch 4 taken 3818 times.
✓ Branch 5 taken 13077 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 3818 times.
✓ Branch 8 taken 920 times.
✓ Branch 9 taken 12157 times.
26624 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1283 26624 dpp->sumA += dpp->weightA;
1284
1285 26624 in_left += dir;
1286 26624 out_left += dir;
1287 26624 in_right += dir;
1288 26624 out_right += dir;
1289 }
1290 13 break;
1291 1 case -3:
1292
2/2
✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 1 times.
2049 while (nb_samples--) {
1293 int32_t sam_A, sam_B, tmp;
1294
1295 2048 sam_A = dpp->samplesA[0];
1296 2048 sam_B = dpp->samplesB[0];
1297
1298 2048 dpp->samplesA[0] = tmp = in_right[0];
1299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
2048 out_right[0] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
1300
8/10
✓ Branch 0 taken 2026 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 1938 times.
✓ Branch 3 taken 88 times.
✓ Branch 4 taken 976 times.
✓ Branch 5 taken 962 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 976 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 962 times.
2048 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1301 2048 dpp->sumB += dpp->weightB;
1302
1303 2048 dpp->samplesB[0] = tmp = in_left[0];
1304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2048 times.
2048 out_left[0] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
1305
8/10
✓ Branch 0 taken 1952 times.
✓ Branch 1 taken 96 times.
✓ Branch 2 taken 1932 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 955 times.
✓ Branch 5 taken 977 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 955 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 977 times.
2048 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1306 2048 dpp->sumA += dpp->weightA;
1307
1308 2048 in_left += dir;
1309 2048 out_left += dir;
1310 2048 in_right += dir;
1311 2048 out_right += dir;
1312 }
1313 1 break;
1314 }
1315 120 }
1316
1317 24 static void reverse_decorr(struct Decorr *dpp)
1318 {
1319
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 1 times.
24 if (dpp->value > MAX_TERM) {
1320 int32_t sam_A, sam_B;
1321
1322
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
23 if (dpp->value & 1) {
1323 12 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1324 12 sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1325 } else {
1326 11 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1327 11 sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1328 }
1329
1330 23 dpp->samplesA[1] = dpp->samplesA[0];
1331 23 dpp->samplesB[1] = dpp->samplesB[0];
1332 23 dpp->samplesA[0] = sam_A;
1333 23 dpp->samplesB[0] = sam_B;
1334
1335
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 11 times.
23 if (dpp->value & 1) {
1336 12 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1337 12 sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1338 } else {
1339 11 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1340 11 sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1341 }
1342
1343 23 dpp->samplesA[1] = sam_A;
1344 23 dpp->samplesB[1] = sam_B;
1345
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (dpp->value > 1) {
1346 int i, j, k;
1347
1348
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
1349 1 i &= (MAX_TERM - 1);
1350 1 j &= (MAX_TERM - 1);
1351 1 dpp->samplesA[i] ^= dpp->samplesA[j];
1352 1 dpp->samplesA[j] ^= dpp->samplesA[i];
1353 1 dpp->samplesA[i] ^= dpp->samplesA[j];
1354 1 dpp->samplesB[i] ^= dpp->samplesB[j];
1355 1 dpp->samplesB[j] ^= dpp->samplesB[i];
1356 1 dpp->samplesB[i] ^= dpp->samplesB[j];
1357 }
1358 }
1359 24 }
1360
1361 120 static void decorr_stereo_quick(int32_t *in_left, int32_t *in_right,
1362 int32_t *out_left, int32_t *out_right,
1363 int nb_samples, struct Decorr *dpp)
1364 {
1365 120 int m = 0, i;
1366
1367 120 dpp->weightA = restore_weight(store_weight(dpp->weightA));
1368 120 dpp->weightB = restore_weight(store_weight(dpp->weightB));
1369
1370
2/2
✓ Branch 0 taken 960 times.
✓ Branch 1 taken 120 times.
1080 for (i = 0; i < MAX_TERM; i++) {
1371 960 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1372 960 dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1373 }
1374
1375
7/7
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 13 times.
✓ Branch 6 taken 1 times.
120 switch (dpp->value) {
1376 21 case 2:
1377
2/2
✓ Branch 0 taken 463050 times.
✓ Branch 1 taken 21 times.
463071 for (i = 0; i < nb_samples; i++) {
1378 int32_t sam, tmp;
1379
1380 463050 sam = dpp->samplesA[0];
1381 463050 dpp->samplesA[0] = dpp->samplesA[1];
1382 463050 out_left[i] = tmp = (dpp->samplesA[1] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1383
4/4
✓ Branch 0 taken 263976 times.
✓ Branch 1 taken 199074 times.
✓ Branch 2 taken 242436 times.
✓ Branch 3 taken 21540 times.
463050 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1384
1385 463050 sam = dpp->samplesB[0];
1386 463050 dpp->samplesB[0] = dpp->samplesB[1];
1387 463050 out_right[i] = tmp = (dpp->samplesB[1] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1388
4/4
✓ Branch 0 taken 400796 times.
✓ Branch 1 taken 62254 times.
✓ Branch 2 taken 380033 times.
✓ Branch 3 taken 20763 times.
463050 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1389 }
1390 21 break;
1391 40 case 17:
1392
2/2
✓ Branch 0 taken 882000 times.
✓ Branch 1 taken 40 times.
882040 for (i = 0; i < nb_samples; i++) {
1393 int32_t sam, tmp;
1394
1395 882000 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1396 882000 dpp->samplesA[1] = dpp->samplesA[0];
1397 882000 out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1398
4/4
✓ Branch 0 taken 693986 times.
✓ Branch 1 taken 188014 times.
✓ Branch 2 taken 659217 times.
✓ Branch 3 taken 34769 times.
882000 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1399
1400 882000 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1401 882000 dpp->samplesB[1] = dpp->samplesB[0];
1402 882000 out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1403
4/4
✓ Branch 0 taken 868910 times.
✓ Branch 1 taken 13090 times.
✓ Branch 2 taken 828745 times.
✓ Branch 3 taken 40165 times.
882000 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1404 }
1405 40 break;
1406 25 case 18:
1407
2/2
✓ Branch 0 taken 551250 times.
✓ Branch 1 taken 25 times.
551275 for (i = 0; i < nb_samples; i++) {
1408 int32_t sam, tmp;
1409
1410 551250 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1411 551250 dpp->samplesA[1] = dpp->samplesA[0];
1412 551250 out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1413
4/4
✓ Branch 0 taken 429384 times.
✓ Branch 1 taken 121866 times.
✓ Branch 2 taken 413959 times.
✓ Branch 3 taken 15425 times.
551250 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1414
1415 551250 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1416 551250 dpp->samplesB[1] = dpp->samplesB[0];
1417 551250 out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1418
4/4
✓ Branch 0 taken 540079 times.
✓ Branch 1 taken 11171 times.
✓ Branch 2 taken 523574 times.
✓ Branch 3 taken 16505 times.
551250 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1419 }
1420 25 break;
1421 17 default: {
1422 17 int k = dpp->value & (MAX_TERM - 1);
1423
1424
2/2
✓ Branch 0 taken 374850 times.
✓ Branch 1 taken 17 times.
374867 for (i = 0; i < nb_samples; i++) {
1425 int32_t sam, tmp;
1426
1427 374850 sam = dpp->samplesA[m];
1428 374850 out_left[i] = tmp = (dpp->samplesA[k] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1429
4/4
✓ Branch 0 taken 238577 times.
✓ Branch 1 taken 136273 times.
✓ Branch 2 taken 215368 times.
✓ Branch 3 taken 23209 times.
374850 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1430
1431 374850 sam = dpp->samplesB[m];
1432 374850 out_right[i] = tmp = (dpp->samplesB[k] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1433
4/4
✓ Branch 0 taken 307328 times.
✓ Branch 1 taken 67522 times.
✓ Branch 2 taken 285909 times.
✓ Branch 3 taken 21419 times.
374850 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1434
1435 374850 m = (m + 1) & (MAX_TERM - 1);
1436 374850 k = (k + 1) & (MAX_TERM - 1);
1437 }
1438
1439
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (m) {
1440 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1441 int k;
1442
1443 17 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1444 17 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1445
1446
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 17 times.
153 for (k = 0; k < MAX_TERM; k++) {
1447 136 dpp->samplesA[k] = temp_A[m];
1448 136 dpp->samplesB[k] = temp_B[m];
1449 136 m = (m + 1) & (MAX_TERM - 1);
1450 }
1451 }
1452 17 break;
1453 }
1454 3 case -1:
1455
2/2
✓ Branch 0 taken 66150 times.
✓ Branch 1 taken 3 times.
66153 for (i = 0; i < nb_samples; i++) {
1456 int32_t sam_A, sam_B, tmp;
1457
1458 66150 sam_A = dpp->samplesA[0];
1459 66150 out_left[i] = tmp = (sam_B = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1460
8/10
✓ Branch 0 taken 62663 times.
✓ Branch 1 taken 3487 times.
✓ Branch 2 taken 56065 times.
✓ Branch 3 taken 6598 times.
✓ Branch 4 taken 28021 times.
✓ Branch 5 taken 28044 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 28021 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 28044 times.
66150 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1461
1462 66150 out_right[i] = tmp = (dpp->samplesA[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1463
8/10
✓ Branch 0 taken 61753 times.
✓ Branch 1 taken 4397 times.
✓ Branch 2 taken 17252 times.
✓ Branch 3 taken 44501 times.
✓ Branch 4 taken 8568 times.
✓ Branch 5 taken 8684 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8568 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 8684 times.
66150 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1464 }
1465 3 break;
1466 13 case -2:
1467
2/2
✓ Branch 0 taken 286650 times.
✓ Branch 1 taken 13 times.
286663 for (i = 0; i < nb_samples; i++) {
1468 int32_t sam_A, sam_B, tmp;
1469
1470 286650 sam_B = dpp->samplesB[0];
1471 286650 out_right[i] = tmp = (sam_A = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1472
9/10
✓ Branch 0 taken 280614 times.
✓ Branch 1 taken 6036 times.
✓ Branch 2 taken 274117 times.
✓ Branch 3 taken 6497 times.
✓ Branch 4 taken 132987 times.
✓ Branch 5 taken 141130 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 132987 times.
✓ Branch 8 taken 7601 times.
✓ Branch 9 taken 133529 times.
286650 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1473
1474 286650 out_left[i] = tmp = (dpp->samplesB[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1475
9/10
✓ Branch 0 taken 280065 times.
✓ Branch 1 taken 6585 times.
✓ Branch 2 taken 102274 times.
✓ Branch 3 taken 177791 times.
✓ Branch 4 taken 46542 times.
✓ Branch 5 taken 55732 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 46542 times.
✓ Branch 8 taken 10080 times.
✓ Branch 9 taken 45652 times.
286650 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1476 }
1477 13 break;
1478 1 case -3:
1479
2/2
✓ Branch 0 taken 22050 times.
✓ Branch 1 taken 1 times.
22051 for (i = 0; i < nb_samples; i++) {
1480 int32_t sam_A, sam_B, tmp;
1481
1482 22050 sam_A = dpp->samplesA[0];
1483 22050 sam_B = dpp->samplesB[0];
1484
1485 22050 dpp->samplesA[0] = tmp = in_right[i];
1486 22050 out_right[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
1487
8/10
✓ Branch 0 taken 21647 times.
✓ Branch 1 taken 403 times.
✓ Branch 2 taken 20357 times.
✓ Branch 3 taken 1290 times.
✓ Branch 4 taken 10187 times.
✓ Branch 5 taken 10170 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10187 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 10170 times.
22050 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1488
1489 22050 dpp->samplesB[0] = tmp = in_left[i];
1490 22050 out_left[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
1491
8/10
✓ Branch 0 taken 20719 times.
✓ Branch 1 taken 1331 times.
✓ Branch 2 taken 20361 times.
✓ Branch 3 taken 358 times.
✓ Branch 4 taken 10201 times.
✓ Branch 5 taken 10160 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10201 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 10160 times.
22050 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1492 }
1493 1 break;
1494 }
1495 120 }
1496
1497 static void decorr_stereo_buffer(WavPackExtraInfo *info,
1498 int32_t *in_left, int32_t *in_right,
1499 int32_t *out_left, int32_t *out_right,
1500 int nb_samples, int tindex)
1501 {
1502 struct Decorr dp = {0}, *dppi = info->dps + tindex;
1503 int delta = dppi->delta, pre_delta;
1504 int term = dppi->value;
1505
1506 if (delta == 7)
1507 pre_delta = 7;
1508 else if (delta < 2)
1509 pre_delta = 3;
1510 else
1511 pre_delta = delta + 1;
1512
1513 dp.value = term;
1514 dp.delta = pre_delta;
1515 decorr_stereo(in_left, in_right, out_left, out_right,
1516 FFMIN(2048, nb_samples), &dp, -1);
1517 dp.delta = delta;
1518
1519 if (tindex == 0) {
1520 reverse_decorr(&dp);
1521 } else {
1522 CLEAR(dp.samplesA);
1523 CLEAR(dp.samplesB);
1524 }
1525
1526 memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
1527 memcpy(dppi->samplesB, dp.samplesB, sizeof(dp.samplesB));
1528 dppi->weightA = dp.weightA;
1529 dppi->weightB = dp.weightB;
1530
1531 if (delta == 0) {
1532 dp.delta = 1;
1533 decorr_stereo(in_left, in_right, out_left, out_right, nb_samples, &dp, 1);
1534 dp.delta = 0;
1535 memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
1536 memcpy(dp.samplesB, dppi->samplesB, sizeof(dp.samplesB));
1537 dppi->weightA = dp.weightA = dp.sumA / nb_samples;
1538 dppi->weightB = dp.weightB = dp.sumB / nb_samples;
1539 }
1540
1541 if (info->gt16bit)
1542 decorr_stereo(in_left, in_right, out_left, out_right,
1543 nb_samples, &dp, 1);
1544 else
1545 decorr_stereo_quick(in_left, in_right, out_left, out_right,
1546 nb_samples, &dp);
1547 }
1548
1549 static void sort_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1550 {
1551 int reversed = 1;
1552 uint32_t bits;
1553
1554 while (reversed) {
1555 int ri, i;
1556
1557 memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
1558 reversed = 0;
1559
1560 for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
1561
1562 if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
1563 break;
1564
1565 if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
1566 decorr_stereo_buffer(info,
1567 s->sampleptrs[ri ][0], s->sampleptrs[ri ][1],
1568 s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1569 s->block_samples, ri);
1570 continue;
1571 }
1572
1573 info->dps[ri ] = s->decorr_passes[ri+1];
1574 info->dps[ri+1] = s->decorr_passes[ri ];
1575
1576 for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
1577 decorr_stereo_buffer(info,
1578 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1579 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1580 s->block_samples, i);
1581
1582 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1583 s->block_samples, info->log_limit);
1584
1585 if (bits < info->best_bits) {
1586 reversed = 1;
1587 info->best_bits = bits;
1588 CLEAR(s->decorr_passes);
1589 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1590 memcpy(s->sampleptrs[info->nterms + 1][0],
1591 s->sampleptrs[i][0], s->block_samples * 4);
1592 memcpy(s->sampleptrs[info->nterms + 1][1],
1593 s->sampleptrs[i][1], s->block_samples * 4);
1594 } else {
1595 info->dps[ri ] = s->decorr_passes[ri ];
1596 info->dps[ri+1] = s->decorr_passes[ri+1];
1597 decorr_stereo_buffer(info,
1598 s->sampleptrs[ri ][0], s->sampleptrs[ri ][1],
1599 s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1600 s->block_samples, ri);
1601 }
1602 }
1603 }
1604 }
1605
1606 static void delta_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1607 {
1608 int lower = 0, delta, d, i;
1609 uint32_t bits;
1610
1611 if (!s->decorr_passes[0].value)
1612 return;
1613 delta = s->decorr_passes[0].delta;
1614
1615 for (d = delta - 1; d >= 0; d--) {
1616 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1617 info->dps[i].value = s->decorr_passes[i].value;
1618 info->dps[i].delta = d;
1619 decorr_stereo_buffer(info,
1620 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1621 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1622 s->block_samples, i);
1623 }
1624
1625 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1626 s->block_samples, info->log_limit);
1627 if (bits >= info->best_bits)
1628 break;
1629 lower = 1;
1630 info->best_bits = bits;
1631 CLEAR(s->decorr_passes);
1632 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1633 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
1634 s->block_samples * 4);
1635 memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[i][1],
1636 s->block_samples * 4);
1637 }
1638
1639 for (d = delta + 1; !lower && d <= 7; d++) {
1640 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1641 info->dps[i].value = s->decorr_passes[i].value;
1642 info->dps[i].delta = d;
1643 decorr_stereo_buffer(info,
1644 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1645 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1646 s->block_samples, i);
1647 }
1648
1649 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1650 s->block_samples, info->log_limit);
1651
1652 if (bits < info->best_bits) {
1653 info->best_bits = bits;
1654 CLEAR(s->decorr_passes);
1655 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1656 memcpy(s->sampleptrs[info->nterms + 1][0],
1657 s->sampleptrs[i][0], s->block_samples * 4);
1658 memcpy(s->sampleptrs[info->nterms + 1][1],
1659 s->sampleptrs[i][1], s->block_samples * 4);
1660 }
1661 else
1662 break;
1663 }
1664 }
1665
1666 static void recurse_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info,
1667 int depth, int delta, uint32_t input_bits)
1668 {
1669 int term, branches = s->num_branches - depth;
1670 int32_t *in_left, *in_right, *out_left, *out_right;
1671 uint32_t term_bits[22], bits;
1672
1673 if (branches < 1 || depth + 1 == info->nterms)
1674 branches = 1;
1675
1676 CLEAR(term_bits);
1677 in_left = s->sampleptrs[depth ][0];
1678 in_right = s->sampleptrs[depth ][1];
1679 out_left = s->sampleptrs[depth + 1][0];
1680 out_right = s->sampleptrs[depth + 1][1];
1681
1682 for (term = -3; term <= 18; term++) {
1683 if (!term || (term > 8 && term < 17))
1684 continue;
1685
1686 if (term == 17 && branches == 1 && depth + 1 < info->nterms)
1687 continue;
1688
1689 if (term == -1 || term == -2)
1690 if (!(s->flags & WV_CROSS_DECORR))
1691 continue;
1692
1693 if (!s->extra_flags && (term > 4 && term < 17))
1694 continue;
1695
1696 info->dps[depth].value = term;
1697 info->dps[depth].delta = delta;
1698 decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1699 s->block_samples, depth);
1700 bits = log2stereo(out_left, out_right, s->block_samples, info->log_limit);
1701
1702 if (bits < info->best_bits) {
1703 info->best_bits = bits;
1704 CLEAR(s->decorr_passes);
1705 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
1706 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[depth + 1][0],
1707 s->block_samples * 4);
1708 memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[depth + 1][1],
1709 s->block_samples * 4);
1710 }
1711
1712 term_bits[term + 3] = bits;
1713 }
1714
1715 while (depth + 1 < info->nterms && branches--) {
1716 uint32_t local_best_bits = input_bits;
1717 int best_term = 0, i;
1718
1719 for (i = 0; i < 22; i++)
1720 if (term_bits[i] && term_bits[i] < local_best_bits) {
1721 local_best_bits = term_bits[i];
1722 best_term = i - 3;
1723 }
1724
1725 if (!best_term)
1726 break;
1727
1728 term_bits[best_term + 3] = 0;
1729
1730 info->dps[depth].value = best_term;
1731 info->dps[depth].delta = delta;
1732 decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1733 s->block_samples, depth);
1734
1735 recurse_stereo(s, info, depth + 1, delta, local_best_bits);
1736 }
1737 }
1738
1739 static void analyze_stereo(WavPackEncodeContext *s,
1740 int32_t *in_left, int32_t *in_right,
1741 int do_samples)
1742 {
1743 WavPackExtraInfo info;
1744 int i;
1745
1746 info.gt16bit = ((s->flags & MAG_MASK) >> MAG_LSB) >= 16;
1747
1748 info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1749 info.log_limit = FFMIN(6912, info.log_limit);
1750
1751 info.nterms = s->num_terms;
1752
1753 if (allocate_buffers2(s, s->num_terms))
1754 return;
1755
1756 memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
1757 memcpy(s->sampleptrs[0][0], in_left, s->block_samples * 4);
1758 memcpy(s->sampleptrs[0][1], in_right, s->block_samples * 4);
1759
1760 for (i = 0; i < info.nterms && info.dps[i].value; i++)
1761 if (info.gt16bit)
1762 decorr_stereo(s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1763 s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1764 s->block_samples, info.dps + i, 1);
1765 else
1766 decorr_stereo_quick(s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1767 s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1768 s->block_samples, info.dps + i);
1769
1770 info.best_bits = log2stereo(s->sampleptrs[info.nterms][0], s->sampleptrs[info.nterms][1],
1771 s->block_samples, 0);
1772
1773 memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
1774 memcpy(s->sampleptrs[info.nterms + 1][1], s->sampleptrs[i][1], s->block_samples * 4);
1775
1776 if (s->extra_flags & EXTRA_BRANCHES)
1777 recurse_stereo(s, &info, 0, (int) floor(s->delta_decay + 0.5),
1778 log2stereo(s->sampleptrs[0][0], s->sampleptrs[0][1],
1779 s->block_samples, 0));
1780
1781 if (s->extra_flags & EXTRA_SORT_FIRST)
1782 sort_stereo(s, &info);
1783
1784 if (s->extra_flags & EXTRA_TRY_DELTAS) {
1785 delta_stereo(s, &info);
1786
1787 if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
1788 s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
1789 else
1790 s->delta_decay = 2.0;
1791 }
1792
1793 if (s->extra_flags & EXTRA_SORT_LAST)
1794 sort_stereo(s, &info);
1795
1796 if (do_samples) {
1797 memcpy(in_left, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
1798 memcpy(in_right, s->sampleptrs[info.nterms + 1][1], s->block_samples * 4);
1799 }
1800
1801 for (i = 0; i < info.nterms; i++)
1802 if (!s->decorr_passes[i].value)
1803 break;
1804
1805 s->num_terms = i;
1806 }
1807
1808 12 static int wv_stereo(WavPackEncodeContext *s,
1809 int32_t *samples_l, int32_t *samples_r,
1810 int no_history, int do_samples)
1811 {
1812 12 struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1813 12 int nb_samples = s->block_samples, ret;
1814 12 int buf_size = sizeof(int32_t) * nb_samples;
1815 12 int log_limit, force_js = 0, force_ts = 0, got_js = 0, pi, i;
1816 12 uint32_t best_size = UINT32_MAX, size;
1817
1818
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (i = 0; i < nb_samples; i++)
1819
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
12 if (samples_l[i] || samples_r[i])
1820 break;
1821
1822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (i == nb_samples) {
1823 s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1824 CLEAR(s->decorr_passes);
1825 CLEAR(s->w);
1826 s->num_terms = 0;
1827 return 0;
1828 }
1829
1830 12 log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1831 12 log_limit = FFMIN(6912, log_limit);
1832
1833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->joint != -1) {
1834 force_js = s->joint;
1835 force_ts = !s->joint;
1836 }
1837
1838
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if ((ret = allocate_buffers(s)) < 0)
1839 return ret;
1840
1841
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
12 if (no_history || s->num_passes >= 7)
1842 1 s->best_decorr = s->mask_decorr = 0;
1843
1844
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 12 times.
37 for (pi = 0; pi < s->num_passes;) {
1845 const WavPackDecorrSpec *wpds;
1846 int nterms, c, j;
1847
1848
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 13 times.
25 if (!pi)
1849 12 c = s->best_decorr;
1850 else {
1851
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
13 if (s->mask_decorr == 0)
1852 2 c = 0;
1853 else
1854 11 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1855
1856
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 12 times.
13 if (c == s->best_decorr) {
1857
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1858 1 continue;
1859 }
1860 }
1861
1862 24 wpds = &s->decorr_specs[c];
1863 24 nterms = decorr_filter_nterms[s->decorr_filter];
1864
1865 while (1) {
1866
4/6
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 21 times.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
24 if (force_js || (wpds->joint_stereo && !force_ts)) {
1867
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!got_js) {
1868 3 av_fast_padded_malloc(&s->js_left, &s->js_left_size, buf_size);
1869 3 av_fast_padded_malloc(&s->js_right, &s->js_right_size, buf_size);
1870 3 memcpy(s->js_left, samples_l, buf_size);
1871 3 memcpy(s->js_right, samples_r, buf_size);
1872
1873
2/2
✓ Branch 0 taken 66150 times.
✓ Branch 1 taken 3 times.
66153 for (i = 0; i < nb_samples; i++)
1874 66150 s->js_right[i] += ((s->js_left[i] -= s->js_right[i]) >> 1);
1875 3 got_js = 1;
1876 }
1877
1878 3 memcpy(s->temp_buffer[0][0], s->js_left, buf_size);
1879 3 memcpy(s->temp_buffer[0][1], s->js_right, buf_size);
1880 } else {
1881 21 memcpy(s->temp_buffer[0][0], samples_l, buf_size);
1882 21 memcpy(s->temp_buffer[0][1], samples_r, buf_size);
1883 }
1884
1885 24 CLEAR(save_decorr_passes);
1886
1887
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
144 for (j = 0; j < nterms; j++) {
1888 120 CLEAR(temp_decorr_pass);
1889 120 temp_decorr_pass.delta = wpds->delta;
1890 120 temp_decorr_pass.value = wpds->terms[j];
1891
1892
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 103 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
120 if (temp_decorr_pass.value < 0 && !(s->flags & WV_CROSS_DECORR))
1893 temp_decorr_pass.value = -3;
1894
1895 120 decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1896 120 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1897 FFMIN(2048, nb_samples), &temp_decorr_pass, -1);
1898
1899
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 24 times.
120 if (j) {
1900 96 CLEAR(temp_decorr_pass.samplesA);
1901 96 CLEAR(temp_decorr_pass.samplesB);
1902 } else {
1903 24 reverse_decorr(&temp_decorr_pass);
1904 }
1905
1906 120 memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1907
1908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16)
1909 decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1910 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1911 nb_samples, &temp_decorr_pass, 1);
1912 else
1913 120 decorr_stereo_quick(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1914 120 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1915 nb_samples, &temp_decorr_pass);
1916 }
1917
1918 24 size = log2stereo(s->temp_buffer[j&1][0], s->temp_buffer[j&1][1],
1919 nb_samples, log_limit);
1920
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
24 if (size != UINT32_MAX || !nterms)
1921 break;
1922 nterms >>= 1;
1923 }
1924
1925
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 if (size < best_size) {
1926 18 memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1927 18 memcpy(s->best_buffer[1], s->temp_buffer[j&1][1], buf_size);
1928 18 memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1929 18 s->num_terms = nterms;
1930 18 s->best_decorr = c;
1931 18 best_size = size;
1932 }
1933
1934
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (pi++)
1935
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1936 }
1937
1938
2/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
12 if (force_js || (s->decorr_specs[s->best_decorr].joint_stereo && !force_ts))
1939 s->flags |= WV_JOINT_STEREO;
1940 else
1941 12 s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1942
1943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (s->extra_flags) {
1944 if (s->flags & WV_JOINT_STEREO) {
1945 analyze_stereo(s, s->js_left, s->js_right, do_samples);
1946
1947 if (do_samples) {
1948 memcpy(samples_l, s->js_left, buf_size);
1949 memcpy(samples_r, s->js_right, buf_size);
1950 }
1951 } else
1952 analyze_stereo(s, samples_l, samples_r, do_samples);
1953
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 } else if (do_samples) {
1954 12 memcpy(samples_l, s->best_buffer[0], buf_size);
1955 12 memcpy(samples_r, s->best_buffer[1], buf_size);
1956 }
1957
1958
3/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1 times.
12 if (s->extra_flags || no_history ||
1959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 s->joint_stereo != s->decorr_specs[s->best_decorr].joint_stereo) {
1960 1 s->joint_stereo = s->decorr_specs[s->best_decorr].joint_stereo;
1961 1 CLEAR(s->w);
1962 1 scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1963 1 scan_word(s, &s->w.c[1], s->best_buffer[1], nb_samples, -1);
1964 }
1965 12 return 0;
1966 }
1967
1968 573304 static void encode_flush(WavPackEncodeContext *s)
1969 {
1970 573304 WavPackWords *w = &s->w;
1971 573304 PutBitContext *pb = &s->pb;
1972
1973
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 573304 times.
573304 if (w->zeros_acc) {
1974 int cbits = count_bits(w->zeros_acc);
1975
1976 do {
1977 if (cbits > 31) {
1978 put_bits(pb, 31, 0x7FFFFFFF);
1979 cbits -= 31;
1980 } else {
1981 put_bits(pb, cbits, (1 << cbits) - 1);
1982 cbits = 0;
1983 }
1984 } while (cbits);
1985
1986 put_bits(pb, 1, 0);
1987
1988 while (w->zeros_acc > 1) {
1989 put_bits(pb, 1, w->zeros_acc & 1);
1990 w->zeros_acc >>= 1;
1991 }