FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpackenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 792 1766 44.8%
Functions: 25 46 54.3%
Branches: 554 1449 38.2%

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