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.

1033 lines
34KB

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