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