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.

1254 lines
41KB

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