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