libavcodec/wavpack.c
Go to the documentation of this file.
00001 /*
00002  * WavPack lossless audio decoder
00003  * Copyright (c) 2006,2011 Konstantin Shishkov
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #define BITSTREAM_READER_LE
00023 
00024 #include "libavutil/audioconvert.h"
00025 #include "avcodec.h"
00026 #include "internal.h"
00027 #include "get_bits.h"
00028 #include "unary.h"
00029 
00035 #define WV_MONO           0x00000004
00036 #define WV_JOINT_STEREO   0x00000010
00037 #define WV_FALSE_STEREO   0x40000000
00038 
00039 #define WV_HYBRID_MODE    0x00000008
00040 #define WV_HYBRID_SHAPE   0x00000008
00041 #define WV_HYBRID_BITRATE 0x00000200
00042 #define WV_HYBRID_BALANCE 0x00000400
00043 
00044 #define WV_FLT_SHIFT_ONES 0x01
00045 #define WV_FLT_SHIFT_SAME 0x02
00046 #define WV_FLT_SHIFT_SENT 0x04
00047 #define WV_FLT_ZERO_SENT  0x08
00048 #define WV_FLT_ZERO_SIGN  0x10
00049 
00050 enum WP_ID_Flags {
00051     WP_IDF_MASK   = 0x1F,
00052     WP_IDF_IGNORE = 0x20,
00053     WP_IDF_ODD    = 0x40,
00054     WP_IDF_LONG   = 0x80
00055 };
00056 
00057 enum WP_ID {
00058     WP_ID_DUMMY = 0,
00059     WP_ID_ENCINFO,
00060     WP_ID_DECTERMS,
00061     WP_ID_DECWEIGHTS,
00062     WP_ID_DECSAMPLES,
00063     WP_ID_ENTROPY,
00064     WP_ID_HYBRID,
00065     WP_ID_SHAPING,
00066     WP_ID_FLOATINFO,
00067     WP_ID_INT32INFO,
00068     WP_ID_DATA,
00069     WP_ID_CORR,
00070     WP_ID_EXTRABITS,
00071     WP_ID_CHANINFO
00072 };
00073 
00074 typedef struct SavedContext {
00075     int offset;
00076     int size;
00077     int bits_used;
00078     uint32_t crc;
00079 } SavedContext;
00080 
00081 #define MAX_TERMS 16
00082 
00083 typedef struct Decorr {
00084     int delta;
00085     int value;
00086     int weightA;
00087     int weightB;
00088     int samplesA[8];
00089     int samplesB[8];
00090 } Decorr;
00091 
00092 typedef struct WvChannel {
00093     int median[3];
00094     int slow_level, error_limit;
00095     int bitrate_acc, bitrate_delta;
00096 } WvChannel;
00097 
00098 typedef struct WavpackFrameContext {
00099     AVCodecContext *avctx;
00100     int frame_flags;
00101     int stereo, stereo_in;
00102     int joint;
00103     uint32_t CRC;
00104     GetBitContext gb;
00105     int got_extra_bits;
00106     uint32_t crc_extra_bits;
00107     GetBitContext gb_extra_bits;
00108     int data_size; // in bits
00109     int samples;
00110     int terms;
00111     Decorr decorr[MAX_TERMS];
00112     int zero, one, zeroes;
00113     int extra_bits;
00114     int and, or, shift;
00115     int post_shift;
00116     int hybrid, hybrid_bitrate;
00117     int hybrid_maxclip, hybrid_minclip;
00118     int float_flag;
00119     int float_shift;
00120     int float_max_exp;
00121     WvChannel ch[2];
00122     int pos;
00123     SavedContext sc, extra_sc;
00124 } WavpackFrameContext;
00125 
00126 #define WV_MAX_FRAME_DECODERS 14
00127 
00128 typedef struct WavpackContext {
00129     AVCodecContext *avctx;
00130     AVFrame frame;
00131 
00132     WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
00133     int fdec_num;
00134 
00135     int multichannel;
00136     int mkv_mode;
00137     int block;
00138     int samples;
00139     int ch_offset;
00140 } WavpackContext;
00141 
00142 // exponent table copied from WavPack source
00143 static const uint8_t wp_exp2_table [256] = {
00144     0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b,
00145     0x0b, 0x0c, 0x0d, 0x0e, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x16,
00146     0x17, 0x18, 0x19, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21, 0x22, 0x23,
00147     0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
00148     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3a, 0x3b, 0x3c, 0x3d,
00149     0x3e, 0x3f, 0x40, 0x41, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b,
00150     0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
00151     0x5b, 0x5c, 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00152     0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00153     0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8a,
00154     0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
00155     0x9c, 0x9d, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad,
00156     0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0,
00157     0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc8, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4,
00158     0xd6, 0xd7, 0xd8, 0xd9, 0xdb, 0xdc, 0xdd, 0xde, 0xe0, 0xe1, 0xe2, 0xe4, 0xe5, 0xe6, 0xe8, 0xe9,
00159     0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff
00160 };
00161 
00162 static const uint8_t wp_log2_table [] = {
00163     0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x10, 0x11, 0x12, 0x14, 0x15,
00164     0x16, 0x18, 0x19, 0x1a, 0x1c, 0x1d, 0x1e, 0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2a,
00165     0x2c, 0x2d, 0x2e, 0x2f, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3d, 0x3e,
00166     0x3f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
00167     0x52, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
00168     0x64, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x74, 0x75,
00169     0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
00170     0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
00171     0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
00172     0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb2,
00173     0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc0,
00174     0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcb, 0xcc, 0xcd, 0xce,
00175     0xcf, 0xd0, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd8, 0xd9, 0xda, 0xdb,
00176     0xdc, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe4, 0xe5, 0xe6, 0xe7, 0xe7,
00177     0xe8, 0xe9, 0xea, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xee, 0xef, 0xf0, 0xf1, 0xf1, 0xf2, 0xf3, 0xf4,
00178     0xf4, 0xf5, 0xf6, 0xf7, 0xf7, 0xf8, 0xf9, 0xf9, 0xfa, 0xfb, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff
00179 };
00180 
00181 static av_always_inline int wp_exp2(int16_t val)
00182 {
00183     int res, neg = 0;
00184 
00185     if (val < 0) {
00186         val = -val;
00187         neg = 1;
00188     }
00189 
00190     res = wp_exp2_table[val & 0xFF] | 0x100;
00191     val >>= 8;
00192     res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val));
00193     return neg ? -res : res;
00194 }
00195 
00196 static av_always_inline int wp_log2(int32_t val)
00197 {
00198     int bits;
00199 
00200     if (!val)
00201         return 0;
00202     if (val == 1)
00203         return 256;
00204     val += val >> 9;
00205     bits = av_log2(val) + 1;
00206     if (bits < 9)
00207         return (bits << 8) + wp_log2_table[(val << (9 - bits)) & 0xFF];
00208     else
00209         return (bits << 8) + wp_log2_table[(val >> (bits - 9)) & 0xFF];
00210 }
00211 
00212 #define LEVEL_DECAY(a)  ((a + 0x80) >> 8)
00213 
00214 // macros for manipulating median values
00215 #define GET_MED(n) ((c->median[n] >> 4) + 1)
00216 #define DEC_MED(n) c->median[n] -= ((c->median[n] + (128 >> n) - 2) / (128 >> n)) * 2
00217 #define INC_MED(n) c->median[n] += ((c->median[n] + (128 >> n)    ) / (128 >> n)) * 5
00218 
00219 // macros for applying weight
00220 #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
00221     if (samples && in) { \
00222         if ((samples ^ in) < 0) { \
00223             weight -= delta; \
00224             if (weight < -1024) \
00225                 weight = -1024; \
00226         } else { \
00227             weight += delta; \
00228             if (weight > 1024) \
00229                 weight = 1024; \
00230         } \
00231     }
00232 
00233 
00234 static av_always_inline int get_tail(GetBitContext *gb, int k)
00235 {
00236     int p, e, res;
00237 
00238     if (k < 1)
00239         return 0;
00240     p = av_log2(k);
00241     e = (1 << (p + 1)) - k - 1;
00242     res = p ? get_bits(gb, p) : 0;
00243     if (res >= e)
00244         res = (res << 1) - e + get_bits1(gb);
00245     return res;
00246 }
00247 
00248 static void update_error_limit(WavpackFrameContext *ctx)
00249 {
00250     int i, br[2], sl[2];
00251 
00252     for (i = 0; i <= ctx->stereo_in; i++) {
00253         ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
00254         br[i] = ctx->ch[i].bitrate_acc >> 16;
00255         sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
00256     }
00257     if (ctx->stereo_in && ctx->hybrid_bitrate) {
00258         int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
00259         if (balance > br[0]) {
00260             br[1] = br[0] << 1;
00261             br[0] = 0;
00262         } else if (-balance > br[0]) {
00263             br[0] <<= 1;
00264             br[1] = 0;
00265         } else {
00266             br[1] = br[0] + balance;
00267             br[0] = br[0] - balance;
00268         }
00269     }
00270     for (i = 0; i <= ctx->stereo_in; i++) {
00271         if (ctx->hybrid_bitrate) {
00272             if (sl[i] - br[i] > -0x100)
00273                 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
00274             else
00275                 ctx->ch[i].error_limit = 0;
00276         } else {
00277             ctx->ch[i].error_limit = wp_exp2(br[i]);
00278         }
00279     }
00280 }
00281 
00282 static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
00283                         int channel, int *last)
00284 {
00285     int t, t2;
00286     int sign, base, add, ret;
00287     WvChannel *c = &ctx->ch[channel];
00288 
00289     *last = 0;
00290 
00291     if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
00292         !ctx->zero && !ctx->one) {
00293         if (ctx->zeroes) {
00294             ctx->zeroes--;
00295             if (ctx->zeroes) {
00296                 c->slow_level -= LEVEL_DECAY(c->slow_level);
00297                 return 0;
00298             }
00299         } else {
00300             t = get_unary_0_33(gb);
00301             if (t >= 2) {
00302                 if (get_bits_left(gb) < t - 1)
00303                     goto error;
00304                 t = get_bits(gb, t - 1) | (1 << (t-1));
00305             } else {
00306                 if (get_bits_left(gb) < 0)
00307                     goto error;
00308             }
00309             ctx->zeroes = t;
00310             if (ctx->zeroes) {
00311                 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
00312                 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
00313                 c->slow_level -= LEVEL_DECAY(c->slow_level);
00314                 return 0;
00315             }
00316         }
00317     }
00318 
00319     if (ctx->zero) {
00320         t = 0;
00321         ctx->zero = 0;
00322     } else {
00323         t = get_unary_0_33(gb);
00324         if (get_bits_left(gb) < 0)
00325             goto error;
00326         if (t == 16) {
00327             t2 = get_unary_0_33(gb);
00328             if (t2 < 2) {
00329                 if (get_bits_left(gb) < 0)
00330                     goto error;
00331                 t += t2;
00332             } else {
00333                 if (get_bits_left(gb) < t2 - 1)
00334                     goto error;
00335                 t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
00336             }
00337         }
00338 
00339         if (ctx->one) {
00340             ctx->one = t & 1;
00341             t = (t >> 1) + 1;
00342         } else {
00343             ctx->one = t & 1;
00344             t >>= 1;
00345         }
00346         ctx->zero = !ctx->one;
00347     }
00348 
00349     if (ctx->hybrid && !channel)
00350         update_error_limit(ctx);
00351 
00352     if (!t) {
00353         base = 0;
00354         add  = GET_MED(0) - 1;
00355         DEC_MED(0);
00356     } else if (t == 1) {
00357         base = GET_MED(0);
00358         add  = GET_MED(1) - 1;
00359         INC_MED(0);
00360         DEC_MED(1);
00361     } else if (t == 2) {
00362         base = GET_MED(0) + GET_MED(1);
00363         add  = GET_MED(2) - 1;
00364         INC_MED(0);
00365         INC_MED(1);
00366         DEC_MED(2);
00367     } else {
00368         base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
00369         add  = GET_MED(2) - 1;
00370         INC_MED(0);
00371         INC_MED(1);
00372         INC_MED(2);
00373     }
00374     if (!c->error_limit) {
00375         ret = base + get_tail(gb, add);
00376         if (get_bits_left(gb) <= 0)
00377             goto error;
00378     } else {
00379         int mid = (base * 2 + add + 1) >> 1;
00380         while (add > c->error_limit) {
00381             if (get_bits_left(gb) <= 0)
00382                 goto error;
00383             if (get_bits1(gb)) {
00384                 add -= (mid - base);
00385                 base = mid;
00386             } else
00387                 add = mid - base - 1;
00388             mid = (base * 2 + add + 1) >> 1;
00389         }
00390         ret = mid;
00391     }
00392     sign = get_bits1(gb);
00393     if (ctx->hybrid_bitrate)
00394         c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
00395     return sign ? ~ret : ret;
00396 
00397 error:
00398     *last = 1;
00399     return 0;
00400 }
00401 
00402 static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
00403                                        int S)
00404 {
00405     int bit;
00406 
00407     if (s->extra_bits){
00408         S <<= s->extra_bits;
00409 
00410         if (s->got_extra_bits && get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
00411             S |= get_bits(&s->gb_extra_bits, s->extra_bits);
00412             *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
00413         }
00414     }
00415 
00416     bit = (S & s->and) | s->or;
00417     bit = ((S + bit) << s->shift) - bit;
00418 
00419     if (s->hybrid)
00420         bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
00421 
00422     return bit << s->post_shift;
00423 }
00424 
00425 static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
00426 {
00427     union {
00428         float    f;
00429         uint32_t u;
00430     } value;
00431 
00432     int sign;
00433     int exp = s->float_max_exp;
00434 
00435     if (s->got_extra_bits) {
00436         const int max_bits  = 1 + 23 + 8 + 1;
00437         const int left_bits = get_bits_left(&s->gb_extra_bits);
00438 
00439         if (left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
00440             return 0.0;
00441     }
00442 
00443     if (S) {
00444         S <<= s->float_shift;
00445         sign = S < 0;
00446         if (sign)
00447             S = -S;
00448         if (S >= 0x1000000) {
00449             if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
00450                 S = get_bits(&s->gb_extra_bits, 23);
00451             else
00452                 S = 0;
00453             exp = 255;
00454         } else if (exp) {
00455             int shift = 23 - av_log2(S);
00456             exp = s->float_max_exp;
00457             if (exp <= shift)
00458                 shift = --exp;
00459             exp -= shift;
00460 
00461             if (shift) {
00462                 S <<= shift;
00463                 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
00464                     (s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SAME) &&
00465                      get_bits1(&s->gb_extra_bits))) {
00466                     S |= (1 << shift) - 1;
00467                 } else if (s->got_extra_bits &&
00468                            (s->float_flag & WV_FLT_SHIFT_SENT)) {
00469                     S |= get_bits(&s->gb_extra_bits, shift);
00470                 }
00471             }
00472         } else {
00473             exp = s->float_max_exp;
00474         }
00475         S &= 0x7fffff;
00476     } else {
00477         sign = 0;
00478         exp = 0;
00479         if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
00480             if (get_bits1(&s->gb_extra_bits)) {
00481                 S = get_bits(&s->gb_extra_bits, 23);
00482                 if (s->float_max_exp >= 25)
00483                     exp = get_bits(&s->gb_extra_bits, 8);
00484                 sign = get_bits1(&s->gb_extra_bits);
00485             } else {
00486                 if (s->float_flag & WV_FLT_ZERO_SIGN)
00487                     sign = get_bits1(&s->gb_extra_bits);
00488             }
00489         }
00490     }
00491 
00492     *crc = *crc * 27 + S * 9 + exp * 3 + sign;
00493 
00494     value.u = (sign << 31) | (exp << 23) | S;
00495     return value.f;
00496 }
00497 
00498 static void wv_reset_saved_context(WavpackFrameContext *s)
00499 {
00500     s->pos = 0;
00501     s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
00502 }
00503 
00504 static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
00505                                    void *dst, const int type)
00506 {
00507     int i, j, count = 0;
00508     int last, t;
00509     int A, B, L, L2, R, R2;
00510     int pos = s->pos;
00511     uint32_t crc = s->sc.crc;
00512     uint32_t crc_extra_bits = s->extra_sc.crc;
00513     int16_t *dst16 = dst;
00514     int32_t *dst32 = dst;
00515     float   *dstfl = dst;
00516     const int channel_pad = s->avctx->channels - 2;
00517 
00518     s->one = s->zero = s->zeroes = 0;
00519     do {
00520         L = wv_get_value(s, gb, 0, &last);
00521         if (last)
00522             break;
00523         R = wv_get_value(s, gb, 1, &last);
00524         if (last)
00525             break;
00526         for (i = 0; i < s->terms; i++) {
00527             t = s->decorr[i].value;
00528             if (t > 0) {
00529                 if (t > 8) {
00530                     if (t & 1) {
00531                         A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00532                         B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
00533                     } else {
00534                         A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00535                         B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
00536                     }
00537                     s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00538                     s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
00539                     j = 0;
00540                 } else {
00541                     A = s->decorr[i].samplesA[pos];
00542                     B = s->decorr[i].samplesB[pos];
00543                     j = (pos + t) & 7;
00544                 }
00545                 if (type != AV_SAMPLE_FMT_S16) {
00546                     L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00547                     R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
00548                 } else {
00549                     L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
00550                     R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
00551                 }
00552                 if (A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00553                 if (B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
00554                 s->decorr[i].samplesA[j] = L = L2;
00555                 s->decorr[i].samplesB[j] = R = R2;
00556             } else if (t == -1) {
00557                 if (type != AV_SAMPLE_FMT_S16)
00558                     L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
00559                 else
00560                     L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
00561                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
00562                 L = L2;
00563                 if (type != AV_SAMPLE_FMT_S16)
00564                     R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
00565                 else
00566                     R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
00567                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
00568                 R = R2;
00569                 s->decorr[i].samplesA[0] = R;
00570             } else {
00571                 if (type != AV_SAMPLE_FMT_S16)
00572                     R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
00573                 else
00574                     R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
00575                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
00576                 R = R2;
00577 
00578                 if (t == -3) {
00579                     R2 = s->decorr[i].samplesA[0];
00580                     s->decorr[i].samplesA[0] = R;
00581                 }
00582 
00583                 if (type != AV_SAMPLE_FMT_S16)
00584                     L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
00585                 else
00586                     L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
00587                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
00588                 L = L2;
00589                 s->decorr[i].samplesB[0] = L;
00590             }
00591         }
00592         pos = (pos + 1) & 7;
00593         if (s->joint)
00594             L += (R -= (L >> 1));
00595         crc = (crc * 3 + L) * 3 + R;
00596 
00597         if (type == AV_SAMPLE_FMT_FLT) {
00598             *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
00599             *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
00600             dstfl += channel_pad;
00601         } else if (type == AV_SAMPLE_FMT_S32) {
00602             *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
00603             *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
00604             dst32 += channel_pad;
00605         } else {
00606             *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
00607             *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
00608             dst16 += channel_pad;
00609         }
00610         count++;
00611     } while (!last && count < s->samples);
00612 
00613     wv_reset_saved_context(s);
00614     if (crc != s->CRC) {
00615         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00616         return -1;
00617     }
00618     if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
00619         av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
00620         return -1;
00621     }
00622 
00623     return count * 2;
00624 }
00625 
00626 static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
00627                                  void *dst, const int type)
00628 {
00629     int i, j, count = 0;
00630     int last, t;
00631     int A, S, T;
00632     int pos = s->pos;
00633     uint32_t crc = s->sc.crc;
00634     uint32_t crc_extra_bits = s->extra_sc.crc;
00635     int16_t *dst16 = dst;
00636     int32_t *dst32 = dst;
00637     float   *dstfl = dst;
00638     const int channel_stride = s->avctx->channels;
00639 
00640     s->one = s->zero = s->zeroes = 0;
00641     do {
00642         T = wv_get_value(s, gb, 0, &last);
00643         S = 0;
00644         if (last)
00645             break;
00646         for (i = 0; i < s->terms; i++) {
00647             t = s->decorr[i].value;
00648             if (t > 8) {
00649                 if (t & 1)
00650                     A =  2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00651                 else
00652                     A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00653                 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00654                 j = 0;
00655             } else {
00656                 A = s->decorr[i].samplesA[pos];
00657                 j = (pos + t) & 7;
00658             }
00659             if (type != AV_SAMPLE_FMT_S16)
00660                 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00661             else
00662                 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
00663             if (A && T)
00664                 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00665             s->decorr[i].samplesA[j] = T = S;
00666         }
00667         pos = (pos + 1) & 7;
00668         crc = crc * 3 + S;
00669 
00670         if (type == AV_SAMPLE_FMT_FLT) {
00671             *dstfl = wv_get_value_float(s, &crc_extra_bits, S);
00672             dstfl += channel_stride;
00673         } else if (type == AV_SAMPLE_FMT_S32) {
00674             *dst32 = wv_get_value_integer(s, &crc_extra_bits, S);
00675             dst32 += channel_stride;
00676         } else {
00677             *dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
00678             dst16 += channel_stride;
00679         }
00680         count++;
00681     } while (!last && count < s->samples);
00682 
00683     wv_reset_saved_context(s);
00684     if (crc != s->CRC) {
00685         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00686         return -1;
00687     }
00688     if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
00689         av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
00690         return -1;
00691     }
00692 
00693     return count;
00694 }
00695 
00696 static av_cold int wv_alloc_frame_context(WavpackContext *c)
00697 {
00698 
00699     if (c->fdec_num == WV_MAX_FRAME_DECODERS)
00700         return -1;
00701 
00702     c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
00703     if (!c->fdec[c->fdec_num])
00704         return -1;
00705     c->fdec_num++;
00706     c->fdec[c->fdec_num - 1]->avctx = c->avctx;
00707     wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
00708 
00709     return 0;
00710 }
00711 
00712 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
00713 {
00714     WavpackContext *s = avctx->priv_data;
00715 
00716     s->avctx = avctx;
00717     if (avctx->bits_per_coded_sample <= 16)
00718         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00719     else
00720         avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00721     if (avctx->channels <= 2 && !avctx->channel_layout)
00722         avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO :
00723                                                          AV_CH_LAYOUT_MONO;
00724 
00725     s->multichannel = avctx->channels > 2;
00726     /* lavf demuxer does not provide extradata, Matroska stores 0x403
00727        there, use this to detect decoding mode for multichannel */
00728     s->mkv_mode = 0;
00729     if (s->multichannel && avctx->extradata && avctx->extradata_size == 2) {
00730         int ver = AV_RL16(avctx->extradata);
00731         if (ver >= 0x402 && ver <= 0x410)
00732             s->mkv_mode = 1;
00733     }
00734 
00735     s->fdec_num = 0;
00736 
00737     avcodec_get_frame_defaults(&s->frame);
00738     avctx->coded_frame = &s->frame;
00739 
00740     return 0;
00741 }
00742 
00743 static av_cold int wavpack_decode_end(AVCodecContext *avctx)
00744 {
00745     WavpackContext *s = avctx->priv_data;
00746     int i;
00747 
00748     for (i = 0; i < s->fdec_num; i++)
00749         av_freep(&s->fdec[i]);
00750     s->fdec_num = 0;
00751 
00752     return 0;
00753 }
00754 
00755 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
00756                                 void *data, int *got_frame_ptr,
00757                                 const uint8_t *buf, int buf_size)
00758 {
00759     WavpackContext *wc = avctx->priv_data;
00760     WavpackFrameContext *s;
00761     void *samples = data;
00762     int samplecount;
00763     int got_terms   = 0, got_weights = 0, got_samples = 0,
00764         got_entropy = 0, got_bs      = 0, got_float   = 0, got_hybrid = 0;
00765     const uint8_t *orig_buf = buf;
00766     const uint8_t *buf_end  = buf + buf_size;
00767     int i, j, id, size, ssize, weights, t;
00768     int bpp, chan, chmask, orig_bpp;
00769 
00770     if (buf_size == 0) {
00771         *got_frame_ptr = 0;
00772         return 0;
00773     }
00774 
00775     if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
00776         av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
00777         return AVERROR_INVALIDDATA;
00778     }
00779 
00780     s = wc->fdec[block_no];
00781     if (!s) {
00782         av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);
00783         return AVERROR_INVALIDDATA;
00784     }
00785 
00786     memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
00787     memset(s->ch, 0, sizeof(s->ch));
00788     s->extra_bits = 0;
00789     s->and = s->or = s->shift = 0;
00790     s->got_extra_bits = 0;
00791 
00792     if (!wc->mkv_mode) {
00793         s->samples = AV_RL32(buf); buf += 4;
00794         if (s->samples != wc->samples)
00795             return AVERROR_INVALIDDATA;
00796 
00797         if (!s->samples) {
00798             *got_frame_ptr = 0;
00799             return 0;
00800         }
00801     } else {
00802         s->samples = wc->samples;
00803     }
00804     s->frame_flags = AV_RL32(buf); buf += 4;
00805     bpp = av_get_bytes_per_sample(avctx->sample_fmt);
00806     samples = (uint8_t*)samples + bpp * wc->ch_offset;
00807     orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
00808 
00809     s->stereo         = !(s->frame_flags & WV_MONO);
00810     s->stereo_in      =  (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
00811     s->joint          =   s->frame_flags & WV_JOINT_STEREO;
00812     s->hybrid         =   s->frame_flags & WV_HYBRID_MODE;
00813     s->hybrid_bitrate =   s->frame_flags & WV_HYBRID_BITRATE;
00814     s->post_shift     = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
00815     s->hybrid_maxclip = (( 1LL << (orig_bpp - 1)) - 1) >> s->post_shift;
00816     s->hybrid_minclip = ((-1LL << (orig_bpp - 1)))     >> s->post_shift;
00817     s->CRC            = AV_RL32(buf); buf += 4;
00818     if (wc->mkv_mode)
00819         buf += 4; //skip block size;
00820 
00821     wc->ch_offset += 1 + s->stereo;
00822 
00823     // parse metadata blocks
00824     while (buf < buf_end) {
00825         id   = *buf++;
00826         size = *buf++;
00827         if (id & WP_IDF_LONG) {
00828             size |= (*buf++) << 8;
00829             size |= (*buf++) << 16;
00830         }
00831         size <<= 1; // size is specified in words
00832         ssize = size;
00833         if (id & WP_IDF_ODD)
00834             size--;
00835         if (size < 0) {
00836             av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
00837             break;
00838         }
00839         if (buf + ssize > buf_end) {
00840             av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
00841             break;
00842         }
00843         if (id & WP_IDF_IGNORE) {
00844             buf += ssize;
00845             continue;
00846         }
00847         switch (id & WP_IDF_MASK) {
00848         case WP_ID_DECTERMS:
00849             if (size > MAX_TERMS) {
00850                 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
00851                 s->terms = 0;
00852                 buf += ssize;
00853                 continue;
00854             }
00855             s->terms = size;
00856             for (i = 0; i < s->terms; i++) {
00857                 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
00858                 s->decorr[s->terms - i - 1].delta = *buf >> 5;
00859                 buf++;
00860             }
00861             got_terms = 1;
00862             break;
00863         case WP_ID_DECWEIGHTS:
00864             if (!got_terms) {
00865                 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00866                 continue;
00867             }
00868             weights = size >> s->stereo_in;
00869             if (weights > MAX_TERMS || weights > s->terms) {
00870                 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
00871                 buf += ssize;
00872                 continue;
00873             }
00874             for (i = 0; i < weights; i++) {
00875                 t = (int8_t)(*buf++);
00876                 s->decorr[s->terms - i - 1].weightA = t << 3;
00877                 if (s->decorr[s->terms - i - 1].weightA > 0)
00878                     s->decorr[s->terms - i - 1].weightA +=
00879                             (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
00880                 if (s->stereo_in) {
00881                     t = (int8_t)(*buf++);
00882                     s->decorr[s->terms - i - 1].weightB = t << 3;
00883                     if (s->decorr[s->terms - i - 1].weightB > 0)
00884                         s->decorr[s->terms - i - 1].weightB +=
00885                                 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
00886                 }
00887             }
00888             got_weights = 1;
00889             break;
00890         case WP_ID_DECSAMPLES:
00891             if (!got_terms) {
00892                 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00893                 continue;
00894             }
00895             t = 0;
00896             for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
00897                 if (s->decorr[i].value > 8) {
00898                     s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00899                     s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00900                     if (s->stereo_in) {
00901                         s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00902                         s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00903                         t += 4;
00904                     }
00905                     t += 4;
00906                 } else if (s->decorr[i].value < 0) {
00907                     s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00908                     s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00909                     t += 4;
00910                 } else {
00911                     for (j = 0; j < s->decorr[i].value; j++) {
00912                         s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00913                         if (s->stereo_in)
00914                             s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00915                     }
00916                     t += s->decorr[i].value * 2 * (s->stereo_in + 1);
00917                 }
00918             }
00919             got_samples = 1;
00920             break;
00921         case WP_ID_ENTROPY:
00922             if (size != 6 * (s->stereo_in + 1)) {
00923                 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, "
00924                        "got %i", 6 * (s->stereo_in + 1), size);
00925                 buf += ssize;
00926                 continue;
00927             }
00928             for (j = 0; j <= s->stereo_in; j++) {
00929                 for (i = 0; i < 3; i++) {
00930                     s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
00931                     buf += 2;
00932                 }
00933             }
00934             got_entropy = 1;
00935             break;
00936         case WP_ID_HYBRID:
00937             if (s->hybrid_bitrate) {
00938                 for (i = 0; i <= s->stereo_in; i++) {
00939                     s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
00940                     buf += 2;
00941                     size -= 2;
00942                 }
00943             }
00944             for (i = 0; i < (s->stereo_in + 1); i++) {
00945                 s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
00946                 buf += 2;
00947                 size -= 2;
00948             }
00949             if (size > 0) {
00950                 for (i = 0; i < (s->stereo_in + 1); i++) {
00951                     s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
00952                     buf += 2;
00953                 }
00954             } else {
00955                 for (i = 0; i < (s->stereo_in + 1); i++)
00956                     s->ch[i].bitrate_delta = 0;
00957             }
00958             got_hybrid = 1;
00959             break;
00960         case WP_ID_INT32INFO:
00961             if (size != 4) {
00962                 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
00963                 buf += ssize;
00964                 continue;
00965             }
00966             if (buf[0])
00967                 s->extra_bits = buf[0];
00968             else if (buf[1])
00969                 s->shift = buf[1];
00970             else if (buf[2]){
00971                 s->and = s->or = 1;
00972                 s->shift = buf[2];
00973             } else if(buf[3]) {
00974                 s->and   = 1;
00975                 s->shift = buf[3];
00976             }
00977             /* original WavPack decoder forces 32-bit lossy sound to be treated
00978              * as 24-bit one in order to have proper clipping
00979              */
00980             if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
00981                 s->post_shift += 8;
00982                 s->shift      -= 8;
00983                 s->hybrid_maxclip >>= 8;
00984                 s->hybrid_minclip >>= 8;
00985             }
00986             buf += 4;
00987             break;
00988         case WP_ID_FLOATINFO:
00989             if (size != 4) {
00990                 av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
00991                 buf += ssize;
00992                 continue;
00993             }
00994             s->float_flag    = buf[0];
00995             s->float_shift   = buf[1];
00996             s->float_max_exp = buf[2];
00997             buf += 4;
00998             got_float = 1;
00999             break;
01000         case WP_ID_DATA:
01001             s->sc.offset = buf - orig_buf;
01002             s->sc.size   = size * 8;
01003             init_get_bits(&s->gb, buf, size * 8);
01004             s->data_size = size * 8;
01005             buf += size;
01006             got_bs = 1;
01007             break;
01008         case WP_ID_EXTRABITS:
01009             if (size <= 4) {
01010                 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
01011                        size);
01012                 buf += size;
01013                 continue;
01014             }
01015             s->extra_sc.offset = buf - orig_buf;
01016             s->extra_sc.size   = size * 8;
01017             init_get_bits(&s->gb_extra_bits, buf, size * 8);
01018             s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
01019             buf += size;
01020             s->got_extra_bits = 1;
01021             break;
01022         case WP_ID_CHANINFO:
01023             if (size <= 1) {
01024                 av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");
01025                 return AVERROR_INVALIDDATA;
01026             }
01027             chan = *buf++;
01028             switch (size - 2) {
01029             case 0: chmask = *buf;         break;
01030             case 1: chmask = AV_RL16(buf); break;
01031             case 2: chmask = AV_RL24(buf); break;
01032             case 3: chmask = AV_RL32(buf); break;
01033             case 5:
01034                 chan |= (buf[1] & 0xF) << 8;
01035                 chmask = AV_RL24(buf + 2);
01036                 break;
01037             default:
01038                 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
01039                        size);
01040                 chan   = avctx->channels;
01041                 chmask = avctx->channel_layout;
01042             }
01043             if (chan != avctx->channels) {
01044                 av_log(avctx, AV_LOG_ERROR,
01045                        "Block reports total %d channels, "
01046                        "decoder believes it's %d channels\n",
01047                        chan, avctx->channels);
01048                 return AVERROR_INVALIDDATA;
01049             }
01050             if (!avctx->channel_layout)
01051                 avctx->channel_layout = chmask;
01052             buf += size - 1;
01053             break;
01054         default:
01055             buf += size;
01056         }
01057         if (id & WP_IDF_ODD)
01058             buf++;
01059     }
01060 
01061     if (!got_terms) {
01062         av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
01063         return AVERROR_INVALIDDATA;
01064     }
01065     if (!got_weights) {
01066         av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
01067         return AVERROR_INVALIDDATA;
01068     }
01069     if (!got_samples) {
01070         av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
01071         return AVERROR_INVALIDDATA;
01072     }
01073     if (!got_entropy) {
01074         av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
01075         return AVERROR_INVALIDDATA;
01076     }
01077     if (s->hybrid && !got_hybrid) {
01078         av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
01079         return AVERROR_INVALIDDATA;
01080     }
01081     if (!got_bs) {
01082         av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
01083         return AVERROR_INVALIDDATA;
01084     }
01085     if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
01086         av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
01087         return AVERROR_INVALIDDATA;
01088     }
01089     if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
01090         const int size   = get_bits_left(&s->gb_extra_bits);
01091         const int wanted = s->samples * s->extra_bits << s->stereo_in;
01092         if (size < wanted) {
01093             av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
01094             s->got_extra_bits = 0;
01095         }
01096     }
01097 
01098     if (s->stereo_in) {
01099         if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
01100             samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
01101         else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
01102             samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
01103         else
01104             samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
01105 
01106         if (samplecount < 0)
01107             return samplecount;
01108 
01109         samplecount >>= 1;
01110     } else {
01111         const int channel_stride = avctx->channels;
01112 
01113         if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
01114             samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
01115         else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
01116             samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
01117         else
01118             samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
01119 
01120         if (samplecount < 0)
01121             return samplecount;
01122 
01123         if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
01124             int16_t *dst = (int16_t*)samples + 1;
01125             int16_t *src = (int16_t*)samples;
01126             int cnt = samplecount;
01127             while (cnt--) {
01128                 *dst = *src;
01129                 src += channel_stride;
01130                 dst += channel_stride;
01131             }
01132         } else if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
01133             int32_t *dst = (int32_t*)samples + 1;
01134             int32_t *src = (int32_t*)samples;
01135             int cnt = samplecount;
01136             while (cnt--) {
01137                 *dst = *src;
01138                 src += channel_stride;
01139                 dst += channel_stride;
01140             }
01141         } else if (s->stereo) {
01142             float *dst = (float*)samples + 1;
01143             float *src = (float*)samples;
01144             int cnt = samplecount;
01145             while (cnt--) {
01146                 *dst = *src;
01147                 src += channel_stride;
01148                 dst += channel_stride;
01149             }
01150         }
01151     }
01152 
01153     *got_frame_ptr = 1;
01154 
01155     return samplecount * bpp;
01156 }
01157 
01158 static void wavpack_decode_flush(AVCodecContext *avctx)
01159 {
01160     WavpackContext *s = avctx->priv_data;
01161     int i;
01162 
01163     for (i = 0; i < s->fdec_num; i++)
01164         wv_reset_saved_context(s->fdec[i]);
01165 }
01166 
01167 static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
01168                                 int *got_frame_ptr, AVPacket *avpkt)
01169 {
01170     WavpackContext *s  = avctx->priv_data;
01171     const uint8_t *buf = avpkt->data;
01172     int buf_size       = avpkt->size;
01173     int frame_size, ret, frame_flags;
01174     int samplecount = 0;
01175 
01176     if (avpkt->size < 12 + s->multichannel * 4)
01177         return AVERROR_INVALIDDATA;
01178 
01179     s->block     = 0;
01180     s->ch_offset = 0;
01181 
01182     /* determine number of samples */
01183     if (s->mkv_mode) {
01184         s->samples  = AV_RL32(buf); buf += 4;
01185         frame_flags = AV_RL32(buf);
01186     } else {
01187         if (s->multichannel) {
01188             s->samples  = AV_RL32(buf + 4);
01189             frame_flags = AV_RL32(buf + 8);
01190         } else {
01191             s->samples  = AV_RL32(buf);
01192             frame_flags = AV_RL32(buf + 4);
01193         }
01194     }
01195     if (s->samples <= 0) {
01196         av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
01197                s->samples);
01198         return AVERROR_INVALIDDATA;
01199     }
01200 
01201     if (frame_flags & 0x80) {
01202         avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01203     } else if ((frame_flags & 0x03) <= 1) {
01204         avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01205     } else {
01206         avctx->sample_fmt = AV_SAMPLE_FMT_S32;
01207     }
01208 
01209     /* get output buffer */
01210     s->frame.nb_samples = s->samples;
01211     if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
01212         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01213         return ret;
01214     }
01215 
01216     while (buf_size > 0) {
01217         if (!s->multichannel) {
01218             frame_size = buf_size;
01219         } else {
01220             if (!s->mkv_mode) {
01221                 frame_size = AV_RL32(buf) - 12; buf += 4; buf_size -= 4;
01222             } else {
01223                 if (buf_size < 12) //MKV files can have zero flags after last block
01224                     break;
01225                 frame_size = AV_RL32(buf + 8) + 12;
01226             }
01227         }
01228         if (frame_size < 0 || frame_size > buf_size) {
01229             av_log(avctx, AV_LOG_ERROR, "Block %d has invalid size (size %d "
01230                    "vs. %d bytes left)\n", s->block, frame_size, buf_size);
01231             wavpack_decode_flush(avctx);
01232             return AVERROR_INVALIDDATA;
01233         }
01234         if ((samplecount = wavpack_decode_block(avctx, s->block,
01235                                                 s->frame.data[0], got_frame_ptr,
01236                                                 buf, frame_size)) < 0) {
01237             wavpack_decode_flush(avctx);
01238             return samplecount;
01239         }
01240         s->block++;
01241         buf += frame_size; buf_size -= frame_size;
01242     }
01243 
01244     if (*got_frame_ptr)
01245         *(AVFrame *)data = s->frame;
01246 
01247     return avpkt->size;
01248 }
01249 
01250 AVCodec ff_wavpack_decoder = {
01251     .name           = "wavpack",
01252     .type           = AVMEDIA_TYPE_AUDIO,
01253     .id             = CODEC_ID_WAVPACK,
01254     .priv_data_size = sizeof(WavpackContext),
01255     .init           = wavpack_decode_init,
01256     .close          = wavpack_decode_end,
01257     .decode         = wavpack_decode_frame,
01258     .flush          = wavpack_decode_flush,
01259     .capabilities   = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01260     .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
01261 };