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.

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