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