wavpack.c 24 KB

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