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->channels > 255) {
132 av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d\n", avctx->channels);
133 return AVERROR(EINVAL);
136 if (!avctx->frame_size) {
138 if (!(avctx->sample_rate & 1))
139 block_samples = avctx->sample_rate / 2;
141 block_samples = avctx->sample_rate;
143 while (block_samples * avctx->channels > WV_MAX_SAMPLES)
146 while (block_samples * avctx->channels < 40000)
148 avctx->frame_size = block_samples;
149 } else if (avctx->frame_size && (avctx->frame_size < 128 ||
150 avctx->frame_size > WV_MAX_SAMPLES)) {
151 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n", avctx->frame_size);
152 return AVERROR(EINVAL);
155 if (avctx->compression_level != FF_COMPRESSION_DEFAULT) {
156 if (avctx->compression_level >= 3) {
157 s->decorr_filter = 3;
159 if (avctx->compression_level >= 8) {
161 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_SORT_LAST|EXTRA_BRANCHES;
162 } else if (avctx->compression_level >= 7) {
164 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
165 } else if (avctx->compression_level >= 6) {
167 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
168 } else if (avctx->compression_level >= 5) {
170 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_SORT_FIRST|EXTRA_BRANCHES;
171 } else if (avctx->compression_level >= 4) {
173 s->extra_flags = EXTRA_TRY_DELTAS|EXTRA_ADJUST_DELTAS|EXTRA_BRANCHES;
175 } else if (avctx->compression_level == 2) {
176 s->decorr_filter = 2;
178 } else if (avctx->compression_level == 1) {
179 s->decorr_filter = 1;
181 } else if (avctx->compression_level < 1) {
182 s->decorr_filter = 0;
187 s->num_decorrs = decorr_filter_sizes[s->decorr_filter];
188 s->decorr_specs = decorr_filters[s->decorr_filter];
190 s->delta_decay = 2.0;
195 static void shift_mono(int32_t *samples, int nb_samples, int shift)
198 for (i = 0; i < nb_samples; i++)
199 samples[i] >>= shift;
202 static void shift_stereo(int32_t *left, int32_t *right,
203 int nb_samples, int shift)
206 for (i = 0; i < nb_samples; i++) {
212 #define FLOAT_SHIFT_ONES 1
213 #define FLOAT_SHIFT_SAME 2
214 #define FLOAT_SHIFT_SENT 4
215 #define FLOAT_ZEROS_SENT 8
216 #define FLOAT_NEG_ZEROS 0x10
217 #define FLOAT_EXCEPTIONS 0x20
219 #define get_mantissa(f) ((f) & 0x7fffff)
220 #define get_exponent(f) (((f) >> 23) & 0xff)
221 #define get_sign(f) (((f) >> 31) & 0x1)
223 static void process_float(WavPackEncodeContext *s, int32_t *sample)
225 int32_t shift_count, value, f = *sample;
227 if (get_exponent(f) == 255) {
228 s->float_flags |= FLOAT_EXCEPTIONS;
231 } else if (get_exponent(f)) {
232 shift_count = s->max_exp - get_exponent(f);
233 value = 0x800000 + get_mantissa(f);
235 shift_count = s->max_exp ? s->max_exp - 1 : 0;
236 value = get_mantissa(f);
239 if (shift_count < 25)
240 value >>= shift_count;
245 if (get_exponent(f) || get_mantissa(f))
247 else if (get_sign(f))
249 } else if (shift_count) {
250 int32_t mask = (1 << shift_count) - 1;
252 if (!(get_mantissa(f) & mask))
254 else if ((get_mantissa(f) & mask) == mask)
261 *sample = get_sign(f) ? -value : value;
264 static int scan_float(WavPackEncodeContext *s,
265 int32_t *samples_l, int32_t *samples_r,
268 uint32_t crc = 0xffffffffu;
271 s->shifted_ones = s->shifted_zeros = s->shifted_both = s->ordata = 0;
272 s->float_shift = s->float_flags = 0;
273 s->false_zeros = s->neg_zeros = 0;
276 if (s->flags & WV_MONO_DATA) {
277 for (i = 0; i < nb_samples; i++) {
278 int32_t f = samples_l[i];
279 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
281 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
282 s->max_exp = get_exponent(f);
285 for (i = 0; i < nb_samples; i++) {
289 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
290 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
291 s->max_exp = get_exponent(f);
294 crc = crc * 27 + get_mantissa(f) * 9 + get_exponent(f) * 3 + get_sign(f);
296 if (get_exponent(f) > s->max_exp && get_exponent(f) < 255)
297 s->max_exp = get_exponent(f);
303 if (s->flags & WV_MONO_DATA) {
304 for (i = 0; i < nb_samples; i++)
305 process_float(s, &samples_l[i]);
307 for (i = 0; i < nb_samples; i++) {
308 process_float(s, &samples_l[i]);
309 process_float(s, &samples_r[i]);
313 s->float_max_exp = s->max_exp;
316 s->float_flags |= FLOAT_SHIFT_SENT;
317 else if (s->shifted_ones && !s->shifted_zeros)
318 s->float_flags |= FLOAT_SHIFT_ONES;
319 else if (s->shifted_ones && s->shifted_zeros)
320 s->float_flags |= FLOAT_SHIFT_SAME;
321 else if (s->ordata && !(s->ordata & 1)) {
325 } while (!(s->ordata & 1));
327 if (s->flags & WV_MONO_DATA)
328 shift_mono(samples_l, nb_samples, s->float_shift);
330 shift_stereo(samples_l, samples_r, nb_samples, s->float_shift);
333 s->flags &= ~MAG_MASK;
336 s->flags += 1 << MAG_LSB;
340 if (s->false_zeros || s->neg_zeros)
341 s->float_flags |= FLOAT_ZEROS_SENT;
344 s->float_flags |= FLOAT_NEG_ZEROS;
346 return s->float_flags & (FLOAT_EXCEPTIONS | FLOAT_ZEROS_SENT |
347 FLOAT_SHIFT_SENT | FLOAT_SHIFT_SAME);
350 static void scan_int23(WavPackEncodeContext *s,
351 int32_t *samples_l, int32_t *samples_r,
354 uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
355 int i, total_shift = 0;
357 s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
359 if (s->flags & WV_MONO_DATA) {
360 for (i = 0; i < nb_samples; i++) {
361 int32_t M = samples_l[i];
363 magdata |= (M < 0) ? ~M : M;
364 xordata |= M ^ -(M & 1);
368 if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
372 for (i = 0; i < nb_samples; i++) {
373 int32_t L = samples_l[i];
374 int32_t R = samples_r[i];
376 magdata |= (L < 0) ? ~L : L;
377 magdata |= (R < 0) ? ~R : R;
378 xordata |= L ^ -(L & 1);
379 xordata |= R ^ -(R & 1);
383 if ((ordata & 1) && !(anddata & 1) && (xordata & 2))
388 s->flags &= ~MAG_MASK;
391 s->flags += 1 << MAG_LSB;
395 if (!(s->flags & MAG_MASK))
400 s->flags -= 1 << MAG_LSB;
404 } while (!(ordata & 1));
405 } else if (anddata & 1) {
407 s->flags -= 1 << MAG_LSB;
411 } while (anddata & 1);
412 } else if (!(xordata & 2)) {
414 s->flags -= 1 << MAG_LSB;
418 } while (!(xordata & 2));
422 s->flags |= WV_INT32_DATA;
424 if (s->flags & WV_MONO_DATA)
425 shift_mono(samples_l, nb_samples, total_shift);
427 shift_stereo(samples_l, samples_r, nb_samples, total_shift);
431 static int scan_int32(WavPackEncodeContext *s,
432 int32_t *samples_l, int32_t *samples_r,
435 uint32_t magdata = 0, ordata = 0, xordata = 0, anddata = ~0;
436 uint32_t crc = 0xffffffffu;
437 int i, total_shift = 0;
439 s->int32_sent_bits = s->int32_zeros = s->int32_ones = s->int32_dups = 0;
441 if (s->flags & WV_MONO_DATA) {
442 for (i = 0; i < nb_samples; i++) {
443 int32_t M = samples_l[i];
445 crc = crc * 9 + (M & 0xffff) * 3 + ((M >> 16) & 0xffff);
446 magdata |= (M < 0) ? ~M : M;
447 xordata |= M ^ -(M & 1);
452 for (i = 0; i < nb_samples; i++) {
453 int32_t L = samples_l[i];
454 int32_t R = samples_r[i];
456 crc = crc * 9 + (L & 0xffff) * 3 + ((L >> 16) & 0xffff);
457 crc = crc * 9 + (R & 0xffff) * 3 + ((R >> 16) & 0xffff);
458 magdata |= (L < 0) ? ~L : L;
459 magdata |= (R < 0) ? ~R : R;
460 xordata |= L ^ -(L & 1);
461 xordata |= R ^ -(R & 1);
468 s->flags &= ~MAG_MASK;
471 s->flags += 1 << MAG_LSB;
475 if (!((s->flags & MAG_MASK) >> MAG_LSB)) {
476 s->flags &= ~WV_INT32_DATA;
482 s->flags -= 1 << MAG_LSB;
486 } while (!(ordata & 1));
487 else if (anddata & 1)
489 s->flags -= 1 << MAG_LSB;
493 } while (anddata & 1);
494 else if (!(xordata & 2))
496 s->flags -= 1 << MAG_LSB;
500 } while (!(xordata & 2));
502 if (((s->flags & MAG_MASK) >> MAG_LSB) > 23) {
503 s->int32_sent_bits = (uint8_t)(((s->flags & MAG_MASK) >> MAG_LSB) - 23);
504 total_shift += s->int32_sent_bits;
505 s->flags &= ~MAG_MASK;
506 s->flags += 23 << MAG_LSB;
510 s->flags |= WV_INT32_DATA;
512 if (s->flags & WV_MONO_DATA)
513 shift_mono(samples_l, nb_samples, total_shift);
515 shift_stereo(samples_l, samples_r, nb_samples, total_shift);
518 return s->int32_sent_bits;
521 static int8_t store_weight(int weight)
523 weight = av_clip(weight, -1024, 1024);
525 weight -= (weight + 64) >> 7;
527 return (weight + 4) >> 3;
530 static int restore_weight(int8_t weight)
534 if ((result = (int) weight << 3) > 0)
535 result += (result + 64) >> 7;
540 static int log2s(int32_t value)
542 return (value < 0) ? -wp_log2(-value) : wp_log2(value);
545 static void decorr_mono(int32_t *in_samples, int32_t *out_samples,
546 int nb_samples, struct Decorr *dpp, int dir)
553 out_samples += (nb_samples - 1);
554 in_samples += (nb_samples - 1);
557 dpp->weightA = restore_weight(store_weight(dpp->weightA));
559 for (i = 0; i < MAX_TERM; i++)
560 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
562 if (dpp->value > MAX_TERM) {
563 while (nb_samples--) {
566 sam_A = ((3 - (dpp->value & 1)) * dpp->samplesA[0] - dpp->samplesA[1]) >> !(dpp->value & 1);
568 dpp->samplesA[1] = dpp->samplesA[0];
569 dpp->samplesA[0] = left = in_samples[0];
571 left -= APPLY_WEIGHT(dpp->weightA, sam_A);
572 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
573 dpp->sumA += dpp->weightA;
574 out_samples[0] = left;
578 } else if (dpp->value > 0) {
579 while (nb_samples--) {
580 int k = (m + dpp->value) & (MAX_TERM - 1);
583 sam_A = dpp->samplesA[m];
584 dpp->samplesA[k] = left = in_samples[0];
585 m = (m + 1) & (MAX_TERM - 1);
587 left -= APPLY_WEIGHT(dpp->weightA, sam_A);
588 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam_A, left);
589 dpp->sumA += dpp->weightA;
590 out_samples[0] = left;
596 if (m && dpp->value > 0 && dpp->value <= MAX_TERM) {
597 int32_t temp_A[MAX_TERM];
599 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
601 for (i = 0; i < MAX_TERM; i++) {
602 dpp->samplesA[i] = temp_A[m];
603 m = (m + 1) & (MAX_TERM - 1);
608 static void reverse_mono_decorr(struct Decorr *dpp)
610 if (dpp->value > MAX_TERM) {
614 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
616 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
618 dpp->samplesA[1] = dpp->samplesA[0];
619 dpp->samplesA[0] = sam_A;
622 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
624 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
626 dpp->samplesA[1] = sam_A;
627 } else if (dpp->value > 1) {
630 for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
633 dpp->samplesA[i] ^= dpp->samplesA[j];
634 dpp->samplesA[j] ^= dpp->samplesA[i];
635 dpp->samplesA[i] ^= dpp->samplesA[j];
640 static uint32_t log2sample(uint32_t v, int limit, uint32_t *result)
644 if ((v += v >> 9) < (1 << 8)) {
645 dbits = nbits_table[v];
646 *result += (dbits << 8) + wp_log2_table[(v << (9 - dbits)) & 0xff];
649 dbits = nbits_table[v >> 8] + 8;
650 else if (v < (1 << 24))
651 dbits = nbits_table[v >> 16] + 16;
653 dbits = nbits_table[v >> 24] + 24;
655 *result += dbits = (dbits << 8) + wp_log2_table[(v >> (dbits - 9)) & 0xff];
657 if (limit && dbits >= limit)
664 static uint32_t log2mono(int32_t *samples, int nb_samples, int limit)
667 while (nb_samples--) {
668 if (log2sample(abs(*samples++), limit, &result))
674 static uint32_t log2stereo(int32_t *samples_l, int32_t *samples_r,
675 int nb_samples, int limit)
678 while (nb_samples--) {
679 if (log2sample(abs(*samples_l++), limit, &result) ||
680 log2sample(abs(*samples_r++), limit, &result))
686 static void decorr_mono_buffer(int32_t *samples, int32_t *outsamples,
687 int nb_samples, struct Decorr *dpp,
690 struct Decorr dp, *dppi = dpp + tindex;
691 int delta = dppi->delta, pre_delta, term = dppi->value;
698 pre_delta = delta + 1;
702 dp.delta = pre_delta;
703 decorr_mono(samples, outsamples, FFMIN(2048, nb_samples), &dp, -1);
707 reverse_mono_decorr(&dp);
711 memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
712 dppi->weightA = dp.weightA;
716 decorr_mono(samples, outsamples, nb_samples, &dp, 1);
718 memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
719 dppi->weightA = dp.weightA = dp.sumA / nb_samples;
722 decorr_mono(samples, outsamples, nb_samples, &dp, 1);
725 static void recurse_mono(WavPackEncodeContext *s, WavPackExtraInfo *info,
726 int depth, int delta, uint32_t input_bits)
728 int term, branches = s->num_branches - depth;
729 int32_t *samples, *outsamples;
730 uint32_t term_bits[22], bits;
732 if (branches < 1 || depth + 1 == info->nterms)
736 samples = s->sampleptrs[depth][0];
737 outsamples = s->sampleptrs[depth + 1][0];
739 for (term = 1; term <= 18; term++) {
740 if (term == 17 && branches == 1 && depth + 1 < info->nterms)
743 if (term > 8 && term < 17)
746 if (!s->extra_flags && (term > 4 && term < 17))
749 info->dps[depth].value = term;
750 info->dps[depth].delta = delta;
751 decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
752 bits = log2mono(outsamples, s->block_samples, info->log_limit);
754 if (bits < info->best_bits) {
755 info->best_bits = bits;
756 CLEAR(s->decorr_passes);
757 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
758 memcpy(s->sampleptrs[info->nterms + 1][0],
759 s->sampleptrs[depth + 1][0], s->block_samples * 4);
762 term_bits[term + 3] = bits;
765 while (depth + 1 < info->nterms && branches--) {
766 uint32_t local_best_bits = input_bits;
767 int best_term = 0, i;
769 for (i = 0; i < 22; i++)
770 if (term_bits[i] && term_bits[i] < local_best_bits) {
771 local_best_bits = term_bits[i];
778 term_bits[best_term + 3] = 0;
780 info->dps[depth].value = best_term;
781 info->dps[depth].delta = delta;
782 decorr_mono_buffer(samples, outsamples, s->block_samples, info->dps, depth);
784 recurse_mono(s, info, depth + 1, delta, local_best_bits);
788 static void sort_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
796 memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
799 for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
801 if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
804 if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
805 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
806 s->block_samples, info->dps, ri);
810 info->dps[ri ] = s->decorr_passes[ri+1];
811 info->dps[ri+1] = s->decorr_passes[ri ];
813 for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
814 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
815 s->block_samples, info->dps, i);
817 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
818 if (bits < info->best_bits) {
820 info->best_bits = bits;
821 CLEAR(s->decorr_passes);
822 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
823 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
824 s->block_samples * 4);
826 info->dps[ri ] = s->decorr_passes[ri];
827 info->dps[ri+1] = s->decorr_passes[ri+1];
828 decorr_mono_buffer(s->sampleptrs[ri][0], s->sampleptrs[ri+1][0],
829 s->block_samples, info->dps, ri);
835 static void delta_mono(WavPackEncodeContext *s, WavPackExtraInfo *info)
837 int lower = 0, delta, d;
840 if (!s->decorr_passes[0].value)
842 delta = s->decorr_passes[0].delta;
844 for (d = delta - 1; d >= 0; d--) {
847 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
848 info->dps[i].value = s->decorr_passes[i].value;
849 info->dps[i].delta = d;
850 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
851 s->block_samples, info->dps, i);
854 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
855 if (bits >= info->best_bits)
859 info->best_bits = bits;
860 CLEAR(s->decorr_passes);
861 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
862 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
863 s->block_samples * 4);
866 for (d = delta + 1; !lower && d <= 7; d++) {
869 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
870 info->dps[i].value = s->decorr_passes[i].value;
871 info->dps[i].delta = d;
872 decorr_mono_buffer(s->sampleptrs[i][0], s->sampleptrs[i+1][0],
873 s->block_samples, info->dps, i);
876 bits = log2mono(s->sampleptrs[i][0], s->block_samples, info->log_limit);
877 if (bits >= info->best_bits)
880 info->best_bits = bits;
881 CLEAR(s->decorr_passes);
882 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
883 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
884 s->block_samples * 4);
888 static int allocate_buffers2(WavPackEncodeContext *s, int nterms)
892 for (i = 0; i < nterms + 2; i++) {
893 av_fast_padded_malloc(&s->sampleptrs[i][0], &s->sampleptrs_size[i][0],
894 s->block_samples * 4);
895 if (!s->sampleptrs[i][0])
896 return AVERROR(ENOMEM);
897 if (!(s->flags & WV_MONO_DATA)) {
898 av_fast_padded_malloc(&s->sampleptrs[i][1], &s->sampleptrs_size[i][1],
899 s->block_samples * 4);
900 if (!s->sampleptrs[i][1])
901 return AVERROR(ENOMEM);
908 static int allocate_buffers(WavPackEncodeContext *s)
912 for (i = 0; i < 2; i++) {
913 av_fast_padded_malloc(&s->best_buffer[0], &s->best_buffer_size[0],
914 s->block_samples * 4);
915 if (!s->best_buffer[0])
916 return AVERROR(ENOMEM);
918 av_fast_padded_malloc(&s->temp_buffer[i][0], &s->temp_buffer_size[i][0],
919 s->block_samples * 4);
920 if (!s->temp_buffer[i][0])
921 return AVERROR(ENOMEM);
922 if (!(s->flags & WV_MONO_DATA)) {
923 av_fast_padded_malloc(&s->best_buffer[1], &s->best_buffer_size[1],
924 s->block_samples * 4);
925 if (!s->best_buffer[1])
926 return AVERROR(ENOMEM);
928 av_fast_padded_malloc(&s->temp_buffer[i][1], &s->temp_buffer_size[i][1],
929 s->block_samples * 4);
930 if (!s->temp_buffer[i][1])
931 return AVERROR(ENOMEM);
938 static void analyze_mono(WavPackEncodeContext *s, int32_t *samples, int do_samples)
940 WavPackExtraInfo info;
943 info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
944 info.log_limit = FFMIN(6912, info.log_limit);
946 info.nterms = s->num_terms;
948 if (allocate_buffers2(s, s->num_terms))
951 memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
952 memcpy(s->sampleptrs[0][0], samples, s->block_samples * 4);
954 for (i = 0; i < info.nterms && info.dps[i].value; i++)
955 decorr_mono(s->sampleptrs[i][0], s->sampleptrs[i + 1][0],
956 s->block_samples, info.dps + i, 1);
958 info.best_bits = log2mono(s->sampleptrs[info.nterms][0], s->block_samples, 0) * 1;
959 memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
961 if (s->extra_flags & EXTRA_BRANCHES)
962 recurse_mono(s, &info, 0, (int) floor(s->delta_decay + 0.5),
963 log2mono(s->sampleptrs[0][0], s->block_samples, 0));
965 if (s->extra_flags & EXTRA_SORT_FIRST)
968 if (s->extra_flags & EXTRA_TRY_DELTAS) {
969 delta_mono(s, &info);
971 if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
972 s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
974 s->delta_decay = 2.0;
977 if (s->extra_flags & EXTRA_SORT_LAST)
981 memcpy(samples, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
983 for (i = 0; i < info.nterms; i++)
984 if (!s->decorr_passes[i].value)
990 static void scan_word(WavPackEncodeContext *s, WvChannel *c,
991 int32_t *samples, int nb_samples, int dir)
994 samples += nb_samples - 1;
996 while (nb_samples--) {
997 uint32_t low, value = labs(samples[0]);
999 if (value < GET_MED(0)) {
1005 if (value - low < GET_MED(1)) {
1011 if (value - low < GET_MED(2)) {
1022 static int wv_mono(WavPackEncodeContext *s, int32_t *samples,
1023 int no_history, int do_samples)
1025 struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1026 int nb_samples = s->block_samples;
1027 int buf_size = sizeof(int32_t) * nb_samples;
1028 uint32_t best_size = UINT32_MAX, size;
1029 int log_limit, pi, i, ret;
1031 for (i = 0; i < nb_samples; i++)
1035 if (i == nb_samples) {
1036 CLEAR(s->decorr_passes);
1042 log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1043 log_limit = FFMIN(6912, log_limit);
1045 if ((ret = allocate_buffers(s)) < 0)
1048 if (no_history || s->num_passes >= 7)
1049 s->best_decorr = s->mask_decorr = 0;
1051 for (pi = 0; pi < s->num_passes;) {
1052 const WavPackDecorrSpec *wpds;
1058 if (s->mask_decorr == 0)
1061 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1063 if (c == s->best_decorr) {
1064 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1069 wpds = &s->decorr_specs[c];
1070 nterms = decorr_filter_nterms[s->decorr_filter];
1073 memcpy(s->temp_buffer[0][0], samples, buf_size);
1074 CLEAR(save_decorr_passes);
1076 for (j = 0; j < nterms; j++) {
1077 CLEAR(temp_decorr_pass);
1078 temp_decorr_pass.delta = wpds->delta;
1079 temp_decorr_pass.value = wpds->terms[j];
1081 if (temp_decorr_pass.value < 0)
1082 temp_decorr_pass.value = 1;
1084 decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1085 FFMIN(nb_samples, 2048), &temp_decorr_pass, -1);
1088 CLEAR(temp_decorr_pass.samplesA);
1090 reverse_mono_decorr(&temp_decorr_pass);
1093 memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1094 decorr_mono(s->temp_buffer[j&1][0], s->temp_buffer[~j&1][0],
1095 nb_samples, &temp_decorr_pass, 1);
1098 size = log2mono(s->temp_buffer[j&1][0], nb_samples, log_limit);
1099 if (size != UINT32_MAX || !nterms)
1104 if (size < best_size) {
1105 memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1106 memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1107 s->num_terms = nterms;
1113 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1117 analyze_mono(s, samples, do_samples);
1118 else if (do_samples)
1119 memcpy(samples, s->best_buffer[0], buf_size);
1121 if (no_history || s->extra_flags) {
1123 scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1128 static void decorr_stereo(int32_t *in_left, int32_t *in_right,
1129 int32_t *out_left, int32_t *out_right,
1130 int nb_samples, struct Decorr *dpp, int dir)
1134 dpp->sumA = dpp->sumB = 0;
1137 out_left += nb_samples - 1;
1138 out_right += nb_samples - 1;
1139 in_left += nb_samples - 1;
1140 in_right += nb_samples - 1;
1143 dpp->weightA = restore_weight(store_weight(dpp->weightA));
1144 dpp->weightB = restore_weight(store_weight(dpp->weightB));
1146 for (i = 0; i < MAX_TERM; i++) {
1147 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1148 dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1151 switch (dpp->value) {
1153 while (nb_samples--) {
1156 sam = dpp->samplesA[0];
1157 dpp->samplesA[0] = dpp->samplesA[1];
1158 out_left[0] = tmp = (dpp->samplesA[1] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1159 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1160 dpp->sumA += dpp->weightA;
1162 sam = dpp->samplesB[0];
1163 dpp->samplesB[0] = dpp->samplesB[1];
1164 out_right[0] = tmp = (dpp->samplesB[1] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1165 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1166 dpp->sumB += dpp->weightB;
1175 while (nb_samples--) {
1178 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1179 dpp->samplesA[1] = dpp->samplesA[0];
1180 out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1181 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1182 dpp->sumA += dpp->weightA;
1184 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1185 dpp->samplesB[1] = dpp->samplesB[0];
1186 out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT (dpp->weightB, sam);
1187 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1188 dpp->sumB += dpp->weightB;
1197 while (nb_samples--) {
1200 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1201 dpp->samplesA[1] = dpp->samplesA[0];
1202 out_left[0] = tmp = (dpp->samplesA[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1203 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1204 dpp->sumA += dpp->weightA;
1206 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1207 dpp->samplesB[1] = dpp->samplesB[0];
1208 out_right[0] = tmp = (dpp->samplesB[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1209 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1210 dpp->sumB += dpp->weightB;
1219 int k = dpp->value & (MAX_TERM - 1);
1221 while (nb_samples--) {
1224 sam = dpp->samplesA[m];
1225 out_left[0] = tmp = (dpp->samplesA[k] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam);
1226 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1227 dpp->sumA += dpp->weightA;
1229 sam = dpp->samplesB[m];
1230 out_right[0] = tmp = (dpp->samplesB[k] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam);
1231 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1232 dpp->sumB += dpp->weightB;
1238 m = (m + 1) & (MAX_TERM - 1);
1239 k = (k + 1) & (MAX_TERM - 1);
1243 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1246 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1247 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1249 for (k = 0; k < MAX_TERM; k++) {
1250 dpp->samplesA[k] = temp_A[m];
1251 dpp->samplesB[k] = temp_B[m];
1252 m = (m + 1) & (MAX_TERM - 1);
1258 while (nb_samples--) {
1259 int32_t sam_A, sam_B, tmp;
1261 sam_A = dpp->samplesA[0];
1262 out_left[0] = tmp = (sam_B = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1263 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1264 dpp->sumA += dpp->weightA;
1266 out_right[0] = tmp = (dpp->samplesA[0] = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1267 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1268 dpp->sumB += dpp->weightB;
1277 while (nb_samples--) {
1278 int32_t sam_A, sam_B, tmp;
1280 sam_B = dpp->samplesB[0];
1281 out_right[0] = tmp = (sam_A = in_right[0]) - APPLY_WEIGHT(dpp->weightB, sam_B);
1282 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1283 dpp->sumB += dpp->weightB;
1285 out_left[0] = tmp = (dpp->samplesB[0] = in_left[0]) - APPLY_WEIGHT(dpp->weightA, sam_A);
1286 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1287 dpp->sumA += dpp->weightA;
1296 while (nb_samples--) {
1297 int32_t sam_A, sam_B, tmp;
1299 sam_A = dpp->samplesA[0];
1300 sam_B = dpp->samplesB[0];
1302 dpp->samplesA[0] = tmp = in_right[0];
1303 out_right[0] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
1304 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1305 dpp->sumB += dpp->weightB;
1307 dpp->samplesB[0] = tmp = in_left[0];
1308 out_left[0] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
1309 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1310 dpp->sumA += dpp->weightA;
1321 static void reverse_decorr(struct Decorr *dpp)
1323 if (dpp->value > MAX_TERM) {
1324 int32_t sam_A, sam_B;
1326 if (dpp->value & 1) {
1327 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1328 sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1330 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1331 sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1334 dpp->samplesA[1] = dpp->samplesA[0];
1335 dpp->samplesB[1] = dpp->samplesB[0];
1336 dpp->samplesA[0] = sam_A;
1337 dpp->samplesB[0] = sam_B;
1339 if (dpp->value & 1) {
1340 sam_A = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1341 sam_B = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1343 sam_A = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
1344 sam_B = (3 * dpp->samplesB[0] - dpp->samplesB[1]) >> 1;
1347 dpp->samplesA[1] = sam_A;
1348 dpp->samplesB[1] = sam_B;
1349 } else if (dpp->value > 1) {
1352 for (i = 0, j = dpp->value - 1, k = 0; k < dpp->value / 2; i++, j--, k++) {
1353 i &= (MAX_TERM - 1);
1354 j &= (MAX_TERM - 1);
1355 dpp->samplesA[i] ^= dpp->samplesA[j];
1356 dpp->samplesA[j] ^= dpp->samplesA[i];
1357 dpp->samplesA[i] ^= dpp->samplesA[j];
1358 dpp->samplesB[i] ^= dpp->samplesB[j];
1359 dpp->samplesB[j] ^= dpp->samplesB[i];
1360 dpp->samplesB[i] ^= dpp->samplesB[j];
1365 static void decorr_stereo_quick(int32_t *in_left, int32_t *in_right,
1366 int32_t *out_left, int32_t *out_right,
1367 int nb_samples, struct Decorr *dpp)
1371 dpp->weightA = restore_weight(store_weight(dpp->weightA));
1372 dpp->weightB = restore_weight(store_weight(dpp->weightB));
1374 for (i = 0; i < MAX_TERM; i++) {
1375 dpp->samplesA[i] = wp_exp2(log2s(dpp->samplesA[i]));
1376 dpp->samplesB[i] = wp_exp2(log2s(dpp->samplesB[i]));
1379 switch (dpp->value) {
1381 for (i = 0; i < nb_samples; i++) {
1384 sam = dpp->samplesA[0];
1385 dpp->samplesA[0] = dpp->samplesA[1];
1386 out_left[i] = tmp = (dpp->samplesA[1] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1387 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1389 sam = dpp->samplesB[0];
1390 dpp->samplesB[0] = dpp->samplesB[1];
1391 out_right[i] = tmp = (dpp->samplesB[1] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1392 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1396 for (i = 0; i < nb_samples; i++) {
1399 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
1400 dpp->samplesA[1] = dpp->samplesA[0];
1401 out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1402 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1404 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
1405 dpp->samplesB[1] = dpp->samplesB[0];
1406 out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1407 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1411 for (i = 0; i < nb_samples; i++) {
1414 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
1415 dpp->samplesA[1] = dpp->samplesA[0];
1416 out_left[i] = tmp = (dpp->samplesA[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1417 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1419 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
1420 dpp->samplesB[1] = dpp->samplesB[0];
1421 out_right[i] = tmp = (dpp->samplesB[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1422 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1426 int k = dpp->value & (MAX_TERM - 1);
1428 for (i = 0; i < nb_samples; i++) {
1431 sam = dpp->samplesA[m];
1432 out_left[i] = tmp = (dpp->samplesA[k] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
1433 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
1435 sam = dpp->samplesB[m];
1436 out_right[i] = tmp = (dpp->samplesB[k] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
1437 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
1439 m = (m + 1) & (MAX_TERM - 1);
1440 k = (k + 1) & (MAX_TERM - 1);
1444 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
1447 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
1448 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
1450 for (k = 0; k < MAX_TERM; k++) {
1451 dpp->samplesA[k] = temp_A[m];
1452 dpp->samplesB[k] = temp_B[m];
1453 m = (m + 1) & (MAX_TERM - 1);
1459 for (i = 0; i < nb_samples; i++) {
1460 int32_t sam_A, sam_B, tmp;
1462 sam_A = dpp->samplesA[0];
1463 out_left[i] = tmp = (sam_B = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1464 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1466 out_right[i] = tmp = (dpp->samplesA[0] = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1467 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1471 for (i = 0; i < nb_samples; i++) {
1472 int32_t sam_A, sam_B, tmp;
1474 sam_B = dpp->samplesB[0];
1475 out_right[i] = tmp = (sam_A = in_right[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
1476 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1478 out_left[i] = tmp = (dpp->samplesB[0] = in_left[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
1479 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1483 for (i = 0; i < nb_samples; i++) {
1484 int32_t sam_A, sam_B, tmp;
1486 sam_A = dpp->samplesA[0];
1487 sam_B = dpp->samplesB[0];
1489 dpp->samplesA[0] = tmp = in_right[i];
1490 out_right[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
1491 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
1493 dpp->samplesB[0] = tmp = in_left[i];
1494 out_left[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
1495 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
1501 static void decorr_stereo_buffer(WavPackExtraInfo *info,
1502 int32_t *in_left, int32_t *in_right,
1503 int32_t *out_left, int32_t *out_right,
1504 int nb_samples, int tindex)
1506 struct Decorr dp = {0}, *dppi = info->dps + tindex;
1507 int delta = dppi->delta, pre_delta;
1508 int term = dppi->value;
1515 pre_delta = delta + 1;
1518 dp.delta = pre_delta;
1519 decorr_stereo(in_left, in_right, out_left, out_right,
1520 FFMIN(2048, nb_samples), &dp, -1);
1524 reverse_decorr(&dp);
1530 memcpy(dppi->samplesA, dp.samplesA, sizeof(dp.samplesA));
1531 memcpy(dppi->samplesB, dp.samplesB, sizeof(dp.samplesB));
1532 dppi->weightA = dp.weightA;
1533 dppi->weightB = dp.weightB;
1537 decorr_stereo(in_left, in_right, out_left, out_right, nb_samples, &dp, 1);
1539 memcpy(dp.samplesA, dppi->samplesA, sizeof(dp.samplesA));
1540 memcpy(dp.samplesB, dppi->samplesB, sizeof(dp.samplesB));
1541 dppi->weightA = dp.weightA = dp.sumA / nb_samples;
1542 dppi->weightB = dp.weightB = dp.sumB / nb_samples;
1546 decorr_stereo(in_left, in_right, out_left, out_right,
1547 nb_samples, &dp, 1);
1549 decorr_stereo_quick(in_left, in_right, out_left, out_right,
1553 static void sort_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1561 memcpy(info->dps, s->decorr_passes, sizeof(s->decorr_passes));
1564 for (ri = 0; ri < info->nterms && s->decorr_passes[ri].value; ri++) {
1566 if (ri + 1 >= info->nterms || !s->decorr_passes[ri+1].value)
1569 if (s->decorr_passes[ri].value == s->decorr_passes[ri+1].value) {
1570 decorr_stereo_buffer(info,
1571 s->sampleptrs[ri ][0], s->sampleptrs[ri ][1],
1572 s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1573 s->block_samples, ri);
1577 info->dps[ri ] = s->decorr_passes[ri+1];
1578 info->dps[ri+1] = s->decorr_passes[ri ];
1580 for (i = ri; i < info->nterms && s->decorr_passes[i].value; i++)
1581 decorr_stereo_buffer(info,
1582 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1583 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1584 s->block_samples, i);
1586 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1587 s->block_samples, info->log_limit);
1589 if (bits < info->best_bits) {
1591 info->best_bits = bits;
1592 CLEAR(s->decorr_passes);
1593 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1594 memcpy(s->sampleptrs[info->nterms + 1][0],
1595 s->sampleptrs[i][0], s->block_samples * 4);
1596 memcpy(s->sampleptrs[info->nterms + 1][1],
1597 s->sampleptrs[i][1], s->block_samples * 4);
1599 info->dps[ri ] = s->decorr_passes[ri ];
1600 info->dps[ri+1] = s->decorr_passes[ri+1];
1601 decorr_stereo_buffer(info,
1602 s->sampleptrs[ri ][0], s->sampleptrs[ri ][1],
1603 s->sampleptrs[ri+1][0], s->sampleptrs[ri+1][1],
1604 s->block_samples, ri);
1610 static void delta_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info)
1612 int lower = 0, delta, d, i;
1615 if (!s->decorr_passes[0].value)
1617 delta = s->decorr_passes[0].delta;
1619 for (d = delta - 1; d >= 0; d--) {
1620 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1621 info->dps[i].value = s->decorr_passes[i].value;
1622 info->dps[i].delta = d;
1623 decorr_stereo_buffer(info,
1624 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1625 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1626 s->block_samples, i);
1629 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1630 s->block_samples, info->log_limit);
1631 if (bits >= info->best_bits)
1634 info->best_bits = bits;
1635 CLEAR(s->decorr_passes);
1636 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1637 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[i][0],
1638 s->block_samples * 4);
1639 memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[i][1],
1640 s->block_samples * 4);
1643 for (d = delta + 1; !lower && d <= 7; d++) {
1644 for (i = 0; i < info->nterms && s->decorr_passes[i].value; i++) {
1645 info->dps[i].value = s->decorr_passes[i].value;
1646 info->dps[i].delta = d;
1647 decorr_stereo_buffer(info,
1648 s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1649 s->sampleptrs[i+1][0], s->sampleptrs[i+1][1],
1650 s->block_samples, i);
1653 bits = log2stereo(s->sampleptrs[i][0], s->sampleptrs[i][1],
1654 s->block_samples, info->log_limit);
1656 if (bits < info->best_bits) {
1657 info->best_bits = bits;
1658 CLEAR(s->decorr_passes);
1659 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * i);
1660 memcpy(s->sampleptrs[info->nterms + 1][0],
1661 s->sampleptrs[i][0], s->block_samples * 4);
1662 memcpy(s->sampleptrs[info->nterms + 1][1],
1663 s->sampleptrs[i][1], s->block_samples * 4);
1670 static void recurse_stereo(WavPackEncodeContext *s, WavPackExtraInfo *info,
1671 int depth, int delta, uint32_t input_bits)
1673 int term, branches = s->num_branches - depth;
1674 int32_t *in_left, *in_right, *out_left, *out_right;
1675 uint32_t term_bits[22], bits;
1677 if (branches < 1 || depth + 1 == info->nterms)
1681 in_left = s->sampleptrs[depth ][0];
1682 in_right = s->sampleptrs[depth ][1];
1683 out_left = s->sampleptrs[depth + 1][0];
1684 out_right = s->sampleptrs[depth + 1][1];
1686 for (term = -3; term <= 18; term++) {
1687 if (!term || (term > 8 && term < 17))
1690 if (term == 17 && branches == 1 && depth + 1 < info->nterms)
1693 if (term == -1 || term == -2)
1694 if (!(s->flags & WV_CROSS_DECORR))
1697 if (!s->extra_flags && (term > 4 && term < 17))
1700 info->dps[depth].value = term;
1701 info->dps[depth].delta = delta;
1702 decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1703 s->block_samples, depth);
1704 bits = log2stereo(out_left, out_right, s->block_samples, info->log_limit);
1706 if (bits < info->best_bits) {
1707 info->best_bits = bits;
1708 CLEAR(s->decorr_passes);
1709 memcpy(s->decorr_passes, info->dps, sizeof(info->dps[0]) * (depth + 1));
1710 memcpy(s->sampleptrs[info->nterms + 1][0], s->sampleptrs[depth + 1][0],
1711 s->block_samples * 4);
1712 memcpy(s->sampleptrs[info->nterms + 1][1], s->sampleptrs[depth + 1][1],
1713 s->block_samples * 4);
1716 term_bits[term + 3] = bits;
1719 while (depth + 1 < info->nterms && branches--) {
1720 uint32_t local_best_bits = input_bits;
1721 int best_term = 0, i;
1723 for (i = 0; i < 22; i++)
1724 if (term_bits[i] && term_bits[i] < local_best_bits) {
1725 local_best_bits = term_bits[i];
1732 term_bits[best_term + 3] = 0;
1734 info->dps[depth].value = best_term;
1735 info->dps[depth].delta = delta;
1736 decorr_stereo_buffer(info, in_left, in_right, out_left, out_right,
1737 s->block_samples, depth);
1739 recurse_stereo(s, info, depth + 1, delta, local_best_bits);
1743 static void analyze_stereo(WavPackEncodeContext *s,
1744 int32_t *in_left, int32_t *in_right,
1747 WavPackExtraInfo info;
1750 info.gt16bit = ((s->flags & MAG_MASK) >> MAG_LSB) >= 16;
1752 info.log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1753 info.log_limit = FFMIN(6912, info.log_limit);
1755 info.nterms = s->num_terms;
1757 if (allocate_buffers2(s, s->num_terms))
1760 memcpy(info.dps, s->decorr_passes, sizeof(info.dps));
1761 memcpy(s->sampleptrs[0][0], in_left, s->block_samples * 4);
1762 memcpy(s->sampleptrs[0][1], in_right, s->block_samples * 4);
1764 for (i = 0; i < info.nterms && info.dps[i].value; i++)
1766 decorr_stereo(s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1767 s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1768 s->block_samples, info.dps + i, 1);
1770 decorr_stereo_quick(s->sampleptrs[i ][0], s->sampleptrs[i ][1],
1771 s->sampleptrs[i + 1][0], s->sampleptrs[i + 1][1],
1772 s->block_samples, info.dps + i);
1774 info.best_bits = log2stereo(s->sampleptrs[info.nterms][0], s->sampleptrs[info.nterms][1],
1775 s->block_samples, 0);
1777 memcpy(s->sampleptrs[info.nterms + 1][0], s->sampleptrs[i][0], s->block_samples * 4);
1778 memcpy(s->sampleptrs[info.nterms + 1][1], s->sampleptrs[i][1], s->block_samples * 4);
1780 if (s->extra_flags & EXTRA_BRANCHES)
1781 recurse_stereo(s, &info, 0, (int) floor(s->delta_decay + 0.5),
1782 log2stereo(s->sampleptrs[0][0], s->sampleptrs[0][1],
1783 s->block_samples, 0));
1785 if (s->extra_flags & EXTRA_SORT_FIRST)
1786 sort_stereo(s, &info);
1788 if (s->extra_flags & EXTRA_TRY_DELTAS) {
1789 delta_stereo(s, &info);
1791 if ((s->extra_flags & EXTRA_ADJUST_DELTAS) && s->decorr_passes[0].value)
1792 s->delta_decay = (float)((s->delta_decay * 2.0 + s->decorr_passes[0].delta) / 3.0);
1794 s->delta_decay = 2.0;
1797 if (s->extra_flags & EXTRA_SORT_LAST)
1798 sort_stereo(s, &info);
1801 memcpy(in_left, s->sampleptrs[info.nterms + 1][0], s->block_samples * 4);
1802 memcpy(in_right, s->sampleptrs[info.nterms + 1][1], s->block_samples * 4);
1805 for (i = 0; i < info.nterms; i++)
1806 if (!s->decorr_passes[i].value)
1812 static int wv_stereo(WavPackEncodeContext *s,
1813 int32_t *samples_l, int32_t *samples_r,
1814 int no_history, int do_samples)
1816 struct Decorr temp_decorr_pass, save_decorr_passes[MAX_TERMS] = {{0}};
1817 int nb_samples = s->block_samples, ret;
1818 int buf_size = sizeof(int32_t) * nb_samples;
1819 int log_limit, force_js = 0, force_ts = 0, got_js = 0, pi, i;
1820 uint32_t best_size = UINT32_MAX, size;
1822 for (i = 0; i < nb_samples; i++)
1823 if (samples_l[i] || samples_r[i])
1826 if (i == nb_samples) {
1827 s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1828 CLEAR(s->decorr_passes);
1834 log_limit = (((s->flags & MAG_MASK) >> MAG_LSB) + 4) * 256;
1835 log_limit = FFMIN(6912, log_limit);
1837 if (s->joint != -1) {
1838 force_js = s->joint;
1839 force_ts = !s->joint;
1842 if ((ret = allocate_buffers(s)) < 0)
1845 if (no_history || s->num_passes >= 7)
1846 s->best_decorr = s->mask_decorr = 0;
1848 for (pi = 0; pi < s->num_passes;) {
1849 const WavPackDecorrSpec *wpds;
1855 if (s->mask_decorr == 0)
1858 c = (s->best_decorr & (s->mask_decorr - 1)) | s->mask_decorr;
1860 if (c == s->best_decorr) {
1861 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1866 wpds = &s->decorr_specs[c];
1867 nterms = decorr_filter_nterms[s->decorr_filter];
1870 if (force_js || (wpds->joint_stereo && !force_ts)) {
1872 av_fast_padded_malloc(&s->js_left, &s->js_left_size, buf_size);
1873 av_fast_padded_malloc(&s->js_right, &s->js_right_size, buf_size);
1874 memcpy(s->js_left, samples_l, buf_size);
1875 memcpy(s->js_right, samples_r, buf_size);
1877 for (i = 0; i < nb_samples; i++)
1878 s->js_right[i] += ((s->js_left[i] -= s->js_right[i]) >> 1);
1882 memcpy(s->temp_buffer[0][0], s->js_left, buf_size);
1883 memcpy(s->temp_buffer[0][1], s->js_right, buf_size);
1885 memcpy(s->temp_buffer[0][0], samples_l, buf_size);
1886 memcpy(s->temp_buffer[0][1], samples_r, buf_size);
1889 CLEAR(save_decorr_passes);
1891 for (j = 0; j < nterms; j++) {
1892 CLEAR(temp_decorr_pass);
1893 temp_decorr_pass.delta = wpds->delta;
1894 temp_decorr_pass.value = wpds->terms[j];
1896 if (temp_decorr_pass.value < 0 && !(s->flags & WV_CROSS_DECORR))
1897 temp_decorr_pass.value = -3;
1899 decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1900 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1901 FFMIN(2048, nb_samples), &temp_decorr_pass, -1);
1904 CLEAR(temp_decorr_pass.samplesA);
1905 CLEAR(temp_decorr_pass.samplesB);
1907 reverse_decorr(&temp_decorr_pass);
1910 memcpy(save_decorr_passes + j, &temp_decorr_pass, sizeof(struct Decorr));
1912 if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16)
1913 decorr_stereo(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1914 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1915 nb_samples, &temp_decorr_pass, 1);
1917 decorr_stereo_quick(s->temp_buffer[ j&1][0], s->temp_buffer[ j&1][1],
1918 s->temp_buffer[~j&1][0], s->temp_buffer[~j&1][1],
1919 nb_samples, &temp_decorr_pass);
1922 size = log2stereo(s->temp_buffer[j&1][0], s->temp_buffer[j&1][1],
1923 nb_samples, log_limit);
1924 if (size != UINT32_MAX || !nterms)
1929 if (size < best_size) {
1930 memcpy(s->best_buffer[0], s->temp_buffer[j&1][0], buf_size);
1931 memcpy(s->best_buffer[1], s->temp_buffer[j&1][1], buf_size);
1932 memcpy(s->decorr_passes, save_decorr_passes, sizeof(struct Decorr) * MAX_TERMS);
1933 s->num_terms = nterms;
1939 s->mask_decorr = s->mask_decorr ? ((s->mask_decorr << 1) & (s->num_decorrs - 1)) : 1;
1942 if (force_js || (s->decorr_specs[s->best_decorr].joint_stereo && !force_ts))
1943 s->flags |= WV_JOINT_STEREO;
1945 s->flags &= ~((uint32_t) WV_JOINT_STEREO);
1947 if (s->extra_flags) {
1948 if (s->flags & WV_JOINT_STEREO) {
1949 analyze_stereo(s, s->js_left, s->js_right, do_samples);
1952 memcpy(samples_l, s->js_left, buf_size);
1953 memcpy(samples_r, s->js_right, buf_size);
1956 analyze_stereo(s, samples_l, samples_r, do_samples);
1957 } else if (do_samples) {
1958 memcpy(samples_l, s->best_buffer[0], buf_size);
1959 memcpy(samples_r, s->best_buffer[1], buf_size);
1962 if (s->extra_flags || no_history ||
1963 s->joint_stereo != s->decorr_specs[s->best_decorr].joint_stereo) {
1964 s->joint_stereo = s->decorr_specs[s->best_decorr].joint_stereo;
1966 scan_word(s, &s->w.c[0], s->best_buffer[0], nb_samples, -1);
1967 scan_word(s, &s->w.c[1], s->best_buffer[1], nb_samples, -1);
1972 #define count_bits(av) ( \
1973 (av) < (1 << 8) ? nbits_table[av] : \
1975 (av) < (1 << 16) ? nbits_table[(av) >> 8] + 8 : \
1976 ((av) < (1 << 24) ? nbits_table[(av) >> 16] + 16 : nbits_table[(av) >> 24] + 24) \
1980 static void encode_flush(WavPackEncodeContext *s)
1982 WavPackWords *w = &s->w;
1983 PutBitContext *pb = &s->pb;
1986 int cbits = count_bits(w->zeros_acc);
1990 put_bits(pb, 31, 0x7FFFFFFF);
1993 put_bits(pb, cbits, (1 << cbits) - 1);
2000 while (w->zeros_acc > 1) {
2001 put_bits(pb, 1, w->zeros_acc & 1);
2008 if (w->holding_one) {
2009 if (w->holding_one >= 16) {
2012 put_bits(pb, 16, (1 << 16) - 1);
2014 w->holding_one -= 16;
2015 cbits = count_bits(w->holding_one);
2019 put_bits(pb, 31, 0x7FFFFFFF);
2022 put_bits(pb, cbits, (1 << cbits) - 1);
2029 while (w->holding_one > 1) {
2030 put_bits(pb, 1, w->holding_one & 1);
2031 w->holding_one >>= 1;
2034 w->holding_zero = 0;
2036 put_bits(pb, w->holding_one, (1 << w->holding_one) - 1);
2042 if (w->holding_zero) {
2044 w->holding_zero = 0;
2047 if (w->pend_count) {
2048 put_bits(pb, w->pend_count, w->pend_data);
2049 w->pend_data = w->pend_count = 0;
2053 static void wavpack_encode_sample(WavPackEncodeContext *s, WvChannel *c, int32_t sample)
2055 WavPackWords *w = &s->w;
2056 uint32_t ones_count, low, high;
2057 int sign = sample < 0;
2059 if (s->w.c[0].median[0] < 2 && !s->w.holding_zero && s->w.c[1].median[0] < 2) {
2067 } else if (sample) {
2068 put_bits(&s->pb, 1, 0);
2070 CLEAR(s->w.c[0].median);
2071 CLEAR(s->w.c[1].median);
2080 if (sample < (int32_t) GET_MED(0)) {
2081 ones_count = low = 0;
2082 high = GET_MED(0) - 1;
2088 if (sample - low < GET_MED(1)) {
2090 high = low + GET_MED(1) - 1;
2096 if (sample - low < GET_MED(2)) {
2098 high = low + GET_MED(2) - 1;
2101 ones_count = 2 + (sample - low) / GET_MED(2);
2102 low += (ones_count - 2) * GET_MED(2);
2103 high = low + GET_MED(2) - 1;
2109 if (w->holding_zero) {
2116 w->holding_zero = 1;
2119 w->holding_zero = 0;
2121 w->holding_zero = 1;
2123 w->holding_one = ones_count * 2;
2126 uint32_t maxcode = high - low, code = sample - low;
2127 int bitcount = count_bits(maxcode);
2128 uint32_t extras = (1 << bitcount) - maxcode - 1;
2130 if (code < extras) {
2131 w->pend_data |= code << w->pend_count;
2132 w->pend_count += bitcount - 1;
2134 w->pend_data |= ((code + extras) >> 1) << w->pend_count;
2135 w->pend_count += bitcount - 1;
2136 w->pend_data |= ((code + extras) & 1) << w->pend_count++;
2140 w->pend_data |= ((int32_t) sign << w->pend_count++);
2142 if (!w->holding_zero)
2146 static void pack_int32(WavPackEncodeContext *s,
2147 int32_t *samples_l, int32_t *samples_r,
2150 const int sent_bits = s->int32_sent_bits;
2151 PutBitContext *pb = &s->pb;
2154 pre_shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2159 if (s->flags & WV_MONO_DATA) {
2160 for (i = 0; i < nb_samples; i++) {
2161 put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2164 for (i = 0; i < nb_samples; i++) {
2165 put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2166 put_sbits(pb, sent_bits, samples_r[i] >> pre_shift);
2171 static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
2173 const int max_exp = s->float_max_exp;
2174 PutBitContext *pb = &s->pb;
2175 int32_t value, shift_count;
2177 if (get_exponent(*sample) == 255) {
2178 if (get_mantissa(*sample)) {
2180 put_bits(pb, 23, get_mantissa(*sample));
2187 } else if (get_exponent(*sample)) {
2188 shift_count = max_exp - get_exponent(*sample);
2189 value = 0x800000 + get_mantissa(*sample);
2191 shift_count = max_exp ? max_exp - 1 : 0;
2192 value = get_mantissa(*sample);
2195 if (shift_count < 25)
2196 value >>= shift_count;
2201 if (s->float_flags & FLOAT_ZEROS_SENT) {
2202 if (get_exponent(*sample) || get_mantissa(*sample)) {
2204 put_bits(pb, 23, get_mantissa(*sample));
2207 put_bits(pb, 8, get_exponent(*sample));
2209 put_bits(pb, 1, get_sign(*sample));
2213 if (s->float_flags & FLOAT_NEG_ZEROS)
2214 put_bits(pb, 1, get_sign(*sample));
2217 } else if (shift_count) {
2218 if (s->float_flags & FLOAT_SHIFT_SENT) {
2219 put_sbits(pb, shift_count, get_mantissa(*sample));
2220 } else if (s->float_flags & FLOAT_SHIFT_SAME) {
2221 put_bits(pb, 1, get_mantissa(*sample) & 1);
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_DATA) {
2493 if (!(s->flags & WV_MONO) && s->optimize_mono) {
2494 int32_t lor = 0, diff = 0;
2496 for (i = 0; i < nb_samples; i++) {
2497 lor |= samples_l[i] | samples_r[i];
2498 diff |= samples_l[i] - samples_r[i];
2504 if (i == nb_samples && lor && !diff) {
2505 s->flags &= ~(WV_JOINT_STEREO | WV_CROSS_DECORR);
2506 s->flags |= WV_FALSE_STEREO;
2508 if (!s->false_stereo) {
2509 s->false_stereo = 1;
2513 } else if (s->false_stereo) {
2514 s->false_stereo = 0;
2520 if (s->flags & SHIFT_MASK) {
2521 int shift = (s->flags & SHIFT_MASK) >> SHIFT_LSB;
2522 int mag = (s->flags & MAG_MASK) >> MAG_LSB;
2524 if (s->flags & WV_MONO_DATA)
2525 shift_mono(samples_l, nb_samples, shift);
2527 shift_stereo(samples_l, samples_r, nb_samples, shift);
2529 if ((mag -= shift) < 0)
2530 s->flags &= ~MAG_MASK;
2532 s->flags -= (1 << MAG_LSB) * shift;
2535 if ((s->flags & WV_FLOAT_DATA) || (s->flags & MAG_MASK) >> MAG_LSB >= 24) {
2536 av_fast_padded_malloc(&s->orig_l, &s->orig_l_size, sizeof(int32_t) * nb_samples);
2537 memcpy(s->orig_l, samples_l, sizeof(int32_t) * nb_samples);
2538 if (!(s->flags & WV_MONO_DATA)) {
2539 av_fast_padded_malloc(&s->orig_r, &s->orig_r_size, sizeof(int32_t) * nb_samples);
2540 memcpy(s->orig_r, samples_r, sizeof(int32_t) * nb_samples);
2543 if (s->flags & WV_FLOAT_DATA)
2544 got_extra = scan_float(s, samples_l, samples_r, nb_samples);
2546 got_extra = scan_int32(s, samples_l, samples_r, nb_samples);
2549 scan_int23(s, samples_l, samples_r, nb_samples);
2550 if (s->shift != s->int32_zeros + s->int32_ones + s->int32_dups) {
2551 s->shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2556 if (!s->num_passes && !s->num_terms) {
2559 if (s->flags & WV_MONO_DATA)
2560 ret = wv_mono(s, samples_l, 1, 0);
2562 ret = wv_stereo(s, samples_l, samples_r, 1, 0);
2566 if (s->flags & WV_MONO_DATA) {
2567 for (i = 0; i < nb_samples; i++)
2568 crc += (crc << 1) + samples_l[i];
2571 ret = wv_mono(s, samples_l, !s->num_terms, 1);
2573 for (i = 0; i < nb_samples; i++)
2574 crc += (crc << 3) + (samples_l[i] << 1) + samples_l[i] + samples_r[i];
2577 ret = wv_stereo(s, samples_l, samples_r, !s->num_terms, 1);
2583 s->flags |= WV_INITIAL_BLOCK;
2585 s->ch_offset += 1 + !(s->flags & WV_MONO);
2587 if (s->ch_offset == s->avctx->channels)
2588 s->flags |= WV_FINAL_BLOCK;
2590 bytestream2_init_writer(&pb, out, out_size);
2591 bytestream2_put_le32(&pb, MKTAG('w', 'v', 'p', 'k'));
2592 bytestream2_put_le32(&pb, 0);
2593 bytestream2_put_le16(&pb, 0x410);
2594 bytestream2_put_le16(&pb, 0);
2595 bytestream2_put_le32(&pb, 0);
2596 bytestream2_put_le32(&pb, s->sample_index);
2597 bytestream2_put_le32(&pb, nb_samples);
2598 bytestream2_put_le32(&pb, s->flags);
2599 bytestream2_put_le32(&pb, crc);
2601 if (s->flags & WV_INITIAL_BLOCK &&
2602 s->avctx->channel_layout != AV_CH_LAYOUT_MONO &&
2603 s->avctx->channel_layout != AV_CH_LAYOUT_STEREO) {
2604 put_metadata_block(&pb, WP_ID_CHANINFO, 5);
2605 bytestream2_put_byte(&pb, s->avctx->channels);
2606 bytestream2_put_le32(&pb, s->avctx->channel_layout);
2607 bytestream2_put_byte(&pb, 0);
2610 if ((s->flags & SRATE_MASK) == SRATE_MASK) {
2611 put_metadata_block(&pb, WP_ID_SAMPLE_RATE, 3);
2612 bytestream2_put_le24(&pb, s->avctx->sample_rate);
2613 bytestream2_put_byte(&pb, 0);
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));
2621 if (s->num_terms & 1)
2622 bytestream2_put_byte(&pb, 0);
2624 #define WRITE_DECWEIGHT(type) do { \
2625 temp = store_weight(type); \
2626 bytestream2_put_byte(&pb, temp); \
2627 type = restore_weight(temp); \
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];
2636 if (store_weight(dpp->weightA) ||
2637 (!(s->flags & WV_MONO_DATA) && store_weight(dpp->weightB)))
2641 for (i = 0; i < s->num_terms; i++) {
2642 struct Decorr *dpp = &s->decorr_passes[i];
2644 WRITE_DECWEIGHT(dpp->weightA);
2645 if (!(s->flags & WV_MONO_DATA))
2646 WRITE_DECWEIGHT(dpp->weightB);
2648 dpp->weightA = dpp->weightB = 0;
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);
2657 #define WRITE_DECSAMPLE(type) do { \
2658 temp = log2s(type); \
2659 type = wp_exp2(temp); \
2660 bytestream2_put_le16(&pb, temp); \
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];
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]);
2676 } else if (dpp->value < 0) {
2677 WRITE_DECSAMPLE(dpp->samplesA[0]);
2678 WRITE_DECSAMPLE(dpp->samplesB[0]);
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]);
2687 CLEAR(dpp->samplesA);
2688 CLEAR(dpp->samplesB);
2691 end = bytestream2_tell_p(&pb);
2692 out[start - 1] = (end - start) >> 1;
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); \
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);
2707 if (s->flags & WV_FLOAT_DATA) {
2708 put_metadata_block(&pb, WP_ID_FLOATINFO, 4);
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);
2715 if (s->flags & WV_INT32_DATA) {
2716 put_metadata_block(&pb, WP_ID_INT32INFO, 4);
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);
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];
2727 for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++) {
2730 if (dpp->value > MAX_TERM) {
2732 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2734 sam = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
2736 dpp->samplesA[1] = dpp->samplesA[0];
2737 dpp->samplesA[0] = code;
2739 sam = dpp->samplesA[m];
2740 dpp->samplesA[(m + dpp->value) & (MAX_TERM - 1)] = code;
2743 code -= APPLY_WEIGHT(dpp->weightA, sam);
2744 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, code);
2747 m = (m + 1) & (MAX_TERM - 1);
2748 samples_l[i] = code;
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];
2756 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2757 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
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);
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);
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);
2777 decorr_stereo_pass_id2(dpp, samples_l, samples_r, nb_samples);
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]);
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]);
2793 flush_put_bits(&s->pb);
2794 data_size = put_bits_count(&s->pb) >> 3;
2795 bytestream2_put_le24(&pb, (data_size + 1) >> 1);
2796 bytestream2_skip_p(&pb, data_size);
2798 bytestream2_put_byte(&pb, 0);
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);
2806 pack_int32(s, s->orig_l, s->orig_r, nb_samples);
2807 flush_put_bits(&s->pb);
2808 data_size = put_bits_count(&s->pb) >> 3;
2809 bytestream2_put_le24(&pb, (data_size + 5) >> 1);
2810 bytestream2_put_le32(&pb, s->crc_x);
2811 bytestream2_skip_p(&pb, data_size);
2813 bytestream2_put_byte(&pb, 0);
2816 block_size = bytestream2_tell_p(&pb);
2817 AV_WL32(out + 4, block_size - 8);
2819 av_assert0(!bytestream2_get_eof(&pb));
2824 static void fill_buffer(WavPackEncodeContext *s,
2825 const int8_t *src, int32_t *dst,
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; \
2836 switch (s->avctx->sample_fmt) {
2837 case AV_SAMPLE_FMT_U8P:
2838 COPY_SAMPLES(int8_t, 0x80, 0);
2840 case AV_SAMPLE_FMT_S16P:
2841 COPY_SAMPLES(int16_t, 0, 0);
2843 case AV_SAMPLE_FMT_S32P:
2844 if (s->avctx->bits_per_raw_sample <= 24) {
2845 COPY_SAMPLES(int32_t, 0, 8);
2848 case AV_SAMPLE_FMT_FLTP:
2849 memcpy(dst, src, nb_samples * 4);
2853 static void set_samplerate(WavPackEncodeContext *s)
2857 for (i = 0; i < 15; i++) {
2858 if (wv_rates[i] == s->avctx->sample_rate)
2862 s->flags = i << SRATE_LSB;
2865 static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2866 const AVFrame *frame, int *got_packet_ptr)
2868 WavPackEncodeContext *s = avctx->priv_data;
2872 s->block_samples = frame->nb_samples;
2873 av_fast_padded_malloc(&s->samples[0], &s->samples_size[0],
2874 sizeof(int32_t) * s->block_samples);
2876 return AVERROR(ENOMEM);
2877 if (avctx->channels > 1) {
2878 av_fast_padded_malloc(&s->samples[1], &s->samples_size[1],
2879 sizeof(int32_t) * s->block_samples);
2881 return AVERROR(ENOMEM);
2884 buf_size = s->block_samples * avctx->channels * 8
2885 + 200 * avctx->channels /* for headers */;
2886 if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
2890 for (s->ch_offset = 0; s->ch_offset < avctx->channels;) {
2893 switch (s->avctx->sample_fmt) {
2894 case AV_SAMPLE_FMT_S16P: s->flags |= 1; break;
2895 case AV_SAMPLE_FMT_S32P: s->flags |= 3 - (s->avctx->bits_per_raw_sample <= 24); break;
2896 case AV_SAMPLE_FMT_FLTP: s->flags |= 3 | WV_FLOAT_DATA;
2899 fill_buffer(s, frame->extended_data[s->ch_offset], s->samples[0], s->block_samples);
2900 if (avctx->channels - s->ch_offset == 1) {
2901 s->flags |= WV_MONO;
2903 s->flags |= WV_CROSS_DECORR;
2904 fill_buffer(s, frame->extended_data[s->ch_offset + 1], s->samples[1], s->block_samples);
2907 s->flags += (1 << MAG_LSB) * ((s->flags & 3) * 8 + 7);
2909 if ((ret = wavpack_encode_block(s, s->samples[0], s->samples[1],
2910 buf, buf_size)) < 0)
2916 s->sample_index += frame->nb_samples;
2918 avpkt->pts = frame->pts;
2919 avpkt->size = buf - avpkt->data;
2920 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
2921 *got_packet_ptr = 1;
2925 static av_cold int wavpack_encode_close(AVCodecContext *avctx)
2927 WavPackEncodeContext *s = avctx->priv_data;
2930 for (i = 0; i < MAX_TERMS + 2; i++) {
2931 av_freep(&s->sampleptrs[i][0]);
2932 av_freep(&s->sampleptrs[i][1]);
2933 s->sampleptrs_size[i][0] = s->sampleptrs_size[i][1] = 0;
2936 for (i = 0; i < 2; i++) {
2937 av_freep(&s->samples[i]);
2938 s->samples_size[i] = 0;
2940 av_freep(&s->best_buffer[i]);
2941 s->best_buffer_size[i] = 0;
2943 av_freep(&s->temp_buffer[i][0]);
2944 av_freep(&s->temp_buffer[i][1]);
2945 s->temp_buffer_size[i][0] = s->temp_buffer_size[i][1] = 0;
2948 av_freep(&s->js_left);
2949 av_freep(&s->js_right);
2950 s->js_left_size = s->js_right_size = 0;
2952 av_freep(&s->orig_l);
2953 av_freep(&s->orig_r);
2954 s->orig_l_size = s->orig_r_size = 0;
2959 #define OFFSET(x) offsetof(WavPackEncodeContext, x)
2960 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2961 static const AVOption options[] = {
2962 { "joint_stereo", "", OFFSET(joint), AV_OPT_TYPE_BOOL, {.i64=-1}, -1, 1, FLAGS },
2963 { "optimize_mono", "", OFFSET(optimize_mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
2967 static const AVClass wavpack_encoder_class = {
2968 .class_name = "WavPack encoder",
2969 .item_name = av_default_item_name,
2971 .version = LIBAVUTIL_VERSION_INT,
2974 AVCodec ff_wavpack_encoder = {
2976 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
2977 .type = AVMEDIA_TYPE_AUDIO,
2978 .id = AV_CODEC_ID_WAVPACK,
2979 .priv_data_size = sizeof(WavPackEncodeContext),
2980 .priv_class = &wavpack_encoder_class,
2981 .init = wavpack_encode_init,
2982 .encode2 = wavpack_encode_frame,
2983 .close = wavpack_encode_close,
2984 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
2985 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8P,
2989 AV_SAMPLE_FMT_NONE },