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