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.

969 lines
32KB

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