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.

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