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.

1147 lines
38KB

  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. #include "libavutil/channel_layout.h"
  22. #define BITSTREAM_READER_LE
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "get_bits.h"
  26. #include "internal.h"
  27. #include "thread.h"
  28. #include "unary.h"
  29. #include "wavpack.h"
  30. /**
  31. * @file
  32. * WavPack lossless audio decoder
  33. */
  34. typedef struct SavedContext {
  35. int offset;
  36. int size;
  37. int bits_used;
  38. uint32_t crc;
  39. } SavedContext;
  40. typedef struct WavpackFrameContext {
  41. AVCodecContext *avctx;
  42. int frame_flags;
  43. int stereo, stereo_in;
  44. int joint;
  45. uint32_t CRC;
  46. GetBitContext gb;
  47. int got_extra_bits;
  48. uint32_t crc_extra_bits;
  49. GetBitContext gb_extra_bits;
  50. int data_size; // in bits
  51. int samples;
  52. int terms;
  53. Decorr decorr[MAX_TERMS];
  54. int zero, one, zeroes;
  55. int extra_bits;
  56. int and, or, shift;
  57. int post_shift;
  58. int hybrid, hybrid_bitrate;
  59. int hybrid_maxclip, hybrid_minclip;
  60. int float_flag;
  61. int float_shift;
  62. int float_max_exp;
  63. WvChannel ch[2];
  64. int pos;
  65. SavedContext sc, extra_sc;
  66. } WavpackFrameContext;
  67. #define WV_MAX_FRAME_DECODERS 14
  68. typedef struct WavpackContext {
  69. AVCodecContext *avctx;
  70. WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
  71. int fdec_num;
  72. int block;
  73. int samples;
  74. int ch_offset;
  75. } WavpackContext;
  76. #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
  77. static av_always_inline int get_tail(GetBitContext *gb, int k)
  78. {
  79. int p, e, res;
  80. if (k < 1)
  81. return 0;
  82. p = av_log2(k);
  83. e = (1 << (p + 1)) - k - 1;
  84. res = get_bitsz(gb, p);
  85. if (res >= e)
  86. res = (res << 1) - e + get_bits1(gb);
  87. return res;
  88. }
  89. static int update_error_limit(WavpackFrameContext *ctx)
  90. {
  91. int i, br[2], sl[2];
  92. for (i = 0; i <= ctx->stereo_in; i++) {
  93. if (ctx->ch[i].bitrate_acc > UINT_MAX - ctx->ch[i].bitrate_delta)
  94. return AVERROR_INVALIDDATA;
  95. ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
  96. br[i] = ctx->ch[i].bitrate_acc >> 16;
  97. sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
  98. }
  99. if (ctx->stereo_in && ctx->hybrid_bitrate) {
  100. int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
  101. if (balance > br[0]) {
  102. br[1] = br[0] << 1;
  103. br[0] = 0;
  104. } else if (-balance > br[0]) {
  105. br[0] <<= 1;
  106. br[1] = 0;
  107. } else {
  108. br[1] = br[0] + balance;
  109. br[0] = br[0] - balance;
  110. }
  111. }
  112. for (i = 0; i <= ctx->stereo_in; i++) {
  113. if (ctx->hybrid_bitrate) {
  114. if (sl[i] - br[i] > -0x100)
  115. ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
  116. else
  117. ctx->ch[i].error_limit = 0;
  118. } else {
  119. ctx->ch[i].error_limit = wp_exp2(br[i]);
  120. }
  121. }
  122. return 0;
  123. }
  124. static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
  125. int channel, int *last)
  126. {
  127. int t, t2;
  128. int sign, base, add, ret;
  129. WvChannel *c = &ctx->ch[channel];
  130. *last = 0;
  131. if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
  132. !ctx->zero && !ctx->one) {
  133. if (ctx->zeroes) {
  134. ctx->zeroes--;
  135. if (ctx->zeroes) {
  136. c->slow_level -= LEVEL_DECAY(c->slow_level);
  137. return 0;
  138. }
  139. } else {
  140. t = get_unary_0_33(gb);
  141. if (t >= 2) {
  142. if (t >= 32 || get_bits_left(gb) < t - 1)
  143. goto error;
  144. t = get_bits_long(gb, t - 1) | (1 << (t - 1));
  145. } else {
  146. if (get_bits_left(gb) < 0)
  147. goto error;
  148. }
  149. ctx->zeroes = t;
  150. if (ctx->zeroes) {
  151. memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
  152. memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
  153. c->slow_level -= LEVEL_DECAY(c->slow_level);
  154. return 0;
  155. }
  156. }
  157. }
  158. if (ctx->zero) {
  159. t = 0;
  160. ctx->zero = 0;
  161. } else {
  162. t = get_unary_0_33(gb);
  163. if (get_bits_left(gb) < 0)
  164. goto error;
  165. if (t == 16) {
  166. t2 = get_unary_0_33(gb);
  167. if (t2 < 2) {
  168. if (get_bits_left(gb) < 0)
  169. goto error;
  170. t += t2;
  171. } else {
  172. if (get_bits_left(gb) < t2 - 1)
  173. goto error;
  174. t += get_bits_long(gb, t2 - 1) | (1 << (t2 - 1));
  175. }
  176. }
  177. if (ctx->one) {
  178. ctx->one = t & 1;
  179. t = (t >> 1) + 1;
  180. } else {
  181. ctx->one = t & 1;
  182. t >>= 1;
  183. }
  184. ctx->zero = !ctx->one;
  185. }
  186. if (ctx->hybrid && !channel) {
  187. if (update_error_limit(ctx) < 0)
  188. goto error;
  189. }
  190. if (!t) {
  191. base = 0;
  192. add = GET_MED(0) - 1;
  193. DEC_MED(0);
  194. } else if (t == 1) {
  195. base = GET_MED(0);
  196. add = GET_MED(1) - 1;
  197. INC_MED(0);
  198. DEC_MED(1);
  199. } else if (t == 2) {
  200. base = GET_MED(0) + GET_MED(1);
  201. add = GET_MED(2) - 1;
  202. INC_MED(0);
  203. INC_MED(1);
  204. DEC_MED(2);
  205. } else {
  206. base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
  207. add = GET_MED(2) - 1;
  208. INC_MED(0);
  209. INC_MED(1);
  210. INC_MED(2);
  211. }
  212. if (!c->error_limit) {
  213. if (add >= 0x2000000U) {
  214. av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
  215. goto error;
  216. }
  217. ret = base + get_tail(gb, add);
  218. if (get_bits_left(gb) <= 0)
  219. goto error;
  220. } else {
  221. int mid = (base * 2U + add + 1) >> 1;
  222. while (add > c->error_limit) {
  223. if (get_bits_left(gb) <= 0)
  224. goto error;
  225. if (get_bits1(gb)) {
  226. add -= (mid - base);
  227. base = mid;
  228. } else
  229. add = mid - base - 1;
  230. mid = (base * 2U + add + 1) >> 1;
  231. }
  232. ret = mid;
  233. }
  234. sign = get_bits1(gb);
  235. if (ctx->hybrid_bitrate)
  236. c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
  237. return sign ? ~ret : ret;
  238. error:
  239. ret = get_bits_left(gb);
  240. if (ret <= 0) {
  241. av_log(ctx->avctx, AV_LOG_ERROR, "Too few bits (%d) left\n", ret);
  242. }
  243. *last = 1;
  244. return 0;
  245. }
  246. static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
  247. unsigned S)
  248. {
  249. unsigned bit;
  250. if (s->extra_bits) {
  251. S *= 1 << s->extra_bits;
  252. if (s->got_extra_bits &&
  253. get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
  254. S |= get_bits_long(&s->gb_extra_bits, s->extra_bits);
  255. *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
  256. }
  257. }
  258. bit = (S & s->and) | s->or;
  259. bit = ((S + bit) << s->shift) - bit;
  260. if (s->hybrid)
  261. bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
  262. return bit << s->post_shift;
  263. }
  264. static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
  265. {
  266. union {
  267. float f;
  268. uint32_t u;
  269. } value;
  270. unsigned int sign;
  271. int exp = s->float_max_exp;
  272. if (s->got_extra_bits) {
  273. const int max_bits = 1 + 23 + 8 + 1;
  274. const int left_bits = get_bits_left(&s->gb_extra_bits);
  275. if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)
  276. return 0.0;
  277. }
  278. if (S) {
  279. S <<= s->float_shift;
  280. sign = S < 0;
  281. if (sign)
  282. S = -S;
  283. if (S >= 0x1000000) {
  284. if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
  285. S = get_bits(&s->gb_extra_bits, 23);
  286. else
  287. S = 0;
  288. exp = 255;
  289. } else if (exp) {
  290. int shift = 23 - av_log2(S);
  291. exp = s->float_max_exp;
  292. if (exp <= shift)
  293. shift = --exp;
  294. exp -= shift;
  295. if (shift) {
  296. S <<= shift;
  297. if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
  298. (s->got_extra_bits &&
  299. (s->float_flag & WV_FLT_SHIFT_SAME) &&
  300. get_bits1(&s->gb_extra_bits))) {
  301. S |= (1 << shift) - 1;
  302. } else if (s->got_extra_bits &&
  303. (s->float_flag & WV_FLT_SHIFT_SENT)) {
  304. S |= get_bits(&s->gb_extra_bits, shift);
  305. }
  306. }
  307. } else {
  308. exp = s->float_max_exp;
  309. }
  310. S &= 0x7fffff;
  311. } else {
  312. sign = 0;
  313. exp = 0;
  314. if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
  315. if (get_bits1(&s->gb_extra_bits)) {
  316. S = get_bits(&s->gb_extra_bits, 23);
  317. if (s->float_max_exp >= 25)
  318. exp = get_bits(&s->gb_extra_bits, 8);
  319. sign = get_bits1(&s->gb_extra_bits);
  320. } else {
  321. if (s->float_flag & WV_FLT_ZERO_SIGN)
  322. sign = get_bits1(&s->gb_extra_bits);
  323. }
  324. }
  325. }
  326. *crc = *crc * 27 + S * 9 + exp * 3 + sign;
  327. value.u = (sign << 31) | (exp << 23) | S;
  328. return value.f;
  329. }
  330. static void wv_reset_saved_context(WavpackFrameContext *s)
  331. {
  332. s->pos = 0;
  333. s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
  334. }
  335. static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
  336. uint32_t crc_extra_bits)
  337. {
  338. if (crc != s->CRC) {
  339. av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
  340. return AVERROR_INVALIDDATA;
  341. }
  342. if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
  343. av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
  344. return AVERROR_INVALIDDATA;
  345. }
  346. return 0;
  347. }
  348. static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
  349. void *dst_l, void *dst_r, const int type)
  350. {
  351. int i, j, count = 0;
  352. int last, t;
  353. int A, B, L, L2, R, R2;
  354. int pos = s->pos;
  355. uint32_t crc = s->sc.crc;
  356. uint32_t crc_extra_bits = s->extra_sc.crc;
  357. int16_t *dst16_l = dst_l;
  358. int16_t *dst16_r = dst_r;
  359. int32_t *dst32_l = dst_l;
  360. int32_t *dst32_r = dst_r;
  361. float *dstfl_l = dst_l;
  362. float *dstfl_r = dst_r;
  363. s->one = s->zero = s->zeroes = 0;
  364. do {
  365. L = wv_get_value(s, gb, 0, &last);
  366. if (last)
  367. break;
  368. R = wv_get_value(s, gb, 1, &last);
  369. if (last)
  370. break;
  371. for (i = 0; i < s->terms; i++) {
  372. t = s->decorr[i].value;
  373. if (t > 0) {
  374. if (t > 8) {
  375. if (t & 1) {
  376. A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  377. B = 2U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
  378. } else {
  379. A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  380. B = (int)(3U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
  381. }
  382. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  383. s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
  384. j = 0;
  385. } else {
  386. A = s->decorr[i].samplesA[pos];
  387. B = s->decorr[i].samplesB[pos];
  388. j = (pos + t) & 7;
  389. }
  390. if (type != AV_SAMPLE_FMT_S16P) {
  391. L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  392. R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
  393. } else {
  394. L2 = L + ((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
  395. R2 = R + ((int)(s->decorr[i].weightB * (unsigned)B + 512) >> 10);
  396. }
  397. if (A && L)
  398. s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  399. if (B && R)
  400. s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
  401. s->decorr[i].samplesA[j] = L = L2;
  402. s->decorr[i].samplesB[j] = R = R2;
  403. } else if (t == -1) {
  404. if (type != AV_SAMPLE_FMT_S16P)
  405. L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
  406. else
  407. L2 = L + ((int)(s->decorr[i].weightA * (unsigned)s->decorr[i].samplesA[0] + 512) >> 10);
  408. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
  409. L = L2;
  410. if (type != AV_SAMPLE_FMT_S16P)
  411. R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
  412. else
  413. R2 = R + ((int)(s->decorr[i].weightB * (unsigned)L2 + 512) >> 10);
  414. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
  415. R = R2;
  416. s->decorr[i].samplesA[0] = R;
  417. } else {
  418. if (type != AV_SAMPLE_FMT_S16P)
  419. R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
  420. else
  421. R2 = R + ((int)(s->decorr[i].weightB * (unsigned)s->decorr[i].samplesB[0] + 512) >> 10);
  422. UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
  423. R = R2;
  424. if (t == -3) {
  425. R2 = s->decorr[i].samplesA[0];
  426. s->decorr[i].samplesA[0] = R;
  427. }
  428. if (type != AV_SAMPLE_FMT_S16P)
  429. L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
  430. else
  431. L2 = L + ((int)(s->decorr[i].weightA * (unsigned)R2 + 512) >> 10);
  432. UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
  433. L = L2;
  434. s->decorr[i].samplesB[0] = L;
  435. }
  436. }
  437. if (type == AV_SAMPLE_FMT_S16P) {
  438. if (FFABS(L) + FFABS(R) > (1<<19)) {
  439. av_log(s->avctx, AV_LOG_ERROR, "sample %d %d too large\n", L, R);
  440. return AVERROR_INVALIDDATA;
  441. }
  442. }
  443. pos = (pos + 1) & 7;
  444. if (s->joint)
  445. L += (unsigned)(R -= (unsigned)(L >> 1));
  446. crc = (crc * 3 + L) * 3 + R;
  447. if (type == AV_SAMPLE_FMT_FLTP) {
  448. *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
  449. *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
  450. } else if (type == AV_SAMPLE_FMT_S32P) {
  451. *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
  452. *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
  453. } else {
  454. *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
  455. *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
  456. }
  457. count++;
  458. } while (!last && count < s->samples);
  459. wv_reset_saved_context(s);
  460. if (last && count < s->samples) {
  461. int size = av_get_bytes_per_sample(type);
  462. memset((uint8_t*)dst_l + count*size, 0, (s->samples-count)*size);
  463. memset((uint8_t*)dst_r + count*size, 0, (s->samples-count)*size);
  464. }
  465. if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
  466. wv_check_crc(s, crc, crc_extra_bits))
  467. return AVERROR_INVALIDDATA;
  468. return 0;
  469. }
  470. static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
  471. void *dst, const int type)
  472. {
  473. int i, j, count = 0;
  474. int last, t;
  475. int A, S, T;
  476. int pos = s->pos;
  477. uint32_t crc = s->sc.crc;
  478. uint32_t crc_extra_bits = s->extra_sc.crc;
  479. int16_t *dst16 = dst;
  480. int32_t *dst32 = dst;
  481. float *dstfl = dst;
  482. s->one = s->zero = s->zeroes = 0;
  483. do {
  484. T = wv_get_value(s, gb, 0, &last);
  485. S = 0;
  486. if (last)
  487. break;
  488. for (i = 0; i < s->terms; i++) {
  489. t = s->decorr[i].value;
  490. if (t > 8) {
  491. if (t & 1)
  492. A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
  493. else
  494. A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
  495. s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
  496. j = 0;
  497. } else {
  498. A = s->decorr[i].samplesA[pos];
  499. j = (pos + t) & 7;
  500. }
  501. if (type != AV_SAMPLE_FMT_S16P)
  502. S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
  503. else
  504. S = T + ((s->decorr[i].weightA * A + 512) >> 10);
  505. if (A && T)
  506. s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
  507. s->decorr[i].samplesA[j] = T = S;
  508. }
  509. pos = (pos + 1) & 7;
  510. crc = crc * 3 + S;
  511. if (type == AV_SAMPLE_FMT_FLTP) {
  512. *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
  513. } else if (type == AV_SAMPLE_FMT_S32P) {
  514. *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
  515. } else {
  516. *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
  517. }
  518. count++;
  519. } while (!last && count < s->samples);
  520. wv_reset_saved_context(s);
  521. if (last && count < s->samples) {
  522. int size = av_get_bytes_per_sample(type);
  523. memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
  524. }
  525. if (s->avctx->err_recognition & AV_EF_CRCCHECK) {
  526. int ret = wv_check_crc(s, crc, crc_extra_bits);
  527. if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
  528. return ret;
  529. }
  530. return 0;
  531. }
  532. static av_cold int wv_alloc_frame_context(WavpackContext *c)
  533. {
  534. if (c->fdec_num == WV_MAX_FRAME_DECODERS)
  535. return -1;
  536. c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
  537. if (!c->fdec[c->fdec_num])
  538. return -1;
  539. c->fdec_num++;
  540. c->fdec[c->fdec_num - 1]->avctx = c->avctx;
  541. wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
  542. return 0;
  543. }
  544. #if HAVE_THREADS
  545. static int init_thread_copy(AVCodecContext *avctx)
  546. {
  547. WavpackContext *s = avctx->priv_data;
  548. s->avctx = avctx;
  549. return 0;
  550. }
  551. #endif
  552. static av_cold int wavpack_decode_init(AVCodecContext *avctx)
  553. {
  554. WavpackContext *s = avctx->priv_data;
  555. s->avctx = avctx;
  556. s->fdec_num = 0;
  557. return 0;
  558. }
  559. static av_cold int wavpack_decode_end(AVCodecContext *avctx)
  560. {
  561. WavpackContext *s = avctx->priv_data;
  562. int i;
  563. for (i = 0; i < s->fdec_num; i++)
  564. av_freep(&s->fdec[i]);
  565. s->fdec_num = 0;
  566. return 0;
  567. }
  568. static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
  569. AVFrame *frame, const uint8_t *buf, int buf_size)
  570. {
  571. WavpackContext *wc = avctx->priv_data;
  572. ThreadFrame tframe = { .f = frame };
  573. WavpackFrameContext *s;
  574. GetByteContext gb;
  575. void *samples_l = NULL, *samples_r = NULL;
  576. int ret;
  577. int got_terms = 0, got_weights = 0, got_samples = 0,
  578. got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
  579. int i, j, id, size, ssize, weights, t;
  580. int bpp, chan = 0, chmask = 0, orig_bpp, sample_rate = 0;
  581. int multiblock;
  582. if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
  583. av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
  584. return AVERROR_INVALIDDATA;
  585. }
  586. s = wc->fdec[block_no];
  587. if (!s) {
  588. av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
  589. block_no);
  590. return AVERROR_INVALIDDATA;
  591. }
  592. memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
  593. memset(s->ch, 0, sizeof(s->ch));
  594. s->extra_bits = 0;
  595. s->and = s->or = s->shift = 0;
  596. s->got_extra_bits = 0;
  597. bytestream2_init(&gb, buf, buf_size);
  598. s->samples = bytestream2_get_le32(&gb);
  599. if (s->samples != wc->samples) {
  600. av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
  601. "a sequence: %d and %d\n", wc->samples, s->samples);
  602. return AVERROR_INVALIDDATA;
  603. }
  604. s->frame_flags = bytestream2_get_le32(&gb);
  605. bpp = av_get_bytes_per_sample(avctx->sample_fmt);
  606. orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
  607. multiblock = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
  608. s->stereo = !(s->frame_flags & WV_MONO);
  609. s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
  610. s->joint = s->frame_flags & WV_JOINT_STEREO;
  611. s->hybrid = s->frame_flags & WV_HYBRID_MODE;
  612. s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
  613. s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
  614. if (s->post_shift < 0 || s->post_shift > 31) {
  615. return AVERROR_INVALIDDATA;
  616. }
  617. s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
  618. s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
  619. s->CRC = bytestream2_get_le32(&gb);
  620. // parse metadata blocks
  621. while (bytestream2_get_bytes_left(&gb)) {
  622. id = bytestream2_get_byte(&gb);
  623. size = bytestream2_get_byte(&gb);
  624. if (id & WP_IDF_LONG) {
  625. size |= (bytestream2_get_byte(&gb)) << 8;
  626. size |= (bytestream2_get_byte(&gb)) << 16;
  627. }
  628. size <<= 1; // size is specified in words
  629. ssize = size;
  630. if (id & WP_IDF_ODD)
  631. size--;
  632. if (size < 0) {
  633. av_log(avctx, AV_LOG_ERROR,
  634. "Got incorrect block %02X with size %i\n", id, size);
  635. break;
  636. }
  637. if (bytestream2_get_bytes_left(&gb) < ssize) {
  638. av_log(avctx, AV_LOG_ERROR,
  639. "Block size %i is out of bounds\n", size);
  640. break;
  641. }
  642. switch (id & WP_IDF_MASK) {
  643. case WP_ID_DECTERMS:
  644. if (size > MAX_TERMS) {
  645. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
  646. s->terms = 0;
  647. bytestream2_skip(&gb, ssize);
  648. continue;
  649. }
  650. s->terms = size;
  651. for (i = 0; i < s->terms; i++) {
  652. uint8_t val = bytestream2_get_byte(&gb);
  653. s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
  654. s->decorr[s->terms - i - 1].delta = val >> 5;
  655. }
  656. got_terms = 1;
  657. break;
  658. case WP_ID_DECWEIGHTS:
  659. if (!got_terms) {
  660. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  661. continue;
  662. }
  663. weights = size >> s->stereo_in;
  664. if (weights > MAX_TERMS || weights > s->terms) {
  665. av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
  666. bytestream2_skip(&gb, ssize);
  667. continue;
  668. }
  669. for (i = 0; i < weights; i++) {
  670. t = (int8_t)bytestream2_get_byte(&gb);
  671. s->decorr[s->terms - i - 1].weightA = t * (1 << 3);
  672. if (s->decorr[s->terms - i - 1].weightA > 0)
  673. s->decorr[s->terms - i - 1].weightA +=
  674. (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
  675. if (s->stereo_in) {
  676. t = (int8_t)bytestream2_get_byte(&gb);
  677. s->decorr[s->terms - i - 1].weightB = t * (1 << 3);
  678. if (s->decorr[s->terms - i - 1].weightB > 0)
  679. s->decorr[s->terms - i - 1].weightB +=
  680. (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
  681. }
  682. }
  683. got_weights = 1;
  684. break;
  685. case WP_ID_DECSAMPLES:
  686. if (!got_terms) {
  687. av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
  688. continue;
  689. }
  690. t = 0;
  691. for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
  692. if (s->decorr[i].value > 8) {
  693. s->decorr[i].samplesA[0] =
  694. wp_exp2(bytestream2_get_le16(&gb));
  695. s->decorr[i].samplesA[1] =
  696. wp_exp2(bytestream2_get_le16(&gb));
  697. if (s->stereo_in) {
  698. s->decorr[i].samplesB[0] =
  699. wp_exp2(bytestream2_get_le16(&gb));
  700. s->decorr[i].samplesB[1] =
  701. wp_exp2(bytestream2_get_le16(&gb));
  702. t += 4;
  703. }
  704. t += 4;
  705. } else if (s->decorr[i].value < 0) {
  706. s->decorr[i].samplesA[0] =
  707. wp_exp2(bytestream2_get_le16(&gb));
  708. s->decorr[i].samplesB[0] =
  709. wp_exp2(bytestream2_get_le16(&gb));
  710. t += 4;
  711. } else {
  712. for (j = 0; j < s->decorr[i].value; j++) {
  713. s->decorr[i].samplesA[j] =
  714. wp_exp2(bytestream2_get_le16(&gb));
  715. if (s->stereo_in) {
  716. s->decorr[i].samplesB[j] =
  717. wp_exp2(bytestream2_get_le16(&gb));
  718. }
  719. }
  720. t += s->decorr[i].value * 2 * (s->stereo_in + 1);
  721. }
  722. }
  723. got_samples = 1;
  724. break;
  725. case WP_ID_ENTROPY:
  726. if (size != 6 * (s->stereo_in + 1)) {
  727. av_log(avctx, AV_LOG_ERROR,
  728. "Entropy vars size should be %i, got %i.\n",
  729. 6 * (s->stereo_in + 1), size);
  730. bytestream2_skip(&gb, ssize);
  731. continue;
  732. }
  733. for (j = 0; j <= s->stereo_in; j++)
  734. for (i = 0; i < 3; i++) {
  735. s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
  736. }
  737. got_entropy = 1;
  738. break;
  739. case WP_ID_HYBRID:
  740. if (s->hybrid_bitrate) {
  741. for (i = 0; i <= s->stereo_in; i++) {
  742. s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
  743. size -= 2;
  744. }
  745. }
  746. for (i = 0; i < (s->stereo_in + 1); i++) {
  747. s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
  748. size -= 2;
  749. }
  750. if (size > 0) {
  751. for (i = 0; i < (s->stereo_in + 1); i++) {
  752. s->ch[i].bitrate_delta =
  753. wp_exp2((int16_t)bytestream2_get_le16(&gb));
  754. }
  755. } else {
  756. for (i = 0; i < (s->stereo_in + 1); i++)
  757. s->ch[i].bitrate_delta = 0;
  758. }
  759. got_hybrid = 1;
  760. break;
  761. case WP_ID_INT32INFO: {
  762. uint8_t val[4];
  763. if (size != 4) {
  764. av_log(avctx, AV_LOG_ERROR,
  765. "Invalid INT32INFO, size = %i\n",
  766. size);
  767. bytestream2_skip(&gb, ssize - 4);
  768. continue;
  769. }
  770. bytestream2_get_buffer(&gb, val, 4);
  771. if (val[0] > 31) {
  772. av_log(avctx, AV_LOG_ERROR,
  773. "Invalid INT32INFO, extra_bits = %d (> 32)\n", val[0]);
  774. continue;
  775. } else if (val[0]) {
  776. s->extra_bits = val[0];
  777. } else if (val[1]) {
  778. s->shift = val[1];
  779. } else if (val[2]) {
  780. s->and = s->or = 1;
  781. s->shift = val[2];
  782. } else if (val[3]) {
  783. s->and = 1;
  784. s->shift = val[3];
  785. }
  786. if (s->shift > 31) {
  787. av_log(avctx, AV_LOG_ERROR,
  788. "Invalid INT32INFO, shift = %d (> 31)\n", s->shift);
  789. s->and = s->or = s->shift = 0;
  790. continue;
  791. }
  792. /* original WavPack decoder forces 32-bit lossy sound to be treated
  793. * as 24-bit one in order to have proper clipping */
  794. if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
  795. s->post_shift += 8;
  796. s->shift -= 8;
  797. s->hybrid_maxclip >>= 8;
  798. s->hybrid_minclip >>= 8;
  799. }
  800. break;
  801. }
  802. case WP_ID_FLOATINFO:
  803. if (size != 4) {
  804. av_log(avctx, AV_LOG_ERROR,
  805. "Invalid FLOATINFO, size = %i\n", size);
  806. bytestream2_skip(&gb, ssize);
  807. continue;
  808. }
  809. s->float_flag = bytestream2_get_byte(&gb);
  810. s->float_shift = bytestream2_get_byte(&gb);
  811. s->float_max_exp = bytestream2_get_byte(&gb);
  812. got_float = 1;
  813. bytestream2_skip(&gb, 1);
  814. break;
  815. case WP_ID_DATA:
  816. s->sc.offset = bytestream2_tell(&gb);
  817. s->sc.size = size * 8;
  818. if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
  819. return ret;
  820. s->data_size = size * 8;
  821. bytestream2_skip(&gb, size);
  822. got_bs = 1;
  823. break;
  824. case WP_ID_EXTRABITS:
  825. if (size <= 4) {
  826. av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
  827. size);
  828. bytestream2_skip(&gb, size);
  829. continue;
  830. }
  831. s->extra_sc.offset = bytestream2_tell(&gb);
  832. s->extra_sc.size = size * 8;
  833. if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
  834. return ret;
  835. s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
  836. bytestream2_skip(&gb, size);
  837. s->got_extra_bits = 1;
  838. break;
  839. case WP_ID_CHANINFO:
  840. if (size <= 1) {
  841. av_log(avctx, AV_LOG_ERROR,
  842. "Insufficient channel information\n");
  843. return AVERROR_INVALIDDATA;
  844. }
  845. chan = bytestream2_get_byte(&gb);
  846. switch (size - 2) {
  847. case 0:
  848. chmask = bytestream2_get_byte(&gb);
  849. break;
  850. case 1:
  851. chmask = bytestream2_get_le16(&gb);
  852. break;
  853. case 2:
  854. chmask = bytestream2_get_le24(&gb);
  855. break;
  856. case 3:
  857. chmask = bytestream2_get_le32(&gb);
  858. break;
  859. case 5:
  860. size = bytestream2_get_byte(&gb);
  861. if (avctx->channels != size)
  862. av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
  863. " instead of %i.\n", size, avctx->channels);
  864. chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
  865. chmask = bytestream2_get_le16(&gb);
  866. break;
  867. default:
  868. av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
  869. size);
  870. chan = avctx->channels;
  871. chmask = avctx->channel_layout;
  872. }
  873. break;
  874. case WP_ID_SAMPLE_RATE:
  875. if (size != 3) {
  876. av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
  877. return AVERROR_INVALIDDATA;
  878. }
  879. sample_rate = bytestream2_get_le24(&gb);
  880. break;
  881. default:
  882. bytestream2_skip(&gb, size);
  883. }
  884. if (id & WP_IDF_ODD)
  885. bytestream2_skip(&gb, 1);
  886. }
  887. if (!got_terms) {
  888. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
  889. return AVERROR_INVALIDDATA;
  890. }
  891. if (!got_weights) {
  892. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
  893. return AVERROR_INVALIDDATA;
  894. }
  895. if (!got_samples) {
  896. av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
  897. return AVERROR_INVALIDDATA;
  898. }
  899. if (!got_entropy) {
  900. av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
  901. return AVERROR_INVALIDDATA;
  902. }
  903. if (s->hybrid && !got_hybrid) {
  904. av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
  905. return AVERROR_INVALIDDATA;
  906. }
  907. if (!got_bs) {
  908. av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
  909. return AVERROR_INVALIDDATA;
  910. }
  911. if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
  912. av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
  913. return AVERROR_INVALIDDATA;
  914. }
  915. if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
  916. const int size = get_bits_left(&s->gb_extra_bits);
  917. const int wanted = s->samples * s->extra_bits << s->stereo_in;
  918. if (size < wanted) {
  919. av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
  920. s->got_extra_bits = 0;
  921. }
  922. }
  923. if (!wc->ch_offset) {
  924. int sr = (s->frame_flags >> 23) & 0xf;
  925. if (sr == 0xf) {
  926. if (!sample_rate) {
  927. av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
  928. return AVERROR_INVALIDDATA;
  929. }
  930. avctx->sample_rate = sample_rate;
  931. } else
  932. avctx->sample_rate = wv_rates[sr];
  933. if (multiblock) {
  934. if (chan)
  935. avctx->channels = chan;
  936. if (chmask)
  937. avctx->channel_layout = chmask;
  938. } else {
  939. avctx->channels = s->stereo ? 2 : 1;
  940. avctx->channel_layout = s->stereo ? AV_CH_LAYOUT_STEREO :
  941. AV_CH_LAYOUT_MONO;
  942. }
  943. /* get output buffer */
  944. frame->nb_samples = s->samples + 1;
  945. if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
  946. return ret;
  947. frame->nb_samples = s->samples;
  948. }
  949. if (wc->ch_offset + s->stereo >= avctx->channels) {
  950. av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
  951. return ((avctx->err_recognition & AV_EF_EXPLODE) || !wc->ch_offset) ? AVERROR_INVALIDDATA : 0;
  952. }
  953. samples_l = frame->extended_data[wc->ch_offset];
  954. if (s->stereo)
  955. samples_r = frame->extended_data[wc->ch_offset + 1];
  956. wc->ch_offset += 1 + s->stereo;
  957. if (s->stereo_in) {
  958. ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
  959. if (ret < 0)
  960. return ret;
  961. } else {
  962. ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
  963. if (ret < 0)
  964. return ret;
  965. if (s->stereo)
  966. memcpy(samples_r, samples_l, bpp * s->samples);
  967. }
  968. return 0;
  969. }
  970. static void wavpack_decode_flush(AVCodecContext *avctx)
  971. {
  972. WavpackContext *s = avctx->priv_data;
  973. int i;
  974. for (i = 0; i < s->fdec_num; i++)
  975. wv_reset_saved_context(s->fdec[i]);
  976. }
  977. static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
  978. int *got_frame_ptr, AVPacket *avpkt)
  979. {
  980. WavpackContext *s = avctx->priv_data;
  981. const uint8_t *buf = avpkt->data;
  982. int buf_size = avpkt->size;
  983. AVFrame *frame = data;
  984. int frame_size, ret, frame_flags;
  985. if (avpkt->size <= WV_HEADER_SIZE)
  986. return AVERROR_INVALIDDATA;
  987. s->block = 0;
  988. s->ch_offset = 0;
  989. /* determine number of samples */
  990. s->samples = AV_RL32(buf + 20);
  991. frame_flags = AV_RL32(buf + 24);
  992. if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
  993. av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
  994. s->samples);
  995. return AVERROR_INVALIDDATA;
  996. }
  997. if (frame_flags & 0x80) {
  998. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  999. } else if ((frame_flags & 0x03) <= 1) {
  1000. avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
  1001. } else {
  1002. avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
  1003. avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
  1004. }
  1005. while (buf_size > 0) {
  1006. if (buf_size <= WV_HEADER_SIZE)
  1007. break;
  1008. frame_size = AV_RL32(buf + 4) - 12;
  1009. buf += 20;
  1010. buf_size -= 20;
  1011. if (frame_size <= 0 || frame_size > buf_size) {
  1012. av_log(avctx, AV_LOG_ERROR,
  1013. "Block %d has invalid size (size %d vs. %d bytes left)\n",
  1014. s->block, frame_size, buf_size);
  1015. wavpack_decode_flush(avctx);
  1016. return AVERROR_INVALIDDATA;
  1017. }
  1018. if ((ret = wavpack_decode_block(avctx, s->block,
  1019. frame, buf, frame_size)) < 0) {
  1020. wavpack_decode_flush(avctx);
  1021. return ret;
  1022. }
  1023. s->block++;
  1024. buf += frame_size;
  1025. buf_size -= frame_size;
  1026. }
  1027. if (s->ch_offset != avctx->channels) {
  1028. av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
  1029. return AVERROR_INVALIDDATA;
  1030. }
  1031. *got_frame_ptr = 1;
  1032. return avpkt->size;
  1033. }
  1034. AVCodec ff_wavpack_decoder = {
  1035. .name = "wavpack",
  1036. .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
  1037. .type = AVMEDIA_TYPE_AUDIO,
  1038. .id = AV_CODEC_ID_WAVPACK,
  1039. .priv_data_size = sizeof(WavpackContext),
  1040. .init = wavpack_decode_init,
  1041. .close = wavpack_decode_end,
  1042. .decode = wavpack_decode_frame,
  1043. .flush = wavpack_decode_flush,
  1044. .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
  1045. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  1046. };