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.

1223 lines
40KB

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