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.

1289 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 -1;
  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 -1;
  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) {
  715. *got_frame_ptr = 0;
  716. return 0;
  717. }
  718. } else {
  719. s->samples = wc->samples;
  720. }
  721. s->frame_flags = AV_RL32(buf);
  722. buf += 4;
  723. bpp = av_get_bytes_per_sample(avctx->sample_fmt);
  724. samples = (uint8_t *)samples + bpp * wc->ch_offset;
  725. orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
  726. s->stereo = !(s->frame_flags & WV_MONO);
  727. s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
  728. s->joint = s->frame_flags & WV_JOINT_STEREO;
  729. s->hybrid = s->frame_flags & WV_HYBRID_MODE;
  730. s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
  731. s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
  732. s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
  733. s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
  734. s->CRC = AV_RL32(buf);
  735. buf += 4;
  736. if (wc->mkv_mode)
  737. buf += 4; // skip block size;
  738. wc->ch_offset += 1 + s->stereo;
  739. // parse metadata blocks
  740. while (buf < buf_end) {
  741. id = *buf++;
  742. size = *buf++;
  743. if (id & WP_IDF_LONG) {
  744. size |= (*buf++) << 8;
  745. size |= (*buf++) << 16;
  746. }
  747. size <<= 1; // size is specified in words
  748. ssize = size;
  749. if (id & WP_IDF_ODD)
  750. size--;
  751. if (size < 0) {
  752. av_log(avctx, AV_LOG_ERROR,
  753. "Got incorrect block %02X with size %i\n", id, size);
  754. break;
  755. }
  756. if (buf + ssize > buf_end) {
  757. av_log(avctx, AV_LOG_ERROR,
  758. "Block size %i is out of bounds\n", size);
  759. break;
  760. }
  761. if (id & WP_IDF_IGNORE) {
  762. buf += ssize;
  763. continue;
  764. }
  765. switch (id & WP_IDF_MASK) {
  766. case WP_ID_DECTERMS:
  767. if (size > MAX_TERMS) {
  768. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
  769. s->terms = 0;
  770. buf += ssize;
  771. continue;
  772. }
  773. s->terms = size;
  774. for (i = 0; i < s->terms; i++) {
  775. s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
  776. s->decorr[s->terms - i - 1].delta = *buf >> 5;
  777. buf++;
  778. }
  779. got_terms = 1;
  780. break;
  781. case WP_ID_DECWEIGHTS:
  782. if (!got_terms) {
  783. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  784. continue;
  785. }
  786. weights = size >> s->stereo_in;
  787. if (weights > MAX_TERMS || weights > s->terms) {
  788. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
  789. buf += ssize;
  790. continue;
  791. }
  792. for (i = 0; i < weights; i++) {
  793. t = (int8_t)(*buf++);
  794. s->decorr[s->terms - i - 1].weightA = t << 3;
  795. if (s->decorr[s->terms - i - 1].weightA > 0)
  796. s->decorr[s->terms - i - 1].weightA +=
  797. (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
  798. if (s->stereo_in) {
  799. t = (int8_t)(*buf++);
  800. s->decorr[s->terms - i - 1].weightB = t << 3;
  801. if (s->decorr[s->terms - i - 1].weightB > 0)
  802. s->decorr[s->terms - i - 1].weightB +=
  803. (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
  804. }
  805. }
  806. got_weights = 1;
  807. break;
  808. case WP_ID_DECSAMPLES:
  809. if (!got_terms) {
  810. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  811. continue;
  812. }
  813. t = 0;
  814. for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
  815. if (s->decorr[i].value > 8) {
  816. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf));
  817. buf += 2;
  818. s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf));
  819. buf += 2;
  820. if (s->stereo_in) {
  821. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf));
  822. buf += 2;
  823. s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf));
  824. buf += 2;
  825. t += 4;
  826. }
  827. t += 4;
  828. } else if (s->decorr[i].value < 0) {
  829. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf));
  830. buf += 2;
  831. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf));
  832. buf += 2;
  833. t += 4;
  834. } else {
  835. for (j = 0; j < s->decorr[i].value; j++) {
  836. s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf));
  837. buf += 2;
  838. if (s->stereo_in) {
  839. s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf));
  840. buf += 2;
  841. }
  842. }
  843. t += s->decorr[i].value * 2 * (s->stereo_in + 1);
  844. }
  845. }
  846. got_samples = 1;
  847. break;
  848. case WP_ID_ENTROPY:
  849. if (size != 6 * (s->stereo_in + 1)) {
  850. av_log(avctx, AV_LOG_ERROR,
  851. "Entropy vars size should be %i, got %i",
  852. 6 * (s->stereo_in + 1), size);
  853. buf += ssize;
  854. continue;
  855. }
  856. for (j = 0; j <= s->stereo_in; j++)
  857. for (i = 0; i < 3; i++) {
  858. s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
  859. buf += 2;
  860. }
  861. got_entropy = 1;
  862. break;
  863. case WP_ID_HYBRID:
  864. if (s->hybrid_bitrate) {
  865. for (i = 0; i <= s->stereo_in; i++) {
  866. s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
  867. buf += 2;
  868. size -= 2;
  869. }
  870. }
  871. for (i = 0; i < (s->stereo_in + 1); i++) {
  872. s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
  873. buf += 2;
  874. size -= 2;
  875. }
  876. if (size > 0) {
  877. for (i = 0; i < (s->stereo_in + 1); i++) {
  878. s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
  879. buf += 2;
  880. }
  881. } else {
  882. for (i = 0; i < (s->stereo_in + 1); i++)
  883. s->ch[i].bitrate_delta = 0;
  884. }
  885. got_hybrid = 1;
  886. break;
  887. case WP_ID_INT32INFO:
  888. if (size != 4) {
  889. av_log(avctx, AV_LOG_ERROR,
  890. "Invalid INT32INFO, size = %i, sent_bits = %i\n",
  891. size, *buf);
  892. buf += ssize;
  893. continue;
  894. }
  895. if (buf[0])
  896. s->extra_bits = buf[0];
  897. else if (buf[1])
  898. s->shift = buf[1];
  899. else if (buf[2]) {
  900. s->and = s->or = 1;
  901. s->shift = buf[2];
  902. } else if (buf[3]) {
  903. s->and = 1;
  904. s->shift = buf[3];
  905. }
  906. /* original WavPack decoder forces 32-bit lossy sound to be treated
  907. * as 24-bit one in order to have proper clipping */
  908. if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
  909. s->post_shift += 8;
  910. s->shift -= 8;
  911. s->hybrid_maxclip >>= 8;
  912. s->hybrid_minclip >>= 8;
  913. }
  914. buf += 4;
  915. break;
  916. case WP_ID_FLOATINFO:
  917. if (size != 4) {
  918. av_log(avctx, AV_LOG_ERROR,
  919. "Invalid FLOATINFO, size = %i\n", size);
  920. buf += ssize;
  921. continue;
  922. }
  923. s->float_flag = buf[0];
  924. s->float_shift = buf[1];
  925. s->float_max_exp = buf[2];
  926. buf += 4;
  927. got_float = 1;
  928. break;
  929. case WP_ID_DATA:
  930. s->sc.offset = buf - orig_buf;
  931. s->sc.size = size * 8;
  932. init_get_bits(&s->gb, buf, size * 8);
  933. s->data_size = size * 8;
  934. buf += size;
  935. got_bs = 1;
  936. break;
  937. case WP_ID_EXTRABITS:
  938. if (size <= 4) {
  939. av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
  940. size);
  941. buf += size;
  942. continue;
  943. }
  944. s->extra_sc.offset = buf - orig_buf;
  945. s->extra_sc.size = size * 8;
  946. init_get_bits(&s->gb_extra_bits, buf, size * 8);
  947. s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
  948. buf += size;
  949. s->got_extra_bits = 1;
  950. break;
  951. case WP_ID_CHANINFO:
  952. if (size <= 1) {
  953. av_log(avctx, AV_LOG_ERROR,
  954. "Insufficient channel information\n");
  955. return -1;
  956. }
  957. chan = *buf++;
  958. switch (size - 2) {
  959. case 0:
  960. chmask = *buf;
  961. break;
  962. case 1:
  963. chmask = AV_RL16(buf);
  964. break;
  965. case 2:
  966. chmask = AV_RL24(buf);
  967. break;
  968. case 3:
  969. chmask = AV_RL32(buf);
  970. break;
  971. case 5:
  972. chan |= (buf[1] & 0xF) << 8;
  973. chmask = AV_RL24(buf + 2);
  974. break;
  975. default:
  976. av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
  977. size);
  978. chan = avctx->channels;
  979. chmask = avctx->channel_layout;
  980. }
  981. if (chan != avctx->channels) {
  982. av_log(avctx, AV_LOG_ERROR,
  983. "Block reports total %d channels, "
  984. "decoder believes it's %d channels\n",
  985. chan, avctx->channels);
  986. return -1;
  987. }
  988. if (!avctx->channel_layout)
  989. avctx->channel_layout = chmask;
  990. buf += size - 1;
  991. break;
  992. default:
  993. buf += size;
  994. }
  995. if (id & WP_IDF_ODD)
  996. buf++;
  997. }
  998. if (!got_terms) {
  999. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
  1000. return -1;
  1001. }
  1002. if (!got_weights) {
  1003. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
  1004. return -1;
  1005. }
  1006. if (!got_samples) {
  1007. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
  1008. return -1;
  1009. }
  1010. if (!got_entropy) {
  1011. av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
  1012. return -1;
  1013. }
  1014. if (s->hybrid && !got_hybrid) {
  1015. av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
  1016. return -1;
  1017. }
  1018. if (!got_bs) {
  1019. av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
  1020. return -1;
  1021. }
  1022. if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
  1023. av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
  1024. return -1;
  1025. }
  1026. if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
  1027. const int size = get_bits_left(&s->gb_extra_bits);
  1028. const int wanted = s->samples * s->extra_bits << s->stereo_in;
  1029. if (size < wanted) {
  1030. av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
  1031. s->got_extra_bits = 0;
  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. *got_frame_ptr = 1;
  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, void *data,
  1094. int *got_frame_ptr, AVPacket *avpkt)
  1095. {
  1096. WavpackContext *s = avctx->priv_data;
  1097. const uint8_t *buf = avpkt->data;
  1098. int buf_size = avpkt->size;
  1099. AVFrame *frame = data;
  1100. int frame_size, ret, frame_flags;
  1101. int samplecount = 0;
  1102. s->block = 0;
  1103. s->ch_offset = 0;
  1104. /* determine number of samples */
  1105. if (s->mkv_mode) {
  1106. s->samples = AV_RL32(buf);
  1107. buf += 4;
  1108. frame_flags = AV_RL32(buf);
  1109. } else {
  1110. if (s->multichannel) {
  1111. s->samples = AV_RL32(buf + 4);
  1112. frame_flags = AV_RL32(buf + 8);
  1113. } else {
  1114. s->samples = AV_RL32(buf);
  1115. frame_flags = AV_RL32(buf + 4);
  1116. }
  1117. }
  1118. if (s->samples <= 0) {
  1119. av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
  1120. s->samples);
  1121. return AVERROR(EINVAL);
  1122. }
  1123. if (frame_flags & 0x80) {
  1124. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  1125. } else if ((frame_flags & 0x03) <= 1) {
  1126. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  1127. } else {
  1128. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  1129. avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
  1130. }
  1131. /* get output buffer */
  1132. frame->nb_samples = s->samples;
  1133. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  1134. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1135. return ret;
  1136. }
  1137. while (buf_size > 0) {
  1138. if (!s->multichannel) {
  1139. frame_size = buf_size;
  1140. } else {
  1141. if (!s->mkv_mode) {
  1142. frame_size = AV_RL32(buf) - 12;
  1143. buf += 4;
  1144. buf_size -= 4;
  1145. } else {
  1146. if (buf_size < 12) // MKV files can have zero flags after last block
  1147. break;
  1148. frame_size = AV_RL32(buf + 8) + 12;
  1149. }
  1150. }
  1151. if (frame_size < 0 || frame_size > buf_size) {
  1152. av_log(avctx, AV_LOG_ERROR,
  1153. "Block %d has invalid size (size %d vs. %d bytes left)\n",
  1154. s->block, frame_size, buf_size);
  1155. wavpack_decode_flush(avctx);
  1156. return -1;
  1157. }
  1158. if ((samplecount = wavpack_decode_block(avctx, s->block,
  1159. frame->data[0], got_frame_ptr,
  1160. buf, frame_size)) < 0) {
  1161. wavpack_decode_flush(avctx);
  1162. return -1;
  1163. }
  1164. s->block++;
  1165. buf += frame_size;
  1166. buf_size -= frame_size;
  1167. }
  1168. return avpkt->size;
  1169. }
  1170. AVCodec ff_wavpack_decoder = {
  1171. .name = "wavpack",
  1172. .type = AVMEDIA_TYPE_AUDIO,
  1173. .id = AV_CODEC_ID_WAVPACK,
  1174. .priv_data_size = sizeof(WavpackContext),
  1175. .init = wavpack_decode_init,
  1176. .close = wavpack_decode_end,
  1177. .decode = wavpack_decode_frame,
  1178. .flush = wavpack_decode_flush,
  1179. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
  1180. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  1181. };