2 * WavPack lossless audio encoder
4 * This file is part of FFmpeg.
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.
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.
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
21 #define BITSTREAM_WRITER_LE
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/opt.h"
28 #include "bytestream.h"
29 #include "wavpackenc.h"
32 #define UPDATE_WEIGHT(weight, delta, source, result) \
33 if (source && result) { \
34 int32_t s = (int32_t) (source ^ result) >> 31; \
35 weight = (delta ^ s) + (weight - s); \
38 #define APPLY_WEIGHT_F(weight, sample) (((((sample & 0xffff) * weight) >> 9) + \
39 (((sample & ~0xffff) >> 9) * weight) + 1) >> 1)
41 #define APPLY_WEIGHT_I(weight, sample) ((weight * sample + 512) >> 10)
43 #define APPLY_WEIGHT(weight, sample) (sample != (short) sample ? \
44 APPLY_WEIGHT_F(weight, sample) : APPLY_WEIGHT_I (weight, sample))
46 #define CLEAR(destin) memset(&destin, 0, sizeof(destin));
49 #define SHIFT_MASK (0x1FU << SHIFT_LSB)
52 #define MAG_MASK (0x1FU << MAG_LSB)
55 #define SRATE_MASK (0xFU << SRATE_LSB)
57 #define EXTRA_TRY_DELTAS 1
58 #define EXTRA_ADJUST_DELTAS 2
59 #define EXTRA_SORT_FIRST 4
60 #define EXTRA_BRANCHES 8
61 #define EXTRA_SORT_LAST 16
63 typedef struct WavPackExtraInfo {
64 struct Decorr dps[MAX_TERMS];
65 int nterms, log_limit, gt16bit;
69 typedef struct WavPackWords {
70 int pend_data, holding_one, zeros_acc;
71 int holding_zero, pend_count;
75 typedef struct WavPackEncodeContext {
77 AVCodecContext *avctx;
82 int stereo, stereo_in;
88 int32_t *sampleptrs[MAX_TERMS+2][2];
89 int sampleptrs_size[MAX_TERMS+2][2];
91 int32_t *temp_buffer[2][2];
92 int temp_buffer_size[2][2];
94 int32_t *best_buffer[2];
95 int best_buffer_size[2];
97 int32_t *js_left, *js_right;
98 int js_left_size, js_right_size;
100 int32_t *orig_l, *orig_r;
101 int orig_l_size, orig_r_size;
103 unsigned extra_flags;
113 uint8_t int32_sent_bits, int32_zeros, int32_ones, int32_dups;
114 uint8_t float_flags, float_shift, float_max_exp, max_exp;
115 int32_t shifted_ones, shifted_zeros, shifted_both;
116 int32_t false_zeros, neg_zeros, ordata;
118 int num_terms, shift, joint_stereo, false_stereo;
119 int num_decorrs, num_passes, best_decorr, mask_decorr;
120 struct Decorr decorr_passes[MAX_TERMS];
121 const WavPackDecorrSpec *decorr_specs;
123 } WavPackEncodeContext;
125 static av_cold int wavpack_encode_init(AVCodecContext *avctx)
127 WavPackEncodeContext *s = avctx->priv_data;
131 if (!avctx->frame_size) {
133 if (!(avctx->sample_rate & 1))
134 block_samples = avctx->sample_rate / 2;
136 block_samples = avctx->sample_rate;
138 while (block_samples * avctx->channels > 150000)
141 while (block_samples * avctx->channels < 40000)
143 avctx->frame_size = block_samples;
144 } else if (avctx->frame_size && (avctx->frame_size < 128 ||
145 avctx->frame_size > WV_MAX_SAMPLES)) {
146 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n", avctx->frame_size);
147 return AVERROR(EINVAL);
150 if (avctx->compression_level != FF_COMPRESSION_DEFAULT) {
151 if (avctx->compression_level >= 3) {
152 s->decorr_filter = 3;
154 if (avctx->compression_level >= 8) {
156 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_SORT_LAST|EXTRA_BRANCHES;
157 } else if (avctx->compression_level >= 7) {
159 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
160 } else if (avctx->compression_level >= 6) {
162 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
163 } else if (avctx->compression_level >= 5) {
165 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
166 } else if (avctx->compression_level >= 4) {
168 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_BRANCHES;
170 } else if (avctx->compression_level == 2) {
171 s->decorr_filter = 2;
173 } else if (avctx->compression_level == 1) {
174 s->decorr_filter = 1;
176 } else if (avctx->compression_level < 1) {
177 s->decorr_filter = 0;
182 s->num_decorrs = decorr_filter_sizes[s->decorr_filter];
183 s->decorr_specs = decorr_filters[s->decorr_filter];
185 s->delta_decay = 2.0;
190 static void shift_mono(int32_t *samples, int nb_samples, int shift)
193 for (i = 0; i < nb_samples; i++)
194 samples[i] >>= shift;
197 static void shift_stereo(int32_t *left, int32_t *right,
198 int nb_samples, int shift)
201 for (i = 0; i < nb_samples; i++) {
207 #define FLOAT_SHIFT_ONES 1
208 #define FLOAT_SHIFT_SAME 2
209 #define FLOAT_SHIFT_SENT 4
210 #define FLOAT_ZEROS_SENT 8
211 #define FLOAT_NEG_ZEROS 0x10
212 #define FLOAT_EXCEPTIONS 0x20
214 #define get_mantissa(f) ((f) & 0x7fffff)
215 #define get_exponent(f) (((f) >> 23) & 0xff)
216 #define get_sign(f) (((f) >> 31) & 0x1)
218 static void process_float(WavPackEncodeContext *s, int32_t *sample)
220 int32_t shift_count, value, f = *sample;
222 if (get_exponent(f) == 255) {
223 s->float_flags |= FLOAT_EXCEPTIONS;
226 } else if (get_exponent(f)) {
227 shift_count = s->max_exp - get_exponent(f);
228 value = 0x800000 + get_mantissa(f);
230 shift_count = s->max_exp ? s->max_exp - 1 : 0;
231 value = get_mantissa(f);
234 if (shift_count < 25)
235 value >>= shift_count;
240 if (get_exponent(f) || get_mantissa(f))
242 else if (get_sign(f))
244 } else if (shift_count) {
245 int32_t mask = (1 << shift_count) - 1;
247 if (!(get_mantissa(f) & mask))
249 else if ((get_mantissa(f) & mask) == mask)
256 *sample = get_sign(f) ? -value : value;
259 static int scan_float(WavPackEncodeContext *s,
260 int32_t *samples_l, int32_t *samples_r,
263 uint32_t crc = 0xffffffffu;
266 s->shifted_ones = s->shifted_zeros = s->shifted_both = s->ordata = 0;
267 s->float_shift = s->float_flags = 0;
268 s->false_zeros = s->neg_zeros = 0;
271 if (s->flags & WV_MONO_DATA) {
272 for (i = 0; i < nb_samples; i++) {
273 int32_t f = samples_l[i];
274 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
276 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
277 s->max_exp = get_exponent(f);
280 for (i = 0; i < nb_samples; i++) {
284 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
285 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
286 s->max_exp = get_exponent(f);
289 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
291 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
292 s->max_exp = get_exponent(f);
298 if (s->flags & WV_MONO_DATA) {
299 for (i = 0; i < nb_samples; i++)
300 process_float(s, &samples_l[i]);
302 for (i = 0; i < nb_samples; i++) {
303 process_float(s, &samples_l[i]);
304 process_float(s, &samples_r[i]);
308 s->float_max_exp = s->max_exp;
311 s->float_flags |= FLOAT_SHIFT_SENT;
312 else if (s->shifted_ones && !s->shifted_zeros)
313 s->float_flags |= FLOAT_SHIFT_ONES;
314 else if (s->shifted_ones && s->shifted_zeros)
315 s->float_flags |= FLOAT_SHIFT_SAME;
316 else if (s->ordata && !(s->ordata & 1)) {
320 } while (!(s->ordata & 1));
322 if (s->flags & WV_MONO_DATA)
323 shift_mono(samples_l, nb_samples, s->float_shift);
325 shift_stereo(samples_l, samples_r, nb_samples, s->float_shift);
328 s->flags &= ~MAG_MASK;
331 s->flags += 1 << MAG_LSB;
335 if (s->false_zeros || s->neg_zeros)
336 s->float_flags |= FLOAT_ZEROS_SENT;
339 s->float_flags |= FLOAT_NEG_ZEROS;
341 return s->float_flags & (FLOAT_EXCEPTIONS | FLOAT_ZEROS_SENT |
342 FLOAT_SHIFT_SENT | FLOAT_SHIFT_SAME);
345 static void scan_int23(WavPackEncodeContext *s,
346 int32_t *samples_l, int32_t *samples_r,
349 uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
350 int i, total_shift = 0;
352 s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
354 if (s->flags & WV_MONO_DATA) {
355 for (i = 0; i < nb_samples; i++) {
356 int32_t M = samples_l[i];
358 magdata |= (M < 0) ? ~M : M;
359 xordata |= M ^ -(M & 1);
363 if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
367 for (i = 0; i < nb_samples; i++) {
368 int32_t L = samples_l[i];
369 int32_t R = samples_r[i];
371 magdata |= (L < 0) ? ~L : L;
372 magdata |= (R < 0) ? ~R : R;
373 xordata |= L ^ -(L & 1);
374 xordata |= R ^ -(R & 1);
378 if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
383 s->flags &= ~MAG_MASK;
386 s->flags += 1 << MAG_LSB;
390 if (!(s->flags & MAG_MASK))
395 s->flags -= 1 << MAG_LSB;
399 } while (!(ordata & 1));
400 } else if (anddata & 1) {
402 s->flags -= 1 << MAG_LSB;
406 } while (anddata & 1);
407 } else if (!(xordata & 2)) {
409 s->flags -= 1 << MAG_LSB;
413 } while (!(xordata & 2));
417 s->flags |= WV_INT32_DATA;
419 if (s->flags & WV_MONO_DATA)
420 shift_mono(samples_l, nb_samples, total_shift);
422 shift_stereo(samples_l, samples_r, nb_samples, total_shift);
426 static int scan_int32(WavPackEncodeContext *s,
427 int32_t *samples_l, int32_t *samples_r,
430 uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
431 uint32_t crc = 0xffffffffu;
432 int i, total_shift = 0;
434 s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
436 if (s->flags & WV_MONO_DATA) {
437 for (i = 0; i < nb_samples; i++) {
438 int32_t M = samples_l[i];
440 crc = crc * 9 + (M & 0xffff) * 3 + ((M >> 16) & 0xffff);
441 magdata |= (M < 0) ? ~M : M;
442 xordata |= M ^ -(M & 1);
447 for (i = 0; i < nb_samples; i++) {
448 int32_t L = samples_l[i];
449 int32_t R = samples_r[i];
451 crc = crc * 9 + (L & 0xffff) * 3 + ((L >> 16) & 0xffff);
452 crc = crc * 9 + (R & 0xffff) * 3 + ((R >> 16) & 0xffff);
453 magdata |= (L < 0) ? ~L : L;
454 magdata |= (R < 0) ? ~R : R;
455 xordata |= L ^ -(L & 1);
456 xordata |= R ^ -(R & 1);
463 s->flags &= ~MAG_MASK;
466 s->flags += 1 << MAG_LSB;
470 if (!((s->flags & MAG_MASK) >> MAG_LSB)) {
471 s->flags &= ~WV_INT32_DATA;
477 s->flags -= 1 << MAG_LSB;
481 } while (!(ordata & 1));
482 else if (anddata & 1)
484 s->flags -= 1 << MAG_LSB;
488 } while (anddata & 1);
489 else if (!(xordata & 2))
491 s->flags -= 1 << MAG_LSB;
495 } while (!(xordata & 2));
497 if (((s->flags & MAG_MASK) >> MAG_LSB) > 23) {
498 s->int32_sent_bits = (uint8_t)(((s->flags & MAG_MASK) >> MAG_LSB) - 23);
499 total_shift += s->int32_sent_bits;
500 s->flags &= ~MAG_MASK;
501 s->flags += 23 << MAG_LSB;
505 s->flags |= WV_INT32_DATA;
507 if (s->flags & WV_MONO_DATA)
508 shift_mono(samples_l, nb_samples, total_shift);
510 shift_stereo(samples_l, samples_r, nb_samples, total_shift);
513 return s->int32_sent_bits;
516 static int8_t store_weight(int weight)
518 weight = av_clip(weight, -1024, 1024);
520 weight -= (weight + 64) >> 7;
522 return (weight + 4) >> 3;
525 static int restore_weight(int8_t weight)
529 if ((result = (int) weight << 3) > 0)
530 result += (result + 64) >> 7;
535 static int log2s(int32_t value)
537 return (value < 0) ? -wp_log2(-value) : wp_log2(value);
540 static void decorr_mono(int32_t *in_samples, int32_t *out_samples,
541 int nb_samples, struct Decorr *dpp, int dir)
548 out_samples += (nb_samples - 1);
549 in_samples += (nb_samples - 1);
552 dpp->weightA = restore_weight(store_weight(dpp->weightA));
554 for (i = 0; i < MAX_TERM; i++)
555 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
557 if (dpp->value > MAX_TERM) {
558 while (nb_samples--) {
561 sam_A = ((3 - (dpp->value & 1)) * dpp->samplesA[0] - dpp->samplesA[1]) >> !(dpp->value & 1);
563 dpp->samplesA[1] = dpp->samplesA[0];
564 dpp->samplesA[0] = left = in_samples[0];
566 left -= APPLY_WEIGHT(dpp->weightA, sam_A);
567 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
568 dpp->sumA += dpp->weightA;
569 out_samples[0] = left;
573 } else if (dpp->value > 0) {
574 while (nb_samples--) {
575 int k = (m + dpp->value) & (MAX_TERM - 1);
578 sam_A = dpp->samplesA[m];
579 dpp->samplesA[k] = left = in_samples[0];
580 m = (m + 1) & (MAX_TERM - 1);
582 left -= APPLY_WEIGHT(dpp->weightA, sam_A);
583 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
584 dpp->sumA += dpp->weightA;
585 out_samples[0] = left;
591 if (m && dpp->value > 0 && dpp->value <= MAX_TERM) {
592 int32_t temp_A[MAX_TERM];
594 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
596 for (i = 0; i < MAX_TERM; i++) {
597 dpp->samplesA[i] = temp_A[m];
598 m = (m + 1) & (MAX_TERM - 1);
603 static void reverse_mono_decorr(struct Decorr *dpp)
605 if (dpp->value > MAX_TERM) {
609 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
611 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
613 dpp->samplesA[1] = dpp->samplesA[0];
614 dpp->samplesA[0] = sam_A;
617 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
619 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
621 dpp->samplesA[1] = sam_A;
622 } else if (dpp->value > 1) {
625 for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
628 dpp->samplesA[i] ^= dpp->samplesA[j];
629 dpp->samplesA[j] ^= dpp->samplesA[i];
630 dpp->samplesA[i] ^= dpp->samplesA[j];
635 static uint32_t log2sample(uint32_t v, int limit, uint32_t *result)
639 if ((v += v >> 9) < (1 << 8)) {
640 dbits = nbits_table[v];
641 result += (dbits << 8) + wp_log2_table[(v << (9 - dbits)) & 0xff];
644 dbits = nbits_table[v >> 8] + 8;
645 else if (v < (1L << 24))
646 dbits = nbits_table[v >> 16] + 16;
648 dbits = nbits_table[v >> 24] + 24;
650 result += dbits = (dbits << 8) + wp_log2_table[(v >> (dbits - 9)) & 0xff];
652 if (limit && dbits >= limit)
659 static uint32_t log2mono(int32_t *samples, int nb_samples, int limit)
662 while (nb_samples--) {
663 if (log2sample(abs(*samples++), limit, &result))
669 static uint32_t log2stereo(int32_t *samples_l, int32_t *samples_r,
670 int nb_samples, int limit)
673 while (nb_samples--) {
674 if (log2sample(abs(*samples_l++), limit, &result) ||
675 log2sample(abs(*samples_r++), limit, &result))
681 static void decorr_mono_buffer(int32_t *samples, int32_t *outsamples,
682 int nb_samples, struct Decorr *dpp,
685 struct Decorr dp, *dppi = dpp + tindex;
686 int delta = dppi->delta, pre_delta, term = dppi->value;
693 pre_delta = delta + 1;
697 dp.delta = pre_delta;
698 decorr_mono(samples, outsamples, FFMIN(2048, nb_samples), &dp, -1);
702 reverse_mono_decorr(&dp);
706 memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
707 dppi->weightA = dp.weightA;
711 decorr_mono(samples, outsamples, nb_samples, &dp, 1);
713 memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
714 dppi->weightA = dp.weightA = dp.sumA / nb_samples;
717 decorr_mono(samples, outsamples, nb_samples, &dp, 1);
720 static void recurse_mono(WavPackEncodeContext *s, WavPackExtraInfo *info,
721 int depth, int delta, uint32_t input_bits)
723 int term, branches = s->num_branches - depth;
724 int32_t *samples, *outsamples;
725 uint32_t term_bits[22], bits;
727 if (branches < 1 || depth + 1 == info->nterms)
731 samples = s->sampleptrs[depth][0];
732 outsamples = s->sampleptrs[depth + 1][0];
734 for (term = 1; term <= 18; term++) {
735 if (term == 17 && branches == 1 && depth + 1 < info->nterms)
738 if (term > 8 && term < 17)
741 if (!s->extra_flags && (term > 4 && term < 17))
744 info->dps[depth].value = term;
745 info->dps[depth].delta = delta;
746 decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
747 bits = log2mono(outsamples, s->block_samples, info->log_limit);
749 if (bits < info->best_bits) {
750 info->best_bits = bits;
751 CLEAR(s->decorr_passes);
752 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
753 memcpy(s->sampleptrs[info->nterms + 1][0],
754 s->sampleptrs[depth + 1][0], s->block_samples * 4);
757 term_bits[term + 3] = bits;
760 while (depth + 1 < info->nterms && branches--) {
761 uint32_t local_best_bits = input_bits;
762 int best_term = 0, i;
764 for (i = 0; i < 22; i++)
765 if (term_bits[i] && term_bits[i] < local_best_bits) {
766 local_best_bits = term_bits[i];
773 term_bits[best_term + 3] = 0;
775 info->dps[depth].value = best_term;
776 info->dps[depth].delta = delta;
777 decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
779 recurse_mono(s, info, depth + 1, delta, local_best_bits);
783 static void sort_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
791 memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
794 for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
796 if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
799 if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
800 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
801 s->block_samples, info->dps, ri);
805 info->dps[ri ] = s->decorr_passes[ri+1];
806 info->dps[ri+1] = s->decorr_passes[ri ];
808 for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
809 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
810 s->block_samples, info->dps, i);
812 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
813 if (bits < info->best_bits) {
815 info->best_bits = bits;
816 CLEAR(s->decorr_passes);
817 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
818 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
819 s->block_samples * 4);
821 info->dps[ri ] = s->decorr_passes[ri];
822 info->dps[ri+1] = s->decorr_passes[ri+1];
823 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
824 s->block_samples, info->dps, ri);
830 static void delta_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
832 int lower = 0, delta, d;
835 if (!s->decorr_passes[0].value)
837 delta = s->decorr_passes[0].delta;
839 for (d = delta - 1; d >= 0; d--) {
842 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
843 info->dps[i].value = s->decorr_passes[i].value;
844 info->dps[i].delta = d;
845 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
846 s->block_samples, info->dps, i);
849 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
850 if (bits >= info->best_bits)
854 info->best_bits = bits;
855 CLEAR(s->decorr_passes);
856 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
857 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
858 s->block_samples * 4);
861 for (d = delta + 1; !lower && d <= 7; d++) {
864 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
865 info->dps[i].value = s->decorr_passes[i].value;
866 info->dps[i].delta = d;
867 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
868 s->block_samples, info->dps, i);
871 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
872 if (bits >= info->best_bits)
875 info->best_bits = bits;
876 CLEAR(s->decorr_passes);
877 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
878 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
879 s->block_samples * 4);
883 static int allocate_buffers2(WavPackEncodeContext *s, int nterms)
887 for (i = 0; i < nterms + 2; i++) {
888 av_fast_padded_malloc(&s->sampleptrs[i][0], &s->sampleptrs_size[i][0],
889 s->block_samples * 4);
890 if (!s->sampleptrs[i][0])
891 return AVERROR(ENOMEM);
892 if (!(s->flags & WV_MONO_DATA)) {
893 av_fast_padded_malloc(&s->sampleptrs[i][1], &s->sampleptrs_size[i][1],
894 s->block_samples * 4);
895 if (!s->sampleptrs[i][1])
896 return AVERROR(ENOMEM);
903 static int allocate_buffers(WavPackEncodeContext *s)
907 for (i = 0; i < 2; i++) {
908 av_fast_padded_malloc(&s->best_buffer[0], &s->best_buffer_size[0],
909 s->block_samples * 4);
910 if (!s->best_buffer[0])
911 return AVERROR(ENOMEM);
913 av_fast_padded_malloc(&s->temp_buffer[i][0], &s->temp_buffer_size[i][0],
914 s->block_samples * 4);
915 if (!s->temp_buffer[i][0])
916 return AVERROR(ENOMEM);
917 if (!(s->flags & WV_MONO_DATA)) {
918 av_fast_padded_malloc(&s->best_buffer[1], &s->best_buffer_size[1],
919 s->block_samples * 4);
920 if (!s->best_buffer[1])
921 return AVERROR(ENOMEM);
923 av_fast_padded_malloc(&s->temp_buffer[i][1], &s->temp_buffer_size[i][1],
924 s->block_samples * 4);
925 if (!s->temp_buffer[i][1])
926 return AVERROR(ENOMEM);
933 static void analyze_mono(WavPackEncodeContext *s, int32_t *samples, int do_samples)
935 WavPackExtraInfo info;
938 info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
939 info.log_limit = FFMIN(6912, info.log_limit);
941 info.nterms = s->num_terms;
943 if (allocate_buffers2(s, s->num_terms))
946 memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
947 memcpy(s->sampleptrs[0][0], samples, s->block_samples * 4);
949 for (i = 0; i < info.nterms && info.dps[i].value; i++)
950 decorr_mono(s->sampleptrs[i][0], s->sampleptrs[i + 1][0],
951 s->block_samples, info.dps + i, 1);
953 info.best_bits = log2mono(s->sampleptrs[info.nterms][0], s->block_samples, 0) * 1;
954 memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
956 if (s->extra_flags & EXTRA_BRANCHES)
957 recurse_mono(s, &info, 0, (int) floor(s->delta_decay + 0.5),
958 log2mono(s->sampleptrs[0][0], s->block_samples, 0));
960 if (s->extra_flags & EXTRA_SORT_FIRST)
963 if (s->extra_flags & EXTRA_TRY_DELTAS) {
964 delta_mono(s, &info);
966 if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
967 s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
969 s->delta_decay = 2.0;
972 if (s->extra_flags & EXTRA_SORT_LAST)
976 memcpy(samples, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
978 for (i = 0; i < info.nterms; i++)
979 if (!s->decorr_passes[i].value)
985 static void scan_word(WavPackEncodeContext *s, WvChannel *c,
986 int32_t *samples, int nb_samples, int dir)
989 samples += nb_samples - 1;
991 while (nb_samples--) {
992 uint32_t low, value = labs(samples[0]);
994 if (value < GET_MED(0)) {
1000 if (value - low < GET_MED(1)) {
1006 if (value - low < GET_MED(2)) {
1017 static int wv_mono(WavPackEncodeContext *s, int32_t *samples,
1018 int no_history, int do_samples)
1020 struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1021 int nb_samples = s->block_samples;
1022 int buf_size = sizeof(int32_t) * nb_samples;
1023 uint32_t best_size = UINT32_MAX, size;
1024 int log_limit, pi, i, ret;
1026 for (i = 0; i < nb_samples; i++)
1030 if (i == nb_samples) {
1031 CLEAR(s->decorr_passes);
1037 log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1038 log_limit = FFMIN(6912, log_limit);
1040 if ((ret = allocate_buffers(s)) < 0)
1043 if (no_history || s->num_passes >= 7)
1044 s->best_decorr = s->mask_decorr = 0;
1046 for (pi = 0; pi < s->num_passes;) {
1047 const WavPackDecorrSpec *wpds;
1053 if (s->mask_decorr == 0)
1056 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1058 if (c == s->best_decorr) {
1059 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1064 wpds = &s->decorr_specs[c];
1065 nterms = decorr_filter_nterms[s->decorr_filter];
1068 memcpy(s->temp_buffer[0][0], samples, buf_size);
1069 CLEAR(save_decorr_passes);
1071 for (j = 0; j < nterms; j++) {
1072 CLEAR(temp_decorr_pass);
1073 temp_decorr_pass.delta = wpds->delta;
1074 temp_decorr_pass.value = wpds->terms[j];
1076 if (temp_decorr_pass.value < 0)
1077 temp_decorr_pass.value = 1;
1079 decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1080 FFMIN(nb_samples, 2048), &temp_decorr_pass, -1);
1083 CLEAR(temp_decorr_pass.samplesA);
1085 reverse_mono_decorr(&temp_decorr_pass);
1088 memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1089 decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1090 nb_samples, &temp_decorr_pass, 1);
1093 size = log2mono(s->temp_buffer[j&1][0], nb_samples, log_limit);
1094 if (size != UINT32_MAX || !nterms)
1099 if (size < best_size) {
1100 memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1101 memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1102 s->num_terms = nterms;
1108 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1112 analyze_mono(s, samples, do_samples);
1113 else if (do_samples)
1114 memcpy(samples, s->best_buffer[0], buf_size);
1116 if (no_history || s->extra_flags) {
1118 scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1123 static void decorr_stereo(int32_t *in_left, int32_t *in_right,
1124 int32_t *out_left, int32_t *out_right,
1125 int nb_samples, struct Decorr *dpp, int dir)
1129 dpp->sumA = dpp->sumB = 0;
1132 out_left += nb_samples - 1;
1133 out_right += nb_samples - 1;
1134 in_left += nb_samples - 1;
1135 in_right += nb_samples - 1;
1138 dpp->weightA = restore_weight(store_weight(dpp->weightA));
1139 dpp->weightB = restore_weight(store_weight(dpp->weightB));
1141 for (i = 0; i < MAX_TERM; i++) {
1142 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1143 dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1146 switch (dpp->value) {
1148 while (nb_samples--) {
1151 sam = dpp->samplesA[0];
1152 dpp->samplesA[0] = dpp->samplesA[1];
1153 out_left[0] = tmp = (dpp->samplesA[1] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1154 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1155 dpp->sumA += dpp->weightA;
1157 sam = dpp->samplesB[0];
1158 dpp->samplesB[0] = dpp->samplesB[1];
1159 out_right[0] = tmp = (dpp->samplesB[1] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1160 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1161 dpp->sumB += dpp->weightB;
1170 while (nb_samples--) {
1173 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1174 dpp->samplesA[1] = dpp->samplesA[0];
1175 out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1176 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1177 dpp->sumA += dpp->weightA;
1179 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1180 dpp->samplesB[1] = dpp->samplesB[0];
1181 out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT (dpp->weightB, sam);
1182 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1183 dpp->sumB += dpp->weightB;
1192 while (nb_samples--) {
1195 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1196 dpp->samplesA[1] = dpp->samplesA[0];
1197 out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1198 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1199 dpp->sumA += dpp->weightA;
1201 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1202 dpp->samplesB[1] = dpp->samplesB[0];
1203 out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1204 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1205 dpp->sumB += dpp->weightB;
1214 int k = dpp->value & (MAX_TERM - 1);
1216 while (nb_samples--) {
1219 sam = dpp->samplesA[m];
1220 out_left[0] = tmp = (dpp->samplesA[k] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1221 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1222 dpp->sumA += dpp->weightA;
1224 sam = dpp->samplesB[m];
1225 out_right[0] = tmp = (dpp->samplesB[k] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1226 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1227 dpp->sumB += dpp->weightB;
1233 m = (m + 1) & (MAX_TERM - 1);
1234 k = (k + 1) & (MAX_TERM - 1);
1238 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1241 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1242 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1244 for (k = 0; k < MAX_TERM; k++) {
1245 dpp->samplesA[k] = temp_A[m];
1246 dpp->samplesB[k] = temp_B[m];
1247 m = (m + 1) & (MAX_TERM - 1);
1253 while (nb_samples--) {
1254 int32_t sam_A, sam_B, tmp;
1256 sam_A = dpp->samplesA[0];
1257 out_left[0] = tmp = (sam_B = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1258 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1259 dpp->sumA += dpp->weightA;
1261 out_right[0] = tmp = (dpp->samplesA[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1262 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1263 dpp->sumB += dpp->weightB;
1272 while (nb_samples--) {
1273 int32_t sam_A, sam_B, tmp;
1275 sam_B = dpp->samplesB[0];
1276 out_right[0] = tmp = (sam_A = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1277 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1278 dpp->sumB += dpp->weightB;
1280 out_left[0] = tmp = (dpp->samplesB[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1281 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1282 dpp->sumA += dpp->weightA;
1291 while (nb_samples--) {
1292 int32_t sam_A, sam_B, tmp;
1294 sam_A = dpp->samplesA[0];
1295 sam_B = dpp->samplesB[0];
1297 dpp->samplesA[0] = tmp = in_right[0];
1298 out_right[0] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
1299 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1300 dpp->sumB += dpp->weightB;
1302 dpp->samplesB[0] = tmp = in_left[0];
1303 out_left[0] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
1304 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1305 dpp->sumA += dpp->weightA;
1316 static void reverse_decorr(struct Decorr *dpp)
1318 if (dpp->value > MAX_TERM) {
1319 int32_t sam_A, sam_B;
1321 if (dpp->value & 1) {
1322 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1323 sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1325 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1326 sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1329 dpp->samplesA[1] = dpp->samplesA[0];
1330 dpp->samplesB[1] = dpp->samplesB[0];
1331 dpp->samplesA[0] = sam_A;
1332 dpp->samplesB[0] = sam_B;
1334 if (dpp->value & 1) {
1335 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1336 sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1338 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1339 sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1342 dpp->samplesA[1] = sam_A;
1343 dpp->samplesB[1] = sam_B;
1344 } else if (dpp->value > 1) {
1347 for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
1348 i &= (MAX_TERM - 1);
1349 j &= (MAX_TERM - 1);
1350 dpp->samplesA[i] ^= dpp->samplesA[j];
1351 dpp->samplesA[j] ^= dpp->samplesA[i];
1352 dpp->samplesA[i] ^= dpp->samplesA[j];
1353 dpp->samplesB[i] ^= dpp->samplesB[j];
1354 dpp->samplesB[j] ^= dpp->samplesB[i];
1355 dpp->samplesB[i] ^= dpp->samplesB[j];
1360 static void decorr_stereo_quick(int32_t *in_left, int32_t *in_right,
1361 int32_t *out_left, int32_t *out_right,
1362 int nb_samples, struct Decorr *dpp)
1366 dpp->weightA = restore_weight(store_weight(dpp->weightA));
1367 dpp->weightB = restore_weight(store_weight(dpp->weightB));
1369 for (i = 0; i < MAX_TERM; i++) {
1370 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1371 dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1374 switch (dpp->value) {
1376 for (i = 0; i < nb_samples; i++) {
1379 sam = dpp->samplesA[0];
1380 dpp->samplesA[0] = dpp->samplesA[1];
1381 out_left[i] = tmp = (dpp->samplesA[1] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1382 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1384 sam = dpp->samplesB[0];
1385 dpp->samplesB[0] = dpp->samplesB[1];
1386 out_right[i] = tmp = (dpp->samplesB[1] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1387 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1391 for (i = 0; i < nb_samples; i++) {
1394 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1395 dpp->samplesA[1] = dpp->samplesA[0];
1396 out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1397 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1399 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1400 dpp->samplesB[1] = dpp->samplesB[0];
1401 out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1402 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1406 for (i = 0; i < nb_samples; i++) {
1409 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1410 dpp->samplesA[1] = dpp->samplesA[0];
1411 out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1412 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1414 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1415 dpp->samplesB[1] = dpp->samplesB[0];
1416 out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1417 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1421 int k = dpp->value & (MAX_TERM - 1);
1423 for (i = 0; i < nb_samples; i++) {
1426 sam = dpp->samplesA[m];
1427 out_left[i] = tmp = (dpp->samplesA[k] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1428 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1430 sam = dpp->samplesB[m];
1431 out_right[i] = tmp = (dpp->samplesB[k] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1432 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1434 m = (m + 1) & (MAX_TERM - 1);
1435 k = (k + 1) & (MAX_TERM - 1);
1439 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1442 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1443 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1445 for (k = 0; k < MAX_TERM; k++) {
1446 dpp->samplesA[k] = temp_A[m];
1447 dpp->samplesB[k] = temp_B[m];
1448 m = (m + 1) & (MAX_TERM - 1);
1454 for (i = 0; i < nb_samples; i++) {
1455 int32_t sam_A, sam_B, tmp;
1457 sam_A = dpp->samplesA[0];
1458 out_left[i] = tmp = (sam_B = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1459 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1461 out_right[i] = tmp = (dpp->samplesA[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1462 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1466 for (i = 0; i < nb_samples; i++) {
1467 int32_t sam_A, sam_B, tmp;
1469 sam_B = dpp->samplesB[0];
1470 out_right[i] = tmp = (sam_A = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1471 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1473 out_left[i] = tmp = (dpp->samplesB[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1474 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1478 for (i = 0; i < nb_samples; i++) {
1479 int32_t sam_A, sam_B, tmp;
1481 sam_A = dpp->samplesA[0];
1482 sam_B = dpp->samplesB[0];
1484 dpp->samplesA[0] = tmp = in_right[i];
1485 out_right[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
1486 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1488 dpp->samplesB[0] = tmp = in_left[i];
1489 out_left[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
1490 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1496 static void decorr_stereo_buffer(WavPackExtraInfo *info,
1497 int32_t *in_left, int32_t *in_right,
1498 int32_t *out_left, int32_t *out_right,
1499 int nb_samples, int tindex)
1501 struct Decorr dp = {0}, *dppi = info->dps + tindex;
1502 int delta = dppi->delta, pre_delta;
1503 int term = dppi->value;
1510 pre_delta = delta + 1;
1513 dp.delta = pre_delta;
1514 decorr_stereo(in_left, in_right, out_left, out_right,
1515 FFMIN(2048, nb_samples), &dp, -1);
1519 reverse_decorr(&dp);
1525 memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
1526 memcpy(dppi->samplesB, dp.samplesB, sizeof(dp.samplesB));
1527 dppi->weightA = dp.weightA;
1528 dppi->weightB = dp.weightB;
1532 decorr_stereo(in_left, in_right, out_left, out_right, nb_samples, &dp, 1);
1534 memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
1535 memcpy(dp.samplesB, dppi->samplesB, sizeof(dp.samplesB));
1536 dppi->weightA = dp.weightA = dp.sumA / nb_samples;
1537 dppi->weightB = dp.weightB = dp.sumB / nb_samples;
1541 decorr_stereo(in_left, in_right, out_left, out_right,
1542 nb_samples, &dp, 1);
1544 decorr_stereo_quick(in_left, in_right, out_left, out_right,
1548 static void sort_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1556 memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
1559 for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
1561 if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
1564 if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
1565 decorr_stereo_buffer(info,
1566 s->sampleptrs[ri ][0], s->sampleptrs[ri ][1],
1567 s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1568 s->block_samples, ri);
1572 info->dps[ri ] = s->decorr_passes[ri+1];
1573 info->dps[ri+1] = s->decorr_passes[ri ];
1575 for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
1576 decorr_stereo_buffer(info,
1577 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1578 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1579 s->block_samples, i);
1581 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1582 s->block_samples, info->log_limit);
1584 if (bits < info->best_bits) {
1586 info->best_bits = bits;
1587 CLEAR(s->decorr_passes);
1588 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1589 memcpy(s->sampleptrs[info->nterms + 1][0],
1590 s->sampleptrs[i][0], s->block_samples * 4);
1591 memcpy(s->sampleptrs[info->nterms + 1][1],
1592 s->sampleptrs[i][1], s->block_samples * 4);
1594 info->dps[ri ] = s->decorr_passes[ri ];
1595 info->dps[ri+1] = s->decorr_passes[ri+1];
1596 decorr_stereo_buffer(info,
1597 s->sampleptrs[ri ][0], s->sampleptrs[ri ][1],
1598 s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1599 s->block_samples, ri);
1605 static void delta_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1607 int lower = 0, delta, d, i;
1610 if (!s->decorr_passes[0].value)
1612 delta = s->decorr_passes[0].delta;
1614 for (d = delta - 1; d >= 0; d--) {
1615 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1616 info->dps[i].value = s->decorr_passes[i].value;
1617 info->dps[i].delta = d;
1618 decorr_stereo_buffer(info,
1619 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1620 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1621 s->block_samples, i);
1624 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1625 s->block_samples, info->log_limit);
1626 if (bits >= info->best_bits)
1629 info->best_bits = bits;
1630 CLEAR(s->decorr_passes);
1631 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1632 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
1633 s->block_samples * 4);
1634 memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[i][1],
1635 s->block_samples * 4);
1638 for (d = delta + 1; !lower && d <= 7; d++) {
1639 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1640 info->dps[i].value = s->decorr_passes[i].value;
1641 info->dps[i].delta = d;
1642 decorr_stereo_buffer(info,
1643 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1644 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1645 s->block_samples, i);
1648 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1649 s->block_samples, info->log_limit);
1651 if (bits < info->best_bits) {
1652 info->best_bits = bits;
1653 CLEAR(s->decorr_passes);
1654 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1655 memcpy(s->sampleptrs[info->nterms + 1][0],
1656 s->sampleptrs[i][0], s->block_samples * 4);
1657 memcpy(s->sampleptrs[info->nterms + 1][1],
1658 s->sampleptrs[i][1], s->block_samples * 4);
1665 static void recurse_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info,
1666 int depth, int delta, uint32_t input_bits)
1668 int term, branches = s->num_branches - depth;
1669 int32_t *in_left, *in_right, *out_left, *out_right;
1670 uint32_t term_bits[22], bits;
1672 if (branches < 1 || depth + 1 == info->nterms)
1676 in_left = s->sampleptrs[depth ][0];
1677 in_right = s->sampleptrs[depth ][1];
1678 out_left = s->sampleptrs[depth + 1][0];
1679 out_right = s->sampleptrs[depth + 1][1];
1681 for (term = -3; term <= 18; term++) {
1682 if (!term || (term > 8 && term < 17))
1685 if (term == 17 && branches == 1 && depth + 1 < info->nterms)
1688 if (term == -1 || term == -2)
1689 if (!(s->flags & WV_CROSS_DECORR))
1692 if (!s->extra_flags && (term > 4 && term < 17))
1695 info->dps[depth].value = term;
1696 info->dps[depth].delta = delta;
1697 decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1698 s->block_samples, depth);
1699 bits = log2stereo(out_left, out_right, s->block_samples, info->log_limit);
1701 if (bits < info->best_bits) {
1702 info->best_bits = bits;
1703 CLEAR(s->decorr_passes);
1704 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
1705 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[depth + 1][0],
1706 s->block_samples * 4);
1707 memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[depth + 1][1],
1708 s->block_samples * 4);
1711 term_bits[term + 3] = bits;
1714 while (depth + 1 < info->nterms && branches--) {
1715 uint32_t local_best_bits = input_bits;
1716 int best_term = 0, i;
1718 for (i = 0; i < 22; i++)
1719 if (term_bits[i] && term_bits[i] < local_best_bits) {
1720 local_best_bits = term_bits[i];
1727 term_bits[best_term + 3] = 0;
1729 info->dps[depth].value = best_term;
1730 info->dps[depth].delta = delta;
1731 decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1732 s->block_samples, depth);
1734 recurse_stereo(s, info, depth + 1, delta, local_best_bits);
1738 static void analyze_stereo(WavPackEncodeContext *s,
1739 int32_t *in_left, int32_t *in_right,
1742 WavPackExtraInfo info;
1745 info.gt16bit = ((s->flags & MAG_MASK) >> MAG_LSB) >= 16;
1747 info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1748 info.log_limit = FFMIN(6912, info.log_limit);
1750 info.nterms = s->num_terms;
1752 if (allocate_buffers2(s, s->num_terms))
1755 memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
1756 memcpy(s->sampleptrs[0][0], in_left, s->block_samples * 4);
1757 memcpy(s->sampleptrs[0][1], in_right, s->block_samples * 4);
1759 for (i = 0; i < info.nterms && info.dps[i].value; i++)
1761 decorr_stereo(s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1762 s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1763 s->block_samples, info.dps + i, 1);
1765 decorr_stereo_quick(s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1766 s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1767 s->block_samples, info.dps + i);
1769 info.best_bits = log2stereo(s->sampleptrs[info.nterms][0], s->sampleptrs[info.nterms][1],
1770 s->block_samples, 0);
1772 memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
1773 memcpy(s->sampleptrs[info.nterms + 1][1], s->sampleptrs[i][1], s->block_samples * 4);
1775 if (s->extra_flags & EXTRA_BRANCHES)
1776 recurse_stereo(s, &info, 0, (int) floor(s->delta_decay + 0.5),
1777 log2stereo(s->sampleptrs[0][0], s->sampleptrs[0][1],
1778 s->block_samples, 0));
1780 if (s->extra_flags & EXTRA_SORT_FIRST)
1781 sort_stereo(s, &info);
1783 if (s->extra_flags & EXTRA_TRY_DELTAS) {
1784 delta_stereo(s, &info);
1786 if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
1787 s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
1789 s->delta_decay = 2.0;
1792 if (s->extra_flags & EXTRA_SORT_LAST)
1793 sort_stereo(s, &info);
1796 memcpy(in_left, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
1797 memcpy(in_right, s->sampleptrs[info.nterms + 1][1], s->block_samples * 4);
1800 for (i = 0; i < info.nterms; i++)
1801 if (!s->decorr_passes[i].value)
1807 static int wv_stereo(WavPackEncodeContext *s,
1808 int32_t *samples_l, int32_t *samples_r,
1809 int no_history, int do_samples)
1811 struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1812 int nb_samples = s->block_samples, ret;
1813 int buf_size = sizeof(int32_t) * nb_samples;
1814 int log_limit, force_js = 0, force_ts = 0, got_js = 0, pi, i;
1815 uint32_t best_size = UINT32_MAX, size;
1817 for (i = 0; i < nb_samples; i++)
1818 if (samples_l[i] || samples_r[i])
1821 if (i == nb_samples) {
1822 s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1823 CLEAR(s->decorr_passes);
1829 log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1830 log_limit = FFMIN(6912, log_limit);
1833 force_js = s->joint > 0;
1834 force_ts = s->joint < 0;
1837 if ((ret = allocate_buffers(s)) < 0)
1840 if (no_history || s->num_passes >= 7)
1841 s->best_decorr = s->mask_decorr = 0;
1843 for (pi = 0; pi < s->num_passes;) {
1844 const WavPackDecorrSpec *wpds;
1850 if (s->mask_decorr == 0)
1853 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1855 if (c == s->best_decorr) {
1856 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1861 wpds = &s->decorr_specs[c];
1862 nterms = decorr_filter_nterms[s->decorr_filter];
1865 if (force_js || (wpds->joint_stereo && !force_ts)) {
1867 av_fast_padded_malloc(&s->js_left, &s->js_left_size, buf_size);
1868 av_fast_padded_malloc(&s->js_right, &s->js_right_size, buf_size);
1869 memcpy(s->js_left, samples_l, buf_size);
1870 memcpy(s->js_right, samples_r, buf_size);
1872 for (i = 0; i < nb_samples; i++)
1873 s->js_right[i] += ((s->js_left[i] -= s->js_right[i]) >> 1);
1877 memcpy(s->temp_buffer[0][0], s->js_left, buf_size);
1878 memcpy(s->temp_buffer[0][1], s->js_right, buf_size);
1880 memcpy(s->temp_buffer[0][0], samples_l, buf_size);
1881 memcpy(s->temp_buffer[0][1], samples_r, buf_size);
1884 CLEAR(save_decorr_passes);
1886 for (j = 0; j < nterms; j++) {
1887 CLEAR(temp_decorr_pass);
1888 temp_decorr_pass.delta = wpds->delta;
1889 temp_decorr_pass.value = wpds->terms[j];
1891 if (temp_decorr_pass.value < 0 && !(s->flags & WV_CROSS_DECORR))
1892 temp_decorr_pass.value = -3;
1894 decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1895 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1896 FFMIN(2048, nb_samples), &temp_decorr_pass, -1);
1899 CLEAR(temp_decorr_pass.samplesA);
1900 CLEAR(temp_decorr_pass.samplesB);
1902 reverse_decorr(&temp_decorr_pass);
1905 memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1907 if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16)
1908 decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1909 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1910 nb_samples, &temp_decorr_pass, 1);
1912 decorr_stereo_quick(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1913 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1914 nb_samples, &temp_decorr_pass);
1917 size = log2stereo(s->temp_buffer[j&1][0], s->temp_buffer[j&1][1],
1918 nb_samples, log_limit);
1919 if (size != UINT32_MAX || !nterms)
1924 if (size < best_size) {
1925 memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1926 memcpy(s->best_buffer[1], s->temp_buffer[j&1][1], buf_size);
1927 memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1928 s->num_terms = nterms;
1934 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1937 if (force_js || (s->decorr_specs[s->best_decorr].joint_stereo && !force_ts))
1938 s->flags |= WV_JOINT_STEREO;
1940 s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1942 if (s->extra_flags) {
1943 if (s->flags & WV_JOINT_STEREO) {
1944 analyze_stereo(s, s->js_left, s->js_right, do_samples);
1947 memcpy(samples_l, s->js_left, buf_size);
1948 memcpy(samples_r, s->js_right, buf_size);
1951 analyze_stereo(s, samples_l, samples_r, do_samples);
1952 } else if (do_samples) {
1953 memcpy(samples_l, s->best_buffer[0], buf_size);
1954 memcpy(samples_r, s->best_buffer[1], buf_size);
1957 if (s->extra_flags || no_history ||
1958 s->joint_stereo != s->decorr_specs[s->best_decorr].joint_stereo) {
1959 s->joint_stereo = s->decorr_specs[s->best_decorr].joint_stereo;
1961 scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1962 scan_word(s, &s->w.c[1], s->best_buffer[1], nb_samples, -1);
1967 #define count_bits(av) ( \
1968 (av) < (1 << 8) ? nbits_table[av] : \
1970 (av) < (1L << 16) ? nbits_table[(av) >> 8] + 8 : \
1971 ((av) < (1L << 24) ? nbits_table[(av) >> 16] + 16 : nbits_table[(av) >> 24] + 24) \
1975 static void encode_flush(WavPackEncodeContext *s)
1977 WavPackWords *w = &s->w;
1978 PutBitContext *pb = &s->pb;
1981 int cbits = count_bits(w->zeros_acc);
1985 put_bits(pb, 31, 0x7FFFFFFF);
1988 put_bits(pb, cbits, (1 << cbits) - 1);
1995 while (w->zeros_acc > 1) {
1996 put_bits(pb, 1, w->zeros_acc & 1);
2003 if (w->holding_one) {
2004 if (w->holding_one >= 16) {
2007 put_bits(pb, 16, (1 << 16) - 1);
2009 w->holding_one -= 16;
2010 cbits = count_bits(w->holding_one);
2014 put_bits(pb, 31, 0x7FFFFFFF);
2017 put_bits(pb, cbits, (1 << cbits) - 1);
2024 while (w->holding_one > 1) {
2025 put_bits(pb, 1, w->holding_one & 1);
2026 w->holding_one >>= 1;
2029 w->holding_zero = 0;
2031 put_bits(pb, w->holding_one, (1 << w->holding_one) - 1);
2037 if (w->holding_zero) {
2039 w->holding_zero = 0;
2042 if (w->pend_count) {
2043 put_bits(pb, w->pend_count, w->pend_data);
2044 w->pend_data = w->pend_count = 0;
2048 static void wavpack_encode_sample(WavPackEncodeContext *s, WvChannel *c, int32_t sample)
2050 WavPackWords *w = &s->w;
2051 uint32_t ones_count, low, high;
2052 int sign = sample < 0;
2054 if (s->w.c[0].median[0] < 2 && !s->w.holding_zero && s->w.c[1].median[0] < 2) {
2062 } else if (sample) {
2063 put_bits(&s->pb, 1, 0);
2065 CLEAR(s->w.c[0].median);
2066 CLEAR(s->w.c[1].median);
2075 if (sample < (int32_t) GET_MED(0)) {
2076 ones_count = low = 0;
2077 high = GET_MED(0) - 1;
2083 if (sample - low < GET_MED(1)) {
2085 high = low + GET_MED(1) - 1;
2091 if (sample - low < GET_MED(2)) {
2093 high = low + GET_MED(2) - 1;
2096 ones_count = 2 + (sample - low) / GET_MED(2);
2097 low += (ones_count - 2) * GET_MED(2);
2098 high = low + GET_MED(2) - 1;
2104 if (w->holding_zero) {
2111 w->holding_zero = 1;
2114 w->holding_zero = 0;
2116 w->holding_zero = 1;
2118 w->holding_one = ones_count * 2;
2121 uint32_t maxcode = high - low, code = sample - low;
2122 int bitcount = count_bits(maxcode);
2123 uint32_t extras = (1 << bitcount) - maxcode - 1;
2125 if (code < extras) {
2126 w->pend_data |= code << w->pend_count;
2127 w->pend_count += bitcount - 1;
2129 w->pend_data |= ((code + extras) >> 1) << w->pend_count;
2130 w->pend_count += bitcount - 1;
2131 w->pend_data |= ((code + extras) & 1) << w->pend_count++;
2135 w->pend_data |= ((int32_t) sign << w->pend_count++);
2137 if (!w->holding_zero)
2141 static void pack_int32(WavPackEncodeContext *s,
2142 int32_t *samples_l, int32_t *samples_r,
2145 const int sent_bits = s->int32_sent_bits;
2146 int32_t value, mask = (1 << sent_bits) - 1;
2147 PutBitContext *pb = &s->pb;
2150 pre_shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2155 if (s->flags & WV_MONO_DATA) {
2156 for (i = 0; i < nb_samples; i++) {
2157 value = (samples_l[i] >> pre_shift) & mask;
2158 put_bits(pb, sent_bits, value);
2161 for (i = 0; i < nb_samples; i++) {
2162 value = (samples_l[i] >> pre_shift) & mask;
2163 put_bits(pb, sent_bits, value);
2164 value = (samples_r[i] >> pre_shift) & mask;
2165 put_bits(pb, sent_bits, value);
2170 static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
2172 const int max_exp = s->float_max_exp;
2173 PutBitContext *pb = &s->pb;
2174 int32_t value, shift_count;
2176 if (get_exponent(*sample) == 255) {
2177 if (get_mantissa(*sample)) {
2179 put_bits(pb, 23, get_mantissa(*sample));
2186 } else if (get_exponent(*sample)) {
2187 shift_count = max_exp - get_exponent(*sample);
2188 value = 0x800000 + get_mantissa(*sample);
2190 shift_count = max_exp ? max_exp - 1 : 0;
2191 value = get_mantissa(*sample);
2194 if (shift_count < 25)
2195 value >>= shift_count;
2200 if (s->float_flags & FLOAT_ZEROS_SENT) {
2201 if (get_exponent(*sample) || get_mantissa(*sample)) {
2203 put_bits(pb, 23, get_mantissa(*sample));
2206 put_bits(pb, 8, get_exponent(*sample));
2208 put_bits(pb, 1, get_sign(*sample));
2212 if (s->float_flags & FLOAT_NEG_ZEROS)
2213 put_bits(pb, 1, get_sign(*sample));
2216 } else if (shift_count) {
2217 if (s->float_flags & FLOAT_SHIFT_SENT) {
2218 int32_t data = get_mantissa(*sample) & ((1 << shift_count) - 1);
2219 put_bits(pb, shift_count, data);
2220 } else if (s->float_flags & FLOAT_SHIFT_SAME) {
2221 put_bits(pb, 1, get_mantissa(*sample) & 1);
2226 static void pack_float(WavPackEncodeContext *s,
2227 int32_t *samples_l, int32_t *samples_r,
2232 if (s->flags & WV_MONO_DATA) {
2233 for (i = 0; i < nb_samples; i++)
2234 pack_float_sample(s, &samples_l[i]);
2236 for (i = 0; i < nb_samples; i++) {
2237 pack_float_sample(s, &samples_l[i]);
2238 pack_float_sample(s, &samples_r[i]);
2243 static void decorr_stereo_pass2(struct Decorr *dpp,
2244 int32_t *samples_l, int32_t *samples_r,
2249 switch (dpp->value) {
2251 for (i = 0; i < nb_samples; i++) {
2254 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2255 dpp->samplesA[1] = dpp->samplesA[0];
2256 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2257 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2259 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2260 dpp->samplesB[1] = dpp->samplesB[0];
2261 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2262 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2266 for (i = 0; i < nb_samples; i++) {
2269 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2270 dpp->samplesA[1] = dpp->samplesA[0];
2271 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2272 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2274 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2275 dpp->samplesB[1] = dpp->samplesB[0];
2276 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2277 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2281 for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2284 sam = dpp->samplesA[m];
2285 samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2286 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2288 sam = dpp->samplesB[m];
2289 samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2290 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2292 m = (m + 1) & (MAX_TERM - 1);
2293 k = (k + 1) & (MAX_TERM - 1);
2296 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2298 memcpy(temp_A, dpp->samplesA, sizeof (dpp->samplesA));
2299 memcpy(temp_B, dpp->samplesB, sizeof (dpp->samplesB));
2301 for (k = 0; k < MAX_TERM; k++) {
2302 dpp->samplesA[k] = temp_A[m];
2303 dpp->samplesB[k] = temp_B[m];
2304 m = (m + 1) & (MAX_TERM - 1);
2309 for (i = 0; i < nb_samples; i++) {
2310 int32_t sam_A, sam_B, tmp;
2312 sam_A = dpp->samplesA[0];
2313 samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2314 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2316 samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2317 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2321 for (i = 0; i < nb_samples; i++) {
2322 int32_t sam_A, sam_B, tmp;
2324 sam_B = dpp->samplesB[0];
2325 samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2326 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2328 samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2329 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2333 for (i = 0; i < nb_samples; i++) {
2334 int32_t sam_A, sam_B, tmp;
2336 sam_A = dpp->samplesA[0];
2337 sam_B = dpp->samplesB[0];
2339 dpp->samplesA[0] = tmp = samples_r[i];
2340 samples_r[i] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
2341 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2343 dpp->samplesB[0] = tmp = samples_l[i];
2344 samples_l[i] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
2345 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2351 #define update_weight_d2(weight, delta, source, result) \
2352 if (source && result) \
2353 weight -= (((source ^ result) >> 29) & 4) - 2;
2355 #define update_weight_clip_d2(weight, delta, source, result) \
2356 if (source && result) { \
2357 const int32_t s = (source ^ result) >> 31; \
2358 if ((weight = (weight ^ s) + (2 - s)) > 1024) weight = 1024; \
2359 weight = (weight ^ s) - s; \
2362 static void decorr_stereo_pass_id2(struct Decorr *dpp,
2363 int32_t *samples_l, int32_t *samples_r,
2368 switch (dpp->value) {
2370 for (i = 0; i < nb_samples; i++) {
2373 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2374 dpp->samplesA[1] = dpp->samplesA[0];
2375 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2376 update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2378 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2379 dpp->samplesB[1] = dpp->samplesB[0];
2380 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2381 update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2385 for (i = 0; i < nb_samples; i++) {
2388 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2389 dpp->samplesA[1] = dpp->samplesA[0];
2390 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2391 update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2393 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2394 dpp->samplesB[1] = dpp->samplesB[0];
2395 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2396 update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2400 for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2403 sam = dpp->samplesA[m];
2404 samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2405 update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2407 sam = dpp->samplesB[m];
2408 samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2409 update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2411 m = (m + 1) & (MAX_TERM - 1);
2412 k = (k + 1) & (MAX_TERM - 1);
2416 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2418 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2419 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2421 for (k = 0; k < MAX_TERM; k++) {
2422 dpp->samplesA[k] = temp_A[m];
2423 dpp->samplesB[k] = temp_B[m];
2424 m = (m + 1) & (MAX_TERM - 1);
2429 for (i = 0; i < nb_samples; i++) {
2430 int32_t sam_A, sam_B, tmp;
2432 sam_A = dpp->samplesA[0];
2433 samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2434 update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2436 samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2437 update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2441 for (i = 0; i < nb_samples; i++) {
2442 int32_t sam_A, sam_B, tmp;
2444 sam_B = dpp->samplesB[0];
2445 samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2446 update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2448 samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2449 update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2453 for (i = 0; i < nb_samples; i++) {
2454 int32_t sam_A, sam_B, tmp;
2456 sam_A = dpp->samplesA[0];
2457 sam_B = dpp->samplesB[0];
2459 dpp->samplesA[0] = tmp = samples_r[i];
2460 samples_r[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
2461 update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2463 dpp->samplesB[0] = tmp = samples_l[i];
2464 samples_l[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
2465 update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2471 static void put_metadata_block(PutByteContext *pb, int flags, int size)
2474 flags |= WP_IDF_ODD;
2476 bytestream2_put_byte(pb, flags);
2477 bytestream2_put_byte(pb, (size + 1) >> 1);
2480 static int wavpack_encode_block(WavPackEncodeContext *s,
2481 int32_t *samples_l, int32_t *samples_r,
2482 uint8_t *out, int out_size)
2484 int block_size, start, end, data_size, tcount, temp, m = 0;
2485 int i, j, ret = 0, got_extra = 0, nb_samples = s->block_samples;
2486 uint32_t crc = 0xffffffffu;
2490 if (!(s->flags & WV_MONO) && s->optimize_mono) {
2491 int32_t lor = 0, diff = 0;
2493 for (i = 0; i < nb_samples; i++) {
2494 lor |= samples_l[i] | samples_r[i];
2495 diff |= samples_l[i] - samples_r[i];
2501 if (i == nb_samples && lor && !diff) {
2502 s->flags &= ~(WV_JOINT_STEREO | WV_CROSS_DECORR);
2503 s->flags |= WV_FALSE_STEREO;
2505 if (!s->false_stereo) {
2506 s->false_stereo = 1;
2510 } else if (s->false_stereo) {
2511 s->false_stereo = 0;
2517 if (s->flags & SHIFT_MASK) {
2518 int shift = (s->flags & SHIFT_MASK) >> SHIFT_LSB;
2519 int mag = (s->flags & MAG_MASK) >> MAG_LSB;
2521 if (s->flags & WV_MONO_DATA)
2522 shift_mono(samples_l, nb_samples, shift);
2524 shift_stereo(samples_l, samples_r, nb_samples, shift);
2526 if ((mag -= shift) < 0)
2527 s->flags &= ~MAG_MASK;
2529 s->flags -= (1 << MAG_LSB) * shift;
2532 if ((s->flags & WV_FLOAT_DATA) || (s->flags & MAG_MASK) >> MAG_LSB >= 24) {
2533 av_fast_padded_malloc(&s->orig_l, &s->orig_l_size, sizeof(int32_t) * nb_samples);
2534 memcpy(s->orig_l, samples_l, sizeof(int32_t) * nb_samples);
2535 if (!(s->flags & WV_MONO_DATA)) {
2536 av_fast_padded_malloc(&s->orig_r, &s->orig_r_size, sizeof(int32_t) * nb_samples);
2537 memcpy(s->orig_r, samples_r, sizeof(int32_t) * nb_samples);
2540 if (s->flags & WV_FLOAT_DATA)
2541 got_extra = scan_float(s, samples_l, samples_r, nb_samples);
2543 got_extra = scan_int32(s, samples_l, samples_r, nb_samples);
2546 scan_int23(s, samples_l, samples_r, nb_samples);
2547 if (s->shift != s->int32_zeros + s->int32_ones + s->int32_dups) {
2548 s->shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2553 if (!s->num_passes && !s->num_terms) {
2556 if (s->flags & WV_MONO_DATA)
2557 ret = wv_mono(s, samples_l, 1, 0);
2559 ret = wv_stereo(s, samples_l, samples_r, 1, 0);
2563 if (s->flags & WV_MONO_DATA) {
2564 for (i = 0; i < nb_samples; i++)
2565 crc += (crc << 1) + samples_l[i];
2568 ret = wv_mono(s, samples_l, !s->num_terms, 1);
2570 for (i = 0; i < nb_samples; i++)
2571 crc += (crc << 3) + (samples_l[i] << 1) + samples_l[i] + samples_r[i];
2574 ret = wv_stereo(s, samples_l, samples_r, !s->num_terms, 1);
2580 s->flags |= WV_INITIAL_BLOCK;
2582 s->ch_offset += 1 + !(s->flags & WV_MONO);
2584 if (s->ch_offset == s->avctx->channels)
2585 s->flags |= WV_FINAL_BLOCK;
2587 bytestream2_init_writer(&pb, out, out_size);
2588 bytestream2_put_le32(&pb, MKTAG('w', 'v', 'p', 'k'));
2589 bytestream2_put_le32(&pb, 0);
2590 bytestream2_put_le16(&pb, 0x410);
2591 bytestream2_put_le16(&pb, 0);
2592 bytestream2_put_le32(&pb, 0);
2593 bytestream2_put_le32(&pb, s->sample_index);
2594 bytestream2_put_le32(&pb, nb_samples);
2595 bytestream2_put_le32(&pb, s->flags);
2596 bytestream2_put_le32(&pb, crc);
2598 if (s->flags & WV_INITIAL_BLOCK &&
2599 s->avctx->channel_layout != AV_CH_LAYOUT_MONO &&
2600 s->avctx->channel_layout != AV_CH_LAYOUT_STEREO) {
2601 put_metadata_block(&pb, WP_ID_CHANINFO, 5);
2602 bytestream2_put_byte(&pb, s->avctx->channels);
2603 bytestream2_put_le32(&pb, s->avctx->channel_layout);
2604 bytestream2_put_byte(&pb, 0);
2607 if ((s->flags & SRATE_MASK) == SRATE_MASK) {
2608 put_metadata_block(&pb, WP_ID_SAMPLE_RATE, 3);
2609 bytestream2_put_le24(&pb, s->avctx->sample_rate);
2610 bytestream2_put_byte(&pb, 0);
2613 put_metadata_block(&pb, WP_ID_DECTERMS, s->num_terms);
2614 for (i = 0; i < s->num_terms; i++) {
2615 struct Decorr *dpp = &s->decorr_passes[i];
2616 bytestream2_put_byte(&pb, ((dpp->value + 5) & 0x1f) | ((dpp->delta << 5) & 0xe0));
2618 if (s->num_terms & 1)
2619 bytestream2_put_byte(&pb, 0);
2621 #define WRITE_DECWEIGHT(type) do { \
2622 temp = store_weight(type); \
2623 bytestream2_put_byte(&pb, temp); \
2624 type = restore_weight(temp); \
2627 bytestream2_put_byte(&pb, WP_ID_DECWEIGHTS);
2628 bytestream2_put_byte(&pb, 0);
2629 start = bytestream2_tell_p(&pb);
2630 for (i = s->num_terms - 1; i >= 0; --i) {
2631 struct Decorr *dpp = &s->decorr_passes[i];
2633 if (store_weight(dpp->weightA) ||
2634 (!(s->flags & WV_MONO_DATA) && store_weight(dpp->weightB)))
2638 for (i = 0; i < s->num_terms; i++) {
2639 struct Decorr *dpp = &s->decorr_passes[i];
2641 WRITE_DECWEIGHT(dpp->weightA);
2642 if (!(s->flags & WV_MONO_DATA))
2643 WRITE_DECWEIGHT(dpp->weightB);
2645 dpp->weightA = dpp->weightB = 0;
2648 end = bytestream2_tell_p(&pb);
2649 out[start - 2] = WP_ID_DECWEIGHTS | (((end - start) & 1) ? WP_IDF_ODD: 0);
2650 out[start - 1] = (end - start + 1) >> 1;
2651 if ((end - start) & 1)
2652 bytestream2_put_byte(&pb, 0);
2654 #define WRITE_DECSAMPLE(type) do { \
2655 temp = log2s(type); \
2656 type = wp_exp2(temp); \
2657 bytestream2_put_le16(&pb, temp); \
2660 bytestream2_put_byte(&pb, WP_ID_DECSAMPLES);
2661 bytestream2_put_byte(&pb, 0);
2662 start = bytestream2_tell_p(&pb);
2663 for (i = 0; i < s->num_terms; i++) {
2664 struct Decorr *dpp = &s->decorr_passes[i];
2666 if (dpp->value > MAX_TERM) {
2667 WRITE_DECSAMPLE(dpp->samplesA[0]);
2668 WRITE_DECSAMPLE(dpp->samplesA[1]);
2669 if (!(s->flags & WV_MONO_DATA)) {
2670 WRITE_DECSAMPLE(dpp->samplesB[0]);
2671 WRITE_DECSAMPLE(dpp->samplesB[1]);
2673 } else if (dpp->value < 0) {
2674 WRITE_DECSAMPLE(dpp->samplesA[0]);
2675 WRITE_DECSAMPLE(dpp->samplesB[0]);
2677 for (j = 0; j < dpp->value; j++) {
2678 WRITE_DECSAMPLE(dpp->samplesA[j]);
2679 if (!(s->flags & WV_MONO_DATA))
2680 WRITE_DECSAMPLE(dpp->samplesB[j]);
2684 CLEAR(dpp->samplesA);
2685 CLEAR(dpp->samplesB);
2688 end = bytestream2_tell_p(&pb);
2689 out[start - 1] = (end - start) >> 1;
2691 #define WRITE_CHAN_ENTROPY(chan) do { \
2692 for (i = 0; i < 3; i++) { \
2693 temp = wp_log2(s->w.c[chan].median[i]); \
2694 bytestream2_put_le16(&pb, temp); \
2695 s->w.c[chan].median[i] = wp_exp2(temp); \
2699 put_metadata_block(&pb, WP_ID_ENTROPY, 6 * (1 + (!(s->flags & WV_MONO_DATA))));
2700 WRITE_CHAN_ENTROPY(0);
2701 if (!(s->flags & WV_MONO_DATA))
2702 WRITE_CHAN_ENTROPY(1);
2704 if (s->flags & WV_FLOAT_DATA) {
2705 put_metadata_block(&pb, WP_ID_FLOATINFO, 4);
2706 bytestream2_put_byte(&pb, s->float_flags);
2707 bytestream2_put_byte(&pb, s->float_shift);
2708 bytestream2_put_byte(&pb, s->float_max_exp);
2709 bytestream2_put_byte(&pb, 127);
2712 if (s->flags & WV_INT32_DATA) {
2713 put_metadata_block(&pb, WP_ID_INT32INFO, 4);
2714 bytestream2_put_byte(&pb, s->int32_sent_bits);
2715 bytestream2_put_byte(&pb, s->int32_zeros);
2716 bytestream2_put_byte(&pb, s->int32_ones);
2717 bytestream2_put_byte(&pb, s->int32_dups);
2720 if (s->flags & WV_MONO_DATA && !s->num_passes) {
2721 for (i = 0; i < nb_samples; i++) {
2722 int32_t code = samples_l[i];
2724 for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++) {
2727 if (dpp->value > MAX_TERM) {
2729 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2731 sam = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
2733 dpp->samplesA[1] = dpp->samplesA[0];
2734 dpp->samplesA[0] = code;
2736 sam = dpp->samplesA[m];
2737 dpp->samplesA[(m + dpp->value) & (MAX_TERM - 1)] = code;
2740 code -= APPLY_WEIGHT(dpp->weightA, sam);
2741 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, code);
2744 m = (m + 1) & (MAX_TERM - 1);
2745 samples_l[i] = code;
2748 for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++)
2749 if (dpp->value > 0 && dpp->value <= MAX_TERM) {
2750 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2753 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2754 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2756 for (k = 0; k < MAX_TERM; k++) {
2757 dpp->samplesA[k] = temp_A[m];
2758 dpp->samplesB[k] = temp_B[m];
2759 m = (m + 1) & (MAX_TERM - 1);
2763 } else if (!s->num_passes) {
2764 if (s->flags & WV_JOINT_STEREO) {
2765 for (i = 0; i < nb_samples; i++)
2766 samples_r[i] += ((samples_l[i] -= samples_r[i]) >> 1);
2769 for (i = 0; i < s->num_terms; i++) {
2770 struct Decorr *dpp = &s->decorr_passes[i];
2771 if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16 || dpp->delta != 2)
2772 decorr_stereo_pass2(dpp, samples_l, samples_r, nb_samples);
2774 decorr_stereo_pass_id2(dpp, samples_l, samples_r, nb_samples);
2778 bytestream2_put_byte(&pb, WP_ID_DATA | WP_IDF_LONG);
2779 init_put_bits(&s->pb, pb.buffer + 3, bytestream2_get_bytes_left_p(&pb));
2780 if (s->flags & WV_MONO_DATA) {
2781 for (i = 0; i < nb_samples; i++)
2782 wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2784 for (i = 0; i < nb_samples; i++) {
2785 wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2786 wavpack_encode_sample(s, &s->w.c[1], s->samples[1][i]);
2790 flush_put_bits(&s->pb);
2791 data_size = put_bits_count(&s->pb) >> 3;
2792 bytestream2_put_le24(&pb, (data_size + 1) >> 1);
2793 bytestream2_skip_p(&pb, data_size);
2795 bytestream2_put_byte(&pb, 0);
2798 bytestream2_put_byte(&pb, WP_ID_EXTRABITS | WP_IDF_LONG);
2799 init_put_bits(&s->pb, pb.buffer + 7, bytestream2_get_bytes_left_p(&pb));
2800 if (s->flags & WV_FLOAT_DATA)
2801 pack_float(s, s->orig_l, s->orig_r, nb_samples);
2803 pack_int32(s, s->orig_l, s->orig_r, nb_samples);
2804 flush_put_bits(&s->pb);
2805 data_size = put_bits_count(&s->pb) >> 3;
2806 bytestream2_put_le24(&pb, (data_size + 5) >> 1);
2807 bytestream2_put_le32(&pb, s->crc_x);
2808 bytestream2_skip_p(&pb, data_size);
2810 bytestream2_put_byte(&pb, 0);
2813 block_size = bytestream2_tell_p(&pb);
2814 AV_WL32(out + 4, block_size - 8);
2819 static void fill_buffer(WavPackEncodeContext *s,
2820 const int8_t *src, int32_t *dst,
2825 #define COPY_SAMPLES(type, offset, shift) do { \
2826 const type *sptr = (const type *)src; \
2827 for (i = 0; i < nb_samples; i++) \
2828 dst[i] = (sptr[i] - offset) >> shift; \
2831 switch (s->avctx->sample_fmt) {
2832 case AV_SAMPLE_FMT_U8P:
2833 COPY_SAMPLES(int8_t, 0x80, 0);
2835 case AV_SAMPLE_FMT_S16P:
2836 COPY_SAMPLES(int16_t, 0, 0);
2838 case AV_SAMPLE_FMT_S32P:
2839 if (s->avctx->bits_per_raw_sample <= 24) {
2840 COPY_SAMPLES(int32_t, 0, 8);
2843 case AV_SAMPLE_FMT_FLTP:
2844 memcpy(dst, src, nb_samples * 4);
2848 static void set_samplerate(WavPackEncodeContext *s)
2852 for (i = 0; i < 15; i++) {
2853 if (wv_rates[i] == s->avctx->sample_rate)
2857 s->flags = i << SRATE_LSB;
2860 static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2861 const AVFrame *frame, int *got_packet_ptr)
2863 WavPackEncodeContext *s = avctx->priv_data;
2867 s->block_samples = frame->nb_samples;
2868 av_fast_padded_malloc(&s->samples[0], &s->samples_size[0],
2869 sizeof(int32_t) * s->block_samples);
2871 return AVERROR(ENOMEM);
2872 if (avctx->channels > 1) {
2873 av_fast_padded_malloc(&s->samples[1], &s->samples_size[1],
2874 sizeof(int32_t) * s->block_samples);
2876 return AVERROR(ENOMEM);
2879 if ((ret = ff_alloc_packet2(avctx, avpkt, s->block_samples * avctx->channels * 8)) < 0)
2882 buf_size = avpkt->size;
2884 for (s->ch_offset = 0; s->ch_offset < avctx->channels;) {
2887 switch (s->avctx->sample_fmt) {
2888 case AV_SAMPLE_FMT_S16P: s->flags |= 1; break;
2889 case AV_SAMPLE_FMT_S32P: s->flags |= 3 - (s->avctx->bits_per_raw_sample <= 24); break;
2890 case AV_SAMPLE_FMT_FLTP: s->flags |= 3 | WV_FLOAT_DATA;
2893 fill_buffer(s, frame->extended_data[s->ch_offset], s->samples[0], s->block_samples);
2894 if (avctx->channels - s->ch_offset == 1) {
2895 s->flags |= WV_MONO;
2897 s->flags |= WV_CROSS_DECORR;
2898 fill_buffer(s, frame->extended_data[s->ch_offset + 1], s->samples[1], s->block_samples);
2901 s->flags += (1 << MAG_LSB) * ((s->flags & 3) * 8 + 7);
2903 if ((ret = wavpack_encode_block(s, s->samples[0], s->samples[1],
2904 buf, buf_size)) < 0)
2910 s->sample_index += frame->nb_samples;
2912 avpkt->pts = frame->pts;
2913 avpkt->size = buf - avpkt->data;
2914 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
2915 *got_packet_ptr = 1;
2919 static av_cold int wavpack_encode_close(AVCodecContext *avctx)
2921 WavPackEncodeContext *s = avctx->priv_data;
2924 for (i = 0; i < MAX_TERMS + 2; i++) {
2925 av_freep(&s->sampleptrs[i][0]);
2926 av_freep(&s->sampleptrs[i][1]);
2927 s->sampleptrs_size[i][0] = s->sampleptrs_size[i][1] = 0;
2930 for (i = 0; i < 2; i++) {
2931 av_freep(&s->samples[i]);
2932 s->samples_size[i] = 0;
2934 av_freep(&s->best_buffer[i]);
2935 s->best_buffer_size[i] = 0;
2937 av_freep(&s->temp_buffer[i][0]);
2938 av_freep(&s->temp_buffer[i][1]);
2939 s->temp_buffer_size[i][0] = s->temp_buffer_size[i][1] = 0;
2942 av_freep(&s->js_left);
2943 av_freep(&s->js_right);
2944 s->js_left_size = s->js_right_size = 0;
2946 av_freep(&s->orig_l);
2947 av_freep(&s->orig_r);
2948 s->orig_l_size = s->orig_r_size = 0;
2953 #define OFFSET(x) offsetof(WavPackEncodeContext, x)
2954 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2955 static const AVOption options[] = {
2956 { "joint_stereo", "", OFFSET(joint), AV_OPT_TYPE_INT, {.i64=0},-1, 1, FLAGS, "joint" },
2957 { "on", "mid/side", 0, AV_OPT_TYPE_CONST, {.i64= 1}, 0, 0, FLAGS, "joint"},
2958 { "off", "left/right", 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "joint"},
2959 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64= 0}, 0, 0, FLAGS, "joint"},
2960 { "optimize_mono", "", OFFSET(optimize_mono), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "opt_mono" },
2961 { "on", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "opt_mono"},
2962 { "off", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "opt_mono"},
2966 static const AVClass wavpack_encoder_class = {
2967 .class_name = "WavPack encoder",
2968 .item_name = av_default_item_name,
2970 .version = LIBAVUTIL_VERSION_INT,
2973 AVCodec ff_wavpack_encoder = {
2975 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
2976 .type = AVMEDIA_TYPE_AUDIO,
2977 .id = AV_CODEC_ID_WAVPACK,
2978 .priv_data_size = sizeof(WavPackEncodeContext),
2979 .priv_class = &wavpack_encoder_class,
2980 .init = wavpack_encode_init,
2981 .encode2 = wavpack_encode_frame,
2982 .close = wavpack_encode_close,
2983 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
2984 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8P,
2988 AV_SAMPLE_FMT_NONE },