FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavpackenc.c
Date: 2026-09-15 03:49:06
Exec Total Coverage
Lines: 761 1769 43.0%
Functions: 25 46 54.3%
Branches: 543 1457 37.3%

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