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.

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