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.

1243 lines
41KB

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