FFmpeg coverage


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

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