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