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