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