Mercurial > libavcodec.hg
annotate wavpack.c @ 5563:538d55152d09 libavcodec
remove code which become unused by the previous changes
| author | michael |
|---|---|
| date | Tue, 21 Aug 2007 00:05:30 +0000 |
| parents | 1c67999f81c8 |
| children | d92fa6e5fc8c |
| rev | line source |
|---|---|
| 3764 | 1 /* |
| 2 * WavPack lossless audio decoder | |
| 3 * Copyright (c) 2006 Konstantin Shishkov | |
| 4 * | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3782
diff
changeset
|
5 * This file is part of FFmpeg. |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3782
diff
changeset
|
6 * |
|
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3782
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
| 3764 | 8 * modify it under the terms of the GNU Lesser General Public |
| 9 * License as published by the Free Software Foundation; either | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3782
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
| 3764 | 11 * |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3782
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
| 3764 | 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 | |
|
3947
c8c591fe26f8
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
3782
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
| 3764 | 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 */ | |
| 21 #define ALT_BITSTREAM_READER_LE | |
| 22 #include "avcodec.h" | |
| 23 #include "bitstream.h" | |
| 24 | |
| 25 /** | |
| 26 * @file wavpack.c | |
| 27 * WavPack lossless audio decoder | |
| 28 */ | |
| 29 | |
| 5538 | 30 #define WV_JOINT_STEREO 0x00000010 |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
31 #define WV_FALSE_STEREO 0x40000000 |
| 3764 | 32 |
| 33 enum WP_ID_Flags{ | |
| 34 WP_IDF_MASK = 0x1F, | |
| 35 WP_IDF_IGNORE = 0x20, | |
| 36 WP_IDF_ODD = 0x40, | |
| 37 WP_IDF_LONG = 0x80 | |
| 38 }; | |
| 39 | |
| 40 enum WP_ID{ | |
| 41 WP_ID_DUMMY = 0, | |
| 42 WP_ID_ENCINFO, | |
| 43 WP_ID_DECTERMS, | |
| 44 WP_ID_DECWEIGHTS, | |
| 45 WP_ID_DECSAMPLES, | |
| 46 WP_ID_ENTROPY, | |
| 47 WP_ID_HYBRID, | |
| 48 WP_ID_SHAPING, | |
| 49 WP_ID_FLOATINFO, | |
| 50 WP_ID_INT32INFO, | |
| 51 WP_ID_DATA, | |
| 52 WP_ID_CORR, | |
| 53 WP_ID_FLT, | |
| 54 WP_ID_CHANINFO | |
| 55 }; | |
| 56 | |
| 57 #define MAX_TERMS 16 | |
| 58 | |
| 59 typedef struct Decorr { | |
| 60 int delta; | |
| 61 int value; | |
| 62 int weightA; | |
| 63 int weightB; | |
| 64 int samplesA[8]; | |
| 65 int samplesB[8]; | |
| 66 } Decorr; | |
| 67 | |
| 68 typedef struct WavpackContext { | |
| 69 AVCodecContext *avctx; | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
70 int stereo, stereo_in; |
| 3764 | 71 int joint; |
| 72 uint32_t CRC; | |
| 73 GetBitContext gb; | |
| 74 int data_size; // in bits | |
| 75 int samples; | |
| 76 int median[6]; | |
| 77 int terms; | |
| 78 Decorr decorr[MAX_TERMS]; | |
| 79 int zero, one, zeroes; | |
|
5482
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
80 int and, or, shift; |
| 3764 | 81 } WavpackContext; |
| 82 | |
| 83 // exponent table copied from WavPack source | |
| 84 static const uint8_t wp_exp2_table [256] = { | |
| 85 0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b, | |
| 86 0x0b, 0x0c, 0x0d, 0x0e, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x16, | |
| 87 0x17, 0x18, 0x19, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21, 0x22, 0x23, | |
| 88 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, | |
| 89 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3a, 0x3b, 0x3c, 0x3d, | |
| 90 0x3e, 0x3f, 0x40, 0x41, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b, | |
| 91 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, | |
| 92 0x5b, 0x5c, 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, | |
| 93 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, | |
| 94 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8a, | |
| 95 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, | |
| 96 0x9c, 0x9d, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, | |
| 97 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, | |
| 98 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc8, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4, | |
| 99 0xd6, 0xd7, 0xd8, 0xd9, 0xdb, 0xdc, 0xdd, 0xde, 0xe0, 0xe1, 0xe2, 0xe4, 0xe5, 0xe6, 0xe8, 0xe9, | |
| 100 0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff | |
| 101 }; | |
| 102 | |
|
4283
d6f83e2f8804
rename always_inline to av_always_inline and move to common.h
mru
parents:
4021
diff
changeset
|
103 static av_always_inline int wp_exp2(int16_t val) |
| 3764 | 104 { |
| 105 int res, neg = 0; | |
| 106 | |
| 107 if(val < 0){ | |
| 108 val = -val; | |
| 109 neg = 1; | |
| 110 } | |
| 111 | |
| 112 res = wp_exp2_table[val & 0xFF] | 0x100; | |
| 113 val >>= 8; | |
| 114 res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val)); | |
| 115 return neg ? -res : res; | |
| 116 } | |
| 117 | |
| 118 // macros for manipulating median values | |
| 119 #define GET_MED(n) ((median[n] >> 4) + 1) | |
| 120 #define DEC_MED(n) median[n] -= ((median[n] + (128>>n) - 2) / (128>>n)) * 2 | |
| 121 #define INC_MED(n) median[n] += ((median[n] + (128>>n)) / (128>>n)) * 5 | |
| 122 | |
| 123 // macros for applying weight | |
| 124 #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \ | |
| 125 if(samples && in){ \ | |
| 126 if((samples ^ in) < 0){ \ | |
| 127 weight -= delta; \ | |
| 128 if(weight < -1024) weight = -1024; \ | |
| 129 }else{ \ | |
| 130 weight += delta; \ | |
| 131 if(weight > 1024) weight = 1024; \ | |
| 132 } \ | |
| 133 } | |
| 134 | |
| 135 | |
|
4283
d6f83e2f8804
rename always_inline to av_always_inline and move to common.h
mru
parents:
4021
diff
changeset
|
136 static av_always_inline int get_tail(GetBitContext *gb, int k) |
| 3764 | 137 { |
| 138 int p, e, res; | |
| 139 | |
| 4421 | 140 if(k<1)return 0; |
| 141 p = av_log2(k); | |
| 3764 | 142 e = (1 << (p + 1)) - k - 1; |
| 3782 | 143 res = p ? get_bits(gb, p) : 0; |
| 3764 | 144 if(res >= e){ |
| 145 res = (res<<1) - e + get_bits1(gb); | |
| 146 } | |
| 147 return res; | |
| 148 } | |
| 149 | |
| 150 static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int *median, int *last) | |
| 151 { | |
| 152 int t, t2; | |
| 153 int sign, base, add, ret; | |
| 154 | |
| 155 *last = 0; | |
| 156 | |
| 157 if((ctx->median[0] < 2U) && (ctx->median[3] < 2U) && !ctx->zero && !ctx->one){ | |
| 158 if(ctx->zeroes){ | |
| 159 ctx->zeroes--; | |
| 160 if(ctx->zeroes) | |
| 161 return 0; | |
| 162 }else{ | |
| 5510 | 163 t = get_unary(gb, 0, 33); |
| 3764 | 164 if(t >= 2) t = get_bits(gb, t - 1) | (1 << (t-1)); |
| 165 ctx->zeroes = t; | |
| 166 if(ctx->zeroes){ | |
| 167 memset(ctx->median, 0, sizeof(ctx->median)); | |
| 168 return 0; | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 if(get_bits_count(gb) >= ctx->data_size){ | |
| 174 *last = 1; | |
| 175 return 0; | |
| 176 } | |
| 177 | |
| 178 if(ctx->zero){ | |
| 179 t = 0; | |
| 180 ctx->zero = 0; | |
| 181 }else{ | |
| 5510 | 182 t = get_unary(gb, 0, 33); |
| 3764 | 183 if(get_bits_count(gb) >= ctx->data_size){ |
| 184 *last = 1; | |
| 185 return 0; | |
| 186 } | |
| 187 if(t == 16) { | |
| 5510 | 188 t2 = get_unary(gb, 0, 33); |
| 3764 | 189 if(t2 < 2) t += t2; |
| 190 else t += get_bits(gb, t2 - 1) | (1 << (t2 - 1)); | |
| 191 } | |
| 192 | |
| 193 if(ctx->one){ | |
| 194 ctx->one = t&1; | |
| 195 t = (t>>1) + 1; | |
| 196 }else{ | |
| 197 ctx->one = t&1; | |
| 198 t >>= 1; | |
| 199 } | |
| 200 ctx->zero = !ctx->one; | |
| 201 } | |
| 202 | |
| 203 if(!t){ | |
| 204 base = 0; | |
| 205 add = GET_MED(0) - 1; | |
| 206 DEC_MED(0); | |
| 207 }else if(t == 1){ | |
| 208 base = GET_MED(0); | |
| 209 add = GET_MED(1) - 1; | |
| 210 INC_MED(0); | |
| 211 DEC_MED(1); | |
| 212 }else if(t == 2){ | |
| 213 base = GET_MED(0) + GET_MED(1); | |
| 214 add = GET_MED(2) - 1; | |
| 215 INC_MED(0); | |
| 216 INC_MED(1); | |
| 217 DEC_MED(2); | |
| 218 }else{ | |
| 219 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2); | |
| 220 add = GET_MED(2) - 1; | |
| 221 INC_MED(0); | |
| 222 INC_MED(1); | |
| 223 INC_MED(2); | |
| 224 } | |
| 225 ret = base + get_tail(gb, add); | |
| 226 sign = get_bits1(gb); | |
| 227 return sign ? ~ret : ret; | |
| 228 } | |
| 229 | |
| 230 static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst) | |
| 231 { | |
| 232 int i, j, count = 0; | |
| 233 int last, t; | |
|
5482
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
234 int A, B, L, L2, R, R2, bit; |
| 3764 | 235 int pos = 0; |
| 236 uint32_t crc = 0xFFFFFFFF; | |
| 237 | |
| 238 s->one = s->zero = s->zeroes = 0; | |
| 239 do{ | |
| 240 L = wv_get_value(s, gb, s->median, &last); | |
| 241 if(last) break; | |
| 242 R = wv_get_value(s, gb, s->median + 3, &last); | |
| 243 if(last) break; | |
| 244 for(i = 0; i < s->terms; i++){ | |
| 245 t = s->decorr[i].value; | |
| 246 j = 0; | |
| 247 if(t > 0){ | |
| 248 if(t > 8){ | |
| 249 if(t & 1){ | |
| 250 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]; | |
| 251 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]; | |
| 252 }else{ | |
| 253 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1; | |
| 254 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1; | |
| 255 } | |
| 256 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0]; | |
| 257 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0]; | |
| 258 j = 0; | |
| 259 }else{ | |
| 260 A = s->decorr[i].samplesA[pos]; | |
| 261 B = s->decorr[i].samplesB[pos]; | |
| 262 j = (pos + t) & 7; | |
| 263 } | |
| 264 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10); | |
| 265 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10); | |
| 266 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta; | |
| 267 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta; | |
| 268 s->decorr[i].samplesA[j] = L = L2; | |
| 269 s->decorr[i].samplesB[j] = R = R2; | |
| 270 }else if(t == -1){ | |
| 271 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10); | |
| 272 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L); | |
| 273 L = L2; | |
| 274 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10); | |
| 275 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R); | |
| 276 R = R2; | |
| 277 s->decorr[i].samplesA[0] = R; | |
| 278 }else{ | |
| 279 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10); | |
| 280 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R); | |
| 281 R = R2; | |
| 282 | |
| 283 if(t == -3){ | |
| 284 R2 = s->decorr[i].samplesA[0]; | |
| 285 s->decorr[i].samplesA[0] = R; | |
| 286 } | |
| 287 | |
| 288 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10); | |
| 289 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L); | |
| 290 L = L2; | |
| 291 s->decorr[i].samplesB[0] = L; | |
| 292 } | |
| 293 } | |
| 294 pos = (pos + 1) & 7; | |
| 295 if(s->joint) | |
| 296 L += (R -= (L >> 1)); | |
| 297 crc = (crc * 3 + L) * 3 + R; | |
|
5482
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
298 bit = (L & s->and) | s->or; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
299 *dst++ = ((L + bit) << s->shift) - bit; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
300 bit = (R & s->and) | s->or; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
301 *dst++ = ((R + bit) << s->shift) - bit; |
| 3764 | 302 count++; |
| 303 }while(!last && count < s->samples); | |
| 304 | |
| 305 if(crc != s->CRC){ | |
| 306 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); | |
| 307 return -1; | |
| 308 } | |
| 309 return count * 2; | |
| 310 } | |
| 311 | |
| 312 static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst) | |
| 313 { | |
| 314 int i, j, count = 0; | |
| 315 int last, t; | |
|
5482
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
316 int A, S, T, bit; |
| 3764 | 317 int pos = 0; |
| 318 uint32_t crc = 0xFFFFFFFF; | |
| 319 | |
| 320 s->one = s->zero = s->zeroes = 0; | |
| 321 do{ | |
| 322 T = wv_get_value(s, gb, s->median, &last); | |
| 323 S = 0; | |
| 324 if(last) break; | |
| 325 for(i = 0; i < s->terms; i++){ | |
| 326 t = s->decorr[i].value; | |
| 327 if(t > 8){ | |
| 328 if(t & 1) | |
| 329 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]; | |
| 330 else | |
| 331 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1; | |
| 332 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0]; | |
| 333 j = 0; | |
| 334 }else{ | |
| 335 A = s->decorr[i].samplesA[pos]; | |
| 336 j = (pos + t) & 7; | |
| 337 } | |
| 338 S = T + ((s->decorr[i].weightA * A + 512) >> 10); | |
| 339 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta; | |
| 340 s->decorr[i].samplesA[j] = T = S; | |
| 341 } | |
| 342 pos = (pos + 1) & 7; | |
| 343 crc = crc * 3 + S; | |
|
5482
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
344 bit = (S & s->and) | s->or; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
345 *dst++ = ((S + bit) << s->shift) - bit; |
| 3764 | 346 count++; |
| 347 }while(!last && count < s->samples); | |
| 348 | |
| 349 if(crc != s->CRC){ | |
| 350 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); | |
| 351 return -1; | |
| 352 } | |
| 353 return count; | |
| 354 } | |
| 355 | |
| 356 static int wavpack_decode_init(AVCodecContext *avctx) | |
| 357 { | |
| 358 WavpackContext *s = avctx->priv_data; | |
| 359 | |
| 360 s->avctx = avctx; | |
| 361 s->stereo = (avctx->channels == 2); | |
| 362 | |
| 363 return 0; | |
| 364 } | |
| 365 | |
| 366 static int wavpack_decode_close(AVCodecContext *avctx) | |
| 367 { | |
| 368 // WavpackContext *s = avctx->priv_data; | |
| 369 | |
| 370 return 0; | |
| 371 } | |
| 372 | |
| 373 static int wavpack_decode_frame(AVCodecContext *avctx, | |
| 374 void *data, int *data_size, | |
| 375 uint8_t *buf, int buf_size) | |
| 376 { | |
| 377 WavpackContext *s = avctx->priv_data; | |
| 378 int16_t *samples = data; | |
| 379 int samplecount; | |
| 380 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0; | |
| 381 uint8_t* buf_end = buf + buf_size; | |
| 382 int i, j, id, size, ssize, weights, t; | |
| 383 | |
| 4690 | 384 if (buf_size == 0){ |
| 385 *data_size = 0; | |
| 386 return 0; | |
| 387 } | |
| 3764 | 388 |
| 389 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr)); | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
390 memset(s->median, 0, sizeof(s->median)); |
|
5482
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
391 s->and = s->or = s->shift = 0; |
| 3764 | 392 |
| 4364 | 393 s->samples = AV_RL32(buf); buf += 4; |
| 4690 | 394 if(!s->samples){ |
| 395 *data_size = 0; | |
| 396 return buf_size; | |
| 397 } | |
| 4017 | 398 /* should not happen but who knows */ |
| 4690 | 399 if(s->samples * 2 * avctx->channels > *data_size){ |
| 4017 | 400 av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n"); |
| 401 return -1; | |
| 402 } | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
403 s->stereo_in = (AV_RL32(buf) & WV_FALSE_STEREO) ? 0 : s->stereo; |
| 5538 | 404 s->joint = AV_RL32(buf) & WV_JOINT_STEREO; buf += 4; |
| 4364 | 405 s->CRC = AV_RL32(buf); buf += 4; |
| 3764 | 406 // parse metadata blocks |
| 407 while(buf < buf_end){ | |
| 408 id = *buf++; | |
| 409 size = *buf++; | |
| 410 if(id & WP_IDF_LONG) { | |
| 411 size |= (*buf++) << 8; | |
| 412 size |= (*buf++) << 16; | |
| 413 } | |
| 414 size <<= 1; // size is specified in words | |
| 415 ssize = size; | |
| 416 if(id & WP_IDF_ODD) size--; | |
| 417 if(size < 0){ | |
| 418 av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size); | |
| 419 break; | |
| 420 } | |
| 421 if(buf + ssize > buf_end){ | |
| 422 av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size); | |
| 423 break; | |
| 424 } | |
| 425 if(id & WP_IDF_IGNORE){ | |
| 426 buf += ssize; | |
| 427 continue; | |
| 428 } | |
| 429 switch(id & WP_IDF_MASK){ | |
| 430 case WP_ID_DECTERMS: | |
| 431 s->terms = size; | |
| 432 if(s->terms > MAX_TERMS){ | |
| 433 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n"); | |
| 434 buf += ssize; | |
| 435 continue; | |
| 436 } | |
| 437 for(i = 0; i < s->terms; i++) { | |
| 438 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5; | |
| 439 s->decorr[s->terms - i - 1].delta = *buf >> 5; | |
| 440 buf++; | |
| 441 } | |
| 442 got_terms = 1; | |
| 443 break; | |
| 444 case WP_ID_DECWEIGHTS: | |
| 445 if(!got_terms){ | |
| 446 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n"); | |
| 447 continue; | |
| 448 } | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
449 weights = size >> s->stereo_in; |
| 3764 | 450 if(weights > MAX_TERMS || weights > s->terms){ |
| 451 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n"); | |
| 452 buf += ssize; | |
| 453 continue; | |
| 454 } | |
| 455 for(i = 0; i < weights; i++) { | |
| 456 t = (int8_t)(*buf++); | |
| 457 s->decorr[s->terms - i - 1].weightA = t << 3; | |
| 458 if(s->decorr[s->terms - i - 1].weightA > 0) | |
| 459 s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7; | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
460 if(s->stereo_in){ |
| 3764 | 461 t = (int8_t)(*buf++); |
| 462 s->decorr[s->terms - i - 1].weightB = t << 3; | |
| 463 if(s->decorr[s->terms - i - 1].weightB > 0) | |
| 464 s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7; | |
| 465 } | |
| 466 } | |
| 467 got_weights = 1; | |
| 468 break; | |
| 469 case WP_ID_DECSAMPLES: | |
| 470 if(!got_terms){ | |
| 471 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n"); | |
| 472 continue; | |
| 473 } | |
| 474 t = 0; | |
| 475 for(i = s->terms - 1; (i >= 0) && (t < size); i--) { | |
| 476 if(s->decorr[i].value > 8){ | |
| 4364 | 477 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2; |
| 478 s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2; | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
479 if(s->stereo_in){ |
| 4364 | 480 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2; |
| 481 s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2; | |
| 3764 | 482 t += 4; |
| 483 } | |
| 484 t += 4; | |
| 485 }else if(s->decorr[i].value < 0){ | |
| 4364 | 486 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2; |
| 487 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2; | |
| 3764 | 488 t += 4; |
| 489 }else{ | |
| 490 for(j = 0; j < s->decorr[i].value; j++){ | |
| 4364 | 491 s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2; |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
492 if(s->stereo_in){ |
| 4364 | 493 s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2; |
| 3764 | 494 } |
| 495 } | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
496 t += s->decorr[i].value * 2 * (s->stereo_in + 1); |
| 3764 | 497 } |
| 498 } | |
| 499 got_samples = 1; | |
| 500 break; | |
| 501 case WP_ID_ENTROPY: | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
502 if(size != 6 * (s->stereo_in + 1)){ |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
503 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size); |
| 3764 | 504 buf += ssize; |
| 505 continue; | |
| 506 } | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
507 for(i = 0; i < 3 * (s->stereo_in + 1); i++){ |
| 4364 | 508 s->median[i] = wp_exp2(AV_RL16(buf)); |
| 3764 | 509 buf += 2; |
| 510 } | |
| 511 got_entropy = 1; | |
| 512 break; | |
|
5482
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
513 case WP_ID_INT32INFO: |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
514 if(size != 4 || *buf){ |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
515 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf); |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
516 buf += ssize; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
517 continue; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
518 } |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
519 if(buf[1]) |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
520 s->shift = buf[1]; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
521 else if(buf[2]){ |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
522 s->and = s->or = 1; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
523 s->shift = buf[2]; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
524 }else if(buf[3]){ |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
525 s->and = 1; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
526 s->shift = buf[3]; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
527 } |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
528 buf += 4; |
|
bba203d5c5e7
Add the handling of the INT32INFO block to the WavPack decoder.
kostya
parents:
4690
diff
changeset
|
529 break; |
| 3764 | 530 case WP_ID_DATA: |
| 531 init_get_bits(&s->gb, buf, size * 8); | |
| 532 s->data_size = size * 8; | |
| 533 buf += size; | |
| 534 got_bs = 1; | |
| 535 break; | |
| 536 default: | |
| 537 buf += size; | |
| 538 } | |
| 539 if(id & WP_IDF_ODD) buf++; | |
| 540 } | |
| 541 if(!got_terms){ | |
| 542 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n"); | |
| 543 return -1; | |
| 544 } | |
| 545 if(!got_weights){ | |
| 546 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n"); | |
| 547 return -1; | |
| 548 } | |
| 549 if(!got_samples){ | |
| 550 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n"); | |
| 551 return -1; | |
| 552 } | |
| 553 if(!got_entropy){ | |
| 554 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n"); | |
| 555 return -1; | |
| 556 } | |
| 557 if(!got_bs){ | |
| 558 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n"); | |
| 559 return -1; | |
| 560 } | |
| 561 | |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
562 if(s->stereo_in) |
| 3764 | 563 samplecount = wv_unpack_stereo(s, &s->gb, samples); |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
564 else{ |
| 3764 | 565 samplecount = wv_unpack_mono(s, &s->gb, samples); |
|
5539
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
566 if(s->stereo){ |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
567 int16_t *dst = samples + samplecount * 2; |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
568 int16_t *src = samples + samplecount; |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
569 int cnt = samplecount; |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
570 while(cnt--){ |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
571 *--dst = *--src; |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
572 *--dst = *src; |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
573 } |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
574 samplecount *= 2; |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
575 } |
|
1c67999f81c8
Support for WavPack version 0x410 (false stereo chunks)
kostya
parents:
5538
diff
changeset
|
576 } |
| 3764 | 577 *data_size = samplecount * 2; |
| 578 | |
| 579 return buf_size; | |
| 580 } | |
| 581 | |
| 582 AVCodec wavpack_decoder = { | |
| 583 "wavpack", | |
| 584 CODEC_TYPE_AUDIO, | |
| 585 CODEC_ID_WAVPACK, | |
| 586 sizeof(WavpackContext), | |
| 587 wavpack_decode_init, | |
| 588 NULL, | |
| 589 wavpack_decode_close, | |
| 590 wavpack_decode_frame, | |
| 591 }; |
