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.

1270 lines
42KB

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