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.

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