]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/wavpack.c
Only compile golomb.c when needed.
[ffmpeg] / libavcodec / wavpack.c
index 4a4a5847e90e52852a1ef9ab60a5d7bd8731e63b..e79d4a570e4900be664bf1e6535b7b3d91c6953b 100644 (file)
@@ -2,18 +2,20 @@
  * WavPack lossless audio decoder
  * Copyright (c) 2006 Konstantin Shishkov
  *
- * This library is free software; you can redistribute it and/or
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #define ALT_BITSTREAM_READER_LE
@@ -96,7 +98,7 @@ static const uint8_t wp_exp2_table [256] = {
     0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff
 };
 
-static always_inline int wp_exp2(int16_t val)
+static av_always_inline int wp_exp2(int16_t val)
 {
     int res, neg = 0;
 
@@ -135,14 +137,14 @@ static inline int get_unary(GetBitContext *gb){
         }
 
 
-static always_inline int get_tail(GetBitContext *gb, int k)
+static av_always_inline int get_tail(GetBitContext *gb, int k)
 {
     int p, e, res;
 
-    if(k<1 || k>65535)return 0;
-    p = av_log2_16bit(k);
+    if(k<1)return 0;
+    p = av_log2(k);
     e = (1 << (p + 1)) - k - 1;
-    res = get_bits(gb, p);
+    res = p ? get_bits(gb, p) : 0;
     if(res >= e){
         res = (res<<1) - e + get_bits1(gb);
     }
@@ -381,13 +383,25 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
     uint8_t* buf_end = buf + buf_size;
     int i, j, id, size, ssize, weights, t;
 
-    if (buf_size == 0) return 0;
+    if (buf_size == 0){
+        *data_size = 0;
+        return 0;
+    }
 
     memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
 
-    s->samples = LE_32(buf); buf += 4;
-    s->joint = LE_32(buf) & WV_JOINT; buf += 4;
-    s->CRC = LE_32(buf); buf += 4;
+    s->samples = AV_RL32(buf); buf += 4;
+    if(!s->samples){
+        *data_size = 0;
+        return buf_size;
+    }
+    /* should not happen but who knows */
+    if(s->samples * 2 * avctx->channels > *data_size){
+        av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
+        return -1;
+    }
+    s->joint = AV_RL32(buf) & WV_JOINT; buf += 4;
+    s->CRC = AV_RL32(buf); buf += 4;
     // parse metadata blocks
     while(buf < buf_end){
         id = *buf++;
@@ -459,23 +473,23 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
             t = 0;
             for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
                 if(s->decorr[i].value > 8){
-                    s->decorr[i].samplesA[0] = wp_exp2(LE_16(buf)); buf += 2;
-                    s->decorr[i].samplesA[1] = wp_exp2(LE_16(buf)); buf += 2;
+                    s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
+                    s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
                     if(s->stereo){
-                        s->decorr[i].samplesB[0] = wp_exp2(LE_16(buf)); buf += 2;
-                        s->decorr[i].samplesB[1] = wp_exp2(LE_16(buf)); buf += 2;
+                        s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
+                        s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
                         t += 4;
                     }
                     t += 4;
                 }else if(s->decorr[i].value < 0){
-                    s->decorr[i].samplesA[0] = wp_exp2(LE_16(buf)); buf += 2;
-                    s->decorr[i].samplesB[0] = wp_exp2(LE_16(buf)); buf += 2;
+                    s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
+                    s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
                     t += 4;
                 }else{
                     for(j = 0; j < s->decorr[i].value; j++){
-                        s->decorr[i].samplesA[j] = wp_exp2(LE_16(buf)); buf += 2;
+                        s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
                         if(s->stereo){
-                            s->decorr[i].samplesB[j] = wp_exp2(LE_16(buf)); buf += 2;
+                            s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
                         }
                     }
                     t += s->decorr[i].value * 2 * avctx->channels;
@@ -490,7 +504,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
                 continue;
             }
             for(i = 0; i < 3 * avctx->channels; i++){
-                s->median[i] = wp_exp2(LE_16(buf));
+                s->median[i] = wp_exp2(AV_RL16(buf));
                 buf += 2;
             }
             got_entropy = 1;