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.

1061 lines
35KB

  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){
  250. if(get_bits_left(gb) < t-1)
  251. goto error;
  252. t = get_bits(gb, t - 1) | (1 << (t-1));
  253. }else{
  254. if(get_bits_left(gb) < 0)
  255. goto error;
  256. }
  257. ctx->zeroes = t;
  258. if(ctx->zeroes){
  259. memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
  260. memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
  261. c->slow_level -= LEVEL_DECAY(c->slow_level);
  262. return 0;
  263. }
  264. }
  265. }
  266. if(ctx->zero){
  267. t = 0;
  268. ctx->zero = 0;
  269. }else{
  270. t = get_unary_0_33(gb);
  271. if(get_bits_left(gb) < 0)
  272. goto error;
  273. if(t == 16) {
  274. t2 = get_unary_0_33(gb);
  275. if(t2 < 2){
  276. if(get_bits_left(gb) < 0)
  277. goto error;
  278. t += t2;
  279. }else{
  280. if(get_bits_left(gb) < t2 - 1)
  281. goto error;
  282. t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
  283. }
  284. }
  285. if(ctx->one){
  286. ctx->one = t&1;
  287. t = (t>>1) + 1;
  288. }else{
  289. ctx->one = t&1;
  290. t >>= 1;
  291. }
  292. ctx->zero = !ctx->one;
  293. }
  294. if(ctx->hybrid && !channel)
  295. update_error_limit(ctx);
  296. if(!t){
  297. base = 0;
  298. add = GET_MED(0) - 1;
  299. DEC_MED(0);
  300. }else if(t == 1){
  301. base = GET_MED(0);
  302. add = GET_MED(1) - 1;
  303. INC_MED(0);
  304. DEC_MED(1);
  305. }else if(t == 2){
  306. base = GET_MED(0) + GET_MED(1);
  307. add = GET_MED(2) - 1;
  308. INC_MED(0);
  309. INC_MED(1);
  310. DEC_MED(2);
  311. }else{
  312. base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
  313. add = GET_MED(2) - 1;
  314. INC_MED(0);
  315. INC_MED(1);
  316. INC_MED(2);
  317. }
  318. if(!c->error_limit){
  319. ret = base + get_tail(gb, add);
  320. if (get_bits_left(gb) <= 0)
  321. goto error;
  322. }else{
  323. int mid = (base*2 + add + 1) >> 1;
  324. while(add > c->error_limit){
  325. if(get_bits_left(gb) <= 0)
  326. goto error;
  327. if(get_bits1(gb)){
  328. add -= (mid - base);
  329. base = mid;
  330. }else
  331. add = mid - base - 1;
  332. mid = (base*2 + add + 1) >> 1;
  333. }
  334. ret = mid;
  335. }
  336. sign = get_bits1(gb);
  337. if(ctx->hybrid_bitrate)
  338. c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
  339. return sign ? ~ret : ret;
  340. error:
  341. *last = 1;
  342. return 0;
  343. }
  344. static inline int wv_get_value_integer(WavpackContext *s, uint32_t *crc, int S)
  345. {
  346. int bit;
  347. if(s->extra_bits){
  348. S <<= s->extra_bits;
  349. if(s->got_extra_bits && get_bits_left(&s->gb_extra_bits) >= s->extra_bits){
  350. S |= get_bits(&s->gb_extra_bits, s->extra_bits);
  351. *crc = *crc * 9 + (S&0xffff) * 3 + ((unsigned)S>>16);
  352. }
  353. }
  354. bit = (S & s->and) | s->or;
  355. return (((S + bit) << s->shift) - bit) << s->post_shift;
  356. }
  357. static float wv_get_value_float(WavpackContext *s, uint32_t *crc, int S)
  358. {
  359. union {
  360. float f;
  361. uint32_t u;
  362. } value;
  363. int sign;
  364. int exp = s->float_max_exp;
  365. if(s->got_extra_bits){
  366. const int max_bits = 1 + 23 + 8 + 1;
  367. const int left_bits = get_bits_left(&s->gb_extra_bits);
  368. if(left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
  369. return 0.0;
  370. }
  371. if(S){
  372. S <<= s->float_shift;
  373. sign = S < 0;
  374. if(sign)
  375. S = -S;
  376. if(S >= 0x1000000){
  377. if(s->got_extra_bits && get_bits1(&s->gb_extra_bits)){
  378. S = get_bits(&s->gb_extra_bits, 23);
  379. }else{
  380. S = 0;
  381. }
  382. exp = 255;
  383. }else if(exp){
  384. int shift = 23 - av_log2(S);
  385. exp = s->float_max_exp;
  386. if(exp <= shift){
  387. shift = --exp;
  388. }
  389. exp -= shift;
  390. if(shift){
  391. S <<= shift;
  392. if((s->float_flag & WV_FLT_SHIFT_ONES) ||
  393. (s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SAME) && get_bits1(&s->gb_extra_bits)) ){
  394. S |= (1 << shift) - 1;
  395. } else if(s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SENT)){
  396. S |= get_bits(&s->gb_extra_bits, shift);
  397. }
  398. }
  399. }else{
  400. exp = s->float_max_exp;
  401. }
  402. S &= 0x7fffff;
  403. }else{
  404. sign = 0;
  405. exp = 0;
  406. if(s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)){
  407. if(get_bits1(&s->gb_extra_bits)){
  408. S = get_bits(&s->gb_extra_bits, 23);
  409. if(s->float_max_exp >= 25)
  410. exp = get_bits(&s->gb_extra_bits, 8);
  411. sign = get_bits1(&s->gb_extra_bits);
  412. }else{
  413. if(s->float_flag & WV_FLT_ZERO_SIGN)
  414. sign = get_bits1(&s->gb_extra_bits);
  415. }
  416. }
  417. }
  418. *crc = *crc * 27 + S * 9 + exp * 3 + sign;
  419. value.u = (sign << 31) | (exp << 23) | S;
  420. return value.f;
  421. }
  422. static void wv_reset_saved_context(WavpackContext *s)
  423. {
  424. s->pos = 0;
  425. s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
  426. }
  427. static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
  428. {
  429. int i, j, count = 0;
  430. int last, t;
  431. int A, B, L, L2, R, R2;
  432. int pos = s->pos;
  433. uint32_t crc = s->sc.crc;
  434. uint32_t crc_extra_bits = s->extra_sc.crc;
  435. int16_t *dst16 = dst;
  436. int32_t *dst32 = dst;
  437. float *dstfl = dst;
  438. if(s->samples_left == s->samples)
  439. s->one = s->zero = s->zeroes = 0;
  440. do{
  441. L = wv_get_value(s, gb, 0, &last);
  442. if(last) break;
  443. R = wv_get_value(s, gb, 1, &last);
  444. if(last) break;
  445. for(i = 0; i < s->terms; i++){
  446. t = s->decorr[i].value;
  447. if(t > 0){
  448. if(t > 8){
  449. if(t & 1){
  450. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  451. B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
  452. }else{
  453. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  454. B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
  455. }
  456. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  457. s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
  458. j = 0;
  459. }else{
  460. A = s->decorr[i].samplesA[pos];
  461. B = s->decorr[i].samplesB[pos];
  462. j = (pos + t) & 7;
  463. }
  464. if(type != SAMPLE_FMT_S16){
  465. L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  466. R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
  467. }else{
  468. L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
  469. R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
  470. }
  471. if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  472. if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
  473. s->decorr[i].samplesA[j] = L = L2;
  474. s->decorr[i].samplesB[j] = R = R2;
  475. }else if(t == -1){
  476. if(type != SAMPLE_FMT_S16)
  477. L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
  478. else
  479. L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
  480. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
  481. L = L2;
  482. if(type != SAMPLE_FMT_S16)
  483. R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
  484. else
  485. R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
  486. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
  487. R = R2;
  488. s->decorr[i].samplesA[0] = R;
  489. }else{
  490. if(type != SAMPLE_FMT_S16)
  491. R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
  492. else
  493. R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
  494. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
  495. R = R2;
  496. if(t == -3){
  497. R2 = s->decorr[i].samplesA[0];
  498. s->decorr[i].samplesA[0] = R;
  499. }
  500. if(type != SAMPLE_FMT_S16)
  501. L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
  502. else
  503. L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
  504. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
  505. L = L2;
  506. s->decorr[i].samplesB[0] = L;
  507. }
  508. }
  509. pos = (pos + 1) & 7;
  510. if(s->joint)
  511. L += (R -= (L >> 1));
  512. crc = (crc * 3 + L) * 3 + R;
  513. if(type == SAMPLE_FMT_FLT){
  514. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
  515. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
  516. } else if(type == SAMPLE_FMT_S32){
  517. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
  518. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
  519. } else {
  520. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
  521. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
  522. }
  523. count++;
  524. }while(!last && count < s->max_samples);
  525. if (last)
  526. s->samples_left = 0;
  527. else
  528. s->samples_left -= count;
  529. if(!s->samples_left){
  530. if(crc != s->CRC){
  531. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  532. return -1;
  533. }
  534. if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
  535. av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
  536. return -1;
  537. }
  538. wv_reset_saved_context(s);
  539. }else{
  540. s->pos = pos;
  541. s->sc.crc = crc;
  542. s->sc.bits_used = get_bits_count(&s->gb);
  543. if(s->got_extra_bits){
  544. s->extra_sc.crc = crc_extra_bits;
  545. s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
  546. }
  547. }
  548. return count * 2;
  549. }
  550. static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
  551. {
  552. int i, j, count = 0;
  553. int last, t;
  554. int A, S, T;
  555. int pos = s->pos;
  556. uint32_t crc = s->sc.crc;
  557. uint32_t crc_extra_bits = s->extra_sc.crc;
  558. int16_t *dst16 = dst;
  559. int32_t *dst32 = dst;
  560. float *dstfl = dst;
  561. if(s->samples_left == s->samples)
  562. s->one = s->zero = s->zeroes = 0;
  563. do{
  564. T = wv_get_value(s, gb, 0, &last);
  565. S = 0;
  566. if(last) break;
  567. for(i = 0; i < s->terms; i++){
  568. t = s->decorr[i].value;
  569. if(t > 8){
  570. if(t & 1)
  571. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  572. else
  573. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  574. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  575. j = 0;
  576. }else{
  577. A = s->decorr[i].samplesA[pos];
  578. j = (pos + t) & 7;
  579. }
  580. if(type != SAMPLE_FMT_S16)
  581. S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  582. else
  583. S = T + ((s->decorr[i].weightA * A + 512) >> 10);
  584. if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  585. s->decorr[i].samplesA[j] = T = S;
  586. }
  587. pos = (pos + 1) & 7;
  588. crc = crc * 3 + S;
  589. if(type == SAMPLE_FMT_FLT)
  590. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
  591. else if(type == SAMPLE_FMT_S32)
  592. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
  593. else
  594. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
  595. count++;
  596. }while(!last && count < s->samples);
  597. if (last)
  598. s->samples_left = 0;
  599. else
  600. s->samples_left -= count;
  601. if(!s->samples_left){
  602. if(crc != s->CRC){
  603. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  604. return -1;
  605. }
  606. if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
  607. av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
  608. return -1;
  609. }
  610. wv_reset_saved_context(s);
  611. }else{
  612. s->pos = pos;
  613. s->sc.crc = crc;
  614. s->sc.bits_used = get_bits_count(&s->gb);
  615. if(s->got_extra_bits){
  616. s->extra_sc.crc = crc_extra_bits;
  617. s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
  618. }
  619. }
  620. return count;
  621. }
  622. static av_cold int wavpack_decode_init(AVCodecContext *avctx)
  623. {
  624. WavpackContext *s = avctx->priv_data;
  625. s->avctx = avctx;
  626. s->stereo = (avctx->channels == 2);
  627. if(avctx->bits_per_coded_sample <= 16)
  628. avctx->sample_fmt = SAMPLE_FMT_S16;
  629. else
  630. avctx->sample_fmt = SAMPLE_FMT_S32;
  631. avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
  632. wv_reset_saved_context(s);
  633. return 0;
  634. }
  635. static int wavpack_decode_frame(AVCodecContext *avctx,
  636. void *data, int *data_size,
  637. AVPacket *avpkt)
  638. {
  639. const uint8_t *buf = avpkt->data;
  640. int buf_size = avpkt->size;
  641. WavpackContext *s = avctx->priv_data;
  642. void *samples = data;
  643. int samplecount;
  644. int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;
  645. int got_hybrid = 0;
  646. const uint8_t* buf_end = buf + buf_size;
  647. int i, j, id, size, ssize, weights, t;
  648. int bpp;
  649. if (buf_size == 0){
  650. *data_size = 0;
  651. return 0;
  652. }
  653. if(!s->samples_left){
  654. memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
  655. memset(s->ch, 0, sizeof(s->ch));
  656. s->extra_bits = 0;
  657. s->and = s->or = s->shift = 0;
  658. s->got_extra_bits = 0;
  659. }
  660. s->samples = AV_RL32(buf); buf += 4;
  661. if(!s->samples){
  662. *data_size = 0;
  663. return buf_size;
  664. }
  665. s->frame_flags = AV_RL32(buf); buf += 4;
  666. if(s->frame_flags&0x80){
  667. bpp = sizeof(float);
  668. avctx->sample_fmt = SAMPLE_FMT_FLT;
  669. } else if((s->frame_flags&0x03) <= 1){
  670. bpp = 2;
  671. avctx->sample_fmt = SAMPLE_FMT_S16;
  672. } else {
  673. bpp = 4;
  674. avctx->sample_fmt = SAMPLE_FMT_S32;
  675. }
  676. s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
  677. s->joint = s->frame_flags & WV_JOINT_STEREO;
  678. s->hybrid = s->frame_flags & WV_HYBRID_MODE;
  679. s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
  680. s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
  681. s->CRC = AV_RL32(buf); buf += 4;
  682. s->max_samples = *data_size / (bpp * avctx->channels);
  683. s->max_samples = FFMIN(s->max_samples, s->samples);
  684. if(s->samples_left > 0){
  685. s->max_samples = FFMIN(s->max_samples, s->samples_left);
  686. buf = buf_end;
  687. }
  688. // parse metadata blocks
  689. while(buf < buf_end){
  690. id = *buf++;
  691. size = *buf++;
  692. if(id & WP_IDF_LONG) {
  693. size |= (*buf++) << 8;
  694. size |= (*buf++) << 16;
  695. }
  696. size <<= 1; // size is specified in words
  697. ssize = size;
  698. if(id & WP_IDF_ODD) size--;
  699. if(size < 0){
  700. av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
  701. break;
  702. }
  703. if(buf + ssize > buf_end){
  704. av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
  705. break;
  706. }
  707. if(id & WP_IDF_IGNORE){
  708. buf += ssize;
  709. continue;
  710. }
  711. switch(id & WP_IDF_MASK){
  712. case WP_ID_DECTERMS:
  713. if(size > MAX_TERMS){
  714. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
  715. s->terms = 0;
  716. buf += ssize;
  717. continue;
  718. }
  719. s->terms = size;
  720. for(i = 0; i < s->terms; i++) {
  721. s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
  722. s->decorr[s->terms - i - 1].delta = *buf >> 5;
  723. buf++;
  724. }
  725. got_terms = 1;
  726. break;
  727. case WP_ID_DECWEIGHTS:
  728. if(!got_terms){
  729. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  730. continue;
  731. }
  732. weights = size >> s->stereo_in;
  733. if(weights > MAX_TERMS || weights > s->terms){
  734. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
  735. buf += ssize;
  736. continue;
  737. }
  738. for(i = 0; i < weights; i++) {
  739. t = (int8_t)(*buf++);
  740. s->decorr[s->terms - i - 1].weightA = t << 3;
  741. if(s->decorr[s->terms - i - 1].weightA > 0)
  742. s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
  743. if(s->stereo_in){
  744. t = (int8_t)(*buf++);
  745. s->decorr[s->terms - i - 1].weightB = t << 3;
  746. if(s->decorr[s->terms - i - 1].weightB > 0)
  747. s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
  748. }
  749. }
  750. got_weights = 1;
  751. break;
  752. case WP_ID_DECSAMPLES:
  753. if(!got_terms){
  754. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  755. continue;
  756. }
  757. t = 0;
  758. for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
  759. if(s->decorr[i].value > 8){
  760. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  761. s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
  762. if(s->stereo_in){
  763. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  764. s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
  765. t += 4;
  766. }
  767. t += 4;
  768. }else if(s->decorr[i].value < 0){
  769. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  770. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  771. t += 4;
  772. }else{
  773. for(j = 0; j < s->decorr[i].value; j++){
  774. s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
  775. if(s->stereo_in){
  776. s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
  777. }
  778. }
  779. t += s->decorr[i].value * 2 * (s->stereo_in + 1);
  780. }
  781. }
  782. got_samples = 1;
  783. break;
  784. case WP_ID_ENTROPY:
  785. if(size != 6 * (s->stereo_in + 1)){
  786. av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);
  787. buf += ssize;
  788. continue;
  789. }
  790. for(j = 0; j <= s->stereo_in; j++){
  791. for(i = 0; i < 3; i++){
  792. s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
  793. buf += 2;
  794. }
  795. }
  796. got_entropy = 1;
  797. break;
  798. case WP_ID_HYBRID:
  799. if(s->hybrid_bitrate){
  800. for(i = 0; i <= s->stereo_in; i++){
  801. s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
  802. buf += 2;
  803. size -= 2;
  804. }
  805. }
  806. for(i = 0; i < (s->stereo_in + 1); i++){
  807. s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
  808. buf += 2;
  809. size -= 2;
  810. }
  811. if(size > 0){
  812. for(i = 0; i < (s->stereo_in + 1); i++){
  813. s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
  814. buf += 2;
  815. }
  816. }else{
  817. for(i = 0; i < (s->stereo_in + 1); i++)
  818. s->ch[i].bitrate_delta = 0;
  819. }
  820. got_hybrid = 1;
  821. break;
  822. case WP_ID_INT32INFO:
  823. if(size != 4){
  824. av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
  825. buf += ssize;
  826. continue;
  827. }
  828. if(buf[0])
  829. s->extra_bits = buf[0];
  830. else if(buf[1])
  831. s->shift = buf[1];
  832. else if(buf[2]){
  833. s->and = s->or = 1;
  834. s->shift = buf[2];
  835. }else if(buf[3]){
  836. s->and = 1;
  837. s->shift = buf[3];
  838. }
  839. buf += 4;
  840. break;
  841. case WP_ID_FLOATINFO:
  842. if(size != 4){
  843. av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
  844. buf += ssize;
  845. continue;
  846. }
  847. s->float_flag = buf[0];
  848. s->float_shift = buf[1];
  849. s->float_max_exp = buf[2];
  850. buf += 4;
  851. got_float = 1;
  852. break;
  853. case WP_ID_DATA:
  854. s->sc.offset = buf - avpkt->data;
  855. s->sc.size = size * 8;
  856. init_get_bits(&s->gb, buf, size * 8);
  857. s->data_size = size * 8;
  858. buf += size;
  859. got_bs = 1;
  860. break;
  861. case WP_ID_EXTRABITS:
  862. if(size <= 4){
  863. av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);
  864. buf += size;
  865. continue;
  866. }
  867. s->extra_sc.offset = buf - avpkt->data;
  868. s->extra_sc.size = size * 8;
  869. init_get_bits(&s->gb_extra_bits, buf, size * 8);
  870. s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
  871. buf += size;
  872. s->got_extra_bits = 1;
  873. break;
  874. default:
  875. buf += size;
  876. }
  877. if(id & WP_IDF_ODD) buf++;
  878. }
  879. if(!s->samples_left){
  880. if(!got_terms){
  881. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
  882. return -1;
  883. }
  884. if(!got_weights){
  885. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
  886. return -1;
  887. }
  888. if(!got_samples){
  889. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
  890. return -1;
  891. }
  892. if(!got_entropy){
  893. av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
  894. return -1;
  895. }
  896. if(s->hybrid && !got_hybrid){
  897. av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
  898. return -1;
  899. }
  900. if(!got_bs){
  901. av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
  902. return -1;
  903. }
  904. if(!got_float && avctx->sample_fmt == SAMPLE_FMT_FLT){
  905. av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
  906. return -1;
  907. }
  908. if(s->got_extra_bits && avctx->sample_fmt != SAMPLE_FMT_FLT){
  909. const int size = get_bits_left(&s->gb_extra_bits);
  910. const int wanted = s->samples * s->extra_bits << s->stereo_in;
  911. if(size < wanted){
  912. av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
  913. s->got_extra_bits = 0;
  914. }
  915. }
  916. s->samples_left = s->samples;
  917. }else{
  918. init_get_bits(&s->gb, avpkt->data + s->sc.offset, s->sc.size);
  919. skip_bits_long(&s->gb, s->sc.bits_used);
  920. if(s->got_extra_bits){
  921. init_get_bits(&s->gb_extra_bits, avpkt->data + s->extra_sc.offset,
  922. s->extra_sc.size);
  923. skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used);
  924. }
  925. }
  926. if(s->stereo_in){
  927. if(avctx->sample_fmt == SAMPLE_FMT_S16)
  928. samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S16);
  929. else if(avctx->sample_fmt == SAMPLE_FMT_S32)
  930. samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S32);
  931. else
  932. samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_FLT);
  933. if (samplecount < 0)
  934. return -1;
  935. }else{
  936. if(avctx->sample_fmt == SAMPLE_FMT_S16)
  937. samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S16);
  938. else if(avctx->sample_fmt == SAMPLE_FMT_S32)
  939. samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S32);
  940. else
  941. samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_FLT);
  942. if (samplecount < 0)
  943. return -1;
  944. if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S16){
  945. int16_t *dst = (int16_t*)samples + samplecount * 2;
  946. int16_t *src = (int16_t*)samples + samplecount;
  947. int cnt = samplecount;
  948. while(cnt-- > 0){
  949. *--dst = *--src;
  950. *--dst = *src;
  951. }
  952. samplecount *= 2;
  953. }else if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S32){
  954. int32_t *dst = (int32_t*)samples + samplecount * 2;
  955. int32_t *src = (int32_t*)samples + samplecount;
  956. int cnt = samplecount;
  957. while(cnt-- > 0){
  958. *--dst = *--src;
  959. *--dst = *src;
  960. }
  961. samplecount *= 2;
  962. }else if(s->stereo){
  963. float *dst = (float*)samples + samplecount * 2;
  964. float *src = (float*)samples + samplecount;
  965. int cnt = samplecount;
  966. while(cnt-- > 0){
  967. *--dst = *--src;
  968. *--dst = *src;
  969. }
  970. samplecount *= 2;
  971. }
  972. }
  973. *data_size = samplecount * bpp;
  974. return s->samples_left > 0 ? 0 : buf_size;
  975. }
  976. AVCodec wavpack_decoder = {
  977. "wavpack",
  978. AVMEDIA_TYPE_AUDIO,
  979. CODEC_ID_WAVPACK,
  980. sizeof(WavpackContext),
  981. wavpack_decode_init,
  982. NULL,
  983. NULL,
  984. wavpack_decode_frame,
  985. .capabilities = CODEC_CAP_SUBFRAMES,
  986. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  987. };