• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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

Generated on Wed Apr 11 2012 07:31:35 for FFmpeg by  doxygen 1.7.1