LCOV - code coverage report
Current view: top level - libavcodec - wavpack.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 556 734 75.7 %
Date: 2017-12-15 11:05:35 Functions: 14 16 87.5 %

          Line data    Source code
       1             : /*
       2             :  * WavPack lossless audio decoder
       3             :  * Copyright (c) 2006,2011 Konstantin Shishkov
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavutil/channel_layout.h"
      23             : 
      24             : #define BITSTREAM_READER_LE
      25             : #include "avcodec.h"
      26             : #include "bytestream.h"
      27             : #include "get_bits.h"
      28             : #include "internal.h"
      29             : #include "thread.h"
      30             : #include "unary.h"
      31             : #include "wavpack.h"
      32             : 
      33             : /**
      34             :  * @file
      35             :  * WavPack lossless audio decoder
      36             :  */
      37             : 
      38             : typedef struct SavedContext {
      39             :     int offset;
      40             :     int size;
      41             :     int bits_used;
      42             :     uint32_t crc;
      43             : } SavedContext;
      44             : 
      45             : typedef struct WavpackFrameContext {
      46             :     AVCodecContext *avctx;
      47             :     int frame_flags;
      48             :     int stereo, stereo_in;
      49             :     int joint;
      50             :     uint32_t CRC;
      51             :     GetBitContext gb;
      52             :     int got_extra_bits;
      53             :     uint32_t crc_extra_bits;
      54             :     GetBitContext gb_extra_bits;
      55             :     int data_size; // in bits
      56             :     int samples;
      57             :     int terms;
      58             :     Decorr decorr[MAX_TERMS];
      59             :     int zero, one, zeroes;
      60             :     int extra_bits;
      61             :     int and, or, shift;
      62             :     int post_shift;
      63             :     int hybrid, hybrid_bitrate;
      64             :     int hybrid_maxclip, hybrid_minclip;
      65             :     int float_flag;
      66             :     int float_shift;
      67             :     int float_max_exp;
      68             :     WvChannel ch[2];
      69             :     int pos;
      70             :     SavedContext sc, extra_sc;
      71             : } WavpackFrameContext;
      72             : 
      73             : #define WV_MAX_FRAME_DECODERS 14
      74             : 
      75             : typedef struct WavpackContext {
      76             :     AVCodecContext *avctx;
      77             : 
      78             :     WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
      79             :     int fdec_num;
      80             : 
      81             :     int block;
      82             :     int samples;
      83             :     int ch_offset;
      84             : } WavpackContext;
      85             : 
      86             : #define LEVEL_DECAY(a)  (((a) + 0x80) >> 8)
      87             : 
      88    28237949 : static av_always_inline int get_tail(GetBitContext *gb, int k)
      89             : {
      90             :     int p, e, res;
      91             : 
      92    28237949 :     if (k < 1)
      93     3061024 :         return 0;
      94    25176925 :     p   = av_log2(k);
      95    25176925 :     e   = (1 << (p + 1)) - k - 1;
      96    25176925 :     res = get_bitsz(gb, p);
      97    25176925 :     if (res >= e)
      98    15852762 :         res = (res << 1) - e + get_bits1(gb);
      99    25176925 :     return res;
     100             : }
     101             : 
     102    34605992 : static int update_error_limit(WavpackFrameContext *ctx)
     103             : {
     104             :     int i, br[2], sl[2];
     105             : 
     106    94481518 :     for (i = 0; i <= ctx->stereo_in; i++) {
     107    59875526 :         if (ctx->ch[i].bitrate_acc > UINT_MAX - ctx->ch[i].bitrate_delta)
     108           0 :             return AVERROR_INVALIDDATA;
     109    59875526 :         ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
     110    59875526 :         br[i]                   = ctx->ch[i].bitrate_acc >> 16;
     111    59875526 :         sl[i]                   = LEVEL_DECAY(ctx->ch[i].slow_level);
     112             :     }
     113    34605992 :     if (ctx->stereo_in && ctx->hybrid_bitrate) {
     114    25269534 :         int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
     115    25269534 :         if (balance > br[0]) {
     116     7996128 :             br[1] = br[0] * 2;
     117     7996128 :             br[0] = 0;
     118    17273406 :         } else if (-balance > br[0]) {
     119     2254389 :             br[0]  *= 2;
     120     2254389 :             br[1]   = 0;
     121             :         } else {
     122    15019017 :             br[1] = br[0] + balance;
     123    15019017 :             br[0] = br[0] - balance;
     124             :         }
     125             :     }
     126    94481518 :     for (i = 0; i <= ctx->stereo_in; i++) {
     127    59875526 :         if (ctx->hybrid_bitrate) {
     128    59875526 :             if (sl[i] - br[i] > -0x100)
     129    59872999 :                 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
     130             :             else
     131        2527 :                 ctx->ch[i].error_limit = 0;
     132             :         } else {
     133           0 :             ctx->ch[i].error_limit = wp_exp2(br[i]);
     134             :         }
     135             :     }
     136             : 
     137    34605992 :     return 0;
     138             : }
     139             : 
     140   350653857 : static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
     141             :                         int channel, int *last)
     142             : {
     143             :     int t, t2;
     144             :     int sign, base, add, ret;
     145   350653857 :     WvChannel *c = &ctx->ch[channel];
     146             : 
     147   350653857 :     *last = 0;
     148             : 
     149   614624249 :     if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
     150   527779716 :         !ctx->zero && !ctx->one) {
     151   263766695 :         if (ctx->zeroes) {
     152   263562428 :             ctx->zeroes--;
     153   263562428 :             if (ctx->zeroes) {
     154   263457445 :                 c->slow_level -= LEVEL_DECAY(c->slow_level);
     155   263457445 :                 return 0;
     156             :             }
     157             :         } else {
     158      204267 :             t = get_unary_0_33(gb);
     159      204267 :             if (t >= 2) {
     160       70183 :                 if (t >= 32 || get_bits_left(gb) < t - 1)
     161             :                     goto error;
     162       70183 :                 t = get_bits_long(gb, t - 1) | (1 << (t - 1));
     163             :             } else {
     164      134084 :                 if (get_bits_left(gb) < 0)
     165           0 :                     goto error;
     166             :             }
     167      204267 :             ctx->zeroes = t;
     168      204267 :             if (ctx->zeroes) {
     169      111005 :                 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
     170      111005 :                 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
     171      111005 :                 c->slow_level -= LEVEL_DECAY(c->slow_level);
     172      111005 :                 return 0;
     173             :             }
     174             :         }
     175             :     }
     176             : 
     177    87085407 :     if (ctx->zero) {
     178    36000670 :         t         = 0;
     179    36000670 :         ctx->zero = 0;
     180             :     } else {
     181    51084737 :         t = get_unary_0_33(gb);
     182    51084737 :         if (get_bits_left(gb) < 0)
     183           0 :             goto error;
     184    51084737 :         if (t == 16) {
     185       20133 :             t2 = get_unary_0_33(gb);
     186       20133 :             if (t2 < 2) {
     187        4851 :                 if (get_bits_left(gb) < 0)
     188           0 :                     goto error;
     189        4851 :                 t += t2;
     190             :             } else {
     191       30564 :                 if (t2 >= 32 || get_bits_left(gb) < t2 - 1)
     192             :                     goto error;
     193       15282 :                 t += get_bits_long(gb, t2 - 1) | (1 << (t2 - 1));
     194             :             }
     195             :         }
     196             : 
     197    51084737 :         if (ctx->one) {
     198    15082701 :             ctx->one = t & 1;
     199    15082701 :             t        = (t >> 1) + 1;
     200             :         } else {
     201    36002036 :             ctx->one = t & 1;
     202    36002036 :             t      >>= 1;
     203             :         }
     204    51084737 :         ctx->zero = !ctx->one;
     205             :     }
     206             : 
     207    87085407 :     if (ctx->hybrid && !channel) {
     208    34605992 :         if (update_error_limit(ctx) < 0)
     209           0 :             goto error;
     210             :     }
     211             : 
     212    87085407 :     if (!t) {
     213    62074613 :         base = 0;
     214    62074613 :         add  = GET_MED(0) - 1;
     215    62074613 :         DEC_MED(0);
     216    25010794 :     } else if (t == 1) {
     217    17835077 :         base = GET_MED(0);
     218    17835077 :         add  = GET_MED(1) - 1;
     219    17835077 :         INC_MED(0);
     220    17835077 :         DEC_MED(1);
     221     7175717 :     } else if (t == 2) {
     222     5060188 :         base = GET_MED(0) + GET_MED(1);
     223     5060188 :         add  = GET_MED(2) - 1;
     224     5060188 :         INC_MED(0);
     225     5060188 :         INC_MED(1);
     226     5060188 :         DEC_MED(2);
     227             :     } else {
     228     2115529 :         base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2U);
     229     2115529 :         add  = GET_MED(2) - 1;
     230     2115529 :         INC_MED(0);
     231     2115529 :         INC_MED(1);
     232     2115529 :         INC_MED(2);
     233             :     }
     234    87085407 :     if (!c->error_limit) {
     235    28237949 :         if (add >= 0x2000000U) {
     236           0 :             av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
     237           0 :             goto error;
     238             :         }
     239    28237949 :         ret = base + get_tail(gb, add);
     240    28237949 :         if (get_bits_left(gb) <= 0)
     241           0 :             goto error;
     242             :     } else {
     243    58847458 :         int mid = (base * 2U + add + 1) >> 1;
     244   143114189 :         while (add > c->error_limit) {
     245    25419273 :             if (get_bits_left(gb) <= 0)
     246           0 :                 goto error;
     247    25419273 :             if (get_bits1(gb)) {
     248    10905078 :                 add -= (mid - (unsigned)base);
     249    10905078 :                 base = mid;
     250             :             } else
     251    14514195 :                 add = mid - (unsigned)base - 1;
     252    25419273 :             mid = (base * 2U + add + 1) >> 1;
     253             :         }
     254    58847458 :         ret = mid;
     255             :     }
     256    87085407 :     sign = get_bits1(gb);
     257    87085407 :     if (ctx->hybrid_bitrate)
     258    59874831 :         c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
     259    87085407 :     return sign ? ~ret : ret;
     260             : 
     261           0 : error:
     262           0 :     ret = get_bits_left(gb);
     263           0 :     if (ret <= 0) {
     264           0 :         av_log(ctx->avctx, AV_LOG_ERROR, "Too few bits (%d) left\n", ret);
     265             :     }
     266           0 :     *last = 1;
     267           0 :     return 0;
     268             : }
     269             : 
     270   347479371 : static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
     271             :                                        unsigned S)
     272             : {
     273             :     unsigned bit;
     274             : 
     275   347479371 :     if (s->extra_bits) {
     276      352800 :         S *= 1 << s->extra_bits;
     277             : 
     278      705600 :         if (s->got_extra_bits &&
     279      352800 :             get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
     280      352800 :             S   |= get_bits_long(&s->gb_extra_bits, s->extra_bits);
     281      352800 :             *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
     282             :         }
     283             :     }
     284             : 
     285   347479371 :     bit = (S & s->and) | s->or;
     286   347479371 :     bit = ((S + bit) << s->shift) - bit;
     287             : 
     288   347479371 :     if (s->hybrid)
     289   320166556 :         bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
     290             : 
     291   347479371 :     return bit << s->post_shift;
     292             : }
     293             : 
     294     3174484 : static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
     295             : {
     296             :     union {
     297             :         float    f;
     298             :         uint32_t u;
     299             :     } value;
     300             : 
     301             :     unsigned int sign;
     302     3174484 :     int exp = s->float_max_exp;
     303             : 
     304     3174484 :     if (s->got_extra_bits) {
     305      828900 :         const int max_bits  = 1 + 23 + 8 + 1;
     306      828900 :         const int left_bits = get_bits_left(&s->gb_extra_bits);
     307             : 
     308      828900 :         if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)
     309           0 :             return 0.0;
     310             :     }
     311             : 
     312     3174484 :     if (S) {
     313     3174479 :         S  *= 1U << s->float_shift;
     314     3174479 :         sign = S < 0;
     315     3174479 :         if (sign)
     316     1588502 :             S = -(unsigned)S;
     317     3174479 :         if (S >= 0x1000000U) {
     318           0 :             if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
     319           0 :                 S = get_bits(&s->gb_extra_bits, 23);
     320             :             else
     321           0 :                 S = 0;
     322           0 :             exp = 255;
     323     3174479 :         } else if (exp) {
     324     3174479 :             int shift = 23 - av_log2(S);
     325     3174479 :             exp = s->float_max_exp;
     326     3174479 :             if (exp <= shift)
     327           0 :                 shift = --exp;
     328     3174479 :             exp -= shift;
     329             : 
     330     3174479 :             if (shift) {
     331     3130357 :                 S <<= shift;
     332     6260714 :                 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
     333     3948278 :                     (s->got_extra_bits &&
     334      817921 :                      (s->float_flag & WV_FLT_SHIFT_SAME) &&
     335           0 :                      get_bits1(&s->gb_extra_bits))) {
     336           0 :                     S |= (1 << shift) - 1;
     337     3948278 :                 } else if (s->got_extra_bits &&
     338      817921 :                            (s->float_flag & WV_FLT_SHIFT_SENT)) {
     339      817921 :                     S |= get_bits(&s->gb_extra_bits, shift);
     340             :                 }
     341             :             }
     342             :         } else {
     343           0 :             exp = s->float_max_exp;
     344             :         }
     345     3174479 :         S &= 0x7fffff;
     346             :     } else {
     347           5 :         sign = 0;
     348           5 :         exp  = 0;
     349           5 :         if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
     350           2 :             if (get_bits1(&s->gb_extra_bits)) {
     351           2 :                 S = get_bits(&s->gb_extra_bits, 23);
     352           2 :                 if (s->float_max_exp >= 25)
     353           2 :                     exp = get_bits(&s->gb_extra_bits, 8);
     354           2 :                 sign = get_bits1(&s->gb_extra_bits);
     355             :             } else {
     356           0 :                 if (s->float_flag & WV_FLT_ZERO_SIGN)
     357           0 :                     sign = get_bits1(&s->gb_extra_bits);
     358             :             }
     359             :         }
     360             :     }
     361             : 
     362     3174484 :     *crc = *crc * 27 + S * 9 + exp * 3 + sign;
     363             : 
     364     3174484 :     value.u = (sign << 31) | (exp << 23) | S;
     365     3174484 :     return value.f;
     366             : }
     367             : 
     368        8395 : static void wv_reset_saved_context(WavpackFrameContext *s)
     369             : {
     370        8395 :     s->pos    = 0;
     371        8395 :     s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
     372        8395 : }
     373             : 
     374           0 : static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
     375             :                                uint32_t crc_extra_bits)
     376             : {
     377           0 :     if (crc != s->CRC) {
     378           0 :         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
     379           0 :         return AVERROR_INVALIDDATA;
     380             :     }
     381           0 :     if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
     382           0 :         av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
     383           0 :         return AVERROR_INVALIDDATA;
     384             :     }
     385             : 
     386           0 :     return 0;
     387             : }
     388             : 
     389        7589 : static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
     390             :                                    void *dst_l, void *dst_r, const int type)
     391             : {
     392        7589 :     int i, j, count = 0;
     393             :     int last, t;
     394             :     int A, B, L, L2, R, R2;
     395        7589 :     int pos                 = s->pos;
     396        7589 :     uint32_t crc            = s->sc.crc;
     397        7589 :     uint32_t crc_extra_bits = s->extra_sc.crc;
     398        7589 :     int16_t *dst16_l        = dst_l;
     399        7589 :     int16_t *dst16_r        = dst_r;
     400        7589 :     int32_t *dst32_l        = dst_l;
     401        7589 :     int32_t *dst32_r        = dst_r;
     402        7589 :     float *dstfl_l          = dst_l;
     403        7589 :     float *dstfl_r          = dst_r;
     404             : 
     405        7589 :     s->one = s->zero = s->zeroes = 0;
     406             :     do {
     407   167521248 :         L = wv_get_value(s, gb, 0, &last);
     408   167521248 :         if (last)
     409           0 :             break;
     410   167521248 :         R = wv_get_value(s, gb, 1, &last);
     411   167521248 :         if (last)
     412           0 :             break;
     413  1041360248 :         for (i = 0; i < s->terms; i++) {
     414   873839000 :             t = s->decorr[i].value;
     415   873839000 :             if (t > 0) {
     416   857098928 :                 if (t > 8) {
     417   486031480 :                     if (t & 1) {
     418   157246340 :                         A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
     419   157246340 :                         B = 2U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
     420             :                     } else {
     421   328785140 :                         A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
     422   328785140 :                         B = (int)(3U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
     423             :                     }
     424   486031480 :                     s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
     425   486031480 :                     s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
     426   486031480 :                     j                        = 0;
     427             :                 } else {
     428   371067448 :                     A = s->decorr[i].samplesA[pos];
     429   371067448 :                     B = s->decorr[i].samplesB[pos];
     430   371067448 :                     j = (pos + t) & 7;
     431             :                 }
     432   857098928 :                 if (type != AV_SAMPLE_FMT_S16P) {
     433    21296280 :                     L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
     434    21296280 :                     R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
     435             :                 } else {
     436   835802648 :                     L2 = L + ((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
     437   835802648 :                     R2 = R + ((int)(s->decorr[i].weightB * (unsigned)B + 512) >> 10);
     438             :                 }
     439   857098928 :                 if (A && L)
     440   189076206 :                     s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
     441   857098928 :                 if (B && R)
     442   187088854 :                     s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
     443   857098928 :                 s->decorr[i].samplesA[j] = L = L2;
     444   857098928 :                 s->decorr[i].samplesB[j] = R = R2;
     445    16740072 :             } else if (t == -1) {
     446     3724236 :                 if (type != AV_SAMPLE_FMT_S16P)
     447           0 :                     L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
     448             :                 else
     449     3724236 :                     L2 = L + ((int)(s->decorr[i].weightA * (unsigned)s->decorr[i].samplesA[0] + 512) >> 10);
     450     3724236 :                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
     451     3724236 :                 L = L2;
     452     3724236 :                 if (type != AV_SAMPLE_FMT_S16P)
     453           0 :                     R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
     454             :                 else
     455     3724236 :                     R2 = R + ((int)(s->decorr[i].weightB * (unsigned)L2 + 512) >> 10);
     456     3724236 :                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
     457     3724236 :                 R                        = R2;
     458     3724236 :                 s->decorr[i].samplesA[0] = R;
     459             :             } else {
     460    13015836 :                 if (type != AV_SAMPLE_FMT_S16P)
     461      815850 :                     R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
     462             :                 else
     463    12199986 :                     R2 = R + ((int)(s->decorr[i].weightB * (unsigned)s->decorr[i].samplesB[0] + 512) >> 10);
     464    13015836 :                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
     465    13015836 :                 R = R2;
     466             : 
     467    13015836 :                 if (t == -3) {
     468    10657190 :                     R2                       = s->decorr[i].samplesA[0];
     469    10657190 :                     s->decorr[i].samplesA[0] = R;
     470             :                 }
     471             : 
     472    13015836 :                 if (type != AV_SAMPLE_FMT_S16P)
     473      815850 :                     L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
     474             :                 else
     475    12199986 :                     L2 = L + ((int)(s->decorr[i].weightA * (unsigned)R2 + 512) >> 10);
     476    13015836 :                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
     477    13015836 :                 L                        = L2;
     478    13015836 :                 s->decorr[i].samplesB[0] = L;
     479             :             }
     480             :         }
     481             : 
     482   167521248 :         if (type == AV_SAMPLE_FMT_S16P) {
     483   163098822 :             if (FFABS(L) + (unsigned)FFABS(R) > (1<<19)) {
     484           1 :                 av_log(s->avctx, AV_LOG_ERROR, "sample %d %d too large\n", L, R);
     485           1 :                 return AVERROR_INVALIDDATA;
     486             :             }
     487             :         }
     488             : 
     489   167521247 :         pos = (pos + 1) & 7;
     490   167521247 :         if (s->joint)
     491   161271851 :             L += (unsigned)(R -= (unsigned)(L >> 1));
     492   167521247 :         crc = (crc * 3 + L) * 3 + R;
     493             : 
     494   167521247 :         if (type == AV_SAMPLE_FMT_FLTP) {
     495     1371242 :             *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
     496     1371242 :             *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
     497   166150005 :         } else if (type == AV_SAMPLE_FMT_S32P) {
     498     3051184 :             *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
     499     3051184 :             *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
     500             :         } else {
     501   163098821 :             *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
     502   163098821 :             *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
     503             :         }
     504   167521247 :         count++;
     505   167521247 :     } while (!last && count < s->samples);
     506             : 
     507        7588 :     wv_reset_saved_context(s);
     508             : 
     509        7588 :     if (last && count < s->samples) {
     510           0 :         int size = av_get_bytes_per_sample(type);
     511           0 :         memset((uint8_t*)dst_l + count*size, 0, (s->samples-count)*size);
     512           0 :         memset((uint8_t*)dst_r + count*size, 0, (s->samples-count)*size);
     513             :     }
     514             : 
     515        7588 :     if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
     516           0 :         wv_check_crc(s, crc, crc_extra_bits))
     517           0 :         return AVERROR_INVALIDDATA;
     518             : 
     519        7588 :     return 0;
     520             : }
     521             : 
     522         689 : static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
     523             :                                  void *dst, const int type)
     524             : {
     525         689 :     int i, j, count = 0;
     526             :     int last, t;
     527             :     int A, S, T;
     528         689 :     int pos                  = s->pos;
     529         689 :     uint32_t crc             = s->sc.crc;
     530         689 :     uint32_t crc_extra_bits  = s->extra_sc.crc;
     531         689 :     int16_t *dst16           = dst;
     532         689 :     int32_t *dst32           = dst;
     533         689 :     float *dstfl             = dst;
     534             : 
     535         689 :     s->one = s->zero = s->zeroes = 0;
     536             :     do {
     537    15611361 :         T = wv_get_value(s, gb, 0, &last);
     538    15611361 :         S = 0;
     539    15611361 :         if (last)
     540           0 :             break;
     541   120488278 :         for (i = 0; i < s->terms; i++) {
     542   104876917 :             t = s->decorr[i].value;
     543   104876917 :             if (t > 8) {
     544    44372416 :                 if (t & 1)
     545    11939195 :                     A =  2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
     546             :                 else
     547    32433221 :                     A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
     548    44372416 :                 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
     549    44372416 :                 j                        = 0;
     550             :             } else {
     551    60504501 :                 A = s->decorr[i].samplesA[pos];
     552    60504501 :                 j = (pos + t) & 7;
     553             :             }
     554   104876917 :             if (type != AV_SAMPLE_FMT_S16P)
     555    29647840 :                 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
     556             :             else
     557    75229077 :                 S = T + ((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
     558   104876917 :             if (A && T)
     559    92156442 :                 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
     560   104876917 :             s->decorr[i].samplesA[j] = T = S;
     561             :         }
     562    15611361 :         pos = (pos + 1) & 7;
     563    15611361 :         crc = crc * 3 + S;
     564             : 
     565    15611361 :         if (type == AV_SAMPLE_FMT_FLTP) {
     566      432000 :             *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
     567    15179361 :         } else if (type == AV_SAMPLE_FMT_S32P) {
     568     4691168 :             *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
     569             :         } else {
     570    10488193 :             *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
     571             :         }
     572    15611361 :         count++;
     573    15611361 :     } while (!last && count < s->samples);
     574             : 
     575         689 :     wv_reset_saved_context(s);
     576             : 
     577         689 :     if (last && count < s->samples) {
     578           0 :         int size = av_get_bytes_per_sample(type);
     579           0 :         memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
     580             :     }
     581             : 
     582         689 :     if (s->avctx->err_recognition & AV_EF_CRCCHECK) {
     583           0 :         int ret = wv_check_crc(s, crc, crc_extra_bits);
     584           0 :         if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
     585           0 :             return ret;
     586             :     }
     587             : 
     588         689 :     return 0;
     589             : }
     590             : 
     591         117 : static av_cold int wv_alloc_frame_context(WavpackContext *c)
     592             : {
     593         117 :     if (c->fdec_num == WV_MAX_FRAME_DECODERS)
     594           0 :         return -1;
     595             : 
     596         117 :     c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
     597         117 :     if (!c->fdec[c->fdec_num])
     598           0 :         return -1;
     599         117 :     c->fdec_num++;
     600         117 :     c->fdec[c->fdec_num - 1]->avctx = c->avctx;
     601         117 :     wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
     602             : 
     603         117 :     return 0;
     604             : }
     605             : 
     606             : #if HAVE_THREADS
     607           0 : static int init_thread_copy(AVCodecContext *avctx)
     608             : {
     609           0 :     WavpackContext *s = avctx->priv_data;
     610           0 :     s->avctx = avctx;
     611           0 :     return 0;
     612             : }
     613             : #endif
     614             : 
     615          63 : static av_cold int wavpack_decode_init(AVCodecContext *avctx)
     616             : {
     617          63 :     WavpackContext *s = avctx->priv_data;
     618             : 
     619          63 :     s->avctx = avctx;
     620             : 
     621          63 :     s->fdec_num = 0;
     622             : 
     623          63 :     return 0;
     624             : }
     625             : 
     626          63 : static av_cold int wavpack_decode_end(AVCodecContext *avctx)
     627             : {
     628          63 :     WavpackContext *s = avctx->priv_data;
     629             :     int i;
     630             : 
     631         180 :     for (i = 0; i < s->fdec_num; i++)
     632         117 :         av_freep(&s->fdec[i]);
     633          63 :     s->fdec_num = 0;
     634             : 
     635          63 :     return 0;
     636             : }
     637             : 
     638        8278 : static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
     639             :                                 AVFrame *frame, const uint8_t *buf, int buf_size)
     640             : {
     641        8278 :     WavpackContext *wc = avctx->priv_data;
     642        8278 :     ThreadFrame tframe = { .f = frame };
     643             :     WavpackFrameContext *s;
     644             :     GetByteContext gb;
     645        8278 :     void *samples_l = NULL, *samples_r = NULL;
     646             :     int ret;
     647        8278 :     int got_terms   = 0, got_weights = 0, got_samples = 0,
     648        8278 :         got_entropy = 0, got_bs      = 0, got_float   = 0, got_hybrid = 0;
     649             :     int i, j, id, size, ssize, weights, t;
     650        8278 :     int bpp, chan = 0, chmask = 0, orig_bpp, sample_rate = 0;
     651             :     int multiblock;
     652             : 
     653        8278 :     if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
     654           0 :         av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
     655           0 :         return AVERROR_INVALIDDATA;
     656             :     }
     657             : 
     658        8278 :     s = wc->fdec[block_no];
     659        8278 :     if (!s) {
     660           0 :         av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
     661             :                block_no);
     662           0 :         return AVERROR_INVALIDDATA;
     663             :     }
     664             : 
     665        8278 :     memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
     666        8278 :     memset(s->ch, 0, sizeof(s->ch));
     667        8278 :     s->extra_bits     = 0;
     668        8278 :     s->and            = s->or = s->shift = 0;
     669        8278 :     s->got_extra_bits = 0;
     670             : 
     671        8278 :     bytestream2_init(&gb, buf, buf_size);
     672             : 
     673        8278 :     s->samples = bytestream2_get_le32(&gb);
     674        8278 :     if (s->samples != wc->samples) {
     675           0 :         av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
     676             :                "a sequence: %d and %d\n", wc->samples, s->samples);
     677           0 :         return AVERROR_INVALIDDATA;
     678             :     }
     679        8278 :     s->frame_flags = bytestream2_get_le32(&gb);
     680        8278 :     bpp            = av_get_bytes_per_sample(avctx->sample_fmt);
     681        8278 :     orig_bpp       = ((s->frame_flags & 0x03) + 1) << 3;
     682        8278 :     multiblock     = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
     683             : 
     684        8278 :     s->stereo         = !(s->frame_flags & WV_MONO);
     685        8278 :     s->stereo_in      =  (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
     686        8278 :     s->joint          =   s->frame_flags & WV_JOINT_STEREO;
     687        8278 :     s->hybrid         =   s->frame_flags & WV_HYBRID_MODE;
     688        8278 :     s->hybrid_bitrate =   s->frame_flags & WV_HYBRID_BITRATE;
     689        8278 :     s->post_shift     = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
     690        8278 :     if (s->post_shift < 0 || s->post_shift > 31) {
     691           0 :         return AVERROR_INVALIDDATA;
     692             :     }
     693        8278 :     s->hybrid_maxclip =  ((1LL << (orig_bpp - 1)) - 1);
     694        8278 :     s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
     695        8278 :     s->CRC            = bytestream2_get_le32(&gb);
     696             : 
     697             :     // parse metadata blocks
     698       66080 :     while (bytestream2_get_bytes_left(&gb)) {
     699       49524 :         id   = bytestream2_get_byte(&gb);
     700       49524 :         size = bytestream2_get_byte(&gb);
     701       49524 :         if (id & WP_IDF_LONG) {
     702        8304 :             size |= (bytestream2_get_byte(&gb)) << 8;
     703        8304 :             size |= (bytestream2_get_byte(&gb)) << 16;
     704             :         }
     705       49524 :         size <<= 1; // size is specified in words
     706       49524 :         ssize  = size;
     707       49524 :         if (id & WP_IDF_ODD)
     708        8736 :             size--;
     709       49524 :         if (size < 0) {
     710           0 :             av_log(avctx, AV_LOG_ERROR,
     711             :                    "Got incorrect block %02X with size %i\n", id, size);
     712           0 :             break;
     713             :         }
     714       49524 :         if (bytestream2_get_bytes_left(&gb) < ssize) {
     715           0 :             av_log(avctx, AV_LOG_ERROR,
     716             :                    "Block size %i is out of bounds\n", size);
     717           0 :             break;
     718             :         }
     719       49524 :         switch (id & WP_IDF_MASK) {
     720        8278 :         case WP_ID_DECTERMS:
     721        8278 :             if (size > MAX_TERMS) {
     722           0 :                 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
     723           0 :                 s->terms = 0;
     724           0 :                 bytestream2_skip(&gb, ssize);
     725           0 :                 continue;
     726             :             }
     727        8278 :             s->terms = size;
     728       52612 :             for (i = 0; i < s->terms; i++) {
     729       44334 :                 uint8_t val = bytestream2_get_byte(&gb);
     730       44334 :                 s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
     731       44334 :                 s->decorr[s->terms - i - 1].delta =  val >> 5;
     732             :             }
     733        8278 :             got_terms = 1;
     734        8278 :             break;
     735        8278 :         case WP_ID_DECWEIGHTS:
     736        8278 :             if (!got_terms) {
     737           0 :                 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
     738           0 :                 continue;
     739             :             }
     740        8278 :             weights = size >> s->stereo_in;
     741        8278 :             if (weights > MAX_TERMS || weights > s->terms) {
     742           0 :                 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
     743           0 :                 bytestream2_skip(&gb, ssize);
     744           0 :                 continue;
     745             :             }
     746       22844 :             for (i = 0; i < weights; i++) {
     747       14566 :                 t = (int8_t)bytestream2_get_byte(&gb);
     748       14566 :                 s->decorr[s->terms - i - 1].weightA = t * (1 << 3);
     749       14566 :                 if (s->decorr[s->terms - i - 1].weightA > 0)
     750       19708 :                     s->decorr[s->terms - i - 1].weightA +=
     751        9854 :                         (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
     752       14566 :                 if (s->stereo_in) {
     753        9951 :                     t = (int8_t)bytestream2_get_byte(&gb);
     754        9951 :                     s->decorr[s->terms - i - 1].weightB = t * (1 << 3);
     755        9951 :                     if (s->decorr[s->terms - i - 1].weightB > 0)
     756       13694 :                         s->decorr[s->terms - i - 1].weightB +=
     757        6847 :                             (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
     758             :                 }
     759             :             }
     760        8278 :             got_weights = 1;
     761        8278 :             break;
     762        8278 :         case WP_ID_DECSAMPLES:
     763        8278 :             if (!got_terms) {
     764           0 :                 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
     765           0 :                 continue;
     766             :             }
     767        8278 :             t = 0;
     768       16550 :             for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
     769        8272 :                 if (s->decorr[i].value > 8) {
     770        8056 :                     s->decorr[i].samplesA[0] =
     771        8056 :                         wp_exp2(bytestream2_get_le16(&gb));
     772        8056 :                     s->decorr[i].samplesA[1] =
     773        8056 :                         wp_exp2(bytestream2_get_le16(&gb));
     774             : 
     775        8056 :                     if (s->stereo_in) {
     776        7452 :                         s->decorr[i].samplesB[0] =
     777        7452 :                             wp_exp2(bytestream2_get_le16(&gb));
     778        7452 :                         s->decorr[i].samplesB[1] =
     779        7452 :                             wp_exp2(bytestream2_get_le16(&gb));
     780        7452 :                         t                       += 4;
     781             :                     }
     782        8056 :                     t += 4;
     783         216 :                 } else if (s->decorr[i].value < 0) {
     784           0 :                     s->decorr[i].samplesA[0] =
     785           0 :                         wp_exp2(bytestream2_get_le16(&gb));
     786           0 :                     s->decorr[i].samplesB[0] =
     787           0 :                         wp_exp2(bytestream2_get_le16(&gb));
     788           0 :                     t                       += 4;
     789             :                 } else {
     790         589 :                     for (j = 0; j < s->decorr[i].value; j++) {
     791         373 :                         s->decorr[i].samplesA[j] =
     792         373 :                             wp_exp2(bytestream2_get_le16(&gb));
     793         373 :                         if (s->stereo_in) {
     794         219 :                             s->decorr[i].samplesB[j] =
     795         219 :                                 wp_exp2(bytestream2_get_le16(&gb));
     796             :                         }
     797             :                     }
     798         216 :                     t += s->decorr[i].value * 2 * (s->stereo_in + 1);
     799             :                 }
     800             :             }
     801        8278 :             got_samples = 1;
     802        8278 :             break;
     803        8278 :         case WP_ID_ENTROPY:
     804        8278 :             if (size != 6 * (s->stereo_in + 1)) {
     805           0 :                 av_log(avctx, AV_LOG_ERROR,
     806             :                        "Entropy vars size should be %i, got %i.\n",
     807           0 :                        6 * (s->stereo_in + 1), size);
     808           0 :                 bytestream2_skip(&gb, ssize);
     809           0 :                 continue;
     810             :             }
     811       24145 :             for (j = 0; j <= s->stereo_in; j++)
     812       63468 :                 for (i = 0; i < 3; i++) {
     813       47601 :                     s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
     814             :                 }
     815        8278 :             got_entropy = 1;
     816        8278 :             break;
     817        7458 :         case WP_ID_HYBRID:
     818        7458 :             if (s->hybrid_bitrate) {
     819       21982 :                 for (i = 0; i <= s->stereo_in; i++) {
     820       14524 :                     s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
     821       14524 :                     size               -= 2;
     822             :                 }
     823             :             }
     824       21982 :             for (i = 0; i < (s->stereo_in + 1); i++) {
     825       14524 :                 s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
     826       14524 :                 size                -= 2;
     827             :             }
     828        7458 :             if (size > 0) {
     829           0 :                 for (i = 0; i < (s->stereo_in + 1); i++) {
     830           0 :                     s->ch[i].bitrate_delta =
     831           0 :                         wp_exp2((int16_t)bytestream2_get_le16(&gb));
     832             :                 }
     833             :             } else {
     834       21982 :                 for (i = 0; i < (s->stereo_in + 1); i++)
     835       14524 :                     s->ch[i].bitrate_delta = 0;
     836             :             }
     837        7458 :             got_hybrid = 1;
     838        7458 :             break;
     839         168 :         case WP_ID_INT32INFO: {
     840             :             uint8_t val[4];
     841         168 :             if (size != 4) {
     842           0 :                 av_log(avctx, AV_LOG_ERROR,
     843             :                        "Invalid INT32INFO, size = %i\n",
     844             :                        size);
     845           0 :                 bytestream2_skip(&gb, ssize - 4);
     846           0 :                 continue;
     847             :             }
     848         168 :             bytestream2_get_buffer(&gb, val, 4);
     849         168 :             if (val[0] > 30) {
     850           0 :                 av_log(avctx, AV_LOG_ERROR,
     851           0 :                        "Invalid INT32INFO, extra_bits = %d (> 30)\n", val[0]);
     852           0 :                 continue;
     853         168 :             } else if (val[0]) {
     854           8 :                 s->extra_bits = val[0];
     855         160 :             } else if (val[1]) {
     856         160 :                 s->shift = val[1];
     857           0 :             } else if (val[2]) {
     858           0 :                 s->and   = s->or = 1;
     859           0 :                 s->shift = val[2];
     860           0 :             } else if (val[3]) {
     861           0 :                 s->and   = 1;
     862           0 :                 s->shift = val[3];
     863             :             }
     864         168 :             if (s->shift > 31) {
     865           0 :                 av_log(avctx, AV_LOG_ERROR,
     866             :                        "Invalid INT32INFO, shift = %d (> 31)\n", s->shift);
     867           0 :                 s->and = s->or = s->shift = 0;
     868           0 :                 continue;
     869             :             }
     870             :             /* original WavPack decoder forces 32-bit lossy sound to be treated
     871             :              * as 24-bit one in order to have proper clipping */
     872         168 :             if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
     873           0 :                 s->post_shift      += 8;
     874           0 :                 s->shift           -= 8;
     875           0 :                 s->hybrid_maxclip >>= 8;
     876           0 :                 s->hybrid_minclip >>= 8;
     877             :             }
     878         168 :             break;
     879             :         }
     880          67 :         case WP_ID_FLOATINFO:
     881          67 :             if (size != 4) {
     882           0 :                 av_log(avctx, AV_LOG_ERROR,
     883             :                        "Invalid FLOATINFO, size = %i\n", size);
     884           0 :                 bytestream2_skip(&gb, ssize);
     885           0 :                 continue;
     886             :             }
     887          67 :             s->float_flag    = bytestream2_get_byte(&gb);
     888          67 :             s->float_shift   = bytestream2_get_byte(&gb);
     889          67 :             s->float_max_exp = bytestream2_get_byte(&gb);
     890          67 :             if (s->float_shift > 31) {
     891           0 :                 av_log(avctx, AV_LOG_ERROR,
     892             :                        "Invalid FLOATINFO, shift = %d (> 31)\n", s->float_shift);
     893           0 :                 s->float_shift = 0;
     894           0 :                 continue;
     895             :             }
     896          67 :             got_float        = 1;
     897          67 :             bytestream2_skip(&gb, 1);
     898          67 :             break;
     899        8278 :         case WP_ID_DATA:
     900        8278 :             s->sc.offset = bytestream2_tell(&gb);
     901        8278 :             s->sc.size   = size * 8;
     902        8278 :             if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
     903           0 :                 return ret;
     904        8278 :             s->data_size = size * 8;
     905        8278 :             bytestream2_skip(&gb, size);
     906        8278 :             got_bs       = 1;
     907        8278 :             break;
     908          26 :         case WP_ID_EXTRABITS:
     909          26 :             if (size <= 4) {
     910           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
     911             :                        size);
     912           0 :                 bytestream2_skip(&gb, size);
     913           0 :                 continue;
     914             :             }
     915          26 :             s->extra_sc.offset = bytestream2_tell(&gb);
     916          26 :             s->extra_sc.size   = size * 8;
     917          26 :             if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
     918           0 :                 return ret;
     919          26 :             s->crc_extra_bits  = get_bits_long(&s->gb_extra_bits, 32);
     920          26 :             bytestream2_skip(&gb, size);
     921          26 :             s->got_extra_bits  = 1;
     922          26 :             break;
     923         297 :         case WP_ID_CHANINFO:
     924         297 :             if (size <= 1) {
     925           0 :                 av_log(avctx, AV_LOG_ERROR,
     926             :                        "Insufficient channel information\n");
     927           0 :                 return AVERROR_INVALIDDATA;
     928             :             }
     929         297 :             chan = bytestream2_get_byte(&gb);
     930         297 :             switch (size - 2) {
     931          23 :             case 0:
     932          23 :                 chmask = bytestream2_get_byte(&gb);
     933          23 :                 break;
     934         274 :             case 1:
     935         274 :                 chmask = bytestream2_get_le16(&gb);
     936         274 :                 break;
     937           0 :             case 2:
     938           0 :                 chmask = bytestream2_get_le24(&gb);
     939           0 :                 break;
     940           0 :             case 3:
     941           0 :                 chmask = bytestream2_get_le32(&gb);
     942           0 :                 break;
     943           0 :             case 5:
     944           0 :                 size = bytestream2_get_byte(&gb);
     945           0 :                 if (avctx->channels != size)
     946           0 :                     av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
     947             :                            " instead of %i.\n", size, avctx->channels);
     948           0 :                 chan  |= (bytestream2_get_byte(&gb) & 0xF) << 8;
     949           0 :                 chmask = bytestream2_get_le16(&gb);
     950           0 :                 break;
     951           0 :             default:
     952           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
     953             :                        size);
     954           0 :                 chan   = avctx->channels;
     955           0 :                 chmask = avctx->channel_layout;
     956             :             }
     957         297 :             break;
     958           0 :         case WP_ID_SAMPLE_RATE:
     959           0 :             if (size != 3) {
     960           0 :                 av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
     961           0 :                 return AVERROR_INVALIDDATA;
     962             :             }
     963           0 :             sample_rate = bytestream2_get_le24(&gb);
     964           0 :             break;
     965         118 :         default:
     966         118 :             bytestream2_skip(&gb, size);
     967             :         }
     968       49524 :         if (id & WP_IDF_ODD)
     969        8736 :             bytestream2_skip(&gb, 1);
     970             :     }
     971             : 
     972        8278 :     if (!got_terms) {
     973           0 :         av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
     974           0 :         return AVERROR_INVALIDDATA;
     975             :     }
     976        8278 :     if (!got_weights) {
     977           0 :         av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
     978           0 :         return AVERROR_INVALIDDATA;
     979             :     }
     980        8278 :     if (!got_samples) {
     981           0 :         av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
     982           0 :         return AVERROR_INVALIDDATA;
     983             :     }
     984        8278 :     if (!got_entropy) {
     985           0 :         av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
     986           0 :         return AVERROR_INVALIDDATA;
     987             :     }
     988        8278 :     if (s->hybrid && !got_hybrid) {
     989           0 :         av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
     990           0 :         return AVERROR_INVALIDDATA;
     991             :     }
     992        8278 :     if (!got_bs) {
     993           0 :         av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
     994           0 :         return AVERROR_INVALIDDATA;
     995             :     }
     996        8278 :     if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
     997           0 :         av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
     998           0 :         return AVERROR_INVALIDDATA;
     999             :     }
    1000        8278 :     if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
    1001           8 :         const int size   = get_bits_left(&s->gb_extra_bits);
    1002           8 :         const int wanted = s->samples * s->extra_bits << s->stereo_in;
    1003           8 :         if (size < wanted) {
    1004           0 :             av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
    1005           0 :             s->got_extra_bits = 0;
    1006             :         }
    1007             :     }
    1008             : 
    1009        8278 :     if (!wc->ch_offset) {
    1010        7546 :         int sr = (s->frame_flags >> 23) & 0xf;
    1011        7546 :         if (sr == 0xf) {
    1012           0 :             if (!sample_rate) {
    1013           0 :                 av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
    1014           0 :                 return AVERROR_INVALIDDATA;
    1015             :             }
    1016           0 :             avctx->sample_rate = sample_rate;
    1017             :         } else
    1018        7546 :             avctx->sample_rate = wv_rates[sr];
    1019             : 
    1020        7546 :         if (multiblock) {
    1021         297 :             if (chan)
    1022         297 :                 avctx->channels = chan;
    1023         297 :             if (chmask)
    1024         297 :                 avctx->channel_layout = chmask;
    1025             :         } else {
    1026        7249 :             avctx->channels       = s->stereo ? 2 : 1;
    1027        7249 :             avctx->channel_layout = s->stereo ? AV_CH_LAYOUT_STEREO :
    1028             :                                                 AV_CH_LAYOUT_MONO;
    1029             :         }
    1030             : 
    1031             :         /* get output buffer */
    1032        7546 :         frame->nb_samples = s->samples + 1;
    1033        7546 :         if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
    1034           0 :             return ret;
    1035        7546 :         frame->nb_samples = s->samples;
    1036             :     }
    1037             : 
    1038        8278 :     if (wc->ch_offset + s->stereo >= avctx->channels) {
    1039           0 :         av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
    1040           0 :         return ((avctx->err_recognition & AV_EF_EXPLODE) || !wc->ch_offset) ? AVERROR_INVALIDDATA : 0;
    1041             :     }
    1042             : 
    1043        8278 :     samples_l = frame->extended_data[wc->ch_offset];
    1044        8278 :     if (s->stereo)
    1045        7631 :         samples_r = frame->extended_data[wc->ch_offset + 1];
    1046             : 
    1047        8278 :     wc->ch_offset += 1 + s->stereo;
    1048             : 
    1049        8278 :     if (s->stereo_in) {
    1050        7589 :         ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
    1051        7589 :         if (ret < 0)
    1052           1 :             return ret;
    1053             :     } else {
    1054         689 :         ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
    1055         689 :         if (ret < 0)
    1056           0 :             return ret;
    1057             : 
    1058         689 :         if (s->stereo)
    1059          42 :             memcpy(samples_r, samples_l, bpp * s->samples);
    1060             :     }
    1061             : 
    1062        8277 :     return 0;
    1063             : }
    1064             : 
    1065           1 : static void wavpack_decode_flush(AVCodecContext *avctx)
    1066             : {
    1067           1 :     WavpackContext *s = avctx->priv_data;
    1068             :     int i;
    1069             : 
    1070           2 :     for (i = 0; i < s->fdec_num; i++)
    1071           1 :         wv_reset_saved_context(s->fdec[i]);
    1072           1 : }
    1073             : 
    1074        7546 : static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
    1075             :                                 int *got_frame_ptr, AVPacket *avpkt)
    1076             : {
    1077        7546 :     WavpackContext *s  = avctx->priv_data;
    1078        7546 :     const uint8_t *buf = avpkt->data;
    1079        7546 :     int buf_size       = avpkt->size;
    1080        7546 :     AVFrame *frame     = data;
    1081             :     int frame_size, ret, frame_flags;
    1082             : 
    1083        7546 :     if (avpkt->size <= WV_HEADER_SIZE)
    1084           0 :         return AVERROR_INVALIDDATA;
    1085             : 
    1086        7546 :     s->block     = 0;
    1087        7546 :     s->ch_offset = 0;
    1088             : 
    1089             :     /* determine number of samples */
    1090        7546 :     s->samples  = AV_RL32(buf + 20);
    1091        7546 :     frame_flags = AV_RL32(buf + 24);
    1092        7546 :     if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
    1093           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
    1094             :                s->samples);
    1095           0 :         return AVERROR_INVALIDDATA;
    1096             :     }
    1097             : 
    1098        7546 :     if (frame_flags & 0x80) {
    1099          67 :         avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
    1100        7479 :     } else if ((frame_flags & 0x03) <= 1) {
    1101        7349 :         avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
    1102             :     } else {
    1103         130 :         avctx->sample_fmt          = AV_SAMPLE_FMT_S32P;
    1104         130 :         avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
    1105             :     }
    1106             : 
    1107       23369 :     while (buf_size > 0) {
    1108        8278 :         if (buf_size <= WV_HEADER_SIZE)
    1109           0 :             break;
    1110        8278 :         frame_size = AV_RL32(buf + 4) - 12;
    1111        8278 :         buf       += 20;
    1112        8278 :         buf_size  -= 20;
    1113        8278 :         if (frame_size <= 0 || frame_size > buf_size) {
    1114           0 :             av_log(avctx, AV_LOG_ERROR,
    1115             :                    "Block %d has invalid size (size %d vs. %d bytes left)\n",
    1116             :                    s->block, frame_size, buf_size);
    1117           0 :             wavpack_decode_flush(avctx);
    1118           0 :             return AVERROR_INVALIDDATA;
    1119             :         }
    1120        8278 :         if ((ret = wavpack_decode_block(avctx, s->block,
    1121             :                                         frame, buf, frame_size)) < 0) {
    1122           1 :             wavpack_decode_flush(avctx);
    1123           1 :             return ret;
    1124             :         }
    1125        8277 :         s->block++;
    1126        8277 :         buf      += frame_size;
    1127        8277 :         buf_size -= frame_size;
    1128             :     }
    1129             : 
    1130        7545 :     if (s->ch_offset != avctx->channels) {
    1131           0 :         av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
    1132           0 :         return AVERROR_INVALIDDATA;
    1133             :     }
    1134             : 
    1135        7545 :     *got_frame_ptr = 1;
    1136             : 
    1137        7545 :     return avpkt->size;
    1138             : }
    1139             : 
    1140             : AVCodec ff_wavpack_decoder = {
    1141             :     .name           = "wavpack",
    1142             :     .long_name      = NULL_IF_CONFIG_SMALL("WavPack"),
    1143             :     .type           = AVMEDIA_TYPE_AUDIO,
    1144             :     .id             = AV_CODEC_ID_WAVPACK,
    1145             :     .priv_data_size = sizeof(WavpackContext),
    1146             :     .init           = wavpack_decode_init,
    1147             :     .close          = wavpack_decode_end,
    1148             :     .decode         = wavpack_decode_frame,
    1149             :     .flush          = wavpack_decode_flush,
    1150             :     .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
    1151             :     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
    1152             : };

Generated by: LCOV version 1.13