]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blobdiff - libavcodec/wavpack.c
Factorize out integer sample value decoding for WavPack.
[frescor/ffmpeg.git] / libavcodec / wavpack.c
index 12eac33fe7685c9619c50303f172f8b0baad26f1..11d23cf834f33d7e75d07a61e94a5608c6d473c5 100644 (file)
@@ -20,7 +20,7 @@
  */
 #define ALT_BITSTREAM_READER_LE
 #include "avcodec.h"
-#include "bitstream.h"
+#include "get_bits.h"
 #include "unary.h"
 
 /**
@@ -57,7 +57,7 @@ enum WP_ID{
     WP_ID_INT32INFO,
     WP_ID_DATA,
     WP_ID_CORR,
-    WP_ID_FLT,
+    WP_ID_EXTRABITS,
     WP_ID_CHANINFO
 };
 
@@ -85,12 +85,17 @@ typedef struct WavpackContext {
     int joint;
     uint32_t CRC;
     GetBitContext gb;
+    int got_extra_bits;
+    uint32_t crc_extra_bits;
+    GetBitContext gb_extra_bits;
     int data_size; // in bits
     int samples;
     int terms;
     Decorr decorr[MAX_TERMS];
     int zero, one, zeroes;
+    int extra_bits;
     int and, or, shift;
+    int post_shift;
     int hybrid, hybrid_bitrate;
     WvChannel ch[2];
 } WavpackContext;
@@ -336,13 +341,32 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int
     return sign ? ~ret : ret;
 }
 
-static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
+static inline int wv_get_value_integer(WavpackContext *s, uint32_t *crc, int S)
+{
+    int bit;
+
+    if(s->extra_bits){
+        S <<= s->extra_bits;
+
+        if(s->got_extra_bits){
+            S |= get_bits(&s->gb_extra_bits, s->extra_bits);
+            *crc = *crc * 9 + (S&0xffff) * 3 + ((unsigned)S>>16);
+        }
+    }
+    bit = (S & s->and) | s->or;
+    return (((S + bit) << s->shift) - bit) << s->post_shift;
+}
+
+static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int hires)
 {
     int i, j, count = 0;
     int last, t;
-    int A, B, L, L2, R, R2, bit;
+    int A, B, L, L2, R, R2;
     int pos = 0;
     uint32_t crc = 0xFFFFFFFF;
+    uint32_t crc_extra_bits = 0xFFFFFFFF;
+    int16_t *dst16 = dst;
+    int32_t *dst32 = dst;
 
     s->one = s->zero = s->zeroes = 0;
     do{
@@ -352,7 +376,6 @@ static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
         if(last) break;
         for(i = 0; i < s->terms; i++){
             t = s->decorr[i].value;
-            j = 0;
             if(t > 0){
                 if(t > 8){
                     if(t & 1){
@@ -370,22 +393,36 @@ static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
                     B = s->decorr[i].samplesB[pos];
                     j = (pos + t) & 7;
                 }
-                L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
-                R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
+                if(hires){
+                    L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
+                    R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
+                }else{
+                    L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
+                    R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
+                }
                 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
                 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
                 s->decorr[i].samplesA[j] = L = L2;
                 s->decorr[i].samplesB[j] = R = R2;
             }else if(t == -1){
-                L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
+                if(hires)
+                    L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
+                else
+                    L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
                 L = L2;
-                R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
+                if(hires)
+                    R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
+                else
+                    R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
                 R = R2;
                 s->decorr[i].samplesA[0] = R;
             }else{
-                R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
+                if(hires)
+                    R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
+                else
+                    R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
                 R = R2;
 
@@ -394,7 +431,10 @@ static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
                     s->decorr[i].samplesA[0] = R;
                 }
 
-                L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
+                if(hires)
+                    L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
+                else
+                    L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
                 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
                 L = L2;
                 s->decorr[i].samplesB[0] = L;
@@ -404,10 +444,14 @@ static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
         if(s->joint)
             L += (R -= (L >> 1));
         crc = (crc * 3 + L) * 3 + R;
-        bit = (L & s->and) | s->or;
-        *dst++ = ((L + bit) << s->shift) - bit;
-        bit = (R & s->and) | s->or;
-        *dst++ = ((R + bit) << s->shift) - bit;
+
+        if(hires){
+            *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
+            *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
+        } else {
+            *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
+            *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
+        }
         count++;
     }while(!last && count < s->samples);
 
@@ -415,16 +459,23 @@ static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
         return -1;
     }
+    if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
+        av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
+        return -1;
+    }
     return count * 2;
 }
 
-static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst)
+static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int hires)
 {
     int i, j, count = 0;
     int last, t;
-    int A, S, T, bit;
+    int A, S, T;
     int pos = 0;
     uint32_t crc = 0xFFFFFFFF;
+    uint32_t crc_extra_bits = 0xFFFFFFFF;
+    int16_t *dst16 = dst;
+    int32_t *dst32 = dst;
 
     s->one = s->zero = s->zeroes = 0;
     do{
@@ -444,14 +495,20 @@ static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst)
                 A = s->decorr[i].samplesA[pos];
                 j = (pos + t) & 7;
             }
-            S = T + ((s->decorr[i].weightA * A + 512) >> 10);
+            if(hires)
+                S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
+            else
+                S = T + ((s->decorr[i].weightA * A + 512) >> 10);
             if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
             s->decorr[i].samplesA[j] = T = S;
         }
         pos = (pos + 1) & 7;
         crc = crc * 3 + S;
-        bit = (S & s->and) | s->or;
-        *dst++ = ((S + bit) << s->shift) - bit;
+
+        if(hires)
+            *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
+        else
+            *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
         count++;
     }while(!last && count < s->samples);
 
@@ -459,6 +516,10 @@ static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst)
         av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
         return -1;
     }
+    if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
+        av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
+        return -1;
+    }
     return count;
 }
 
@@ -468,7 +529,10 @@ static av_cold int wavpack_decode_init(AVCodecContext *avctx)
 
     s->avctx = avctx;
     s->stereo = (avctx->channels == 2);
-    avctx->sample_fmt = SAMPLE_FMT_S16;
+    if(avctx->bits_per_coded_sample <= 16)
+        avctx->sample_fmt = SAMPLE_FMT_S16;
+    else
+        avctx->sample_fmt = SAMPLE_FMT_S32;
     avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
 
     return 0;
@@ -476,15 +540,18 @@ static av_cold int wavpack_decode_init(AVCodecContext *avctx)
 
 static int wavpack_decode_frame(AVCodecContext *avctx,
                             void *data, int *data_size,
-                            const uint8_t *buf, int buf_size)
+                            AVPacket *avpkt)
 {
+    const uint8_t *buf = avpkt->data;
+    int buf_size = avpkt->size;
     WavpackContext *s = avctx->priv_data;
-    int16_t *samples = data;
+    void *samples = data;
     int samplecount;
     int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0;
     int got_hybrid = 0;
     const uint8_t* buf_end = buf + buf_size;
     int i, j, id, size, ssize, weights, t;
+    int bpp;
 
     if (buf_size == 0){
         *data_size = 0;
@@ -493,24 +560,36 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
 
     memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
     memset(s->ch, 0, sizeof(s->ch));
+    s->extra_bits = 0;
     s->and = s->or = s->shift = 0;
+    s->got_extra_bits = 0;
 
     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->frame_flags = AV_RL32(buf); buf += 4;
+    if((s->frame_flags&0x03) <= 1){
+        bpp = 2;
+        avctx->sample_fmt = SAMPLE_FMT_S16;
+    } else {
+        bpp = 4;
+        avctx->sample_fmt = SAMPLE_FMT_S32;
+    }
     s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
     s->joint = s->frame_flags & WV_JOINT_STEREO;
     s->hybrid = s->frame_flags & WV_HYBRID_MODE;
     s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
+    s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
     s->CRC = AV_RL32(buf); buf += 4;
+
+    /* should not happen but who knows */
+    if(s->samples * bpp * avctx->channels > *data_size){
+        av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
+        return -1;
+    }
+
     // parse metadata blocks
     while(buf < buf_end){
         id = *buf++;
@@ -645,12 +724,14 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
             got_hybrid = 1;
             break;
         case WP_ID_INT32INFO:
-            if(size != 4 || *buf){
+            if(size != 4){
                 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
                 buf += ssize;
                 continue;
             }
-            if(buf[1])
+            if(buf[0])
+                s->extra_bits = buf[0];
+            else if(buf[1])
                 s->shift = buf[1];
             else if(buf[2]){
                 s->and = s->or = 1;
@@ -667,6 +748,17 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
             buf += size;
             got_bs = 1;
             break;
+        case WP_ID_EXTRABITS:
+            if(size <= 4){
+                av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);
+                buf += size;
+                continue;
+            }
+            init_get_bits(&s->gb_extra_bits, buf, size * 8);
+            s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
+            buf += size;
+            s->got_extra_bits = 1;
+            break;
         default:
             buf += size;
         }
@@ -696,14 +788,37 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
         av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
         return -1;
     }
+    if(s->got_extra_bits){
+        const int size = s->gb_extra_bits.size_in_bits - get_bits_count(&s->gb_extra_bits);
+        const int wanted = s->samples * s->extra_bits << s->stereo_in;
+        if(size < wanted){
+            av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
+            s->got_extra_bits = 0;
+        }
+    }
 
-    if(s->stereo_in)
-        samplecount = wv_unpack_stereo(s, &s->gb, samples);
-    else{
-        samplecount = wv_unpack_mono(s, &s->gb, samples);
-        if(s->stereo){
-            int16_t *dst = samples + samplecount * 2;
-            int16_t *src = samples + samplecount;
+    if(s->stereo_in){
+        if(bpp == 2)
+            samplecount = wv_unpack_stereo(s, &s->gb, samples, 0);
+        else
+            samplecount = wv_unpack_stereo(s, &s->gb, samples, 1);
+    }else{
+        if(bpp == 2)
+            samplecount = wv_unpack_mono(s, &s->gb, samples, 0);
+        else
+            samplecount = wv_unpack_mono(s, &s->gb, samples, 1);
+        if(s->stereo && bpp == 2){
+            int16_t *dst = (int16_t*)samples + samplecount * 2;
+            int16_t *src = (int16_t*)samples + samplecount;
+            int cnt = samplecount;
+            while(cnt--){
+                *--dst = *--src;
+                *--dst = *src;
+            }
+            samplecount *= 2;
+        }else if(s->stereo){ //32-bit output
+            int32_t *dst = (int32_t*)samples + samplecount * 2;
+            int32_t *src = (int32_t*)samples + samplecount;
             int cnt = samplecount;
             while(cnt--){
                 *--dst = *--src;
@@ -712,7 +827,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
             samplecount *= 2;
         }
     }
-    *data_size = samplecount * 2;
+    *data_size = samplecount * bpp;
 
     return buf_size;
 }