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.

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