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.

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