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 > WV_MAX_SAMPLES)
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 < (1 << 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);
1832 if (s->joint != -1) {
1833 force_js = s->joint;
1834 force_ts = !s->joint;
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) < (1 << 16) ? nbits_table[(av) >> 8] + 8 : \
1971 ((av) < (1 << 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 PutBitContext *pb = &s->pb;
2149 pre_shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2154 if (s->flags & WV_MONO_DATA) {
2155 for (i = 0; i < nb_samples; i++) {
2156 put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2159 for (i = 0; i < nb_samples; i++) {
2160 put_sbits(pb, sent_bits, samples_l[i] >> pre_shift);
2161 put_sbits(pb, sent_bits, samples_r[i] >> pre_shift);
2166 static void pack_float_sample(WavPackEncodeContext *s, int32_t *sample)
2168 const int max_exp = s->float_max_exp;
2169 PutBitContext *pb = &s->pb;
2170 int32_t value, shift_count;
2172 if (get_exponent(*sample) == 255) {
2173 if (get_mantissa(*sample)) {
2175 put_bits(pb, 23, get_mantissa(*sample));
2182 } else if (get_exponent(*sample)) {
2183 shift_count = max_exp - get_exponent(*sample);
2184 value = 0x800000 + get_mantissa(*sample);
2186 shift_count = max_exp ? max_exp - 1 : 0;
2187 value = get_mantissa(*sample);
2190 if (shift_count < 25)
2191 value >>= shift_count;
2196 if (s->float_flags & FLOAT_ZEROS_SENT) {
2197 if (get_exponent(*sample) || get_mantissa(*sample)) {
2199 put_bits(pb, 23, get_mantissa(*sample));
2202 put_bits(pb, 8, get_exponent(*sample));
2204 put_bits(pb, 1, get_sign(*sample));
2208 if (s->float_flags & FLOAT_NEG_ZEROS)
2209 put_bits(pb, 1, get_sign(*sample));
2212 } else if (shift_count) {
2213 if (s->float_flags & FLOAT_SHIFT_SENT) {
2214 int32_t data = get_mantissa(*sample) & ((1 << shift_count) - 1);
2215 put_bits(pb, shift_count, data);
2216 } else if (s->float_flags & FLOAT_SHIFT_SAME) {
2217 put_bits(pb, 1, get_mantissa(*sample) & 1);
2222 static void pack_float(WavPackEncodeContext *s,
2223 int32_t *samples_l, int32_t *samples_r,
2228 if (s->flags & WV_MONO_DATA) {
2229 for (i = 0; i < nb_samples; i++)
2230 pack_float_sample(s, &samples_l[i]);
2232 for (i = 0; i < nb_samples; i++) {
2233 pack_float_sample(s, &samples_l[i]);
2234 pack_float_sample(s, &samples_r[i]);
2239 static void decorr_stereo_pass2(struct Decorr *dpp,
2240 int32_t *samples_l, int32_t *samples_r,
2245 switch (dpp->value) {
2247 for (i = 0; i < nb_samples; i++) {
2250 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2251 dpp->samplesA[1] = dpp->samplesA[0];
2252 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2253 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2255 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2256 dpp->samplesB[1] = dpp->samplesB[0];
2257 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2258 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2262 for (i = 0; i < nb_samples; i++) {
2265 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2266 dpp->samplesA[1] = dpp->samplesA[0];
2267 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2268 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2270 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2271 dpp->samplesB[1] = dpp->samplesB[0];
2272 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2273 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2277 for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2280 sam = dpp->samplesA[m];
2281 samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam);
2282 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, tmp);
2284 sam = dpp->samplesB[m];
2285 samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam);
2286 UPDATE_WEIGHT(dpp->weightB, dpp->delta, sam, tmp);
2288 m = (m + 1) & (MAX_TERM - 1);
2289 k = (k + 1) & (MAX_TERM - 1);
2292 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2294 memcpy(temp_A, dpp->samplesA, sizeof (dpp->samplesA));
2295 memcpy(temp_B, dpp->samplesB, sizeof (dpp->samplesB));
2297 for (k = 0; k < MAX_TERM; k++) {
2298 dpp->samplesA[k] = temp_A[m];
2299 dpp->samplesB[k] = temp_B[m];
2300 m = (m + 1) & (MAX_TERM - 1);
2305 for (i = 0; i < nb_samples; i++) {
2306 int32_t sam_A, sam_B, tmp;
2308 sam_A = dpp->samplesA[0];
2309 samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2310 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2312 samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2313 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2317 for (i = 0; i < nb_samples; i++) {
2318 int32_t sam_A, sam_B, tmp;
2320 sam_B = dpp->samplesB[0];
2321 samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT(dpp->weightB, sam_B);
2322 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2324 samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT(dpp->weightA, sam_A);
2325 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2329 for (i = 0; i < nb_samples; i++) {
2330 int32_t sam_A, sam_B, tmp;
2332 sam_A = dpp->samplesA[0];
2333 sam_B = dpp->samplesB[0];
2335 dpp->samplesA[0] = tmp = samples_r[i];
2336 samples_r[i] = tmp -= APPLY_WEIGHT(dpp->weightB, sam_B);
2337 UPDATE_WEIGHT_CLIP(dpp->weightB, dpp->delta, sam_B, tmp);
2339 dpp->samplesB[0] = tmp = samples_l[i];
2340 samples_l[i] = tmp -= APPLY_WEIGHT(dpp->weightA, sam_A);
2341 UPDATE_WEIGHT_CLIP(dpp->weightA, dpp->delta, sam_A, tmp);
2347 #define update_weight_d2(weight, delta, source, result) \
2348 if (source && result) \
2349 weight -= (((source ^ result) >> 29) & 4) - 2;
2351 #define update_weight_clip_d2(weight, delta, source, result) \
2352 if (source && result) { \
2353 const int32_t s = (source ^ result) >> 31; \
2354 if ((weight = (weight ^ s) + (2 - s)) > 1024) weight = 1024; \
2355 weight = (weight ^ s) - s; \
2358 static void decorr_stereo_pass_id2(struct Decorr *dpp,
2359 int32_t *samples_l, int32_t *samples_r,
2364 switch (dpp->value) {
2366 for (i = 0; i < nb_samples; i++) {
2369 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2370 dpp->samplesA[1] = dpp->samplesA[0];
2371 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2372 update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2374 sam = 2 * dpp->samplesB[0] - dpp->samplesB[1];
2375 dpp->samplesB[1] = dpp->samplesB[0];
2376 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2377 update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2381 for (i = 0; i < nb_samples; i++) {
2384 sam = dpp->samplesA[0] + ((dpp->samplesA[0] - dpp->samplesA[1]) >> 1);
2385 dpp->samplesA[1] = dpp->samplesA[0];
2386 samples_l[i] = tmp = (dpp->samplesA[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2387 update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2389 sam = dpp->samplesB[0] + ((dpp->samplesB[0] - dpp->samplesB[1]) >> 1);
2390 dpp->samplesB[1] = dpp->samplesB[0];
2391 samples_r[i] = tmp = (dpp->samplesB[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2392 update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2396 for (m = 0, k = dpp->value & (MAX_TERM - 1), i = 0; i < nb_samples; i++) {
2399 sam = dpp->samplesA[m];
2400 samples_l[i] = tmp = (dpp->samplesA[k] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam);
2401 update_weight_d2(dpp->weightA, dpp->delta, sam, tmp);
2403 sam = dpp->samplesB[m];
2404 samples_r[i] = tmp = (dpp->samplesB[k] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam);
2405 update_weight_d2(dpp->weightB, dpp->delta, sam, tmp);
2407 m = (m + 1) & (MAX_TERM - 1);
2408 k = (k + 1) & (MAX_TERM - 1);
2412 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2414 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2415 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2417 for (k = 0; k < MAX_TERM; k++) {
2418 dpp->samplesA[k] = temp_A[m];
2419 dpp->samplesB[k] = temp_B[m];
2420 m = (m + 1) & (MAX_TERM - 1);
2425 for (i = 0; i < nb_samples; i++) {
2426 int32_t sam_A, sam_B, tmp;
2428 sam_A = dpp->samplesA[0];
2429 samples_l[i] = tmp = (sam_B = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2430 update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2432 samples_r[i] = tmp = (dpp->samplesA[0] = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2433 update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2437 for (i = 0; i < nb_samples; i++) {
2438 int32_t sam_A, sam_B, tmp;
2440 sam_B = dpp->samplesB[0];
2441 samples_r[i] = tmp = (sam_A = samples_r[i]) - APPLY_WEIGHT_I(dpp->weightB, sam_B);
2442 update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2444 samples_l[i] = tmp = (dpp->samplesB[0] = samples_l[i]) - APPLY_WEIGHT_I(dpp->weightA, sam_A);
2445 update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2449 for (i = 0; i < nb_samples; i++) {
2450 int32_t sam_A, sam_B, tmp;
2452 sam_A = dpp->samplesA[0];
2453 sam_B = dpp->samplesB[0];
2455 dpp->samplesA[0] = tmp = samples_r[i];
2456 samples_r[i] = tmp -= APPLY_WEIGHT_I(dpp->weightB, sam_B);
2457 update_weight_clip_d2(dpp->weightB, dpp->delta, sam_B, tmp);
2459 dpp->samplesB[0] = tmp = samples_l[i];
2460 samples_l[i] = tmp -= APPLY_WEIGHT_I(dpp->weightA, sam_A);
2461 update_weight_clip_d2(dpp->weightA, dpp->delta, sam_A, tmp);
2467 static void put_metadata_block(PutByteContext *pb, int flags, int size)
2470 flags |= WP_IDF_ODD;
2472 bytestream2_put_byte(pb, flags);
2473 bytestream2_put_byte(pb, (size + 1) >> 1);
2476 static int wavpack_encode_block(WavPackEncodeContext *s,
2477 int32_t *samples_l, int32_t *samples_r,
2478 uint8_t *out, int out_size)
2480 int block_size, start, end, data_size, tcount, temp, m = 0;
2481 int i, j, ret = 0, got_extra = 0, nb_samples = s->block_samples;
2482 uint32_t crc = 0xffffffffu;
2486 if (s->flags & WV_MONO_DATA) {
2489 if (!(s->flags & WV_MONO) && s->optimize_mono) {
2490 int32_t lor = 0, diff = 0;
2492 for (i = 0; i < nb_samples; i++) {
2493 lor |= samples_l[i] | samples_r[i];
2494 diff |= samples_l[i] - samples_r[i];
2500 if (i == nb_samples && lor && !diff) {
2501 s->flags &= ~(WV_JOINT_STEREO | WV_CROSS_DECORR);
2502 s->flags |= WV_FALSE_STEREO;
2504 if (!s->false_stereo) {
2505 s->false_stereo = 1;
2509 } else if (s->false_stereo) {
2510 s->false_stereo = 0;
2516 if (s->flags & SHIFT_MASK) {
2517 int shift = (s->flags & SHIFT_MASK) >> SHIFT_LSB;
2518 int mag = (s->flags & MAG_MASK) >> MAG_LSB;
2520 if (s->flags & WV_MONO_DATA)
2521 shift_mono(samples_l, nb_samples, shift);
2523 shift_stereo(samples_l, samples_r, nb_samples, shift);
2525 if ((mag -= shift) < 0)
2526 s->flags &= ~MAG_MASK;
2528 s->flags -= (1 << MAG_LSB) * shift;
2531 if ((s->flags & WV_FLOAT_DATA) || (s->flags & MAG_MASK) >> MAG_LSB >= 24) {
2532 av_fast_padded_malloc(&s->orig_l, &s->orig_l_size, sizeof(int32_t) * nb_samples);
2533 memcpy(s->orig_l, samples_l, sizeof(int32_t) * nb_samples);
2534 if (!(s->flags & WV_MONO_DATA)) {
2535 av_fast_padded_malloc(&s->orig_r, &s->orig_r_size, sizeof(int32_t) * nb_samples);
2536 memcpy(s->orig_r, samples_r, sizeof(int32_t) * nb_samples);
2539 if (s->flags & WV_FLOAT_DATA)
2540 got_extra = scan_float(s, samples_l, samples_r, nb_samples);
2542 got_extra = scan_int32(s, samples_l, samples_r, nb_samples);
2545 scan_int23(s, samples_l, samples_r, nb_samples);
2546 if (s->shift != s->int32_zeros + s->int32_ones + s->int32_dups) {
2547 s->shift = s->int32_zeros + s->int32_ones + s->int32_dups;
2552 if (!s->num_passes && !s->num_terms) {
2555 if (s->flags & WV_MONO_DATA)
2556 ret = wv_mono(s, samples_l, 1, 0);
2558 ret = wv_stereo(s, samples_l, samples_r, 1, 0);
2562 if (s->flags & WV_MONO_DATA) {
2563 for (i = 0; i < nb_samples; i++)
2564 crc += (crc << 1) + samples_l[i];
2567 ret = wv_mono(s, samples_l, !s->num_terms, 1);
2569 for (i = 0; i < nb_samples; i++)
2570 crc += (crc << 3) + (samples_l[i] << 1) + samples_l[i] + samples_r[i];
2573 ret = wv_stereo(s, samples_l, samples_r, !s->num_terms, 1);
2579 s->flags |= WV_INITIAL_BLOCK;
2581 s->ch_offset += 1 + !(s->flags & WV_MONO);
2583 if (s->ch_offset == s->avctx->channels)
2584 s->flags |= WV_FINAL_BLOCK;
2586 bytestream2_init_writer(&pb, out, out_size);
2587 bytestream2_put_le32(&pb, MKTAG('w', 'v', 'p', 'k'));
2588 bytestream2_put_le32(&pb, 0);
2589 bytestream2_put_le16(&pb, 0x410);
2590 bytestream2_put_le16(&pb, 0);
2591 bytestream2_put_le32(&pb, 0);
2592 bytestream2_put_le32(&pb, s->sample_index);
2593 bytestream2_put_le32(&pb, nb_samples);
2594 bytestream2_put_le32(&pb, s->flags);
2595 bytestream2_put_le32(&pb, crc);
2597 if (s->flags & WV_INITIAL_BLOCK &&
2598 s->avctx->channel_layout != AV_CH_LAYOUT_MONO &&
2599 s->avctx->channel_layout != AV_CH_LAYOUT_STEREO) {
2600 put_metadata_block(&pb, WP_ID_CHANINFO, 5);
2601 bytestream2_put_byte(&pb, s->avctx->channels);
2602 bytestream2_put_le32(&pb, s->avctx->channel_layout);
2603 bytestream2_put_byte(&pb, 0);
2606 if ((s->flags & SRATE_MASK) == SRATE_MASK) {
2607 put_metadata_block(&pb, WP_ID_SAMPLE_RATE, 3);
2608 bytestream2_put_le24(&pb, s->avctx->sample_rate);
2609 bytestream2_put_byte(&pb, 0);
2612 put_metadata_block(&pb, WP_ID_DECTERMS, s->num_terms);
2613 for (i = 0; i < s->num_terms; i++) {
2614 struct Decorr *dpp = &s->decorr_passes[i];
2615 bytestream2_put_byte(&pb, ((dpp->value + 5) & 0x1f) | ((dpp->delta << 5) & 0xe0));
2617 if (s->num_terms & 1)
2618 bytestream2_put_byte(&pb, 0);
2620 #define WRITE_DECWEIGHT(type) do { \
2621 temp = store_weight(type); \
2622 bytestream2_put_byte(&pb, temp); \
2623 type = restore_weight(temp); \
2626 bytestream2_put_byte(&pb, WP_ID_DECWEIGHTS);
2627 bytestream2_put_byte(&pb, 0);
2628 start = bytestream2_tell_p(&pb);
2629 for (i = s->num_terms - 1; i >= 0; --i) {
2630 struct Decorr *dpp = &s->decorr_passes[i];
2632 if (store_weight(dpp->weightA) ||
2633 (!(s->flags & WV_MONO_DATA) && store_weight(dpp->weightB)))
2637 for (i = 0; i < s->num_terms; i++) {
2638 struct Decorr *dpp = &s->decorr_passes[i];
2640 WRITE_DECWEIGHT(dpp->weightA);
2641 if (!(s->flags & WV_MONO_DATA))
2642 WRITE_DECWEIGHT(dpp->weightB);
2644 dpp->weightA = dpp->weightB = 0;
2647 end = bytestream2_tell_p(&pb);
2648 out[start - 2] = WP_ID_DECWEIGHTS | (((end - start) & 1) ? WP_IDF_ODD: 0);
2649 out[start - 1] = (end - start + 1) >> 1;
2650 if ((end - start) & 1)
2651 bytestream2_put_byte(&pb, 0);
2653 #define WRITE_DECSAMPLE(type) do { \
2654 temp = log2s(type); \
2655 type = wp_exp2(temp); \
2656 bytestream2_put_le16(&pb, temp); \
2659 bytestream2_put_byte(&pb, WP_ID_DECSAMPLES);
2660 bytestream2_put_byte(&pb, 0);
2661 start = bytestream2_tell_p(&pb);
2662 for (i = 0; i < s->num_terms; i++) {
2663 struct Decorr *dpp = &s->decorr_passes[i];
2665 if (dpp->value > MAX_TERM) {
2666 WRITE_DECSAMPLE(dpp->samplesA[0]);
2667 WRITE_DECSAMPLE(dpp->samplesA[1]);
2668 if (!(s->flags & WV_MONO_DATA)) {
2669 WRITE_DECSAMPLE(dpp->samplesB[0]);
2670 WRITE_DECSAMPLE(dpp->samplesB[1]);
2672 } else if (dpp->value < 0) {
2673 WRITE_DECSAMPLE(dpp->samplesA[0]);
2674 WRITE_DECSAMPLE(dpp->samplesB[0]);
2676 for (j = 0; j < dpp->value; j++) {
2677 WRITE_DECSAMPLE(dpp->samplesA[j]);
2678 if (!(s->flags & WV_MONO_DATA))
2679 WRITE_DECSAMPLE(dpp->samplesB[j]);
2683 CLEAR(dpp->samplesA);
2684 CLEAR(dpp->samplesB);
2687 end = bytestream2_tell_p(&pb);
2688 out[start - 1] = (end - start) >> 1;
2690 #define WRITE_CHAN_ENTROPY(chan) do { \
2691 for (i = 0; i < 3; i++) { \
2692 temp = wp_log2(s->w.c[chan].median[i]); \
2693 bytestream2_put_le16(&pb, temp); \
2694 s->w.c[chan].median[i] = wp_exp2(temp); \
2698 put_metadata_block(&pb, WP_ID_ENTROPY, 6 * (1 + (!(s->flags & WV_MONO_DATA))));
2699 WRITE_CHAN_ENTROPY(0);
2700 if (!(s->flags & WV_MONO_DATA))
2701 WRITE_CHAN_ENTROPY(1);
2703 if (s->flags & WV_FLOAT_DATA) {
2704 put_metadata_block(&pb, WP_ID_FLOATINFO, 4);
2705 bytestream2_put_byte(&pb, s->float_flags);
2706 bytestream2_put_byte(&pb, s->float_shift);
2707 bytestream2_put_byte(&pb, s->float_max_exp);
2708 bytestream2_put_byte(&pb, 127);
2711 if (s->flags & WV_INT32_DATA) {
2712 put_metadata_block(&pb, WP_ID_INT32INFO, 4);
2713 bytestream2_put_byte(&pb, s->int32_sent_bits);
2714 bytestream2_put_byte(&pb, s->int32_zeros);
2715 bytestream2_put_byte(&pb, s->int32_ones);
2716 bytestream2_put_byte(&pb, s->int32_dups);
2719 if (s->flags & WV_MONO_DATA && !s->num_passes) {
2720 for (i = 0; i < nb_samples; i++) {
2721 int32_t code = samples_l[i];
2723 for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++) {
2726 if (dpp->value > MAX_TERM) {
2728 sam = 2 * dpp->samplesA[0] - dpp->samplesA[1];
2730 sam = (3 * dpp->samplesA[0] - dpp->samplesA[1]) >> 1;
2732 dpp->samplesA[1] = dpp->samplesA[0];
2733 dpp->samplesA[0] = code;
2735 sam = dpp->samplesA[m];
2736 dpp->samplesA[(m + dpp->value) & (MAX_TERM - 1)] = code;
2739 code -= APPLY_WEIGHT(dpp->weightA, sam);
2740 UPDATE_WEIGHT(dpp->weightA, dpp->delta, sam, code);
2743 m = (m + 1) & (MAX_TERM - 1);
2744 samples_l[i] = code;
2747 for (tcount = s->num_terms, dpp = s->decorr_passes; tcount--; dpp++)
2748 if (dpp->value > 0 && dpp->value <= MAX_TERM) {
2749 int32_t temp_A[MAX_TERM], temp_B[MAX_TERM];
2752 memcpy(temp_A, dpp->samplesA, sizeof(dpp->samplesA));
2753 memcpy(temp_B, dpp->samplesB, sizeof(dpp->samplesB));
2755 for (k = 0; k < MAX_TERM; k++) {
2756 dpp->samplesA[k] = temp_A[m];
2757 dpp->samplesB[k] = temp_B[m];
2758 m = (m + 1) & (MAX_TERM - 1);
2762 } else if (!s->num_passes) {
2763 if (s->flags & WV_JOINT_STEREO) {
2764 for (i = 0; i < nb_samples; i++)
2765 samples_r[i] += ((samples_l[i] -= samples_r[i]) >> 1);
2768 for (i = 0; i < s->num_terms; i++) {
2769 struct Decorr *dpp = &s->decorr_passes[i];
2770 if (((s->flags & MAG_MASK) >> MAG_LSB) >= 16 || dpp->delta != 2)
2771 decorr_stereo_pass2(dpp, samples_l, samples_r, nb_samples);
2773 decorr_stereo_pass_id2(dpp, samples_l, samples_r, nb_samples);
2777 bytestream2_put_byte(&pb, WP_ID_DATA | WP_IDF_LONG);
2778 init_put_bits(&s->pb, pb.buffer + 3, bytestream2_get_bytes_left_p(&pb));
2779 if (s->flags & WV_MONO_DATA) {
2780 for (i = 0; i < nb_samples; i++)
2781 wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2783 for (i = 0; i < nb_samples; i++) {
2784 wavpack_encode_sample(s, &s->w.c[0], s->samples[0][i]);
2785 wavpack_encode_sample(s, &s->w.c[1], s->samples[1][i]);
2789 flush_put_bits(&s->pb);
2790 data_size = put_bits_count(&s->pb) >> 3;
2791 bytestream2_put_le24(&pb, (data_size + 1) >> 1);
2792 bytestream2_skip_p(&pb, data_size);
2794 bytestream2_put_byte(&pb, 0);
2797 bytestream2_put_byte(&pb, WP_ID_EXTRABITS | WP_IDF_LONG);
2798 init_put_bits(&s->pb, pb.buffer + 7, bytestream2_get_bytes_left_p(&pb));
2799 if (s->flags & WV_FLOAT_DATA)
2800 pack_float(s, s->orig_l, s->orig_r, nb_samples);
2802 pack_int32(s, s->orig_l, s->orig_r, nb_samples);
2803 flush_put_bits(&s->pb);
2804 data_size = put_bits_count(&s->pb) >> 3;
2805 bytestream2_put_le24(&pb, (data_size + 5) >> 1);
2806 bytestream2_put_le32(&pb, s->crc_x);
2807 bytestream2_skip_p(&pb, data_size);
2809 bytestream2_put_byte(&pb, 0);
2812 block_size = bytestream2_tell_p(&pb);
2813 AV_WL32(out + 4, block_size - 8);
2815 av_assert0(!bytestream2_get_eof(&pb));
2820 static void fill_buffer(WavPackEncodeContext *s,
2821 const int8_t *src, int32_t *dst,
2826 #define COPY_SAMPLES(type, offset, shift) do { \
2827 const type *sptr = (const type *)src; \
2828 for (i = 0; i < nb_samples; i++) \
2829 dst[i] = (sptr[i] - offset) >> shift; \
2832 switch (s->avctx->sample_fmt) {
2833 case AV_SAMPLE_FMT_U8P:
2834 COPY_SAMPLES(int8_t, 0x80, 0);
2836 case AV_SAMPLE_FMT_S16P:
2837 COPY_SAMPLES(int16_t, 0, 0);
2839 case AV_SAMPLE_FMT_S32P:
2840 if (s->avctx->bits_per_raw_sample <= 24) {
2841 COPY_SAMPLES(int32_t, 0, 8);
2844 case AV_SAMPLE_FMT_FLTP:
2845 memcpy(dst, src, nb_samples * 4);
2849 static void set_samplerate(WavPackEncodeContext *s)
2853 for (i = 0; i < 15; i++) {
2854 if (wv_rates[i] == s->avctx->sample_rate)
2858 s->flags = i << SRATE_LSB;
2861 static int wavpack_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2862 const AVFrame *frame, int *got_packet_ptr)
2864 WavPackEncodeContext *s = avctx->priv_data;
2868 s->block_samples = frame->nb_samples;
2869 av_fast_padded_malloc(&s->samples[0], &s->samples_size[0],
2870 sizeof(int32_t) * s->block_samples);
2872 return AVERROR(ENOMEM);
2873 if (avctx->channels > 1) {
2874 av_fast_padded_malloc(&s->samples[1], &s->samples_size[1],
2875 sizeof(int32_t) * s->block_samples);
2877 return AVERROR(ENOMEM);
2880 buf_size = s->block_samples * avctx->channels * 8
2881 + 200 /* for headers */;
2882 if ((ret = ff_alloc_packet2(avctx, avpkt, buf_size, 0)) < 0)
2886 for (s->ch_offset = 0; s->ch_offset < avctx->channels;) {
2889 switch (s->avctx->sample_fmt) {
2890 case AV_SAMPLE_FMT_S16P: s->flags |= 1; break;
2891 case AV_SAMPLE_FMT_S32P: s->flags |= 3 - (s->avctx->bits_per_raw_sample <= 24); break;
2892 case AV_SAMPLE_FMT_FLTP: s->flags |= 3 | WV_FLOAT_DATA;
2895 fill_buffer(s, frame->extended_data[s->ch_offset], s->samples[0], s->block_samples);
2896 if (avctx->channels - s->ch_offset == 1) {
2897 s->flags |= WV_MONO;
2899 s->flags |= WV_CROSS_DECORR;
2900 fill_buffer(s, frame->extended_data[s->ch_offset + 1], s->samples[1], s->block_samples);
2903 s->flags += (1 << MAG_LSB) * ((s->flags & 3) * 8 + 7);
2905 if ((ret = wavpack_encode_block(s, s->samples[0], s->samples[1],
2906 buf, buf_size)) < 0)
2912 s->sample_index += frame->nb_samples;
2914 avpkt->pts = frame->pts;
2915 avpkt->size = buf - avpkt->data;
2916 avpkt->duration = ff_samples_to_time_base(avctx, frame->nb_samples);
2917 *got_packet_ptr = 1;
2921 static av_cold int wavpack_encode_close(AVCodecContext *avctx)
2923 WavPackEncodeContext *s = avctx->priv_data;
2926 for (i = 0; i < MAX_TERMS + 2; i++) {
2927 av_freep(&s->sampleptrs[i][0]);
2928 av_freep(&s->sampleptrs[i][1]);
2929 s->sampleptrs_size[i][0] = s->sampleptrs_size[i][1] = 0;
2932 for (i = 0; i < 2; i++) {
2933 av_freep(&s->samples[i]);
2934 s->samples_size[i] = 0;
2936 av_freep(&s->best_buffer[i]);
2937 s->best_buffer_size[i] = 0;
2939 av_freep(&s->temp_buffer[i][0]);
2940 av_freep(&s->temp_buffer[i][1]);
2941 s->temp_buffer_size[i][0] = s->temp_buffer_size[i][1] = 0;
2944 av_freep(&s->js_left);
2945 av_freep(&s->js_right);
2946 s->js_left_size = s->js_right_size = 0;
2948 av_freep(&s->orig_l);
2949 av_freep(&s->orig_r);
2950 s->orig_l_size = s->orig_r_size = 0;
2955 #define OFFSET(x) offsetof(WavPackEncodeContext, x)
2956 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2957 static const AVOption options[] = {
2958 { "joint_stereo", "", OFFSET(joint), AV_OPT_TYPE_BOOL, {.i64=-1}, -1, 1, FLAGS },
2959 { "optimize_mono", "", OFFSET(optimize_mono), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
2963 static const AVClass wavpack_encoder_class = {
2964 .class_name = "WavPack encoder",
2965 .item_name = av_default_item_name,
2967 .version = LIBAVUTIL_VERSION_INT,
2970 AVCodec ff_wavpack_encoder = {
2972 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
2973 .type = AVMEDIA_TYPE_AUDIO,
2974 .id = AV_CODEC_ID_WAVPACK,
2975 .priv_data_size = sizeof(WavPackEncodeContext),
2976 .priv_class = &wavpack_encoder_class,
2977 .init = wavpack_encode_init,
2978 .encode2 = wavpack_encode_frame,
2979 .close = wavpack_encode_close,
2980 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME,
2981 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_U8P,
2985 AV_SAMPLE_FMT_NONE },