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.

1307 lines
44KB

  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, 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 = dst;
  478. int32_t *dst32 = dst;
  479. float *dstfl = dst;
  480. const int channel_pad = s->avctx->channels - 2;
  481. s->one = s->zero = s->zeroes = 0;
  482. do {
  483. L = wv_get_value(s, gb, 0, &last);
  484. if (last)
  485. break;
  486. R = wv_get_value(s, gb, 1, &last);
  487. if (last)
  488. break;
  489. for (i = 0; i < s->terms; i++) {
  490. t = s->decorr[i].value;
  491. if (t > 0) {
  492. if (t > 8) {
  493. if (t & 1) {
  494. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  495. B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
  496. } else {
  497. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  498. B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
  499. }
  500. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  501. s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
  502. j = 0;
  503. } else {
  504. A = s->decorr[i].samplesA[pos];
  505. B = s->decorr[i].samplesB[pos];
  506. j = (pos + t) & 7;
  507. }
  508. if (type != AV_SAMPLE_FMT_S16) {
  509. L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  510. R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
  511. } else {
  512. L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
  513. R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
  514. }
  515. if (A && L)
  516. s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  517. if (B && R)
  518. s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
  519. s->decorr[i].samplesA[j] = L = L2;
  520. s->decorr[i].samplesB[j] = R = R2;
  521. } else if (t == -1) {
  522. if (type != AV_SAMPLE_FMT_S16)
  523. L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
  524. else
  525. L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
  526. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
  527. L = L2;
  528. if (type != AV_SAMPLE_FMT_S16)
  529. R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
  530. else
  531. R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
  532. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
  533. R = R2;
  534. s->decorr[i].samplesA[0] = R;
  535. } else {
  536. if (type != AV_SAMPLE_FMT_S16)
  537. R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
  538. else
  539. R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
  540. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
  541. R = R2;
  542. if (t == -3) {
  543. R2 = s->decorr[i].samplesA[0];
  544. s->decorr[i].samplesA[0] = R;
  545. }
  546. if (type != AV_SAMPLE_FMT_S16)
  547. L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
  548. else
  549. L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
  550. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
  551. L = L2;
  552. s->decorr[i].samplesB[0] = L;
  553. }
  554. }
  555. pos = (pos + 1) & 7;
  556. if (s->joint)
  557. L += (R -= (L >> 1));
  558. crc = (crc * 3 + L) * 3 + R;
  559. if (type == AV_SAMPLE_FMT_FLT) {
  560. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
  561. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
  562. dstfl += channel_pad;
  563. } else if (type == AV_SAMPLE_FMT_S32) {
  564. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
  565. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
  566. dst32 += channel_pad;
  567. } else {
  568. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
  569. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
  570. dst16 += channel_pad;
  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 count * 2;
  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. const int channel_stride = s->avctx->channels;
  593. s->one = s->zero = s->zeroes = 0;
  594. do {
  595. T = wv_get_value(s, gb, 0, &last);
  596. S = 0;
  597. if (last)
  598. break;
  599. for (i = 0; i < s->terms; i++) {
  600. t = s->decorr[i].value;
  601. if (t > 8) {
  602. if (t & 1)
  603. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  604. else
  605. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  606. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  607. j = 0;
  608. } else {
  609. A = s->decorr[i].samplesA[pos];
  610. j = (pos + t) & 7;
  611. }
  612. if (type != AV_SAMPLE_FMT_S16)
  613. S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  614. else
  615. S = T + ((s->decorr[i].weightA * A + 512) >> 10);
  616. if (A && T)
  617. s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  618. s->decorr[i].samplesA[j] = T = S;
  619. }
  620. pos = (pos + 1) & 7;
  621. crc = crc * 3 + S;
  622. if (type == AV_SAMPLE_FMT_FLT) {
  623. *dstfl = wv_get_value_float(s, &crc_extra_bits, S);
  624. dstfl += channel_stride;
  625. } else if (type == AV_SAMPLE_FMT_S32) {
  626. *dst32 = wv_get_value_integer(s, &crc_extra_bits, S);
  627. dst32 += channel_stride;
  628. } else {
  629. *dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
  630. dst16 += channel_stride;
  631. }
  632. count++;
  633. } while (!last && count < s->samples);
  634. wv_reset_saved_context(s);
  635. if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
  636. wv_check_crc(s, crc, crc_extra_bits))
  637. return AVERROR_INVALIDDATA;
  638. return count;
  639. }
  640. static av_cold int wv_alloc_frame_context(WavpackContext *c)
  641. {
  642. if (c->fdec_num == WV_MAX_FRAME_DECODERS)
  643. return -1;
  644. c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
  645. if (!c->fdec[c->fdec_num])
  646. return -1;
  647. c->fdec_num++;
  648. c->fdec[c->fdec_num - 1]->avctx = c->avctx;
  649. wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
  650. return 0;
  651. }
  652. static av_cold int wavpack_decode_init(AVCodecContext *avctx)
  653. {
  654. WavpackContext *s = avctx->priv_data;
  655. s->avctx = avctx;
  656. if (avctx->bits_per_coded_sample <= 16)
  657. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  658. else
  659. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  660. if (avctx->channels <= 2 && !avctx->channel_layout)
  661. avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
  662. : AV_CH_LAYOUT_MONO;
  663. s->multichannel = avctx->channels > 2;
  664. /* lavf demuxer does not provide extradata, Matroska stores 0x403
  665. * there, use this to detect decoding mode for multichannel */
  666. s->mkv_mode = 0;
  667. if (s->multichannel && avctx->extradata && avctx->extradata_size == 2) {
  668. int ver = AV_RL16(avctx->extradata);
  669. if (ver >= 0x402 && ver <= 0x410)
  670. s->mkv_mode = 1;
  671. }
  672. s->fdec_num = 0;
  673. return 0;
  674. }
  675. static av_cold int wavpack_decode_end(AVCodecContext *avctx)
  676. {
  677. WavpackContext *s = avctx->priv_data;
  678. int i;
  679. for (i = 0; i < s->fdec_num; i++)
  680. av_freep(&s->fdec[i]);
  681. s->fdec_num = 0;
  682. return 0;
  683. }
  684. static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
  685. void *data, int *got_frame_ptr,
  686. const uint8_t *buf, int buf_size)
  687. {
  688. WavpackContext *wc = avctx->priv_data;
  689. WavpackFrameContext *s;
  690. GetByteContext gb;
  691. void *samples = data;
  692. int samplecount;
  693. int got_terms = 0, got_weights = 0, got_samples = 0,
  694. got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
  695. int i, j, id, size, ssize, weights, t;
  696. int bpp, chan, chmask, orig_bpp;
  697. if (buf_size == 0) {
  698. *got_frame_ptr = 0;
  699. return 0;
  700. }
  701. if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
  702. av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
  703. return AVERROR_INVALIDDATA;
  704. }
  705. s = wc->fdec[block_no];
  706. if (!s) {
  707. av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
  708. block_no);
  709. return AVERROR_INVALIDDATA;
  710. }
  711. if (wc->ch_offset >= avctx->channels) {
  712. av_log(avctx, AV_LOG_ERROR, "too many channels\n");
  713. return -1;
  714. }
  715. memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
  716. memset(s->ch, 0, sizeof(s->ch));
  717. s->extra_bits = 0;
  718. s->and = s->or = s->shift = 0;
  719. s->got_extra_bits = 0;
  720. bytestream2_init(&gb, buf, buf_size);
  721. if (!wc->mkv_mode) {
  722. s->samples = bytestream2_get_le32(&gb);
  723. if (s->samples != wc->samples) {
  724. av_log(avctx, AV_LOG_ERROR, "mismatching sample count in block");
  725. return AVERROR_INVALIDDATA;
  726. }
  727. if (!s->samples) {
  728. *got_frame_ptr = 0;
  729. return 0;
  730. }
  731. } else {
  732. s->samples = wc->samples;
  733. }
  734. s->frame_flags = bytestream2_get_le32(&gb);
  735. bpp = av_get_bytes_per_sample(avctx->sample_fmt);
  736. samples = (uint8_t *)samples + bpp * wc->ch_offset;
  737. orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
  738. s->stereo = !(s->frame_flags & WV_MONO);
  739. s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
  740. s->joint = s->frame_flags & WV_JOINT_STEREO;
  741. s->hybrid = s->frame_flags & WV_HYBRID_MODE;
  742. s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
  743. s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
  744. s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
  745. s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
  746. s->CRC = bytestream2_get_le32(&gb);
  747. if (wc->mkv_mode)
  748. bytestream2_skip(&gb, 4); // skip block size;
  749. wc->ch_offset += 1 + s->stereo;
  750. // parse metadata blocks
  751. while (bytestream2_get_bytes_left(&gb)) {
  752. id = bytestream2_get_byte(&gb);
  753. size = bytestream2_get_byte(&gb);
  754. if (id & WP_IDF_LONG) {
  755. size |= (bytestream2_get_byte(&gb)) << 8;
  756. size |= (bytestream2_get_byte(&gb)) << 16;
  757. }
  758. size <<= 1; // size is specified in words
  759. ssize = size;
  760. if (id & WP_IDF_ODD)
  761. size--;
  762. if (size < 0) {
  763. av_log(avctx, AV_LOG_ERROR,
  764. "Got incorrect block %02X with size %i\n", id, size);
  765. break;
  766. }
  767. if (bytestream2_get_bytes_left(&gb) < ssize) {
  768. av_log(avctx, AV_LOG_ERROR,
  769. "Block size %i is out of bounds\n", size);
  770. break;
  771. }
  772. if (id & WP_IDF_IGNORE) {
  773. bytestream2_skip(&gb, ssize);
  774. continue;
  775. }
  776. switch (id & WP_IDF_MASK) {
  777. case WP_ID_DECTERMS:
  778. if (size > MAX_TERMS) {
  779. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
  780. s->terms = 0;
  781. bytestream2_skip(&gb, ssize);
  782. continue;
  783. }
  784. s->terms = size;
  785. for (i = 0; i < s->terms; i++) {
  786. uint8_t val = bytestream2_get_byte(&gb);
  787. s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
  788. s->decorr[s->terms - i - 1].delta = val >> 5;
  789. }
  790. got_terms = 1;
  791. break;
  792. case WP_ID_DECWEIGHTS:
  793. if (!got_terms) {
  794. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  795. continue;
  796. }
  797. weights = size >> s->stereo_in;
  798. if (weights > MAX_TERMS || weights > s->terms) {
  799. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
  800. bytestream2_skip(&gb, ssize);
  801. continue;
  802. }
  803. for (i = 0; i < weights; i++) {
  804. t = (int8_t)bytestream2_get_byte(&gb);
  805. s->decorr[s->terms - i - 1].weightA = t << 3;
  806. if (s->decorr[s->terms - i - 1].weightA > 0)
  807. s->decorr[s->terms - i - 1].weightA +=
  808. (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
  809. if (s->stereo_in) {
  810. t = (int8_t)bytestream2_get_byte(&gb);
  811. s->decorr[s->terms - i - 1].weightB = t << 3;
  812. if (s->decorr[s->terms - i - 1].weightB > 0)
  813. s->decorr[s->terms - i - 1].weightB +=
  814. (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
  815. }
  816. }
  817. got_weights = 1;
  818. break;
  819. case WP_ID_DECSAMPLES:
  820. if (!got_terms) {
  821. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  822. continue;
  823. }
  824. t = 0;
  825. for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
  826. if (s->decorr[i].value > 8) {
  827. s->decorr[i].samplesA[0] =
  828. wp_exp2(bytestream2_get_le16(&gb));
  829. s->decorr[i].samplesA[1] =
  830. wp_exp2(bytestream2_get_le16(&gb));
  831. if (s->stereo_in) {
  832. s->decorr[i].samplesB[0] =
  833. wp_exp2(bytestream2_get_le16(&gb));
  834. s->decorr[i].samplesB[1] =
  835. wp_exp2(bytestream2_get_le16(&gb));
  836. t += 4;
  837. }
  838. t += 4;
  839. } else if (s->decorr[i].value < 0) {
  840. s->decorr[i].samplesA[0] =
  841. wp_exp2(bytestream2_get_le16(&gb));
  842. s->decorr[i].samplesB[0] =
  843. wp_exp2(bytestream2_get_le16(&gb));
  844. t += 4;
  845. } else {
  846. for (j = 0; j < s->decorr[i].value; j++) {
  847. s->decorr[i].samplesA[j] =
  848. wp_exp2(bytestream2_get_le16(&gb));
  849. if (s->stereo_in) {
  850. s->decorr[i].samplesB[j] =
  851. wp_exp2(bytestream2_get_le16(&gb));
  852. }
  853. }
  854. t += s->decorr[i].value * 2 * (s->stereo_in + 1);
  855. }
  856. }
  857. got_samples = 1;
  858. break;
  859. case WP_ID_ENTROPY:
  860. if (size != 6 * (s->stereo_in + 1)) {
  861. av_log(avctx, AV_LOG_ERROR,
  862. "Entropy vars size should be %i, got %i",
  863. 6 * (s->stereo_in + 1), size);
  864. bytestream2_skip(&gb, ssize);
  865. continue;
  866. }
  867. for (j = 0; j <= s->stereo_in; j++)
  868. for (i = 0; i < 3; i++) {
  869. s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
  870. }
  871. got_entropy = 1;
  872. break;
  873. case WP_ID_HYBRID:
  874. if (s->hybrid_bitrate) {
  875. for (i = 0; i <= s->stereo_in; i++) {
  876. s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
  877. size -= 2;
  878. }
  879. }
  880. for (i = 0; i < (s->stereo_in + 1); i++) {
  881. s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
  882. size -= 2;
  883. }
  884. if (size > 0) {
  885. for (i = 0; i < (s->stereo_in + 1); i++) {
  886. s->ch[i].bitrate_delta =
  887. wp_exp2((int16_t)bytestream2_get_le16(&gb));
  888. }
  889. } else {
  890. for (i = 0; i < (s->stereo_in + 1); i++)
  891. s->ch[i].bitrate_delta = 0;
  892. }
  893. got_hybrid = 1;
  894. break;
  895. case WP_ID_INT32INFO: {
  896. uint8_t val[4];
  897. if (size != 4) {
  898. av_log(avctx, AV_LOG_ERROR,
  899. "Invalid INT32INFO, size = %i\n",
  900. size);
  901. bytestream2_skip(&gb, ssize - 4);
  902. continue;
  903. }
  904. bytestream2_get_buffer(&gb, val, 4);
  905. if (val[0]) {
  906. s->extra_bits = val[0];
  907. } else if (val[1]) {
  908. s->shift = val[1];
  909. } else if (val[2]) {
  910. s->and = s->or = 1;
  911. s->shift = val[2];
  912. } else if (val[3]) {
  913. s->and = 1;
  914. s->shift = val[3];
  915. }
  916. /* original WavPack decoder forces 32-bit lossy sound to be treated
  917. * as 24-bit one in order to have proper clipping */
  918. if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
  919. s->post_shift += 8;
  920. s->shift -= 8;
  921. s->hybrid_maxclip >>= 8;
  922. s->hybrid_minclip >>= 8;
  923. }
  924. break;
  925. }
  926. case WP_ID_FLOATINFO:
  927. if (size != 4) {
  928. av_log(avctx, AV_LOG_ERROR,
  929. "Invalid FLOATINFO, size = %i\n", size);
  930. bytestream2_skip(&gb, ssize);
  931. continue;
  932. }
  933. s->float_flag = bytestream2_get_byte(&gb);
  934. s->float_shift = bytestream2_get_byte(&gb);
  935. s->float_max_exp = bytestream2_get_byte(&gb);
  936. got_float = 1;
  937. bytestream2_skip(&gb, 1);
  938. break;
  939. case WP_ID_DATA:
  940. s->sc.offset = bytestream2_tell(&gb);
  941. s->sc.size = size * 8;
  942. init_get_bits(&s->gb, gb.buffer, size * 8);
  943. s->data_size = size * 8;
  944. bytestream2_skip(&gb, size);
  945. got_bs = 1;
  946. break;
  947. case WP_ID_EXTRABITS:
  948. if (size <= 4) {
  949. av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
  950. size);
  951. bytestream2_skip(&gb, size);
  952. continue;
  953. }
  954. s->extra_sc.offset = bytestream2_tell(&gb);
  955. s->extra_sc.size = size * 8;
  956. init_get_bits(&s->gb_extra_bits, gb.buffer, size * 8);
  957. s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
  958. bytestream2_skip(&gb, size);
  959. s->got_extra_bits = 1;
  960. break;
  961. case WP_ID_CHANINFO:
  962. if (size <= 1) {
  963. av_log(avctx, AV_LOG_ERROR,
  964. "Insufficient channel information\n");
  965. return AVERROR_INVALIDDATA;
  966. }
  967. chan = bytestream2_get_byte(&gb);
  968. switch (size - 2) {
  969. case 0:
  970. chmask = bytestream2_get_byte(&gb);
  971. break;
  972. case 1:
  973. chmask = bytestream2_get_le16(&gb);
  974. break;
  975. case 2:
  976. chmask = bytestream2_get_le24(&gb);
  977. break;
  978. case 3:
  979. chmask = bytestream2_get_le32(&gb);;
  980. break;
  981. case 5:
  982. bytestream2_skip(&gb, 1);
  983. chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
  984. chmask = bytestream2_get_le16(&gb);
  985. break;
  986. default:
  987. av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
  988. size);
  989. chan = avctx->channels;
  990. chmask = avctx->channel_layout;
  991. }
  992. if (chan != avctx->channels) {
  993. av_log(avctx, AV_LOG_ERROR,
  994. "Block reports total %d channels, "
  995. "decoder believes it's %d channels\n",
  996. chan, avctx->channels);
  997. return AVERROR_INVALIDDATA;
  998. }
  999. if (!avctx->channel_layout)
  1000. avctx->channel_layout = chmask;
  1001. break;
  1002. default:
  1003. bytestream2_skip(&gb, size);
  1004. }
  1005. if (id & WP_IDF_ODD)
  1006. bytestream2_skip(&gb, 1);
  1007. }
  1008. if (!got_terms) {
  1009. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
  1010. return AVERROR_INVALIDDATA;
  1011. }
  1012. if (!got_weights) {
  1013. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
  1014. return AVERROR_INVALIDDATA;
  1015. }
  1016. if (!got_samples) {
  1017. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
  1018. return AVERROR_INVALIDDATA;
  1019. }
  1020. if (!got_entropy) {
  1021. av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
  1022. return AVERROR_INVALIDDATA;
  1023. }
  1024. if (s->hybrid && !got_hybrid) {
  1025. av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
  1026. return AVERROR_INVALIDDATA;
  1027. }
  1028. if (!got_bs) {
  1029. av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
  1030. return AVERROR_INVALIDDATA;
  1031. }
  1032. if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
  1033. av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
  1034. return AVERROR_INVALIDDATA;
  1035. }
  1036. if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
  1037. const int size = get_bits_left(&s->gb_extra_bits);
  1038. const int wanted = s->samples * s->extra_bits << s->stereo_in;
  1039. if (size < wanted) {
  1040. av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
  1041. s->got_extra_bits = 0;
  1042. }
  1043. }
  1044. if (s->stereo_in) {
  1045. if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
  1046. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
  1047. else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1048. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
  1049. else
  1050. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
  1051. if (samplecount < 0)
  1052. return samplecount;
  1053. samplecount >>= 1;
  1054. } else {
  1055. const int channel_stride = avctx->channels;
  1056. if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
  1057. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
  1058. else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1059. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
  1060. else
  1061. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
  1062. if (samplecount < 0)
  1063. return samplecount;
  1064. if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
  1065. int16_t *dst = (int16_t *)samples + 1;
  1066. int16_t *src = (int16_t *)samples;
  1067. int cnt = samplecount;
  1068. while (cnt--) {
  1069. *dst = *src;
  1070. src += channel_stride;
  1071. dst += channel_stride;
  1072. }
  1073. } else if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
  1074. int32_t *dst = (int32_t *)samples + 1;
  1075. int32_t *src = (int32_t *)samples;
  1076. int cnt = samplecount;
  1077. while (cnt--) {
  1078. *dst = *src;
  1079. src += channel_stride;
  1080. dst += channel_stride;
  1081. }
  1082. } else if (s->stereo) {
  1083. float *dst = (float *)samples + 1;
  1084. float *src = (float *)samples;
  1085. int cnt = samplecount;
  1086. while (cnt--) {
  1087. *dst = *src;
  1088. src += channel_stride;
  1089. dst += channel_stride;
  1090. }
  1091. }
  1092. }
  1093. *got_frame_ptr = 1;
  1094. return samplecount * bpp;
  1095. }
  1096. static void wavpack_decode_flush(AVCodecContext *avctx)
  1097. {
  1098. WavpackContext *s = avctx->priv_data;
  1099. int i;
  1100. for (i = 0; i < s->fdec_num; i++)
  1101. wv_reset_saved_context(s->fdec[i]);
  1102. }
  1103. static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
  1104. int *got_frame_ptr, AVPacket *avpkt)
  1105. {
  1106. WavpackContext *s = avctx->priv_data;
  1107. const uint8_t *buf = avpkt->data;
  1108. int buf_size = avpkt->size;
  1109. AVFrame *frame = data;
  1110. int frame_size, ret, frame_flags;
  1111. int samplecount = 0;
  1112. if (avpkt->size < 12 + s->multichannel * 4)
  1113. return AVERROR_INVALIDDATA;
  1114. s->block = 0;
  1115. s->ch_offset = 0;
  1116. /* determine number of samples */
  1117. if (s->mkv_mode) {
  1118. s->samples = AV_RL32(buf);
  1119. buf += 4;
  1120. frame_flags = AV_RL32(buf);
  1121. } else {
  1122. if (s->multichannel) {
  1123. s->samples = AV_RL32(buf + 4);
  1124. frame_flags = AV_RL32(buf + 8);
  1125. } else {
  1126. s->samples = AV_RL32(buf);
  1127. frame_flags = AV_RL32(buf + 4);
  1128. }
  1129. }
  1130. if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
  1131. av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
  1132. s->samples);
  1133. return AVERROR_INVALIDDATA;
  1134. }
  1135. if (frame_flags & 0x80) {
  1136. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  1137. } else if ((frame_flags & 0x03) <= 1) {
  1138. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  1139. } else {
  1140. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  1141. avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
  1142. }
  1143. /* get output buffer */
  1144. frame->nb_samples = s->samples + 1;
  1145. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  1146. return ret;
  1147. frame->nb_samples = s->samples;
  1148. while (buf_size > 0) {
  1149. if (!s->multichannel) {
  1150. frame_size = buf_size;
  1151. } else {
  1152. if (!s->mkv_mode) {
  1153. frame_size = AV_RL32(buf) - 12;
  1154. buf += 4;
  1155. buf_size -= 4;
  1156. } else {
  1157. if (buf_size < 12) // MKV files can have zero flags after last block
  1158. break;
  1159. frame_size = AV_RL32(buf + 8) + 12;
  1160. }
  1161. }
  1162. if (frame_size < 0 || frame_size > buf_size) {
  1163. av_log(avctx, AV_LOG_ERROR,
  1164. "Block %d has invalid size (size %d vs. %d bytes left)\n",
  1165. s->block, frame_size, buf_size);
  1166. wavpack_decode_flush(avctx);
  1167. return AVERROR_INVALIDDATA;
  1168. }
  1169. if ((samplecount = wavpack_decode_block(avctx, s->block,
  1170. frame->data[0], got_frame_ptr,
  1171. buf, frame_size)) < 0) {
  1172. wavpack_decode_flush(avctx);
  1173. return samplecount;
  1174. }
  1175. s->block++;
  1176. buf += frame_size;
  1177. buf_size -= frame_size;
  1178. }
  1179. return avpkt->size;
  1180. }
  1181. AVCodec ff_wavpack_decoder = {
  1182. .name = "wavpack",
  1183. .type = AVMEDIA_TYPE_AUDIO,
  1184. .id = AV_CODEC_ID_WAVPACK,
  1185. .priv_data_size = sizeof(WavpackContext),
  1186. .init = wavpack_decode_init,
  1187. .close = wavpack_decode_end,
  1188. .decode = wavpack_decode_frame,
  1189. .flush = wavpack_decode_flush,
  1190. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
  1191. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  1192. };