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.

1246 lines
41KB

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