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.

1292 lines
43KB

  1. /*
  2. * WavPack lossless audio decoder
  3. * Copyright (c) 2006,2011 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/channel_layout.h"
  23. #include "avcodec.h"
  24. #include "get_bits.h"
  25. #include "internal.h"
  26. #include "unary.h"
  27. /**
  28. * @file
  29. * WavPack lossless audio decoder
  30. */
  31. #define WV_MONO 0x00000004
  32. #define WV_JOINT_STEREO 0x00000010
  33. #define WV_FALSE_STEREO 0x40000000
  34. #define WV_HYBRID_MODE 0x00000008
  35. #define WV_HYBRID_SHAPE 0x00000008
  36. #define WV_HYBRID_BITRATE 0x00000200
  37. #define WV_HYBRID_BALANCE 0x00000400
  38. #define WV_FLT_SHIFT_ONES 0x01
  39. #define WV_FLT_SHIFT_SAME 0x02
  40. #define WV_FLT_SHIFT_SENT 0x04
  41. #define WV_FLT_ZERO_SENT 0x08
  42. #define WV_FLT_ZERO_SIGN 0x10
  43. enum WP_ID_Flags {
  44. WP_IDF_MASK = 0x1F,
  45. WP_IDF_IGNORE = 0x20,
  46. WP_IDF_ODD = 0x40,
  47. WP_IDF_LONG = 0x80
  48. };
  49. enum WP_ID {
  50. WP_ID_DUMMY = 0,
  51. WP_ID_ENCINFO,
  52. WP_ID_DECTERMS,
  53. WP_ID_DECWEIGHTS,
  54. WP_ID_DECSAMPLES,
  55. WP_ID_ENTROPY,
  56. WP_ID_HYBRID,
  57. WP_ID_SHAPING,
  58. WP_ID_FLOATINFO,
  59. WP_ID_INT32INFO,
  60. WP_ID_DATA,
  61. WP_ID_CORR,
  62. WP_ID_EXTRABITS,
  63. WP_ID_CHANINFO
  64. };
  65. typedef struct SavedContext {
  66. int offset;
  67. int size;
  68. int bits_used;
  69. uint32_t crc;
  70. } SavedContext;
  71. #define MAX_TERMS 16
  72. typedef struct Decorr {
  73. int delta;
  74. int value;
  75. int weightA;
  76. int weightB;
  77. int samplesA[8];
  78. int samplesB[8];
  79. } Decorr;
  80. typedef struct WvChannel {
  81. int median[3];
  82. int slow_level, error_limit;
  83. int bitrate_acc, bitrate_delta;
  84. } WvChannel;
  85. typedef struct WavpackFrameContext {
  86. AVCodecContext *avctx;
  87. int frame_flags;
  88. int stereo, stereo_in;
  89. int joint;
  90. uint32_t CRC;
  91. GetBitContext gb;
  92. int got_extra_bits;
  93. uint32_t crc_extra_bits;
  94. GetBitContext gb_extra_bits;
  95. int data_size; // in bits
  96. int samples;
  97. int terms;
  98. Decorr decorr[MAX_TERMS];
  99. int zero, one, zeroes;
  100. int extra_bits;
  101. int and, or, shift;
  102. int post_shift;
  103. int hybrid, hybrid_bitrate;
  104. int hybrid_maxclip, hybrid_minclip;
  105. int float_flag;
  106. int float_shift;
  107. int float_max_exp;
  108. WvChannel ch[2];
  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 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 &&
  367. get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
  368. S |= get_bits(&s->gb_extra_bits, s->extra_bits);
  369. *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
  370. }
  371. }
  372. bit = (S & s->and) | s->or;
  373. bit = ((S + bit) << s->shift) - bit;
  374. if (s->hybrid)
  375. bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
  376. return bit << s->post_shift;
  377. }
  378. static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
  379. {
  380. union {
  381. float f;
  382. uint32_t u;
  383. } value;
  384. unsigned int sign;
  385. int exp = s->float_max_exp;
  386. if (s->got_extra_bits) {
  387. const int max_bits = 1 + 23 + 8 + 1;
  388. const int left_bits = get_bits_left(&s->gb_extra_bits);
  389. if (left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
  390. return 0.0;
  391. }
  392. if (S) {
  393. S <<= s->float_shift;
  394. sign = S < 0;
  395. if (sign)
  396. S = -S;
  397. if (S >= 0x1000000) {
  398. if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
  399. S = get_bits(&s->gb_extra_bits, 23);
  400. else
  401. S = 0;
  402. exp = 255;
  403. } else if (exp) {
  404. int shift = 23 - av_log2(S);
  405. exp = s->float_max_exp;
  406. if (exp <= shift)
  407. shift = --exp;
  408. exp -= shift;
  409. if (shift) {
  410. S <<= shift;
  411. if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
  412. (s->got_extra_bits &&
  413. (s->float_flag & WV_FLT_SHIFT_SAME) &&
  414. get_bits1(&s->gb_extra_bits))) {
  415. S |= (1 << shift) - 1;
  416. } else if (s->got_extra_bits &&
  417. (s->float_flag & WV_FLT_SHIFT_SENT)) {
  418. S |= get_bits(&s->gb_extra_bits, shift);
  419. }
  420. }
  421. } else {
  422. exp = s->float_max_exp;
  423. }
  424. S &= 0x7fffff;
  425. } else {
  426. sign = 0;
  427. exp = 0;
  428. if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
  429. if (get_bits1(&s->gb_extra_bits)) {
  430. S = get_bits(&s->gb_extra_bits, 23);
  431. if (s->float_max_exp >= 25)
  432. exp = get_bits(&s->gb_extra_bits, 8);
  433. sign = get_bits1(&s->gb_extra_bits);
  434. } else {
  435. if (s->float_flag & WV_FLT_ZERO_SIGN)
  436. sign = get_bits1(&s->gb_extra_bits);
  437. }
  438. }
  439. }
  440. *crc = *crc * 27 + S * 9 + exp * 3 + sign;
  441. value.u = (sign << 31) | (exp << 23) | S;
  442. return value.f;
  443. }
  444. static void wv_reset_saved_context(WavpackFrameContext *s)
  445. {
  446. s->pos = 0;
  447. s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
  448. }
  449. static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
  450. uint32_t crc_extra_bits)
  451. {
  452. if (crc != s->CRC) {
  453. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  454. return AVERROR_INVALIDDATA;
  455. }
  456. if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
  457. av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
  458. return AVERROR_INVALIDDATA;
  459. }
  460. return 0;
  461. }
  462. static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
  463. void *dst, const int type)
  464. {
  465. int i, j, count = 0;
  466. int last, t;
  467. int A, B, L, L2, R, R2;
  468. int pos = s->pos;
  469. uint32_t crc = s->sc.crc;
  470. uint32_t crc_extra_bits = s->extra_sc.crc;
  471. int16_t *dst16 = dst;
  472. int32_t *dst32 = dst;
  473. float *dstfl = dst;
  474. const int channel_pad = s->avctx->channels - 2;
  475. s->one = s->zero = s->zeroes = 0;
  476. do {
  477. L = wv_get_value(s, gb, 0, &last);
  478. if (last)
  479. break;
  480. R = wv_get_value(s, gb, 1, &last);
  481. if (last)
  482. break;
  483. for (i = 0; i < s->terms; i++) {
  484. t = s->decorr[i].value;
  485. if (t > 0) {
  486. if (t > 8) {
  487. if (t & 1) {
  488. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  489. B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
  490. } else {
  491. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  492. B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
  493. }
  494. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  495. s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
  496. j = 0;
  497. } else {
  498. A = s->decorr[i].samplesA[pos];
  499. B = s->decorr[i].samplesB[pos];
  500. j = (pos + t) & 7;
  501. }
  502. if (type != AV_SAMPLE_FMT_S16) {
  503. L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  504. R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
  505. } else {
  506. L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
  507. R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
  508. }
  509. if (A && L)
  510. s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  511. if (B && R)
  512. s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
  513. s->decorr[i].samplesA[j] = L = L2;
  514. s->decorr[i].samplesB[j] = R = R2;
  515. } else if (t == -1) {
  516. if (type != AV_SAMPLE_FMT_S16)
  517. L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
  518. else
  519. L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
  520. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
  521. L = L2;
  522. if (type != AV_SAMPLE_FMT_S16)
  523. R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
  524. else
  525. R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
  526. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
  527. R = R2;
  528. s->decorr[i].samplesA[0] = R;
  529. } else {
  530. if (type != AV_SAMPLE_FMT_S16)
  531. R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
  532. else
  533. R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
  534. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
  535. R = R2;
  536. if (t == -3) {
  537. R2 = s->decorr[i].samplesA[0];
  538. s->decorr[i].samplesA[0] = R;
  539. }
  540. if (type != AV_SAMPLE_FMT_S16)
  541. L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
  542. else
  543. L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
  544. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
  545. L = L2;
  546. s->decorr[i].samplesB[0] = L;
  547. }
  548. }
  549. pos = (pos + 1) & 7;
  550. if (s->joint)
  551. L += (R -= (L >> 1));
  552. crc = (crc * 3 + L) * 3 + R;
  553. if (type == AV_SAMPLE_FMT_FLT) {
  554. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
  555. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
  556. dstfl += channel_pad;
  557. } else if (type == AV_SAMPLE_FMT_S32) {
  558. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
  559. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
  560. dst32 += channel_pad;
  561. } else {
  562. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
  563. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
  564. dst16 += channel_pad;
  565. }
  566. count++;
  567. } while (!last && count < s->samples);
  568. wv_reset_saved_context(s);
  569. if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
  570. wv_check_crc(s, crc, crc_extra_bits))
  571. return AVERROR_INVALIDDATA;
  572. return count * 2;
  573. }
  574. static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
  575. void *dst, const int type)
  576. {
  577. int i, j, count = 0;
  578. int last, t;
  579. int A, S, T;
  580. int pos = s->pos;
  581. uint32_t crc = s->sc.crc;
  582. uint32_t crc_extra_bits = s->extra_sc.crc;
  583. int16_t *dst16 = dst;
  584. int32_t *dst32 = dst;
  585. float *dstfl = dst;
  586. const int channel_stride = s->avctx->channels;
  587. s->one = s->zero = s->zeroes = 0;
  588. do {
  589. T = wv_get_value(s, gb, 0, &last);
  590. S = 0;
  591. if (last)
  592. break;
  593. for (i = 0; i < s->terms; i++) {
  594. t = s->decorr[i].value;
  595. if (t > 8) {
  596. if (t & 1)
  597. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  598. else
  599. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  600. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  601. j = 0;
  602. } else {
  603. A = s->decorr[i].samplesA[pos];
  604. j = (pos + t) & 7;
  605. }
  606. if (type != AV_SAMPLE_FMT_S16)
  607. S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  608. else
  609. S = T + ((s->decorr[i].weightA * A + 512) >> 10);
  610. if (A && T)
  611. s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  612. s->decorr[i].samplesA[j] = T = S;
  613. }
  614. pos = (pos + 1) & 7;
  615. crc = crc * 3 + S;
  616. if (type == AV_SAMPLE_FMT_FLT) {
  617. *dstfl = wv_get_value_float(s, &crc_extra_bits, S);
  618. dstfl += channel_stride;
  619. } else if (type == AV_SAMPLE_FMT_S32) {
  620. *dst32 = wv_get_value_integer(s, &crc_extra_bits, S);
  621. dst32 += channel_stride;
  622. } else {
  623. *dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
  624. dst16 += channel_stride;
  625. }
  626. count++;
  627. } while (!last && count < s->samples);
  628. wv_reset_saved_context(s);
  629. if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
  630. wv_check_crc(s, crc, crc_extra_bits))
  631. return AVERROR_INVALIDDATA;
  632. return count;
  633. }
  634. static av_cold int wv_alloc_frame_context(WavpackContext *c)
  635. {
  636. if (c->fdec_num == WV_MAX_FRAME_DECODERS)
  637. return -1;
  638. c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
  639. if (!c->fdec[c->fdec_num])
  640. return -1;
  641. c->fdec_num++;
  642. c->fdec[c->fdec_num - 1]->avctx = c->avctx;
  643. wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
  644. return 0;
  645. }
  646. static av_cold int wavpack_decode_init(AVCodecContext *avctx)
  647. {
  648. WavpackContext *s = avctx->priv_data;
  649. s->avctx = avctx;
  650. if (avctx->bits_per_coded_sample <= 16)
  651. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  652. else
  653. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  654. if (avctx->channels <= 2 && !avctx->channel_layout)
  655. avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
  656. : AV_CH_LAYOUT_MONO;
  657. s->multichannel = avctx->channels > 2;
  658. /* lavf demuxer does not provide extradata, Matroska stores 0x403
  659. * there, use this to detect decoding mode for multichannel */
  660. s->mkv_mode = 0;
  661. if (s->multichannel && avctx->extradata && avctx->extradata_size == 2) {
  662. int ver = AV_RL16(avctx->extradata);
  663. if (ver >= 0x402 && ver <= 0x410)
  664. s->mkv_mode = 1;
  665. }
  666. s->fdec_num = 0;
  667. return 0;
  668. }
  669. static av_cold int wavpack_decode_end(AVCodecContext *avctx)
  670. {
  671. WavpackContext *s = avctx->priv_data;
  672. int i;
  673. for (i = 0; i < s->fdec_num; i++)
  674. av_freep(&s->fdec[i]);
  675. s->fdec_num = 0;
  676. return 0;
  677. }
  678. static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
  679. void *data, int *got_frame_ptr,
  680. const uint8_t *buf, int buf_size)
  681. {
  682. WavpackContext *wc = avctx->priv_data;
  683. WavpackFrameContext *s;
  684. void *samples = data;
  685. int samplecount;
  686. int got_terms = 0, got_weights = 0, got_samples = 0,
  687. got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
  688. const uint8_t *orig_buf = buf;
  689. const uint8_t *buf_end = buf + buf_size;
  690. int i, j, id, size, ssize, weights, t;
  691. int bpp, chan, chmask, orig_bpp;
  692. if (buf_size == 0) {
  693. *got_frame_ptr = 0;
  694. return 0;
  695. }
  696. if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
  697. av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
  698. return AVERROR_INVALIDDATA;
  699. }
  700. s = wc->fdec[block_no];
  701. if (!s) {
  702. av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
  703. block_no);
  704. return AVERROR_INVALIDDATA;
  705. }
  706. memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
  707. memset(s->ch, 0, sizeof(s->ch));
  708. s->extra_bits = 0;
  709. s->and = s->or = s->shift = 0;
  710. s->got_extra_bits = 0;
  711. if (!wc->mkv_mode) {
  712. s->samples = AV_RL32(buf);
  713. buf += 4;
  714. if (s->samples != wc->samples)
  715. return AVERROR_INVALIDDATA;
  716. if (!s->samples) {
  717. *got_frame_ptr = 0;
  718. return 0;
  719. }
  720. } else {
  721. s->samples = wc->samples;
  722. }
  723. s->frame_flags = AV_RL32(buf);
  724. buf += 4;
  725. bpp = av_get_bytes_per_sample(avctx->sample_fmt);
  726. samples = (uint8_t *)samples + bpp * wc->ch_offset;
  727. orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
  728. s->stereo = !(s->frame_flags & WV_MONO);
  729. s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
  730. s->joint = s->frame_flags & WV_JOINT_STEREO;
  731. s->hybrid = s->frame_flags & WV_HYBRID_MODE;
  732. s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
  733. s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
  734. s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
  735. s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
  736. s->CRC = AV_RL32(buf);
  737. buf += 4;
  738. if (wc->mkv_mode)
  739. buf += 4; // skip block size;
  740. wc->ch_offset += 1 + s->stereo;
  741. // parse metadata blocks
  742. while (buf < buf_end) {
  743. id = *buf++;
  744. size = *buf++;
  745. if (id & WP_IDF_LONG) {
  746. size |= (*buf++) << 8;
  747. size |= (*buf++) << 16;
  748. }
  749. size <<= 1; // size is specified in words
  750. ssize = size;
  751. if (id & WP_IDF_ODD)
  752. size--;
  753. if (size < 0) {
  754. av_log(avctx, AV_LOG_ERROR,
  755. "Got incorrect block %02X with size %i\n", id, size);
  756. break;
  757. }
  758. if (buf + ssize > buf_end) {
  759. av_log(avctx, AV_LOG_ERROR,
  760. "Block size %i is out of bounds\n", size);
  761. break;
  762. }
  763. if (id & WP_IDF_IGNORE) {
  764. buf += ssize;
  765. continue;
  766. }
  767. switch (id & WP_IDF_MASK) {
  768. case WP_ID_DECTERMS:
  769. if (size > MAX_TERMS) {
  770. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
  771. s->terms = 0;
  772. buf += ssize;
  773. continue;
  774. }
  775. s->terms = size;
  776. for (i = 0; i < s->terms; i++) {
  777. s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
  778. s->decorr[s->terms - i - 1].delta = *buf >> 5;
  779. buf++;
  780. }
  781. got_terms = 1;
  782. break;
  783. case WP_ID_DECWEIGHTS:
  784. if (!got_terms) {
  785. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  786. continue;
  787. }
  788. weights = size >> s->stereo_in;
  789. if (weights > MAX_TERMS || weights > s->terms) {
  790. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
  791. buf += ssize;
  792. continue;
  793. }
  794. for (i = 0; i < weights; i++) {
  795. t = (int8_t)(*buf++);
  796. s->decorr[s->terms - i - 1].weightA = t << 3;
  797. if (s->decorr[s->terms - i - 1].weightA > 0)
  798. s->decorr[s->terms - i - 1].weightA +=
  799. (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
  800. if (s->stereo_in) {
  801. t = (int8_t)(*buf++);
  802. s->decorr[s->terms - i - 1].weightB = t << 3;
  803. if (s->decorr[s->terms - i - 1].weightB > 0)
  804. s->decorr[s->terms - i - 1].weightB +=
  805. (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
  806. }
  807. }
  808. got_weights = 1;
  809. break;
  810. case WP_ID_DECSAMPLES:
  811. if (!got_terms) {
  812. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  813. continue;
  814. }
  815. t = 0;
  816. for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
  817. if (s->decorr[i].value > 8) {
  818. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf));
  819. buf += 2;
  820. s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf));
  821. buf += 2;
  822. if (s->stereo_in) {
  823. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf));
  824. buf += 2;
  825. s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf));
  826. buf += 2;
  827. t += 4;
  828. }
  829. t += 4;
  830. } else if (s->decorr[i].value < 0) {
  831. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf));
  832. buf += 2;
  833. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf));
  834. buf += 2;
  835. t += 4;
  836. } else {
  837. for (j = 0; j < s->decorr[i].value; j++) {
  838. s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf));
  839. buf += 2;
  840. if (s->stereo_in) {
  841. s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf));
  842. buf += 2;
  843. }
  844. }
  845. t += s->decorr[i].value * 2 * (s->stereo_in + 1);
  846. }
  847. }
  848. got_samples = 1;
  849. break;
  850. case WP_ID_ENTROPY:
  851. if (size != 6 * (s->stereo_in + 1)) {
  852. av_log(avctx, AV_LOG_ERROR,
  853. "Entropy vars size should be %i, got %i",
  854. 6 * (s->stereo_in + 1), size);
  855. buf += ssize;
  856. continue;
  857. }
  858. for (j = 0; j <= s->stereo_in; j++)
  859. for (i = 0; i < 3; i++) {
  860. s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
  861. buf += 2;
  862. }
  863. got_entropy = 1;
  864. break;
  865. case WP_ID_HYBRID:
  866. if (s->hybrid_bitrate) {
  867. for (i = 0; i <= s->stereo_in; i++) {
  868. s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
  869. buf += 2;
  870. size -= 2;
  871. }
  872. }
  873. for (i = 0; i < (s->stereo_in + 1); i++) {
  874. s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
  875. buf += 2;
  876. size -= 2;
  877. }
  878. if (size > 0) {
  879. for (i = 0; i < (s->stereo_in + 1); i++) {
  880. s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
  881. buf += 2;
  882. }
  883. } else {
  884. for (i = 0; i < (s->stereo_in + 1); i++)
  885. s->ch[i].bitrate_delta = 0;
  886. }
  887. got_hybrid = 1;
  888. break;
  889. case WP_ID_INT32INFO:
  890. if (size != 4) {
  891. av_log(avctx, AV_LOG_ERROR,
  892. "Invalid INT32INFO, size = %i, sent_bits = %i\n",
  893. size, *buf);
  894. buf += ssize;
  895. continue;
  896. }
  897. if (buf[0])
  898. s->extra_bits = buf[0];
  899. else if (buf[1])
  900. s->shift = buf[1];
  901. else if (buf[2]) {
  902. s->and = s->or = 1;
  903. s->shift = buf[2];
  904. } else if (buf[3]) {
  905. s->and = 1;
  906. s->shift = buf[3];
  907. }
  908. /* original WavPack decoder forces 32-bit lossy sound to be treated
  909. * as 24-bit one in order to have proper clipping */
  910. if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
  911. s->post_shift += 8;
  912. s->shift -= 8;
  913. s->hybrid_maxclip >>= 8;
  914. s->hybrid_minclip >>= 8;
  915. }
  916. buf += 4;
  917. break;
  918. case WP_ID_FLOATINFO:
  919. if (size != 4) {
  920. av_log(avctx, AV_LOG_ERROR,
  921. "Invalid FLOATINFO, size = %i\n", size);
  922. buf += ssize;
  923. continue;
  924. }
  925. s->float_flag = buf[0];
  926. s->float_shift = buf[1];
  927. s->float_max_exp = buf[2];
  928. buf += 4;
  929. got_float = 1;
  930. break;
  931. case WP_ID_DATA:
  932. s->sc.offset = buf - orig_buf;
  933. s->sc.size = size * 8;
  934. init_get_bits(&s->gb, buf, size * 8);
  935. s->data_size = size * 8;
  936. buf += size;
  937. got_bs = 1;
  938. break;
  939. case WP_ID_EXTRABITS:
  940. if (size <= 4) {
  941. av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
  942. size);
  943. buf += size;
  944. continue;
  945. }
  946. s->extra_sc.offset = buf - orig_buf;
  947. s->extra_sc.size = size * 8;
  948. init_get_bits(&s->gb_extra_bits, buf, size * 8);
  949. s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
  950. buf += size;
  951. s->got_extra_bits = 1;
  952. break;
  953. case WP_ID_CHANINFO:
  954. if (size <= 1) {
  955. av_log(avctx, AV_LOG_ERROR,
  956. "Insufficient channel information\n");
  957. return AVERROR_INVALIDDATA;
  958. }
  959. chan = *buf++;
  960. switch (size - 2) {
  961. case 0:
  962. chmask = *buf;
  963. break;
  964. case 1:
  965. chmask = AV_RL16(buf);
  966. break;
  967. case 2:
  968. chmask = AV_RL24(buf);
  969. break;
  970. case 3:
  971. chmask = AV_RL32(buf);
  972. break;
  973. case 5:
  974. chan |= (buf[1] & 0xF) << 8;
  975. chmask = AV_RL24(buf + 2);
  976. break;
  977. default:
  978. av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
  979. size);
  980. chan = avctx->channels;
  981. chmask = avctx->channel_layout;
  982. }
  983. if (chan != avctx->channels) {
  984. av_log(avctx, AV_LOG_ERROR,
  985. "Block reports total %d channels, "
  986. "decoder believes it's %d channels\n",
  987. chan, avctx->channels);
  988. return AVERROR_INVALIDDATA;
  989. }
  990. if (!avctx->channel_layout)
  991. avctx->channel_layout = chmask;
  992. buf += size - 1;
  993. break;
  994. default:
  995. buf += size;
  996. }
  997. if (id & WP_IDF_ODD)
  998. buf++;
  999. }
  1000. if (!got_terms) {
  1001. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
  1002. return AVERROR_INVALIDDATA;
  1003. }
  1004. if (!got_weights) {
  1005. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
  1006. return AVERROR_INVALIDDATA;
  1007. }
  1008. if (!got_samples) {
  1009. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
  1010. return AVERROR_INVALIDDATA;
  1011. }
  1012. if (!got_entropy) {
  1013. av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
  1014. return AVERROR_INVALIDDATA;
  1015. }
  1016. if (s->hybrid && !got_hybrid) {
  1017. av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
  1018. return AVERROR_INVALIDDATA;
  1019. }
  1020. if (!got_bs) {
  1021. av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
  1022. return AVERROR_INVALIDDATA;
  1023. }
  1024. if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
  1025. av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
  1026. return AVERROR_INVALIDDATA;
  1027. }
  1028. if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
  1029. const int size = get_bits_left(&s->gb_extra_bits);
  1030. const int wanted = s->samples * s->extra_bits << s->stereo_in;
  1031. if (size < wanted) {
  1032. av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
  1033. s->got_extra_bits = 0;
  1034. }
  1035. }
  1036. if (s->stereo_in) {
  1037. if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
  1038. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
  1039. else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1040. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
  1041. else
  1042. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
  1043. if (samplecount < 0)
  1044. return samplecount;
  1045. samplecount >>= 1;
  1046. } else {
  1047. const int channel_stride = avctx->channels;
  1048. if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
  1049. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
  1050. else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1051. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
  1052. else
  1053. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
  1054. if (samplecount < 0)
  1055. return samplecount;
  1056. if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
  1057. int16_t *dst = (int16_t *)samples + 1;
  1058. int16_t *src = (int16_t *)samples;
  1059. int cnt = samplecount;
  1060. while (cnt--) {
  1061. *dst = *src;
  1062. src += channel_stride;
  1063. dst += channel_stride;
  1064. }
  1065. } else if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
  1066. int32_t *dst = (int32_t *)samples + 1;
  1067. int32_t *src = (int32_t *)samples;
  1068. int cnt = samplecount;
  1069. while (cnt--) {
  1070. *dst = *src;
  1071. src += channel_stride;
  1072. dst += channel_stride;
  1073. }
  1074. } else if (s->stereo) {
  1075. float *dst = (float *)samples + 1;
  1076. float *src = (float *)samples;
  1077. int cnt = samplecount;
  1078. while (cnt--) {
  1079. *dst = *src;
  1080. src += channel_stride;
  1081. dst += channel_stride;
  1082. }
  1083. }
  1084. }
  1085. *got_frame_ptr = 1;
  1086. return samplecount * bpp;
  1087. }
  1088. static void wavpack_decode_flush(AVCodecContext *avctx)
  1089. {
  1090. WavpackContext *s = avctx->priv_data;
  1091. int i;
  1092. for (i = 0; i < s->fdec_num; i++)
  1093. wv_reset_saved_context(s->fdec[i]);
  1094. }
  1095. static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
  1096. int *got_frame_ptr, AVPacket *avpkt)
  1097. {
  1098. WavpackContext *s = avctx->priv_data;
  1099. const uint8_t *buf = avpkt->data;
  1100. int buf_size = avpkt->size;
  1101. AVFrame *frame = data;
  1102. int frame_size, ret, frame_flags;
  1103. int samplecount = 0;
  1104. s->block = 0;
  1105. s->ch_offset = 0;
  1106. /* determine number of samples */
  1107. if (s->mkv_mode) {
  1108. s->samples = AV_RL32(buf);
  1109. buf += 4;
  1110. frame_flags = AV_RL32(buf);
  1111. } else {
  1112. if (s->multichannel) {
  1113. s->samples = AV_RL32(buf + 4);
  1114. frame_flags = AV_RL32(buf + 8);
  1115. } else {
  1116. s->samples = AV_RL32(buf);
  1117. frame_flags = AV_RL32(buf + 4);
  1118. }
  1119. }
  1120. if (s->samples <= 0) {
  1121. av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
  1122. s->samples);
  1123. return AVERROR_INVALIDDATA;
  1124. }
  1125. if (frame_flags & 0x80) {
  1126. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  1127. } else if ((frame_flags & 0x03) <= 1) {
  1128. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  1129. } else {
  1130. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  1131. avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
  1132. }
  1133. /* get output buffer */
  1134. frame->nb_samples = s->samples;
  1135. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  1136. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1137. return ret;
  1138. }
  1139. while (buf_size > 0) {
  1140. if (!s->multichannel) {
  1141. frame_size = buf_size;
  1142. } else {
  1143. if (!s->mkv_mode) {
  1144. frame_size = AV_RL32(buf) - 12;
  1145. buf += 4;
  1146. buf_size -= 4;
  1147. } else {
  1148. if (buf_size < 12) // MKV files can have zero flags after last block
  1149. break;
  1150. frame_size = AV_RL32(buf + 8) + 12;
  1151. }
  1152. }
  1153. if (frame_size < 0 || frame_size > buf_size) {
  1154. av_log(avctx, AV_LOG_ERROR,
  1155. "Block %d has invalid size (size %d vs. %d bytes left)\n",
  1156. s->block, frame_size, buf_size);
  1157. wavpack_decode_flush(avctx);
  1158. return AVERROR_INVALIDDATA;
  1159. }
  1160. if ((samplecount = wavpack_decode_block(avctx, s->block,
  1161. frame->data[0], got_frame_ptr,
  1162. buf, frame_size)) < 0) {
  1163. wavpack_decode_flush(avctx);
  1164. return samplecount;
  1165. }
  1166. s->block++;
  1167. buf += frame_size;
  1168. buf_size -= frame_size;
  1169. }
  1170. return avpkt->size;
  1171. }
  1172. AVCodec ff_wavpack_decoder = {
  1173. .name = "wavpack",
  1174. .type = AVMEDIA_TYPE_AUDIO,
  1175. .id = AV_CODEC_ID_WAVPACK,
  1176. .priv_data_size = sizeof(WavpackContext),
  1177. .init = wavpack_decode_init,
  1178. .close = wavpack_decode_end,
  1179. .decode = wavpack_decode_frame,
  1180. .flush = wavpack_decode_flush,
  1181. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
  1182. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  1183. };