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.

1262 lines
41KB

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