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