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.

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