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.

1274 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. /**
  28. * @file
  29. * WavPack lossless audio decoder
  30. */
  31. #define WV_MONO 0x00000004
  32. #define WV_JOINT_STEREO 0x00000010
  33. #define WV_FALSE_STEREO 0x40000000
  34. #define WV_HYBRID_MODE 0x00000008
  35. #define WV_HYBRID_SHAPE 0x00000008
  36. #define WV_HYBRID_BITRATE 0x00000200
  37. #define WV_HYBRID_BALANCE 0x00000400
  38. #define WV_FLT_SHIFT_ONES 0x01
  39. #define WV_FLT_SHIFT_SAME 0x02
  40. #define WV_FLT_SHIFT_SENT 0x04
  41. #define WV_FLT_ZERO_SENT 0x08
  42. #define WV_FLT_ZERO_SIGN 0x10
  43. #define WV_MAX_SAMPLES 131072
  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. AVFrame frame;
  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. ret = base + get_tail(gb, add);
  338. if (get_bits_left(gb) <= 0)
  339. goto error;
  340. } else {
  341. int mid = (base * 2 + add + 1) >> 1;
  342. while (add > c->error_limit) {
  343. if (get_bits_left(gb) <= 0)
  344. goto error;
  345. if (get_bits1(gb)) {
  346. add -= (mid - base);
  347. base = mid;
  348. } else
  349. add = mid - base - 1;
  350. mid = (base * 2 + add + 1) >> 1;
  351. }
  352. ret = mid;
  353. }
  354. sign = get_bits1(gb);
  355. if (ctx->hybrid_bitrate)
  356. c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
  357. return sign ? ~ret : ret;
  358. error:
  359. *last = 1;
  360. return 0;
  361. }
  362. static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
  363. int S)
  364. {
  365. int bit;
  366. if (s->extra_bits){
  367. S <<= s->extra_bits;
  368. if (s->got_extra_bits && 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 && (s->float_flag & WV_FLT_SHIFT_SAME) &&
  414. get_bits1(&s->gb_extra_bits))) {
  415. S |= (1 << shift) - 1;
  416. } else if (s->got_extra_bits &&
  417. (s->float_flag & WV_FLT_SHIFT_SENT)) {
  418. S |= get_bits(&s->gb_extra_bits, shift);
  419. }
  420. }
  421. } else {
  422. exp = s->float_max_exp;
  423. }
  424. S &= 0x7fffff;
  425. } else {
  426. sign = 0;
  427. exp = 0;
  428. if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
  429. if (get_bits1(&s->gb_extra_bits)) {
  430. S = get_bits(&s->gb_extra_bits, 23);
  431. if (s->float_max_exp >= 25)
  432. exp = get_bits(&s->gb_extra_bits, 8);
  433. sign = get_bits1(&s->gb_extra_bits);
  434. } else {
  435. if (s->float_flag & WV_FLT_ZERO_SIGN)
  436. sign = get_bits1(&s->gb_extra_bits);
  437. }
  438. }
  439. }
  440. *crc = *crc * 27 + S * 9 + exp * 3 + sign;
  441. value.u = (sign << 31) | (exp << 23) | S;
  442. return value.f;
  443. }
  444. static void wv_reset_saved_context(WavpackFrameContext *s)
  445. {
  446. s->pos = 0;
  447. s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
  448. }
  449. static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
  450. uint32_t crc_extra_bits)
  451. {
  452. if (crc != s->CRC) {
  453. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  454. return AVERROR_INVALIDDATA;
  455. }
  456. if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
  457. av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
  458. return AVERROR_INVALIDDATA;
  459. }
  460. return 0;
  461. }
  462. static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
  463. void *dst, const int type)
  464. {
  465. int i, j, count = 0;
  466. int last, t;
  467. int A, B, L, L2, R, R2;
  468. int pos = s->pos;
  469. uint32_t crc = s->sc.crc;
  470. uint32_t crc_extra_bits = s->extra_sc.crc;
  471. int16_t *dst16 = dst;
  472. int32_t *dst32 = dst;
  473. float *dstfl = dst;
  474. const int channel_pad = s->avctx->channels - 2;
  475. s->one = s->zero = s->zeroes = 0;
  476. do {
  477. L = wv_get_value(s, gb, 0, &last);
  478. if (last)
  479. break;
  480. R = wv_get_value(s, gb, 1, &last);
  481. if (last)
  482. break;
  483. for (i = 0; i < s->terms; i++) {
  484. t = s->decorr[i].value;
  485. if (t > 0) {
  486. if (t > 8) {
  487. if (t & 1) {
  488. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  489. B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
  490. } else {
  491. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  492. B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
  493. }
  494. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  495. s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
  496. j = 0;
  497. } else {
  498. A = s->decorr[i].samplesA[pos];
  499. B = s->decorr[i].samplesB[pos];
  500. j = (pos + t) & 7;
  501. }
  502. if (type != AV_SAMPLE_FMT_S16) {
  503. L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  504. R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
  505. } else {
  506. L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
  507. R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
  508. }
  509. if (A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  510. if (B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
  511. s->decorr[i].samplesA[j] = L = L2;
  512. s->decorr[i].samplesB[j] = R = R2;
  513. } else if (t == -1) {
  514. if (type != AV_SAMPLE_FMT_S16)
  515. L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
  516. else
  517. L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
  518. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
  519. L = L2;
  520. if (type != AV_SAMPLE_FMT_S16)
  521. R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
  522. else
  523. R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
  524. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
  525. R = R2;
  526. s->decorr[i].samplesA[0] = R;
  527. } else {
  528. if (type != AV_SAMPLE_FMT_S16)
  529. R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
  530. else
  531. R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
  532. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
  533. R = R2;
  534. if (t == -3) {
  535. R2 = s->decorr[i].samplesA[0];
  536. s->decorr[i].samplesA[0] = R;
  537. }
  538. if (type != AV_SAMPLE_FMT_S16)
  539. L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
  540. else
  541. L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
  542. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
  543. L = L2;
  544. s->decorr[i].samplesB[0] = L;
  545. }
  546. }
  547. pos = (pos + 1) & 7;
  548. if (s->joint)
  549. L += (R -= (L >> 1));
  550. crc = (crc * 3 + L) * 3 + R;
  551. if (type == AV_SAMPLE_FMT_FLT) {
  552. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
  553. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
  554. dstfl += channel_pad;
  555. } else if (type == AV_SAMPLE_FMT_S32) {
  556. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
  557. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
  558. dst32 += channel_pad;
  559. } else {
  560. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
  561. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
  562. dst16 += channel_pad;
  563. }
  564. count++;
  565. } while (!last && count < s->samples);
  566. wv_reset_saved_context(s);
  567. if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
  568. wv_check_crc(s, crc, crc_extra_bits))
  569. return AVERROR_INVALIDDATA;
  570. return count * 2;
  571. }
  572. static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
  573. void *dst, const int type)
  574. {
  575. int i, j, count = 0;
  576. int last, t;
  577. int A, S, T;
  578. int pos = s->pos;
  579. uint32_t crc = s->sc.crc;
  580. uint32_t crc_extra_bits = s->extra_sc.crc;
  581. int16_t *dst16 = dst;
  582. int32_t *dst32 = dst;
  583. float *dstfl = dst;
  584. const int channel_stride = s->avctx->channels;
  585. s->one = s->zero = s->zeroes = 0;
  586. do {
  587. T = wv_get_value(s, gb, 0, &last);
  588. S = 0;
  589. if (last)
  590. break;
  591. for (i = 0; i < s->terms; i++) {
  592. t = s->decorr[i].value;
  593. if (t > 8) {
  594. if (t & 1)
  595. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  596. else
  597. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  598. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  599. j = 0;
  600. } else {
  601. A = s->decorr[i].samplesA[pos];
  602. j = (pos + t) & 7;
  603. }
  604. if (type != AV_SAMPLE_FMT_S16)
  605. S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  606. else
  607. S = T + ((s->decorr[i].weightA * A + 512) >> 10);
  608. if (A && T)
  609. s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  610. s->decorr[i].samplesA[j] = T = S;
  611. }
  612. pos = (pos + 1) & 7;
  613. crc = crc * 3 + S;
  614. if (type == AV_SAMPLE_FMT_FLT) {
  615. *dstfl = wv_get_value_float(s, &crc_extra_bits, S);
  616. dstfl += channel_stride;
  617. } else if (type == AV_SAMPLE_FMT_S32) {
  618. *dst32 = wv_get_value_integer(s, &crc_extra_bits, S);
  619. dst32 += channel_stride;
  620. } else {
  621. *dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
  622. dst16 += channel_stride;
  623. }
  624. count++;
  625. } while (!last && count < s->samples);
  626. wv_reset_saved_context(s);
  627. if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
  628. wv_check_crc(s, crc, crc_extra_bits))
  629. return AVERROR_INVALIDDATA;
  630. return count;
  631. }
  632. static av_cold int wv_alloc_frame_context(WavpackContext *c)
  633. {
  634. if (c->fdec_num == WV_MAX_FRAME_DECODERS)
  635. return -1;
  636. c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
  637. if (!c->fdec[c->fdec_num])
  638. return -1;
  639. c->fdec_num++;
  640. c->fdec[c->fdec_num - 1]->avctx = c->avctx;
  641. wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
  642. return 0;
  643. }
  644. static av_cold int wavpack_decode_init(AVCodecContext *avctx)
  645. {
  646. WavpackContext *s = avctx->priv_data;
  647. s->avctx = avctx;
  648. if (avctx->bits_per_coded_sample <= 16)
  649. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  650. else
  651. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  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. avcodec_get_frame_defaults(&s->frame);
  666. avctx->coded_frame = &s->frame;
  667. return 0;
  668. }
  669. static av_cold int wavpack_decode_end(AVCodecContext *avctx)
  670. {
  671. WavpackContext *s = avctx->priv_data;
  672. int i;
  673. for (i = 0; i < s->fdec_num; i++)
  674. av_freep(&s->fdec[i]);
  675. s->fdec_num = 0;
  676. return 0;
  677. }
  678. static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
  679. void *data, int *got_frame_ptr,
  680. const uint8_t *buf, int buf_size)
  681. {
  682. WavpackContext *wc = avctx->priv_data;
  683. WavpackFrameContext *s;
  684. void *samples = data;
  685. int samplecount;
  686. int got_terms = 0, got_weights = 0, got_samples = 0,
  687. got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
  688. const uint8_t *orig_buf = buf;
  689. const uint8_t *buf_end = buf + buf_size;
  690. int i, j, id, size, ssize, weights, t;
  691. int bpp, chan, chmask, orig_bpp;
  692. if (buf_size == 0) {
  693. *got_frame_ptr = 0;
  694. return 0;
  695. }
  696. if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
  697. av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
  698. return -1;
  699. }
  700. s = wc->fdec[block_no];
  701. if (!s) {
  702. av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);
  703. return -1;
  704. }
  705. if (wc->ch_offset >= avctx->channels) {
  706. av_log(avctx, AV_LOG_ERROR, "too many channels\n");
  707. return -1;
  708. }
  709. memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
  710. memset(s->ch, 0, sizeof(s->ch));
  711. s->extra_bits = 0;
  712. s->and = s->or = s->shift = 0;
  713. s->got_extra_bits = 0;
  714. if (!wc->mkv_mode) {
  715. s->samples = AV_RL32(buf); buf += 4;
  716. if (!s->samples) {
  717. *got_frame_ptr = 0;
  718. return 0;
  719. }
  720. if (s->samples > wc->samples) {
  721. av_log(avctx, AV_LOG_ERROR, "too many samples in block");
  722. return -1;
  723. }
  724. } else {
  725. s->samples = wc->samples;
  726. }
  727. s->frame_flags = AV_RL32(buf); buf += 4;
  728. bpp = av_get_bytes_per_sample(avctx->sample_fmt);
  729. samples = (uint8_t*)samples + bpp * wc->ch_offset;
  730. orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
  731. s->stereo = !(s->frame_flags & WV_MONO);
  732. s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
  733. s->joint = s->frame_flags & WV_JOINT_STEREO;
  734. s->hybrid = s->frame_flags & WV_HYBRID_MODE;
  735. s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
  736. s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
  737. s->hybrid_maxclip = (( 1LL << (orig_bpp - 1)) - 1);
  738. s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
  739. s->CRC = AV_RL32(buf); buf += 4;
  740. if (wc->mkv_mode)
  741. buf += 4; //skip block size;
  742. wc->ch_offset += 1 + s->stereo;
  743. // parse metadata blocks
  744. while (buf < buf_end) {
  745. id = *buf++;
  746. size = *buf++;
  747. if (id & WP_IDF_LONG) {
  748. size |= (*buf++) << 8;
  749. size |= (*buf++) << 16;
  750. }
  751. size <<= 1; // size is specified in words
  752. ssize = size;
  753. if (id & WP_IDF_ODD)
  754. size--;
  755. if (size < 0) {
  756. av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
  757. break;
  758. }
  759. if (buf + ssize > buf_end) {
  760. av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
  761. break;
  762. }
  763. if (id & WP_IDF_IGNORE) {
  764. buf += ssize;
  765. continue;
  766. }
  767. switch (id & WP_IDF_MASK) {
  768. case WP_ID_DECTERMS:
  769. if (size > MAX_TERMS) {
  770. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
  771. s->terms = 0;
  772. buf += ssize;
  773. continue;
  774. }
  775. s->terms = size;
  776. for (i = 0; i < s->terms; i++) {
  777. s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
  778. s->decorr[s->terms - i - 1].delta = *buf >> 5;
  779. buf++;
  780. }
  781. got_terms = 1;
  782. break;
  783. case WP_ID_DECWEIGHTS:
  784. if (!got_terms) {
  785. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  786. continue;
  787. }
  788. weights = size >> s->stereo_in;
  789. if (weights > MAX_TERMS || weights > s->terms) {
  790. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
  791. buf += ssize;
  792. continue;
  793. }
  794. for (i = 0; i < weights; i++) {
  795. t = (int8_t)(*buf++);
  796. s->decorr[s->terms - i - 1].weightA = t << 3;
  797. if (s->decorr[s->terms - i - 1].weightA > 0)
  798. s->decorr[s->terms - i - 1].weightA +=
  799. (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
  800. if (s->stereo_in) {
  801. t = (int8_t)(*buf++);
  802. s->decorr[s->terms - i - 1].weightB = t << 3;
  803. if (s->decorr[s->terms - i - 1].weightB > 0)
  804. s->decorr[s->terms - i - 1].weightB +=
  805. (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
  806. }
  807. }
  808. got_weights = 1;
  809. break;
  810. case WP_ID_DECSAMPLES:
  811. if (!got_terms) {
  812. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  813. continue;
  814. }
  815. t = 0;
  816. for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
  817. if (s->decorr[i].value > 8) {
  818. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  819. s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
  820. if (s->stereo_in) {
  821. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  822. s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
  823. t += 4;
  824. }
  825. t += 4;
  826. } else if (s->decorr[i].value < 0) {
  827. s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  828. s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
  829. t += 4;
  830. } else {
  831. for (j = 0; j < s->decorr[i].value; j++) {
  832. s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
  833. if (s->stereo_in) {
  834. s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
  835. }
  836. }
  837. t += s->decorr[i].value * 2 * (s->stereo_in + 1);
  838. }
  839. }
  840. got_samples = 1;
  841. break;
  842. case WP_ID_ENTROPY:
  843. if (size != 6 * (s->stereo_in + 1)) {
  844. av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, "
  845. "got %i", 6 * (s->stereo_in + 1), size);
  846. buf += ssize;
  847. continue;
  848. }
  849. for (j = 0; j <= s->stereo_in; j++) {
  850. for (i = 0; i < 3; i++) {
  851. s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
  852. buf += 2;
  853. }
  854. }
  855. got_entropy = 1;
  856. break;
  857. case WP_ID_HYBRID:
  858. if (s->hybrid_bitrate) {
  859. for (i = 0; i <= s->stereo_in; i++) {
  860. s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
  861. buf += 2;
  862. size -= 2;
  863. }
  864. }
  865. for (i = 0; i < (s->stereo_in + 1); i++) {
  866. s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
  867. buf += 2;
  868. size -= 2;
  869. }
  870. if (size > 0) {
  871. for (i = 0; i < (s->stereo_in + 1); i++) {
  872. s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
  873. buf += 2;
  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. if (size != 4) {
  883. av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
  884. buf += ssize;
  885. continue;
  886. }
  887. if (buf[0])
  888. s->extra_bits = buf[0];
  889. else if (buf[1])
  890. s->shift = buf[1];
  891. else if (buf[2]){
  892. s->and = s->or = 1;
  893. s->shift = buf[2];
  894. } else if(buf[3]) {
  895. s->and = 1;
  896. s->shift = buf[3];
  897. }
  898. /* original WavPack decoder forces 32-bit lossy sound to be treated
  899. * as 24-bit one in order to have proper clipping
  900. */
  901. if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
  902. s->post_shift += 8;
  903. s->shift -= 8;
  904. s->hybrid_maxclip >>= 8;
  905. s->hybrid_minclip >>= 8;
  906. }
  907. buf += 4;
  908. break;
  909. case WP_ID_FLOATINFO:
  910. if (size != 4) {
  911. av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
  912. buf += ssize;
  913. continue;
  914. }
  915. s->float_flag = buf[0];
  916. s->float_shift = buf[1];
  917. s->float_max_exp = buf[2];
  918. buf += 4;
  919. got_float = 1;
  920. break;
  921. case WP_ID_DATA:
  922. s->sc.offset = buf - orig_buf;
  923. s->sc.size = size * 8;
  924. init_get_bits(&s->gb, buf, size * 8);
  925. s->data_size = size * 8;
  926. buf += size;
  927. got_bs = 1;
  928. break;
  929. case WP_ID_EXTRABITS:
  930. if (size <= 4) {
  931. av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
  932. size);
  933. buf += size;
  934. continue;
  935. }
  936. s->extra_sc.offset = buf - orig_buf;
  937. s->extra_sc.size = size * 8;
  938. init_get_bits(&s->gb_extra_bits, buf, size * 8);
  939. s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
  940. buf += size;
  941. s->got_extra_bits = 1;
  942. break;
  943. case WP_ID_CHANINFO:
  944. if (size <= 1) {
  945. av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");
  946. return -1;
  947. }
  948. chan = *buf++;
  949. switch (size - 2) {
  950. case 0: chmask = *buf; break;
  951. case 1: chmask = AV_RL16(buf); break;
  952. case 2: chmask = AV_RL24(buf); break;
  953. case 3: chmask = AV_RL32(buf); break;
  954. case 5:
  955. chan |= (buf[1] & 0xF) << 8;
  956. chmask = AV_RL24(buf + 2);
  957. break;
  958. default:
  959. av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
  960. size);
  961. chan = avctx->channels;
  962. chmask = avctx->channel_layout;
  963. }
  964. if (chan != avctx->channels) {
  965. av_log(avctx, AV_LOG_ERROR, "Block reports total %d channels, "
  966. "decoder believes it's %d channels\n", chan,
  967. avctx->channels);
  968. return -1;
  969. }
  970. if (!avctx->channel_layout)
  971. avctx->channel_layout = chmask;
  972. buf += size - 1;
  973. break;
  974. default:
  975. buf += size;
  976. }
  977. if (id & WP_IDF_ODD)
  978. buf++;
  979. }
  980. if (!got_terms) {
  981. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
  982. return -1;
  983. }
  984. if (!got_weights) {
  985. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
  986. return -1;
  987. }
  988. if (!got_samples) {
  989. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
  990. return -1;
  991. }
  992. if (!got_entropy) {
  993. av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
  994. return -1;
  995. }
  996. if (s->hybrid && !got_hybrid) {
  997. av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
  998. return -1;
  999. }
  1000. if (!got_bs) {
  1001. av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
  1002. return -1;
  1003. }
  1004. if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
  1005. av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
  1006. return -1;
  1007. }
  1008. if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
  1009. const int size = get_bits_left(&s->gb_extra_bits);
  1010. const int wanted = s->samples * s->extra_bits << s->stereo_in;
  1011. if (size < wanted) {
  1012. av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
  1013. s->got_extra_bits = 0;
  1014. }
  1015. }
  1016. if (s->stereo_in) {
  1017. if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
  1018. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
  1019. else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1020. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
  1021. else
  1022. samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
  1023. if (samplecount < 0)
  1024. return -1;
  1025. samplecount >>= 1;
  1026. } else {
  1027. const int channel_stride = avctx->channels;
  1028. if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
  1029. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
  1030. else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
  1031. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
  1032. else
  1033. samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
  1034. if (samplecount < 0)
  1035. return -1;
  1036. if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
  1037. int16_t *dst = (int16_t*)samples + 1;
  1038. int16_t *src = (int16_t*)samples;
  1039. int cnt = samplecount;
  1040. while (cnt--) {
  1041. *dst = *src;
  1042. src += channel_stride;
  1043. dst += channel_stride;
  1044. }
  1045. } else if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
  1046. int32_t *dst = (int32_t*)samples + 1;
  1047. int32_t *src = (int32_t*)samples;
  1048. int cnt = samplecount;
  1049. while (cnt--) {
  1050. *dst = *src;
  1051. src += channel_stride;
  1052. dst += channel_stride;
  1053. }
  1054. } else if (s->stereo) {
  1055. float *dst = (float*)samples + 1;
  1056. float *src = (float*)samples;
  1057. int cnt = samplecount;
  1058. while (cnt--) {
  1059. *dst = *src;
  1060. src += channel_stride;
  1061. dst += channel_stride;
  1062. }
  1063. }
  1064. }
  1065. *got_frame_ptr = 1;
  1066. return samplecount * bpp;
  1067. }
  1068. static void wavpack_decode_flush(AVCodecContext *avctx)
  1069. {
  1070. WavpackContext *s = avctx->priv_data;
  1071. int i;
  1072. for (i = 0; i < s->fdec_num; i++)
  1073. wv_reset_saved_context(s->fdec[i]);
  1074. }
  1075. static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
  1076. int *got_frame_ptr, AVPacket *avpkt)
  1077. {
  1078. WavpackContext *s = avctx->priv_data;
  1079. const uint8_t *buf = avpkt->data;
  1080. int buf_size = avpkt->size;
  1081. int frame_size, ret, frame_flags;
  1082. int samplecount = 0;
  1083. s->block = 0;
  1084. s->ch_offset = 0;
  1085. /* determine number of samples */
  1086. if (s->mkv_mode) {
  1087. s->samples = AV_RL32(buf); buf += 4;
  1088. frame_flags = AV_RL32(buf);
  1089. } else {
  1090. if (s->multichannel) {
  1091. s->samples = AV_RL32(buf + 4);
  1092. frame_flags = AV_RL32(buf + 8);
  1093. } else {
  1094. s->samples = AV_RL32(buf);
  1095. frame_flags = AV_RL32(buf + 4);
  1096. }
  1097. }
  1098. if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
  1099. av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
  1100. s->samples);
  1101. return AVERROR(EINVAL);
  1102. }
  1103. if (frame_flags & 0x80) {
  1104. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  1105. } else if ((frame_flags & 0x03) <= 1) {
  1106. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  1107. } else {
  1108. avctx->sample_fmt = AV_SAMPLE_FMT_S32;
  1109. avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
  1110. }
  1111. /* get output buffer */
  1112. s->frame.nb_samples = s->samples + 1;
  1113. if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
  1114. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1115. return ret;
  1116. }
  1117. s->frame.nb_samples = s->samples;
  1118. while (buf_size > 0) {
  1119. if (!s->multichannel) {
  1120. frame_size = buf_size;
  1121. } else {
  1122. if (!s->mkv_mode) {
  1123. frame_size = AV_RL32(buf) - 12; buf += 4; buf_size -= 4;
  1124. } else {
  1125. if (buf_size < 12) //MKV files can have zero flags after last block
  1126. break;
  1127. frame_size = AV_RL32(buf + 8) + 12;
  1128. }
  1129. }
  1130. if (frame_size < 0 || frame_size > buf_size) {
  1131. av_log(avctx, AV_LOG_ERROR, "Block %d has invalid size (size %d "
  1132. "vs. %d bytes left)\n", s->block, frame_size, buf_size);
  1133. wavpack_decode_flush(avctx);
  1134. return AVERROR_INVALIDDATA;
  1135. }
  1136. if ((samplecount = wavpack_decode_block(avctx, s->block,
  1137. s->frame.data[0], got_frame_ptr,
  1138. buf, frame_size)) < 0) {
  1139. wavpack_decode_flush(avctx);
  1140. return AVERROR_INVALIDDATA;
  1141. }
  1142. s->block++;
  1143. buf += frame_size; buf_size -= frame_size;
  1144. }
  1145. if (*got_frame_ptr)
  1146. *(AVFrame *)data = s->frame;
  1147. return avpkt->size;
  1148. }
  1149. AVCodec ff_wavpack_decoder = {
  1150. .name = "wavpack",
  1151. .type = AVMEDIA_TYPE_AUDIO,
  1152. .id = AV_CODEC_ID_WAVPACK,
  1153. .priv_data_size = sizeof(WavpackContext),
  1154. .init = wavpack_decode_init,
  1155. .close = wavpack_decode_end,
  1156. .decode = wavpack_decode_frame,
  1157. .flush = wavpack_decode_flush,
  1158. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
  1159. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  1160. };