FFmpeg coverage


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