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.

1243 lines
42KB

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