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