You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

798 lines
27KB

  1. /*
  2. * WavPack lossless audio decoder
  3. * Copyright (c) 2006 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. #define ALT_BITSTREAM_READER_LE
  22. #include "avcodec.h"
  23. #include "get_bits.h"
  24. #include "unary.h"
  25. /**
  26. * @file libavcodec/wavpack.c
  27. * WavPack lossless audio decoder
  28. */
  29. #define WV_MONO 0x00000004
  30. #define WV_JOINT_STEREO 0x00000010
  31. #define WV_FALSE_STEREO 0x40000000
  32. #define WV_HYBRID_MODE 0x00000008
  33. #define WV_HYBRID_SHAPE 0x00000008
  34. #define WV_HYBRID_BITRATE 0x00000200
  35. #define WV_HYBRID_BALANCE 0x00000400
  36. enum WP_ID_Flags{
  37. WP_IDF_MASK = 0x1F,
  38. WP_IDF_IGNORE = 0x20,
  39. WP_IDF_ODD = 0x40,
  40. WP_IDF_LONG = 0x80
  41. };
  42. enum WP_ID{
  43. WP_ID_DUMMY = 0,
  44. WP_ID_ENCINFO,
  45. WP_ID_DECTERMS,
  46. WP_ID_DECWEIGHTS,
  47. WP_ID_DECSAMPLES,
  48. WP_ID_ENTROPY,
  49. WP_ID_HYBRID,
  50. WP_ID_SHAPING,
  51. WP_ID_FLOATINFO,
  52. WP_ID_INT32INFO,
  53. WP_ID_DATA,
  54. WP_ID_CORR,
  55. WP_ID_FLT,
  56. WP_ID_CHANINFO
  57. };
  58. #define MAX_TERMS 16
  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. typedef struct WvChannel {
  68. int median[3];
  69. int slow_level, error_limit;
  70. int bitrate_acc, bitrate_delta;
  71. } WvChannel;
  72. typedef struct WavpackContext {
  73. AVCodecContext *avctx;
  74. int frame_flags;
  75. int stereo, stereo_in;
  76. int joint;
  77. uint32_t CRC;
  78. GetBitContext gb;
  79. int data_size; // in bits
  80. int samples;
  81. int terms;
  82. Decorr decorr[MAX_TERMS];
  83. int zero, one, zeroes;
  84. int and, or, shift;
  85. int post_shift;
  86. int hybrid, hybrid_bitrate;
  87. WvChannel ch[2];
  88. } WavpackContext;
  89. // exponent table copied from WavPack source
  90. static const uint8_t wp_exp2_table [256] = {
  91. 0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b,
  92. 0x0b, 0x0c, 0x0d, 0x0e, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x16,
  93. 0x17, 0x18, 0x19, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21, 0x22, 0x23,
  94. 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
  95. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3a, 0x3b, 0x3c, 0x3d,
  96. 0x3e, 0x3f, 0x40, 0x41, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b,
  97. 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
  98. 0x5b, 0x5c, 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  99. 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  100. 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8a,
  101. 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
  102. 0x9c, 0x9d, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad,
  103. 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0,
  104. 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc8, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4,
  105. 0xd6, 0xd7, 0xd8, 0xd9, 0xdb, 0xdc, 0xdd, 0xde, 0xe0, 0xe1, 0xe2, 0xe4, 0xe5, 0xe6, 0xe8, 0xe9,
  106. 0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff
  107. };
  108. static const uint8_t wp_log2_table [] = {
  109. 0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x10, 0x11, 0x12, 0x14, 0x15,
  110. 0x16, 0x18, 0x19, 0x1a, 0x1c, 0x1d, 0x1e, 0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2a,
  111. 0x2c, 0x2d, 0x2e, 0x2f, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3d, 0x3e,
  112. 0x3f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
  113. 0x52, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
  114. 0x64, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x74, 0x75,
  115. 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
  116. 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
  117. 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
  118. 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb2,
  119. 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc0,
  120. 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcb, 0xcc, 0xcd, 0xce,
  121. 0xcf, 0xd0, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd8, 0xd9, 0xda, 0xdb,
  122. 0xdc, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe4, 0xe5, 0xe6, 0xe7, 0xe7,
  123. 0xe8, 0xe9, 0xea, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xee, 0xef, 0xf0, 0xf1, 0xf1, 0xf2, 0xf3, 0xf4,
  124. 0xf4, 0xf5, 0xf6, 0xf7, 0xf7, 0xf8, 0xf9, 0xf9, 0xfa, 0xfb, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff
  125. };
  126. static av_always_inline int wp_exp2(int16_t val)
  127. {
  128. int res, neg = 0;
  129. if(val < 0){
  130. val = -val;
  131. neg = 1;
  132. }
  133. res = wp_exp2_table[val & 0xFF] | 0x100;
  134. val >>= 8;
  135. res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val));
  136. return neg ? -res : res;
  137. }
  138. static av_always_inline int wp_log2(int32_t val)
  139. {
  140. int bits;
  141. if(!val)
  142. return 0;
  143. if(val == 1)
  144. return 256;
  145. val += val >> 9;
  146. bits = av_log2(val) + 1;
  147. if(bits < 9)
  148. return (bits << 8) + wp_log2_table[(val << (9 - bits)) & 0xFF];
  149. else
  150. return (bits << 8) + wp_log2_table[(val >> (bits - 9)) & 0xFF];
  151. }
  152. #define LEVEL_DECAY(a) ((a + 0x80) >> 8)
  153. // macros for manipulating median values
  154. #define GET_MED(n) ((c->median[n] >> 4) + 1)
  155. #define DEC_MED(n) c->median[n] -= ((c->median[n] + (128>>n) - 2) / (128>>n)) * 2
  156. #define INC_MED(n) c->median[n] += ((c->median[n] + (128>>n)) / (128>>n)) * 5
  157. // macros for applying weight
  158. #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
  159. if(samples && in){ \
  160. if((samples ^ in) < 0){ \
  161. weight -= delta; \
  162. if(weight < -1024) weight = -1024; \
  163. }else{ \
  164. weight += delta; \
  165. if(weight > 1024) weight = 1024; \
  166. } \
  167. }
  168. static av_always_inline int get_tail(GetBitContext *gb, int k)
  169. {
  170. int p, e, res;
  171. if(k<1)return 0;
  172. p = av_log2(k);
  173. e = (1 << (p + 1)) - k - 1;
  174. res = p ? get_bits(gb, p) : 0;
  175. if(res >= e){
  176. res = (res<<1) - e + get_bits1(gb);
  177. }
  178. return res;
  179. }
  180. static void update_error_limit(WavpackContext *ctx)
  181. {
  182. int i, br[2], sl[2];
  183. for(i = 0; i <= ctx->stereo_in; i++){
  184. ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
  185. br[i] = ctx->ch[i].bitrate_acc >> 16;
  186. sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
  187. }
  188. if(ctx->stereo_in && ctx->hybrid_bitrate){
  189. int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
  190. if(balance > br[0]){
  191. br[1] = br[0] << 1;
  192. br[0] = 0;
  193. }else if(-balance > br[0]){
  194. br[0] <<= 1;
  195. br[1] = 0;
  196. }else{
  197. br[1] = br[0] + balance;
  198. br[0] = br[0] - balance;
  199. }
  200. }
  201. for(i = 0; i <= ctx->stereo_in; i++){
  202. if(ctx->hybrid_bitrate){
  203. if(sl[i] - br[i] > -0x100)
  204. ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
  205. else
  206. ctx->ch[i].error_limit = 0;
  207. }else{
  208. ctx->ch[i].error_limit = wp_exp2(br[i]);
  209. }
  210. }
  211. }
  212. static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int *last)
  213. {
  214. int t, t2;
  215. int sign, base, add, ret;
  216. WvChannel *c = &ctx->ch[channel];
  217. *last = 0;
  218. if((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) && !ctx->zero && !ctx->one){
  219. if(ctx->zeroes){
  220. ctx->zeroes--;
  221. if(ctx->zeroes){
  222. c->slow_level -= LEVEL_DECAY(c->slow_level);
  223. return 0;
  224. }
  225. }else{
  226. t = get_unary_0_33(gb);
  227. if(t >= 2) t = get_bits(gb, t - 1) | (1 << (t-1));
  228. ctx->zeroes = t;
  229. if(ctx->zeroes){
  230. memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
  231. memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
  232. c->slow_level -= LEVEL_DECAY(c->slow_level);
  233. return 0;
  234. }
  235. }
  236. }
  237. if(get_bits_count(gb) >= ctx->data_size){
  238. *last = 1;
  239. return 0;
  240. }
  241. if(ctx->zero){
  242. t = 0;
  243. ctx->zero = 0;
  244. }else{
  245. t = get_unary_0_33(gb);
  246. if(get_bits_count(gb) >= ctx->data_size){
  247. *last = 1;
  248. return 0;
  249. }
  250. if(t == 16) {
  251. t2 = get_unary_0_33(gb);
  252. if(t2 < 2) t += t2;
  253. else t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
  254. }
  255. if(ctx->one){
  256. ctx->one = t&1;
  257. t = (t>>1) + 1;
  258. }else{
  259. ctx->one = t&1;
  260. t >>= 1;
  261. }
  262. ctx->zero = !ctx->one;
  263. }
  264. if(ctx->hybrid && !channel)
  265. update_error_limit(ctx);
  266. if(!t){
  267. base = 0;
  268. add = GET_MED(0) - 1;
  269. DEC_MED(0);
  270. }else if(t == 1){
  271. base = GET_MED(0);
  272. add = GET_MED(1) - 1;
  273. INC_MED(0);
  274. DEC_MED(1);
  275. }else if(t == 2){
  276. base = GET_MED(0) + GET_MED(1);
  277. add = GET_MED(2) - 1;
  278. INC_MED(0);
  279. INC_MED(1);
  280. DEC_MED(2);
  281. }else{
  282. base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
  283. add = GET_MED(2) - 1;
  284. INC_MED(0);
  285. INC_MED(1);
  286. INC_MED(2);
  287. }
  288. if(!c->error_limit){
  289. ret = base + get_tail(gb, add);
  290. }else{
  291. int mid = (base*2 + add + 1) >> 1;
  292. while(add > c->error_limit){
  293. if(get_bits1(gb)){
  294. add -= (mid - base);
  295. base = mid;
  296. }else
  297. add = mid - base - 1;
  298. mid = (base*2 + add + 1) >> 1;
  299. }
  300. ret = mid;
  301. }
  302. sign = get_bits1(gb);
  303. if(ctx->hybrid_bitrate)
  304. c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
  305. return sign ? ~ret : ret;
  306. }
  307. static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int hires)
  308. {
  309. int i, j, count = 0;
  310. int last, t;
  311. int A, B, L, L2, R, R2, bit;
  312. int pos = 0;
  313. uint32_t crc = 0xFFFFFFFF;
  314. int16_t *dst16 = dst;
  315. int32_t *dst32 = dst;
  316. s->one = s->zero = s->zeroes = 0;
  317. do{
  318. L = wv_get_value(s, gb, 0, &last);
  319. if(last) break;
  320. R = wv_get_value(s, gb, 1, &last);
  321. if(last) break;
  322. for(i = 0; i < s->terms; i++){
  323. t = s->decorr[i].value;
  324. j = 0;
  325. if(t > 0){
  326. if(t > 8){
  327. if(t & 1){
  328. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  329. B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
  330. }else{
  331. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  332. B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
  333. }
  334. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  335. s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
  336. j = 0;
  337. }else{
  338. A = s->decorr[i].samplesA[pos];
  339. B = s->decorr[i].samplesB[pos];
  340. j = (pos + t) & 7;
  341. }
  342. if(hires){
  343. L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  344. R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
  345. }else{
  346. L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
  347. R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
  348. }
  349. if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  350. if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
  351. s->decorr[i].samplesA[j] = L = L2;
  352. s->decorr[i].samplesB[j] = R = R2;
  353. }else if(t == -1){
  354. if(hires)
  355. L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
  356. else
  357. L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
  358. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
  359. L = L2;
  360. if(hires)
  361. R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
  362. else
  363. R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
  364. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
  365. R = R2;
  366. s->decorr[i].samplesA[0] = R;
  367. }else{
  368. if(hires)
  369. R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
  370. else
  371. R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
  372. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
  373. R = R2;
  374. if(t == -3){
  375. R2 = s->decorr[i].samplesA[0];
  376. s->decorr[i].samplesA[0] = R;
  377. }
  378. if(hires)
  379. L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
  380. else
  381. L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
  382. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
  383. L = L2;
  384. s->decorr[i].samplesB[0] = L;
  385. }
  386. }
  387. pos = (pos + 1) & 7;
  388. if(s->joint)
  389. L += (R -= (L >> 1));
  390. crc = (crc * 3 + L) * 3 + R;
  391. bit = (L & s->and) | s->or;
  392. if(hires)
  393. *dst32++ = (((L + bit) << s->shift) - bit) << s->post_shift;
  394. else
  395. *dst16++ = (((L + bit) << s->shift) - bit) << s->post_shift;
  396. bit = (R & s->and) | s->or;
  397. if(hires)
  398. *dst32++ = (((R + bit) << s->shift) - bit) << s->post_shift;
  399. else
  400. *dst16++ = (((R + bit) << s->shift) - bit) << s->post_shift;
  401. count++;
  402. }while(!last && count < s->samples);
  403. if(crc != s->CRC){
  404. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  405. return -1;
  406. }
  407. return count * 2;
  408. }
  409. static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int hires)
  410. {
  411. int i, j, count = 0;
  412. int last, t;
  413. int A, S, T, bit;
  414. int pos = 0;
  415. uint32_t crc = 0xFFFFFFFF;
  416. int16_t *dst16 = dst;
  417. int32_t *dst32 = dst;
  418. s->one = s->zero = s->zeroes = 0;
  419. do{
  420. T = wv_get_value(s, gb, 0, &last);
  421. S = 0;
  422. if(last) break;
  423. for(i = 0; i < s->terms; i++){
  424. t = s->decorr[i].value;
  425. if(t > 8){
  426. if(t & 1)
  427. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  428. else
  429. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  430. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  431. j = 0;
  432. }else{
  433. A = s->decorr[i].samplesA[pos];
  434. j = (pos + t) & 7;
  435. }
  436. if(hires)
  437. S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  438. else
  439. S = T + ((s->decorr[i].weightA * A + 512) >> 10);
  440. if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  441. s->decorr[i].samplesA[j] = T = S;
  442. }
  443. pos = (pos + 1) & 7;
  444. crc = crc * 3 + S;
  445. bit = (S & s->and) | s->or;
  446. if(hires)
  447. *dst32++ = (((S + bit) << s->shift) - bit) << s->post_shift;
  448. else
  449. *dst16++ = (((S + bit) << s->shift) - bit) << s->post_shift;
  450. count++;
  451. }while(!last && count < s->samples);
  452. if(crc != s->CRC){
  453. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  454. return -1;
  455. }
  456. return count;
  457. }
  458. static av_cold int wavpack_decode_init(AVCodecContext *avctx)
  459. {
  460. WavpackContext *s = avctx->priv_data;
  461. s->avctx = avctx;
  462. s->stereo = (avctx->channels == 2);
  463. if(avctx->bits_per_coded_sample <= 16)
  464. avctx->sample_fmt = SAMPLE_FMT_S16;
  465. else
  466. avctx->sample_fmt = SAMPLE_FMT_S32;
  467. avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
  468. return 0;
  469. }
  470. static int wavpack_decode_frame(AVCodecContext *avctx,
  471. void *data, int *data_size,
  472. AVPacket *avpkt)
  473. {
  474. const uint8_t *buf = avpkt->data;
  475. int buf_size = avpkt->size;
  476. WavpackContext *s = avctx->priv_data;
  477. void *samples = data;
  478. int samplecount;
  479. int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0;
  480. int got_hybrid = 0;
  481. const uint8_t* buf_end = buf + buf_size;
  482. int i, j, id, size, ssize, weights, t;
  483. int bpp;
  484. if (buf_size == 0){
  485. *data_size = 0;
  486. return 0;
  487. }
  488. memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
  489. memset(s->ch, 0, sizeof(s->ch));
  490. s->and = s->or = s->shift = 0;
  491. s->samples = AV_RL32(buf); buf += 4;
  492. if(!s->samples){
  493. *data_size = 0;
  494. return buf_size;
  495. }
  496. s->frame_flags = AV_RL32(buf); buf += 4;
  497. if((s->frame_flags&0x03) <= 1){
  498. bpp = 2;
  499. avctx->sample_fmt = SAMPLE_FMT_S16;
  500. } else {
  501. bpp = 4;
  502. avctx->sample_fmt = SAMPLE_FMT_S32;
  503. }
  504. s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
  505. s->joint = s->frame_flags & WV_JOINT_STEREO;
  506. s->hybrid = s->frame_flags & WV_HYBRID_MODE;
  507. s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
  508. s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
  509. s->CRC = AV_RL32(buf); buf += 4;
  510. /* should not happen but who knows */
  511. if(s->samples * bpp * avctx->channels > *data_size){
  512. av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
  513. return -1;
  514. }
  515. // parse metadata blocks
  516. while(buf < buf_end){
  517. id = *buf++;
  518. size = *buf++;
  519. if(id & WP_IDF_LONG) {
  520. size |= (*buf++) << 8;
  521. size |= (*buf++) << 16;
  522. }
  523. size <<= 1; // size is specified in words
  524. ssize = size;
  525. if(id & WP_IDF_ODD) size--;
  526. if(size < 0){
  527. av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
  528. break;
  529. }
  530. if(buf + ssize > buf_end){
  531. av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
  532. break;
  533. }
  534. if(id & WP_IDF_IGNORE){
  535. buf += ssize;
  536. continue;
  537. }
  538. switch(id & WP_IDF_MASK){
  539. case WP_ID_DECTERMS:
  540. s->terms = size;
  541. if(s->terms > MAX_TERMS){
  542. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
  543. buf += ssize;
  544. continue;
  545. }
  546. for(i = 0; i < s->terms; i++) {
  547. s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
  548. s->decorr[s->terms - i - 1].delta = *buf >> 5;
  549. buf++;
  550. }
  551. got_terms = 1;
  552. break;
  553. case WP_ID_DECWEIGHTS:
  554. if(!got_terms){
  555. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  556. continue;
  557. }
  558. weights = size >> s->stereo_in;
  559. if(weights > MAX_TERMS || weights > s->terms){
  560. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
  561. buf += ssize;
  562. continue;
  563. }
  564. for(i = 0; i < weights; i++) {
  565. t = (int8_t)(*buf++);
  566. s->decorr[s->terms - i - 1].weightA = t << 3;
  567. if(s->decorr[s->terms - i - 1].weightA > 0)
  568. s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
  569. if(s->stereo_in){
  570. t = (int8_t)(*buf++);
  571. s->decorr[s->terms - i - 1].weightB = t << 3;
  572. if(s->decorr[s->terms - i - 1].weightB > 0)
  573. s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
  574. }
  575. }
  576. got_weights = 1;
  577. break;
  578. case WP_ID_DECSAMPLES:
  579. if(!got_terms){
  580. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  581. continue;
  582. }
  583. t = 0;
  584. for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
  585. if(s->decorr[i].value > 8){
  586. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  587. s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
  588. if(s->stereo_in){
  589. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  590. s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
  591. t += 4;
  592. }
  593. t += 4;
  594. }else if(s->decorr[i].value < 0){
  595. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  596. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  597. t += 4;
  598. }else{
  599. for(j = 0; j < s->decorr[i].value; j++){
  600. s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
  601. if(s->stereo_in){
  602. s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
  603. }
  604. }
  605. t += s->decorr[i].value * 2 * (s->stereo_in + 1);
  606. }
  607. }
  608. got_samples = 1;
  609. break;
  610. case WP_ID_ENTROPY:
  611. if(size != 6 * (s->stereo_in + 1)){
  612. av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);
  613. buf += ssize;
  614. continue;
  615. }
  616. for(j = 0; j <= s->stereo_in; j++){
  617. for(i = 0; i < 3; i++){
  618. s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
  619. buf += 2;
  620. }
  621. }
  622. got_entropy = 1;
  623. break;
  624. case WP_ID_HYBRID:
  625. if(s->hybrid_bitrate){
  626. for(i = 0; i <= s->stereo_in; i++){
  627. s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
  628. buf += 2;
  629. size -= 2;
  630. }
  631. }
  632. for(i = 0; i < (s->stereo_in + 1); i++){
  633. s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
  634. buf += 2;
  635. size -= 2;
  636. }
  637. if(size > 0){
  638. for(i = 0; i < (s->stereo_in + 1); i++){
  639. s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
  640. buf += 2;
  641. }
  642. }else{
  643. for(i = 0; i < (s->stereo_in + 1); i++)
  644. s->ch[i].bitrate_delta = 0;
  645. }
  646. got_hybrid = 1;
  647. break;
  648. case WP_ID_INT32INFO:
  649. if(size != 4){
  650. av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
  651. buf += ssize;
  652. continue;
  653. }
  654. if(buf[0])
  655. s->post_shift = buf[0];
  656. else if(buf[1])
  657. s->shift = buf[1];
  658. else if(buf[2]){
  659. s->and = s->or = 1;
  660. s->shift = buf[2];
  661. }else if(buf[3]){
  662. s->and = 1;
  663. s->shift = buf[3];
  664. }
  665. buf += 4;
  666. break;
  667. case WP_ID_DATA:
  668. init_get_bits(&s->gb, buf, size * 8);
  669. s->data_size = size * 8;
  670. buf += size;
  671. got_bs = 1;
  672. break;
  673. default:
  674. buf += size;
  675. }
  676. if(id & WP_IDF_ODD) buf++;
  677. }
  678. if(!got_terms){
  679. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
  680. return -1;
  681. }
  682. if(!got_weights){
  683. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
  684. return -1;
  685. }
  686. if(!got_samples){
  687. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
  688. return -1;
  689. }
  690. if(!got_entropy){
  691. av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
  692. return -1;
  693. }
  694. if(s->hybrid && !got_hybrid){
  695. av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
  696. return -1;
  697. }
  698. if(!got_bs){
  699. av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
  700. return -1;
  701. }
  702. if(s->stereo_in){
  703. if(bpp == 2)
  704. samplecount = wv_unpack_stereo(s, &s->gb, samples, 0);
  705. else
  706. samplecount = wv_unpack_stereo(s, &s->gb, samples, 1);
  707. }else{
  708. if(bpp == 2)
  709. samplecount = wv_unpack_mono(s, &s->gb, samples, 0);
  710. else
  711. samplecount = wv_unpack_mono(s, &s->gb, samples, 1);
  712. if(s->stereo && bpp == 2){
  713. int16_t *dst = (int16_t*)samples + samplecount * 2;
  714. int16_t *src = (int16_t*)samples + samplecount;
  715. int cnt = samplecount;
  716. while(cnt--){
  717. *--dst = *--src;
  718. *--dst = *src;
  719. }
  720. samplecount *= 2;
  721. }else if(s->stereo){ //32-bit output
  722. int32_t *dst = (int32_t*)samples + samplecount * 2;
  723. int32_t *src = (int32_t*)samples + samplecount;
  724. int cnt = samplecount;
  725. while(cnt--){
  726. *--dst = *--src;
  727. *--dst = *src;
  728. }
  729. samplecount *= 2;
  730. }
  731. }
  732. *data_size = samplecount * bpp;
  733. return buf_size;
  734. }
  735. AVCodec wavpack_decoder = {
  736. "wavpack",
  737. CODEC_TYPE_AUDIO,
  738. CODEC_ID_WAVPACK,
  739. sizeof(WavpackContext),
  740. wavpack_decode_init,
  741. NULL,
  742. NULL,
  743. wavpack_decode_frame,
  744. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  745. };