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.

1239 lines
42KB

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