1 /*
2 * WavPack decoder/encoder common code
3 * Copyright (c) 2006,2011 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef AVCODEC_WAVPACK_H
23 #define AVCODEC_WAVPACK_H
24
25 #include "libavutil/common.h"
26
27 #define MAX_TERMS 16
28 #define MAX_TERM 8
29
30 #define WV_HEADER_SIZE 32
31
32 #define WV_MONO 0x00000004
33 #define WV_JOINT_STEREO 0x00000010
34 #define WV_CROSS_DECORR 0x00000020
35 #define WV_FLOAT_DATA 0x00000080
36 #define WV_INT32_DATA 0x00000100
37 #define WV_FALSE_STEREO 0x40000000
38 #define WV_DSD_DATA 0x80000000
39
40 #define WV_HYBRID_MODE 0x00000008
41 #define WV_HYBRID_SHAPE 0x00000008
42 #define WV_HYBRID_BITRATE 0x00000200
43 #define WV_HYBRID_BALANCE 0x00000400
44 #define WV_INITIAL_BLOCK 0x00000800
45 #define WV_FINAL_BLOCK 0x00001000
46
47 #define WV_MONO_DATA (WV_MONO | WV_FALSE_STEREO)
48
49 #define WV_SINGLE_BLOCK (WV_INITIAL_BLOCK | WV_FINAL_BLOCK)
50
51 #define WV_FLT_SHIFT_ONES 0x01
52 #define WV_FLT_SHIFT_SAME 0x02
53 #define WV_FLT_SHIFT_SENT 0x04
54 #define WV_FLT_ZERO_SENT 0x08
55 #define WV_FLT_ZERO_SIGN 0x10
56
57 #define WV_MAX_SAMPLES 150000
58
59 enum WP_ID_Flags {
60 WP_IDF_MASK = 0x3F,
61 WP_IDF_IGNORE = 0x20,
62 WP_IDF_ODD = 0x40,
63 WP_IDF_LONG = 0x80
64 };
65
66 enum WP_ID {
67 WP_ID_DUMMY = 0,
68 WP_ID_ENCINFO,
69 WP_ID_DECTERMS,
70 WP_ID_DECWEIGHTS,
71 WP_ID_DECSAMPLES,
72 WP_ID_ENTROPY,
73 WP_ID_HYBRID,
74 WP_ID_SHAPING,
75 WP_ID_FLOATINFO,
76 WP_ID_INT32INFO,
77 WP_ID_DATA,
78 WP_ID_CORR,
79 WP_ID_EXTRABITS,
80 WP_ID_CHANINFO,
81 WP_ID_DSD_DATA,
82 WP_ID_SAMPLE_RATE = 0x27,
83 };
84
85 typedef struct Decorr {
86 int delta;
87 int value;
88 int weightA;
89 int weightB;
90 int samplesA[MAX_TERM];
91 int samplesB[MAX_TERM];
92 int sumA;
93 int sumB;
94 } Decorr;
95
96 typedef struct WvChannel {
97 int median[3];
98 int slow_level, error_limit;
99 unsigned bitrate_acc, bitrate_delta;
100 } WvChannel;
101
102 // macros for manipulating median values
103 #define GET_MED(n) ((c->median[n] >> 4) + 1)
104 #define DEC_MED(n) c->median[n] -= ((int)(c->median[n] + (128U >> (n)) - 2) / (128 >> (n))) * 2U
105 #define INC_MED(n) c->median[n] += ((int)(c->median[n] + (128U >> (n)) ) / (128 >> (n))) * 5U
106
107 // macros for applying weight
108 #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
109 if ((samples) && (in)) { \
110 if (((samples) ^ (in)) < 0) { \
111 (weight) -= (delta); \
112 if ((weight) < -1024) \
113 (weight) = -1024; \
114 } else { \
115 (weight) += (delta); \
116 if ((weight) > 1024) \
117 (weight) = 1024; \
118 } \
119 }
120
121 static const int wv_rates[16] = {
122 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
123 32000, 44100, 48000, 64000, 88200, 96000, 192000, 0
124 };
125
126 // exponent table copied from WavPack source
127 extern const uint8_t ff_wp_exp2_table[256];
128 extern const uint8_t ff_wp_log2_table[256];
129
wp_exp2(int16_t val)130 static av_always_inline int wp_exp2(int16_t val)
131 {
132 int res, neg = 0;
133
134 if (val < 0) {
135 val = -val;
136 neg = 1;
137 }
138
139 res = ff_wp_exp2_table[val & 0xFF] | 0x100;
140 val >>= 8;
141 if (val > 31U)
142 return INT_MIN;
143 res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val));
144 return neg ? -res : res;
145 }
146
wp_log2(uint32_t val)147 static av_always_inline int wp_log2(uint32_t val)
148 {
149 int bits;
150
151 if (!val)
152 return 0;
153 if (val == 1)
154 return 256;
155 val += val >> 9;
156 bits = av_log2(val) + 1;
157 if (bits < 9)
158 return (bits << 8) + ff_wp_log2_table[(val << (9 - bits)) & 0xFF];
159 else
160 return (bits << 8) + ff_wp_log2_table[(val >> (bits - 9)) & 0xFF];
161 }
162
163 #endif /* AVCODEC_WAVPACK_H */
164