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 > WV_MAX_SAMPLES)
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 < (1 << 16))
644  dbits = nbits_table[v >> 8] + 8;
645  else if (v < (1 << 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) < (1 << 16) ? nbits_table[(av) >> 8] + 8 : \
1971  ((av) < (1 << 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  PutBitContext *pb = &s->pb;
2147  int i, pre_shift;
2148 
2149  pre_shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2150 
2151  if (!sent_bits)
2152  return;
2153 
2154  if (s->flags & WV_MONO_DATA) {
2155  for (i = 0; i < nb_samples; i++) {
2156  put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2157  }
2158  } else {
2159  for (i = 0; i < nb_samples; i++) {
2160  put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2161  put_sbits(pb, sent_bits, samples_r[i] >> pre_shift);
2162  }
2163  }
2164 }
2165 
2167 {
2168  const int max_exp = s->float_max_exp;
2169  PutBitContext *pb = &s->pb;
2170  int32_t value, shift_count;
2171 
2172  if (get_exponent(*sample) == 255) {
2173  if (get_mantissa(*sample)) {
2174  put_bits(pb, 1, 1);
2175  put_bits(pb, 23, get_mantissa(*sample));
2176  } else {
2177  put_bits(pb, 1, 0);
2178  }
2179 
2180  value = 0x1000000;
2181  shift_count = 0;
2182  } else if (get_exponent(*sample)) {
2183  shift_count = max_exp - get_exponent(*sample);
2184  value = 0x800000 + get_mantissa(*sample);
2185  } else {
2186  shift_count = max_exp ? max_exp - 1 : 0;
2187  value = get_mantissa(*sample);
2188  }
2189 
2190  if (shift_count < 25)
2191  value >>= shift_count;
2192  else
2193  value = 0;
2194 
2195  if (!value) {
2196  if (s->float_flags & FLOAT_ZEROS_SENT) {
2197  if (get_exponent(*sample) || get_mantissa(*sample)) {
2198  put_bits(pb, 1, 1);
2199  put_bits(pb, 23, get_mantissa(*sample));
2200 
2201  if (max_exp >= 25)
2202  put_bits(pb, 8, get_exponent(*sample));
2203 
2204  put_bits(pb, 1, get_sign(*sample));
2205  } else {
2206  put_bits(pb, 1, 0);
2207 
2208  if (s->float_flags & FLOAT_NEG_ZEROS)
2209  put_bits(pb, 1, get_sign(*sample));
2210  }
2211  }
2212  } else if (shift_count) {
2213  if (s->float_flags & FLOAT_SHIFT_SENT) {
2214  int32_t data = get_mantissa(*sample) & ((1 << shift_count) - 1);
2215  put_bits(pb, shift_count, data);
2216  } else if (s->float_flags & FLOAT_SHIFT_SAME) {
2217  put_bits(pb, 1, get_mantissa(*sample) & 1);
2218  }
2219  }
2220 }
2221 
2223  int32_t *samples_l, int32_t *samples_r,
2224  int nb_samples)
2225 {
2226  int i;
2227 
2228  if (s->flags & WV_MONO_DATA) {
2229  for (i = 0; i < nb_samples; i++)
2230  pack_float_sample(s, &samples_l[i]);
2231  } else {
2232  for (i = 0; i < nb_samples; i++) {
2233  pack_float_sample(s, &samples_l[i]);
2234  pack_float_sample(s, &samples_r[i]);
2235  }
2236  }
2237 }
2238 
2239 static void decorr_stereo_pass2(struct Decorr *dpp,
2240  int32_t *samples_l, int32_t *samples_r,
2241  int nb_samples)
2242 {
2243  int i, m, k;
2244 
2245  switch (dpp->value) {
2246  case 17:
2247  for (i = 0; i < nb_samples; i++) {
2248  int32_t sam, tmp;
2249 
2250  sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2251  dpp->samplesA[1] = dpp->samplesA[0];
2252  samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2253  UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2254 
2255  sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2256  dpp->samplesB[1] = dpp->samplesB[0];
2257  samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2258  UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2259  }
2260  break;
2261  case 18:
2262  for (i = 0; i < nb_samples; i++) {
2263  int32_t sam, tmp;
2264 
2265  sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2266  dpp->samplesA[1] = dpp->samplesA[0];
2267  samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2268  UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2269 
2270  sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2271  dpp->samplesB[1] = dpp->samplesB[0];
2272  samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2273  UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2274  }
2275  break;
2276  default:
2277  for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2278  int32_t sam, tmp;
2279 
2280  sam = dpp->samplesA[m];
2281  samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2282  UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2283 
2284  sam = dpp->samplesB[m];
2285  samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2286  UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2287 
2288  m = (m + 1) & (MAX_TERM - 1);
2289  k = (k + 1) & (MAX_TERM - 1);
2290  }
2291  if (m) {
2292  int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2293 
2294  memcpy(temp_A, dpp->samplesA, sizeof (dpp->samplesA));
2295  memcpy(temp_B, dpp->samplesB, sizeof (dpp->samplesB));
2296 
2297  for (k = 0; k < MAX_TERM; k++) {
2298  dpp->samplesA[k] = temp_A[m];
2299  dpp->samplesB[k] = temp_B[m];
2300  m = (m + 1) & (MAX_TERM - 1);
2301  }
2302  }
2303  break;
2304  case -1:
2305  for (i = 0; i < nb_samples; i++) {
2306  int32_t sam_A, sam_B, tmp;
2307 
2308  sam_A = dpp->samplesA[0];
2309  samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2310  UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2311 
2312  samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2313  UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2314  }
2315  break;
2316  case -2:
2317  for (i = 0; i < nb_samples; i++) {
2318  int32_t sam_A, sam_B, tmp;
2319 
2320  sam_B = dpp->samplesB[0];
2321  samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2322  UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2323 
2324  samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2325  UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2326  }
2327  break;
2328  case -3:
2329  for (i = 0; i < nb_samples; i++) {
2330  int32_t sam_A, sam_B, tmp;
2331 
2332  sam_A = dpp->samplesA[0];
2333  sam_B = dpp->samplesB[0];
2334 
2335  dpp->samplesA[0] = tmp = samples_r[i];
2336  samples_r[i] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
2337  UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2338 
2339  dpp->samplesB[0] = tmp = samples_l[i];
2340  samples_l[i] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
2341  UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2342  }
2343  break;
2344  }
2345 }
2346 
2347 #define update_weight_d2(weight, delta, source, result) \
2348  if (source && result) \
2349  weight -= (((source ^ result) >> 29) & 4) - 2;
2350 
2351 #define update_weight_clip_d2(weight, delta, source, result) \
2352  if (source && result) { \
2353  const int32_t s = (source ^ result) >> 31; \
2354  if ((weight = (weight ^ s) + (2 - s)) > 1024) weight = 1024; \
2355  weight = (weight ^ s) - s; \
2356  }
2357 
2358 static void decorr_stereo_pass_id2(struct Decorr *dpp,
2359  int32_t *samples_l, int32_t *samples_r,
2360  int nb_samples)
2361 {
2362  int i, m, k;
2363 
2364  switch (dpp->value) {
2365  case 17:
2366  for (i = 0; i < nb_samples; i++) {
2367  int32_t sam, tmp;
2368 
2369  sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2370  dpp->samplesA[1] = dpp->samplesA[0];
2371  samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2372  update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2373 
2374  sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2375  dpp->samplesB[1] = dpp->samplesB[0];
2376  samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2377  update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2378  }
2379  break;
2380  case 18:
2381  for (i = 0; i < nb_samples; i++) {
2382  int32_t sam, tmp;
2383 
2384  sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2385  dpp->samplesA[1] = dpp->samplesA[0];
2386  samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2387  update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2388 
2389  sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2390  dpp->samplesB[1] = dpp->samplesB[0];
2391  samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2392  update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2393  }
2394  break;
2395  default:
2396  for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2397  int32_t sam, tmp;
2398 
2399  sam = dpp->samplesA[m];
2400  samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2401  update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2402 
2403  sam = dpp->samplesB[m];
2404  samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2405  update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2406 
2407  m = (m + 1) & (MAX_TERM - 1);
2408  k = (k + 1) & (MAX_TERM - 1);
2409  }
2410 
2411  if (m) {
2412  int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2413 
2414  memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2415  memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2416 
2417  for (k = 0; k < MAX_TERM; k++) {
2418  dpp->samplesA[k] = temp_A[m];
2419  dpp->samplesB[k] = temp_B[m];
2420  m = (m + 1) & (MAX_TERM - 1);
2421  }
2422  }
2423  break;
2424  case -1:
2425  for (i = 0; i < nb_samples; i++) {
2426  int32_t sam_A, sam_B, tmp;
2427 
2428  sam_A = dpp->samplesA[0];
2429  samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2430  update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2431 
2432  samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2433  update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2434  }
2435  break;
2436  case -2:
2437  for (i = 0; i < nb_samples; i++) {
2438  int32_t sam_A, sam_B, tmp;
2439 
2440  sam_B = dpp->samplesB[0];
2441  samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2442  update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2443 
2444  samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2445  update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2446  }
2447  break;
2448  case -3:
2449  for (i = 0; i < nb_samples; i++) {
2450  int32_t sam_A, sam_B, tmp;
2451 
2452  sam_A = dpp->samplesA[0];
2453  sam_B = dpp->samplesB[0];
2454 
2455  dpp->samplesA[0] = tmp = samples_r[i];
2456  samples_r[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
2457  update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2458 
2459  dpp->samplesB[0] = tmp = samples_l[i];
2460  samples_l[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
2461  update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2462  }
2463  break;
2464  }
2465 }
2466 
2467 static void put_metadata_block(PutByteContext *pb, int flags, int size)
2468 {
2469  if (size & 1)
2470  flags |= WP_IDF_ODD;
2471 
2472  bytestream2_put_byte(pb, flags);
2473  bytestream2_put_byte(pb, (size + 1) >> 1);
2474 }
2475 
2477  int32_t *samples_l, int32_t *samples_r,
2478  uint8_t *out, int out_size)
2479 {
2480  int block_size, start, end, data_size, tcount, temp, m = 0;
2481  int i, j, ret = 0, got_extra = 0, nb_samples = s->block_samples;
2482  uint32_t crc = 0xffffffffu;
2483  struct Decorr *dpp;
2484  PutByteContext pb;
2485 
2486  if (s->flags & WV_MONO_DATA) {
2487  CLEAR(s->w);
2488  }
2489  if (!(s->flags & WV_MONO) && s->optimize_mono) {
2490  int32_t lor = 0, diff = 0;
2491 
2492  for (i = 0; i < nb_samples; i++) {
2493  lor |= samples_l[i] | samples_r[i];
2494  diff |= samples_l[i] - samples_r[i];
2495 
2496  if (lor && diff)
2497  break;
2498  }
2499 
2500  if (i == nb_samples && lor && !diff) {
2502  s->flags |= WV_FALSE_STEREO;
2503 
2504  if (!s->false_stereo) {
2505  s->false_stereo = 1;
2506  s->num_terms = 0;
2507  CLEAR(s->w);
2508  }
2509  } else if (s->false_stereo) {
2510  s->false_stereo = 0;
2511  s->num_terms = 0;
2512  CLEAR(s->w);
2513  }
2514  }
2515 
2516  if (s->flags & SHIFT_MASK) {
2517  int shift = (s->flags & SHIFT_MASK) >> SHIFT_LSB;
2518  int mag = (s->flags & MAG_MASK) >> MAG_LSB;
2519 
2520  if (s->flags & WV_MONO_DATA)
2521  shift_mono(samples_l, nb_samples, shift);
2522  else
2523  shift_stereo(samples_l, samples_r, nb_samples, shift);
2524 
2525  if ((mag -= shift) < 0)
2526  s->flags &= ~MAG_MASK;
2527  else
2528  s->flags -= (1 << MAG_LSB) * shift;
2529  }
2530 
2531  if ((s->flags & WV_FLOAT_DATA) || (s->flags & MAG_MASK) >> MAG_LSB >= 24) {
2532  av_fast_padded_malloc(&s->orig_l, &s->orig_l_size, sizeof(int32_t) * nb_samples);
2533  memcpy(s->orig_l, samples_l, sizeof(int32_t) * nb_samples);
2534  if (!(s->flags & WV_MONO_DATA)) {
2535  av_fast_padded_malloc(&s->orig_r, &s->orig_r_size, sizeof(int32_t) * nb_samples);
2536  memcpy(s->orig_r, samples_r, sizeof(int32_t) * nb_samples);
2537  }
2538 
2539  if (s->flags & WV_FLOAT_DATA)
2540  got_extra = scan_float(s, samples_l, samples_r, nb_samples);
2541  else
2542  got_extra = scan_int32(s, samples_l, samples_r, nb_samples);
2543  s->num_terms = 0;
2544  } else {
2545  scan_int23(s, samples_l, samples_r, nb_samples);
2546  if (s->shift != s->int32_zeros + s->int32_ones + s->int32_dups) {
2547  s->shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2548  s->num_terms = 0;
2549  }
2550  }
2551 
2552  if (!s->num_passes && !s->num_terms) {
2553  s->num_passes = 1;
2554 
2555  if (s->flags & WV_MONO_DATA)
2556  ret = wv_mono(s, samples_l, 1, 0);
2557  else
2558  ret = wv_stereo(s, samples_l, samples_r, 1, 0);
2559 
2560  s->num_passes = 0;
2561  }
2562  if (s->flags & WV_MONO_DATA) {
2563  for (i = 0; i < nb_samples; i++)
2564  crc += (crc << 1) + samples_l[i];
2565 
2566  if (s->num_passes)
2567  ret = wv_mono(s, samples_l, !s->num_terms, 1);
2568  } else {
2569  for (i = 0; i < nb_samples; i++)
2570  crc += (crc << 3) + (samples_l[i] << 1) + samples_l[i] + samples_r[i];
2571 
2572  if (s->num_passes)
2573  ret = wv_stereo(s, samples_l, samples_r, !s->num_terms, 1);
2574  }
2575  if (ret < 0)
2576  return ret;
2577 
2578  if (!s->ch_offset)
2579  s->flags |= WV_INITIAL_BLOCK;
2580 
2581  s->ch_offset += 1 + !(s->flags & WV_MONO);
2582 
2583  if (s->ch_offset == s->avctx->channels)
2584  s->flags |= WV_FINAL_BLOCK;
2585 
2586  bytestream2_init_writer(&pb, out, out_size);
2587  bytestream2_put_le32(&pb, MKTAG('w', 'v', 'p', 'k'));
2588  bytestream2_put_le32(&pb, 0);
2589  bytestream2_put_le16(&pb, 0x410);
2590  bytestream2_put_le16(&pb, 0);
2591  bytestream2_put_le32(&pb, 0);
2592  bytestream2_put_le32(&pb, s->sample_index);
2593  bytestream2_put_le32(&pb, nb_samples);
2594  bytestream2_put_le32(&pb, s->flags);
2595  bytestream2_put_le32(&pb, crc);
2596 
2597  if (s->flags & WV_INITIAL_BLOCK &&
2601  bytestream2_put_byte(&pb, s->avctx->channels);
2602  bytestream2_put_le32(&pb, s->avctx->channel_layout);
2603  bytestream2_put_byte(&pb, 0);
2604  }
2605 
2606  if ((s->flags & SRATE_MASK) == SRATE_MASK) {
2608  bytestream2_put_le24(&pb, s->avctx->sample_rate);
2609  bytestream2_put_byte(&pb, 0);
2610  }
2611 
2613  for (i = 0; i < s->num_terms; i++) {
2614  struct Decorr *dpp = &s->decorr_passes[i];
2615  bytestream2_put_byte(&pb, ((dpp->value + 5) & 0x1f) | ((dpp->delta << 5) & 0xe0));
2616  }
2617  if (s->num_terms & 1)
2618  bytestream2_put_byte(&pb, 0);
2619 
2620 #define WRITE_DECWEIGHT(type) do { \
2621  temp = store_weight(type); \
2622  bytestream2_put_byte(&pb, temp); \
2623  type = restore_weight(temp); \
2624  } while (0)
2625 
2626  bytestream2_put_byte(&pb, WP_ID_DECWEIGHTS);
2627  bytestream2_put_byte(&pb, 0);
2628  start = bytestream2_tell_p(&pb);
2629  for (i = s->num_terms - 1; i >= 0; --i) {
2630  struct Decorr *dpp = &s->decorr_passes[i];
2631 
2632  if (store_weight(dpp->weightA) ||
2633  (!(s->flags & WV_MONO_DATA) && store_weight(dpp->weightB)))
2634  break;
2635  }
2636  tcount = i + 1;
2637  for (i = 0; i < s->num_terms; i++) {
2638  struct Decorr *dpp = &s->decorr_passes[i];
2639  if (i < tcount) {
2640  WRITE_DECWEIGHT(dpp->weightA);
2641  if (!(s->flags & WV_MONO_DATA))
2642  WRITE_DECWEIGHT(dpp->weightB);
2643  } else {
2644  dpp->weightA = dpp->weightB = 0;
2645  }
2646  }
2647  end = bytestream2_tell_p(&pb);
2648  out[start - 2] = WP_ID_DECWEIGHTS | (((end - start) & 1) ? WP_IDF_ODD: 0);
2649  out[start - 1] = (end - start + 1) >> 1;
2650  if ((end - start) & 1)
2651  bytestream2_put_byte(&pb, 0);
2652 
2653 #define WRITE_DECSAMPLE(type) do { \
2654  temp = log2s(type); \
2655  type = wp_exp2(temp); \
2656  bytestream2_put_le16(&pb, temp); \
2657  } while (0)
2658 
2659  bytestream2_put_byte(&pb, WP_ID_DECSAMPLES);
2660  bytestream2_put_byte(&pb, 0);
2661  start = bytestream2_tell_p(&pb);
2662  for (i = 0; i < s->num_terms; i++) {
2663  struct Decorr *dpp = &s->decorr_passes[i];
2664  if (i == 0) {
2665  if (dpp->value > MAX_TERM) {
2666  WRITE_DECSAMPLE(dpp->samplesA[0]);
2667  WRITE_DECSAMPLE(dpp->samplesA[1]);
2668  if (!(s->flags & WV_MONO_DATA)) {
2669  WRITE_DECSAMPLE(dpp->samplesB[0]);
2670  WRITE_DECSAMPLE(dpp->samplesB[1]);
2671  }
2672  } else if (dpp->value < 0) {
2673  WRITE_DECSAMPLE(dpp->samplesA[0]);
2674  WRITE_DECSAMPLE(dpp->samplesB[0]);
2675  } else {
2676  for (j = 0; j < dpp->value; j++) {
2677  WRITE_DECSAMPLE(dpp->samplesA[j]);
2678  if (!(s->flags & WV_MONO_DATA))
2679  WRITE_DECSAMPLE(dpp->samplesB[j]);
2680  }
2681  }
2682  } else {
2683  CLEAR(dpp->samplesA);
2684  CLEAR(dpp->samplesB);
2685  }
2686  }
2687  end = bytestream2_tell_p(&pb);
2688  out[start - 1] = (end - start) >> 1;
2689 
2690 #define WRITE_CHAN_ENTROPY(chan) do { \
2691  for (i = 0; i < 3; i++) { \
2692  temp = wp_log2(s->w.c[chan].median[i]); \
2693  bytestream2_put_le16(&pb, temp); \
2694  s->w.c[chan].median[i] = wp_exp2(temp); \
2695  } \
2696  } while (0)
2697 
2698  put_metadata_block(&pb, WP_ID_ENTROPY, 6 * (1 + (!(s->flags & WV_MONO_DATA))));
2699  WRITE_CHAN_ENTROPY(0);
2700  if (!(s->flags & WV_MONO_DATA))
2701  WRITE_CHAN_ENTROPY(1);
2702 
2703  if (s->flags & WV_FLOAT_DATA) {
2705  bytestream2_put_byte(&pb, s->float_flags);
2706  bytestream2_put_byte(&pb, s->float_shift);
2707  bytestream2_put_byte(&pb, s->float_max_exp);
2708  bytestream2_put_byte(&pb, 127);
2709  }
2710 
2711  if (s->flags & WV_INT32_DATA) {
2713  bytestream2_put_byte(&pb, s->int32_sent_bits);
2714  bytestream2_put_byte(&pb, s->int32_zeros);
2715  bytestream2_put_byte(&pb, s->int32_ones);
2716  bytestream2_put_byte(&pb, s->int32_dups);
2717  }
2718 
2719  if (s->flags & WV_MONO_DATA && !s->num_passes) {
2720  for (i = 0; i < nb_samples; i++) {
2721  int32_t code = samples_l[i];
2722 
2723  for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++) {
2724  int32_t sam;
2725 
2726  if (dpp->value > MAX_TERM) {
2727  if (dpp->value & 1)
2728  sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2729  else
2730  sam = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
2731 
2732  dpp->samplesA[1] = dpp->samplesA[0];
2733  dpp->samplesA[0] = code;
2734  } else {
2735  sam = dpp->samplesA[m];
2736  dpp->samplesA[(m + dpp->value) & (MAX_TERM - 1)] = code;
2737  }
2738 
2739  code -= APPLY_WEIGHT(dpp->weightA, sam);
2740  UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, code);
2741  }
2742 
2743  m = (m + 1) & (MAX_TERM - 1);
2744  samples_l[i] = code;
2745  }
2746  if (m) {
2747  for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++)
2748  if (dpp->value > 0 && dpp->value <= MAX_TERM) {
2749  int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2750  int k;
2751 
2752  memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2753  memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2754 
2755  for (k = 0; k < MAX_TERM; k++) {
2756  dpp->samplesA[k] = temp_A[m];
2757  dpp->samplesB[k] = temp_B[m];
2758  m = (m + 1) & (MAX_TERM - 1);
2759  }
2760  }
2761  }
2762  } else if (!s->num_passes) {
2763  if (s->flags & WV_JOINT_STEREO) {
2764  for (i = 0; i < nb_samples; i++)
2765  samples_r[i] += ((samples_l[i] -= samples_r[i]) >> 1);
2766  }
2767 
2768  for (i = 0; i < s->num_terms; i++) {
2769  struct Decorr *dpp = &s->decorr_passes[i];
2770  if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16 || dpp->delta != 2)
2771  decorr_stereo_pass2(dpp, samples_l, samples_r, nb_samples);
2772  else
2773  decorr_stereo_pass_id2(dpp, samples_l, samples_r, nb_samples);
2774  }
2775  }
2776 
2777  bytestream2_put_byte(&pb, WP_ID_DATA | WP_IDF_LONG);
2779  if (s->flags & WV_MONO_DATA) {
2780  for (i = 0; i < nb_samples; i++)
2781  wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2782  } else {
2783  for (i = 0; i < nb_samples; i++) {
2784  wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2785  wavpack_encode_sample(s, &s->w.c[1], s->samples[1][i]);
2786  }
2787  }
2788  encode_flush(s);
2789  flush_put_bits(&s->pb);
2790  data_size = put_bits_count(&s->pb) >> 3;
2791  bytestream2_put_le24(&pb, (data_size + 1) >> 1);
2792  bytestream2_skip_p(&pb, data_size);
2793  if (data_size & 1)
2794  bytestream2_put_byte(&pb, 0);
2795 
2796  if (got_extra) {
2797  bytestream2_put_byte(&pb, WP_ID_EXTRABITS | WP_IDF_LONG);
2799  if (s->flags & WV_FLOAT_DATA)
2800  pack_float(s, s->orig_l, s->orig_r, nb_samples);
2801  else
2802  pack_int32(s, s->orig_l, s->orig_r, nb_samples);
2803  flush_put_bits(&s->pb);
2804  data_size = put_bits_count(&s->pb) >> 3;
2805  bytestream2_put_le24(&pb, (data_size + 5) >> 1);
2806  bytestream2_put_le32(&pb, s->crc_x);
2807  bytestream2_skip_p(&pb, data_size);
2808  if (data_size & 1)
2809  bytestream2_put_byte(&pb, 0);
2810  }
2811 
2812  block_size = bytestream2_tell_p(&pb);
2813  AV_WL32(out + 4, block_size - 8);
2814 
2816 
2817  return block_size;
2818 }
2819 
2821  const int8_t *src, int32_t *dst,
2822  int nb_samples)
2823 {
2824  int i;
2825 
2826 #define COPY_SAMPLES(type, offset, shift) do { \
2827  const type *sptr = (const type *)src; \
2828  for (i = 0; i < nb_samples; i++) \
2829  dst[i] = (sptr[i] - offset) >> shift; \
2830  } while (0)
2831 
2832  switch (s->avctx->sample_fmt) {
2833  case AV_SAMPLE_FMT_U8P:
2834  COPY_SAMPLES(int8_t, 0x80, 0);
2835  break;
2836  case AV_SAMPLE_FMT_S16P:
2837  COPY_SAMPLES(int16_t, 0, 0);
2838  break;
2839  case AV_SAMPLE_FMT_S32P:
2840  if (s->avctx->bits_per_raw_sample <= 24) {
2841  COPY_SAMPLES(int32_t, 0, 8);
2842  break;
2843  }
2844  case AV_SAMPLE_FMT_FLTP:
2845  memcpy(dst, src, nb_samples * 4);
2846  }
2847 }
2848 
2850 {
2851  int i;
2852 
2853  for (i = 0; i < 15; i++) {
2854  if (wv_rates[i] == s->avctx->sample_rate)
2855  break;
2856  }
2857 
2858  s->flags = i << SRATE_LSB;
2859 }
2860 
2862  const AVFrame *frame, int *got_packet_ptr)
2863 {
2864  WavPackEncodeContext *s = avctx->priv_data;
2865  int buf_size, ret;
2866  uint8_t *buf;
2867 
2868  s->block_samples = frame->nb_samples;
2870  sizeof(int32_t) * s->block_samples);
2871  if (!s->samples[0])
2872  return AVERROR(ENOMEM);
2873  if (avctx->channels > 1) {
2875  sizeof(int32_t) * s->block_samples);
2876  if (!s->samples[1])
2877  return AVERROR(ENOMEM);
2878  }
2879 
2880  buf_size = s->block_samples * avctx->channels * 8
2881  + 200 /* for headers */;
2882  if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size)) < 0)
2883  return ret;
2884  buf = avpkt->data;
2885 
2886  for (s->ch_offset = 0; s->ch_offset < avctx->channels;) {
2887  set_samplerate(s);
2888 
2889  switch (s->avctx->sample_fmt) {
2890  case AV_SAMPLE_FMT_S16P: s->flags |= 1; break;
2891  case AV_SAMPLE_FMT_S32P: s->flags |= 3 - (s->avctx->bits_per_raw_sample <= 24); break;
2892  case AV_SAMPLE_FMT_FLTP: s->flags |= 3 | WV_FLOAT_DATA;
2893  }
2894 
2895  fill_buffer(s, frame->extended_data[s->ch_offset], s->samples[0], s->block_samples);
2896  if (avctx->channels - s->ch_offset == 1) {
2897  s->flags |= WV_MONO;
2898  } else {
2899  s->flags |= WV_CROSS_DECORR;
2900  fill_buffer(s, frame->extended_data[s->ch_offset + 1], s->samples[1], s->block_samples);
2901  }
2902 
2903  s->flags += (1 << MAG_LSB) * ((s->flags & 3) * 8 + 7);
2904 
2905  if ((ret = wavpack_encode_block(s, s->samples[0], s->samples[1],
2906  buf, buf_size)) < 0)
2907  return ret;
2908 
2909  buf += ret;
2910  buf_size -= ret;
2911  }
2912  s->sample_index += frame->nb_samples;
2913 
2914  avpkt->pts = frame->pts;
2915  avpkt->size = buf - avpkt->data;
2916  avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
2917  *got_packet_ptr = 1;
2918  return 0;
2919 }
2920 
2922 {
2923  WavPackEncodeContext *s = avctx->priv_data;
2924  int i;
2925 
2926  for (i = 0; i < MAX_TERMS + 2; i++) {
2927  av_freep(&s->sampleptrs[i][0]);
2928  av_freep(&s->sampleptrs[i][1]);
2929  s->sampleptrs_size[i][0] = s->sampleptrs_size[i][1] = 0;
2930  }
2931 
2932  for (i = 0; i < 2; i++) {
2933  av_freep(&s->samples[i]);
2934  s->samples_size[i] = 0;
2935 
2936  av_freep(&s->best_buffer[i]);
2937  s->best_buffer_size[i] = 0;
2938 
2939  av_freep(&s->temp_buffer[i][0]);
2940  av_freep(&s->temp_buffer[i][1]);
2941  s->temp_buffer_size[i][0] = s->temp_buffer_size[i][1] = 0;
2942  }
2943 
2944  av_freep(&s->js_left);
2945  av_freep(&s->js_right);
2946  s->js_left_size = s->js_right_size = 0;
2947 
2948  av_freep(&s->orig_l);
2949  av_freep(&s->orig_r);
2950  s->orig_l_size = s->orig_r_size = 0;
2951 
2952  return 0;
2953 }
2954 
2955 #define OFFSET(x) offsetof(WavPackEncodeContext, x)
2956 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2957 static const AVOption options[] = {
2958  { "joint_stereo", "", OFFSET(joint), AV_OPT_TYPE_INT, {.i64=0},-1, 1, FLAGS, "joint" },
2959  { "on", "mid/side", 0, AV_OPT_TYPE_CONST, {.i64= 1}, 0, 0, FLAGS, "joint"},
2960  { "off", "left/right", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "joint"},
2961  { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 0}, 0, 0, FLAGS, "joint"},
2962  { "optimize_mono", "", OFFSET(optimize_mono), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "opt_mono" },
2963  { "on", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "opt_mono"},
2964  { "off", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "opt_mono"},
2965  { NULL },
2966 };
2967 
2969  .class_name = "WavPack encoder",
2970  .item_name = av_default_item_name,
2971  .option = options,
2972  .version = LIBAVUTIL_VERSION_INT,
2973 };
2974 
2976  .name = "wavpack",
2977  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
2978  .type = AVMEDIA_TYPE_AUDIO,
2979  .id = AV_CODEC_ID_WAVPACK,
2980  .priv_data_size = sizeof(WavPackEncodeContext),
2981  .priv_class = &wavpack_encoder_class,
2983  .encode2 = wavpack_encode_frame,
2984  .close = wavpack_encode_close,
2985  .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
2986  .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8P,
2991 };
#define MAX_TERM
Definition: wavpack.h:28
int delta
Definition: wavpack.h:84
float, planar
Definition: samplefmt.h:70
#define MAG_LSB
Definition: wavpackenc.c:51
#define NULL
Definition: coverity.c:32
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1736
#define WRITE_DECWEIGHT(type)
float v
const char * s
Definition: avisynth_c.h:631
#define FF_COMPRESSION_DEFAULT
Definition: avcodec.h:1328
#define count_bits(av)
Definition: wavpackenc.c:1967
#define EXTRA_TRY_DELTAS
Definition: wavpackenc.c:57
static int shift(int a, int b)
Definition: sonic.c:82
int median[3]
Definition: wavpack.h:95
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:192
static av_cold int wavpack_encode_close(AVCodecContext *avctx)
Definition: wavpackenc.c:2921
static void shift_stereo(int32_t *left, int32_t *right, int nb_samples, int shift)
Definition: wavpackenc.c:197
static int scan_float(WavPackEncodeContext *s, int32_t *samples_l, int32_t *samples_r, int nb_samples)
Definition: wavpackenc.c:259
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:160
static int wv_stereo(WavPackEncodeContext *s, int32_t *samples_l, int32_t *samples_r, int no_history, int do_samples)
Definition: wavpackenc.c:1807
static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: wavpackenc.c:2861
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
static void process_float(WavPackEncodeContext *s, int32_t *sample)
Definition: wavpackenc.c:218
Definition: wvdec.c:32
else temp
Definition: vf_mcdeint.c:257
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
static void shift_mono(int32_t *samples, int nb_samples, int shift)
Definition: wavpackenc.c:190
int samplesA[MAX_TERM]
Definition: wavpack.h:88
int size
Definition: avcodec.h:1163
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:141
static void delta_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
Definition: wavpackenc.c:830
static int allocate_buffers2(WavPackEncodeContext *s, int nterms)
Definition: wavpackenc.c:883
static void sort_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
Definition: wavpackenc.c:1548
#define WV_FLOAT_DATA
Definition: wavpack.h:35
#define MAX_TERMS
Definition: wavpack.h:27
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional FF_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:139
int weightB
Definition: wavpack.h:87
static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
Definition: wavpackenc.c:2166
Definition: vf_geq.c:45
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2727
#define AV_CH_LAYOUT_STEREO
static void put_metadata_block(PutByteContext *pb, int flags, int size)
Definition: wavpackenc.c:2467
static void wavpack_encode_sample(WavPackEncodeContext *s, WvChannel *c, int32_t sample)
Definition: wavpackenc.c:2048
#define sample
AVCodec.
Definition: avcodec.h:3181
static uint32_t log2mono(int32_t *samples, int nb_samples, int limit)
Definition: wavpackenc.c:659
static av_cold int wavpack_encode_init(AVCodecContext *avctx)
Definition: wavpackenc.c:125
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int pend_count
Definition: wavpackenc.c:71
#define M(a, b)
Definition: vp3dsp.c:44
if()
Definition: avfilter.c:975
uint8_t bits
Definition: crc.c:295
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1993
#define UPDATE_WEIGHT(weight, delta, source, result)
Definition: wavpackenc.c:32
uint8_t
#define av_cold
Definition: attributes.h:74
static void pack_int32(WavPackEncodeContext *s, int32_t *samples_l, int32_t *samples_r, int nb_samples)
Definition: wavpackenc.c:2141
const WavPackDecorrSpec * decorr_specs
Definition: wavpackenc.c:121
float delta
AVOptions.
static void decorr_stereo(int32_t *in_left, int32_t *in_right, int32_t *out_left, int32_t *out_right, int nb_samples, struct Decorr *dpp, int dir)
Definition: wavpackenc.c:1123
#define FLOAT_ZEROS_SENT
Definition: wavpackenc.c:210
#define APPLY_WEIGHT_I(weight, sample)
Definition: wavpackenc.c:41
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
#define CLEAR(destin)
Definition: wavpackenc.c:46
AVCodec ff_wavpack_encoder
Definition: wavpackenc.c:2975
int value
Definition: wavpack.h:85
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
static void delta_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
Definition: wavpackenc.c:1605
#define OFFSET(x)
Definition: wavpackenc.c:2955
static AVFrame * frame
#define WRITE_CHAN_ENTROPY(chan)
uint8_t * data
Definition: avcodec.h:1162
static void fill_buffer(WavPackEncodeContext *s, const int8_t *src, int32_t *dst, int nb_samples)
Definition: wavpackenc.c:2820
static void decorr_mono_buffer(int32_t *samples, int32_t *outsamples, int nb_samples, struct Decorr *dpp, int tindex)
Definition: wavpackenc.c:681
#define get_sign(f)
Definition: wavpackenc.c:216
struct Decorr decorr_passes[MAX_TERMS]
Definition: wavpackenc.c:120
#define WV_INT32_DATA
Definition: wavpack.h:36
#define APPLY_WEIGHT(weight, sample)
Definition: wavpackenc.c:43
WvChannel c[2]
Definition: wavpackenc.c:72
ptrdiff_t size
Definition: opengl_enc.c:101
static void analyze_stereo(WavPackEncodeContext *s, int32_t *in_left, int32_t *in_right, int do_samples)
Definition: wavpackenc.c:1738
static const AVOption options[]
Definition: wavpackenc.c:2957
static void reverse_mono_decorr(struct Decorr *dpp)
Definition: wavpackenc.c:603
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1180
#define av_log(a,...)
unsigned m
Definition: audioconvert.c:187
int32_t * sampleptrs[MAX_TERMS+2][2]
Definition: wavpackenc.c:88
#define EXTRA_SORT_LAST
Definition: wavpackenc.c:61
int holding_zero
Definition: wavpackenc.c:71
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
Definition: bytestream.h:157
static const uint16_t mask[17]
Definition: lzw.c:38
#define EXTRA_ADJUST_DELTAS
Definition: wavpackenc.c:58
#define WV_FINAL_BLOCK
Definition: wavpack.h:44
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: avcodec.h:829
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
static void set_samplerate(WavPackEncodeContext *s)
Definition: wavpackenc.c:2849
static av_always_inline int wp_exp2(int16_t val)
Definition: wavpack.h:163
#define SHIFT_MASK
Definition: wavpackenc.c:49
static void recurse_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info, int depth, int delta, uint32_t input_bits)
Definition: wavpackenc.c:1665
#define get_exponent(f)
Definition: wavpackenc.c:215
const char * name
Name of the codec implementation.
Definition: avcodec.h:3188
PutBitContext pb
Definition: wavpackenc.c:78
static void decorr_stereo_pass_id2(struct Decorr *dpp, int32_t *samples_l, int32_t *samples_r, int nb_samples)
Definition: wavpackenc.c:2358
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:191
Libavcodec external API header.
#define SRATE_LSB
Definition: wavpackenc.c:54
int8_t terms[MAX_TERMS+1]
Definition: wavpackenc.h:27
int depth
Definition: v4l.c:61
static int log2s(int32_t value)
Definition: wavpackenc.c:535
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2046
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:85
static const WavPackDecorrSpec *const decorr_filters[]
Definition: wavpackenc.h:640
int weightA
Definition: wavpack.h:86
#define WV_MONO_DATA
Definition: wavpack.h:46
int32_t * js_right
Definition: wavpackenc.c:97
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:174
#define FFMIN(a, b)
Definition: common.h:66
static void decorr_stereo_pass2(struct Decorr *dpp, int32_t *samples_l, int32_t *samples_r, int nb_samples)
Definition: wavpackenc.c:2239
static void scan_word(WavPackEncodeContext *s, WvChannel *c, int32_t *samples, int nb_samples, int dir)
Definition: wavpackenc.c:985
signed 32 bits, planar
Definition: samplefmt.h:69
static const int wv_rates[16]
Definition: wavpack.h:119
#define WV_FALSE_STEREO
Definition: wavpack.h:37
ret
Definition: avfilter.c:974
int holding_one
Definition: wavpackenc.c:70
#define EXTRA_BRANCHES
Definition: wavpackenc.c:60
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
#define FLOAT_SHIFT_SENT
Definition: wavpackenc.c:209
int32_t
#define SHIFT_LSB
Definition: wavpackenc.c:48
static void pack_float(WavPackEncodeContext *s, int32_t *samples_l, int32_t *samples_r, int nb_samples)
Definition: wavpackenc.c:2222
#define get_mantissa(f)
Definition: wavpackenc.c:214
float u
unsigned 8 bits, planar
Definition: samplefmt.h:67
#define L(x)
Definition: vp56_arith.h:36
#define FLAGS
Definition: wavpackenc.c:2956
static uint32_t log2sample(uint32_t v, int limit, uint32_t *result)
Definition: wavpackenc.c:635
#define MAG_MASK
Definition: wavpackenc.c:52
#define SRATE_MASK
Definition: wavpackenc.c:55
#define update_weight_d2(weight, delta, source, result)
Definition: wavpackenc.c:2347
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2005
int8_t joint_stereo
Definition: wavpackenc.h:27
#define GET_MED(n)
Definition: wavpack.h:101
uint8_t * buffer
Definition: bytestream.h:38
static void decorr_stereo_buffer(WavPackExtraInfo *info, int32_t *in_left, int32_t *in_right, int32_t *out_left, int32_t *out_right, int nb_samples, int tindex)
Definition: wavpackenc.c:1496
AVS_Value src
Definition: avisynth_c.h:482
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
int compression_level
Definition: avcodec.h:1327
#define WV_INITIAL_BLOCK
Definition: wavpack.h:43
int sample_rate
samples per second
Definition: avcodec.h:1985
static int allocate_buffers(WavPackEncodeContext *s)
Definition: wavpackenc.c:903
static void analyze_mono(WavPackEncodeContext *s, int32_t *samples, int do_samples)
Definition: wavpackenc.c:933
main external API structure.
Definition: avcodec.h:1241
static int scan_int32(WavPackEncodeContext *s, int32_t *samples_l, int32_t *samples_r, int nb_samples)
Definition: wavpackenc.c:426
#define FLOAT_NEG_ZEROS
Definition: wavpackenc.c:211
static void decorr_stereo_quick(int32_t *in_left, int32_t *in_right, int32_t *out_left, int32_t *out_right, int nb_samples, struct Decorr *dpp)
Definition: wavpackenc.c:1360
int samplesB[MAX_TERM]
Definition: wavpack.h:89
void * buf
Definition: avisynth_c.h:553
int sumA
Definition: wavpack.h:90
#define WRITE_DECSAMPLE(type)
Describe the class of an AVClass context structure.
Definition: log.h:67
#define FLOAT_SHIFT_SAME
Definition: wavpackenc.c:208
#define WV_JOINT_STEREO
Definition: wavpack.h:33
static int8_t store_weight(int weight)
Definition: wavpackenc.c:516
int32_t * temp_buffer[2][2]
Definition: wavpackenc.c:91
int32_t * best_buffer[2]
Definition: wavpackenc.c:94
static void scan_int23(WavPackEncodeContext *s, int32_t *samples_l, int32_t *samples_r, int nb_samples)
Definition: wavpackenc.c:345
static int weight(int i, int blen, int offset)
Definition: diracdec.c:1298
#define update_weight_clip_d2(weight, delta, source, result)
Definition: wavpackenc.c:2351
int sumB
Definition: wavpack.h:91
static int flags
Definition: cpu.c:47
#define UPDATE_WEIGHT_CLIP(weight, delta, samples, in)
Definition: wavpack.h:106
#define FLOAT_EXCEPTIONS
Definition: wavpackenc.c:212
static int restore_weight(int8_t weight)
Definition: wavpackenc.c:525
static uint32_t log2stereo(int32_t *samples_l, int32_t *samples_r, int nb_samples, int limit)
Definition: wavpackenc.c:669
#define FLOAT_SHIFT_ONES
Definition: wavpackenc.c:207
static const AVClass wavpack_encoder_class
Definition: wavpackenc.c:2968
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
#define WV_CROSS_DECORR
Definition: wavpack.h:34
static int wavpack_encode_block(WavPackEncodeContext *s, int32_t *samples_l, int32_t *samples_r, uint8_t *out, int out_size)
Definition: wavpackenc.c:2476
static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
Definition: bytestream.h:326
static const uint8_t decorr_filter_nterms[]
Definition: wavpackenc.h:651
static double c[64]
#define INC_MED(n)
Definition: wavpack.h:103
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
static av_always_inline int wp_log2(int32_t val)
Definition: wavpack.h:178
void * priv_data
Definition: avcodec.h:1283
static void encode_flush(WavPackEncodeContext *s)
Definition: wavpackenc.c:1975
static void recurse_mono(WavPackEncodeContext *s, WavPackExtraInfo *info, int depth, int delta, uint32_t input_bits)
Definition: wavpackenc.c:720
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static int wv_mono(WavPackEncodeContext *s, int32_t *samples, int no_history, int do_samples)
Definition: wavpackenc.c:1017
struct Decorr dps[MAX_TERMS]
Definition: wavpackenc.c:64
#define COPY_SAMPLES(type, offset, shift)
int channels
number of audio channels
Definition: avcodec.h:1986
uint32_t best_bits
Definition: wavpackenc.c:66
#define EXTRA_SORT_FIRST
Definition: wavpackenc.c:59
static void decorr_mono(int32_t *in_samples, int32_t *out_samples, int nb_samples, struct Decorr *dpp, int dir)
Definition: wavpackenc.c:540
static void reverse_decorr(struct Decorr *dpp)
Definition: wavpackenc.c:1316
#define WV_MAX_SAMPLES
Definition: wavpack.h:56
int sampleptrs_size[MAX_TERMS+2][2]
Definition: wavpackenc.c:89
#define DEC_MED(n)
Definition: wavpack.h:102
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
WavPackWords w
Definition: wavpackenc.c:111
static const uint8_t wp_log2_table[]
Definition: wavpack.h:144
static const uint16_t decorr_filter_sizes[]
Definition: wavpackenc.h:644
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:701
int temp_buffer_size[2][2]
Definition: wavpackenc.c:92
Definition: wavpack.h:83
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:553
signed 16 bits, planar
Definition: samplefmt.h:68
int32_t * samples[2]
Definition: wavpackenc.c:85
AVCodecContext * avctx
Definition: wavpackenc.c:77
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
Definition: internal.h:225
static void sort_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
Definition: wavpackenc.c:783
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:215
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:315
static const int8_t nbits_table[]
Definition: wavpackenc.h:653
This structure stores compressed data.
Definition: avcodec.h:1139
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:225
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1155
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
bitstream writer API