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.

1226 lines
41KB

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