]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/wavpack.c
Apply 'cold' attribute to init/uninit functions in libavcodec
[ffmpeg] / libavcodec / wavpack.c
index 53ba34b69dd1b0c0325ba70b9d64f9c19d3519a8..20340cccae7134125eabad08fcb9e8893fef0afc 100644 (file)
 #define ALT_BITSTREAM_READER_LE
 #include "avcodec.h"
 #include "bitstream.h"
+#include "unary.h"
 
 /**
  * @file wavpack.c
  * WavPack lossless audio decoder
  */
 
-#define WV_JOINT 0x0010
+#define WV_JOINT_STEREO 0x00000010
+#define WV_FALSE_STEREO 0x40000000
 
 enum WP_ID_Flags{
     WP_IDF_MASK   = 0x1F,
@@ -66,7 +68,7 @@ typedef struct Decorr {
 
 typedef struct WavpackContext {
     AVCodecContext *avctx;
-    int stereo;
+    int stereo, stereo_in;
     int joint;
     uint32_t CRC;
     GetBitContext gb;
@@ -76,6 +78,7 @@ typedef struct WavpackContext {
     int terms;
     Decorr decorr[MAX_TERMS];
     int zero, one, zeroes;
+    int and, or, shift;
 } WavpackContext;
 
 // exponent table copied from WavPack source
@@ -98,7 +101,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;
 
@@ -113,12 +116,6 @@ static always_inline int wp_exp2(int16_t val)
     return neg ? -res : res;
 }
 
-static inline int get_unary(GetBitContext *gb){
-    int r=0;
-    while(get_bits1(gb) && r<33)r++;
-    return r;
-}
-
 // macros for manipulating median values
 #define GET_MED(n) ((median[n] >> 4) + 1)
 #define DEC_MED(n) median[n] -= ((median[n] + (128>>n) - 2) / (128>>n)) * 2
@@ -137,12 +134,12 @@ 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 = p ? get_bits(gb, p) : 0;
     if(res >= e){
@@ -164,7 +161,7 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int *median, int
             if(ctx->zeroes)
                 return 0;
         }else{
-            t = get_unary(gb);
+            t = get_unary_0_33(gb);
             if(t >= 2) t = get_bits(gb, t - 1) | (1 << (t-1));
             ctx->zeroes = t;
             if(ctx->zeroes){
@@ -183,13 +180,13 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int *median, int
         t = 0;
         ctx->zero = 0;
     }else{
-        t = get_unary(gb);
+        t = get_unary_0_33(gb);
         if(get_bits_count(gb) >= ctx->data_size){
             *last = 1;
             return 0;
         }
         if(t == 16) {
-            t2 = get_unary(gb);
+            t2 = get_unary_0_33(gb);
             if(t2 < 2) t += t2;
             else t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
         }
@@ -235,7 +232,7 @@ static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
 {
     int i, j, count = 0;
     int last, t;
-    int A, B, L, L2, R, R2;
+    int A, B, L, L2, R, R2, bit;
     int pos = 0;
     uint32_t crc = 0xFFFFFFFF;
 
@@ -299,9 +296,10 @@ 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;
-        *dst++ = L;
-        *dst++ = 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;
         count++;
     }while(!last && count < s->samples);
 
@@ -316,7 +314,7 @@ static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst)
 {
     int i, j, count = 0;
     int last, t;
-    int A, S, T;
+    int A, S, T, bit;
     int pos = 0;
     uint32_t crc = 0xFFFFFFFF;
 
@@ -344,7 +342,8 @@ static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst)
         }
         pos = (pos + 1) & 7;
         crc = crc * 3 + S;
-        *dst++ = S;
+        bit = (S & s->and) | s->or;
+        *dst++ = ((S + bit) << s->shift) - bit;
         count++;
     }while(!last && count < s->samples);
 
@@ -355,7 +354,7 @@ static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst)
     return count;
 }
 
-static int wavpack_decode_init(AVCodecContext *avctx)
+static av_cold int wavpack_decode_init(AVCodecContext *avctx)
 {
     WavpackContext *s = avctx->priv_data;
 
@@ -365,36 +364,39 @@ static int wavpack_decode_init(AVCodecContext *avctx)
     return 0;
 }
 
-static int wavpack_decode_close(AVCodecContext *avctx)
-{
-//    WavpackContext *s = avctx->priv_data;
-
-    return 0;
-}
-
 static int wavpack_decode_frame(AVCodecContext *avctx,
                             void *data, int *data_size,
-                            uint8_t *buf, int buf_size)
+                            const uint8_t *buf, int buf_size)
 {
     WavpackContext *s = avctx->priv_data;
     int16_t *samples = data;
     int samplecount;
     int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0;
-    uint8_t* buf_end = buf + buf_size;
+    const 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));
+    memset(s->median, 0, sizeof(s->median));
+    s->and = s->or = s->shift = 0;
 
-    s->samples = 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 > AVCODEC_MAX_AUDIO_FRAME_SIZE){
+    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 = LE_32(buf) & WV_JOINT; buf += 4;
-    s->CRC = LE_32(buf); buf += 4;
+    s->stereo_in = (AV_RL32(buf) & WV_FALSE_STEREO) ? 0 : s->stereo;
+    s->joint = AV_RL32(buf) & WV_JOINT_STEREO; buf += 4;
+    s->CRC = AV_RL32(buf); buf += 4;
     // parse metadata blocks
     while(buf < buf_end){
         id = *buf++;
@@ -438,7 +440,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
                 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
                 continue;
             }
-            weights = size >> s->stereo;
+            weights = size >> s->stereo_in;
             if(weights > MAX_TERMS || weights > s->terms){
                 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
                 buf += ssize;
@@ -449,7 +451,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
                 s->decorr[s->terms - i - 1].weightA = t << 3;
                 if(s->decorr[s->terms - i - 1].weightA > 0)
                     s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
-                if(s->stereo){
+                if(s->stereo_in){
                     t = (int8_t)(*buf++);
                     s->decorr[s->terms - i - 1].weightB = t << 3;
                     if(s->decorr[s->terms - i - 1].weightB > 0)
@@ -466,42 +468,59 @@ 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;
-                    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].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
+                    s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
+                    if(s->stereo_in){
+                        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;
-                        if(s->stereo){
-                            s->decorr[i].samplesB[j] = wp_exp2(LE_16(buf)); buf += 2;
+                        s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
+                        if(s->stereo_in){
+                            s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
                         }
                     }
-                    t += s->decorr[i].value * 2 * avctx->channels;
+                    t += s->decorr[i].value * 2 * (s->stereo_in + 1);
                 }
             }
             got_samples = 1;
             break;
         case WP_ID_ENTROPY:
-            if(size != 6 * avctx->channels){
-                av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * avctx->channels, size);
+            if(size != 6 * (s->stereo_in + 1)){
+                av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);
                 buf += ssize;
                 continue;
             }
-            for(i = 0; i < 3 * avctx->channels; i++){
-                s->median[i] = wp_exp2(LE_16(buf));
+            for(i = 0; i < 3 * (s->stereo_in + 1); i++){
+                s->median[i] = wp_exp2(AV_RL16(buf));
                 buf += 2;
             }
             got_entropy = 1;
             break;
+        case WP_ID_INT32INFO:
+            if(size != 4 || *buf){
+                av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
+                buf += ssize;
+                continue;
+            }
+            if(buf[1])
+                s->shift = buf[1];
+            else if(buf[2]){
+                s->and = s->or = 1;
+                s->shift = buf[2];
+            }else if(buf[3]){
+                s->and = 1;
+                s->shift = buf[3];
+            }
+            buf += 4;
+            break;
         case WP_ID_DATA:
             init_get_bits(&s->gb, buf, size * 8);
             s->data_size = size * 8;
@@ -534,10 +553,21 @@ static int wavpack_decode_frame(AVCodecContext *avctx,
         return -1;
     }
 
-    if(s->stereo)
+    if(s->stereo_in)
         samplecount = wv_unpack_stereo(s, &s->gb, samples);
-    else
+    else{
         samplecount = wv_unpack_mono(s, &s->gb, samples);
+        if(s->stereo){
+            int16_t *dst = samples + samplecount * 2;
+            int16_t *src = samples + samplecount;
+            int cnt = samplecount;
+            while(cnt--){
+                *--dst = *--src;
+                *--dst = *src;
+            }
+            samplecount *= 2;
+        }
+    }
     *data_size = samplecount * 2;
 
     return buf_size;
@@ -550,6 +580,6 @@ AVCodec wavpack_decoder = {
     sizeof(WavpackContext),
     wavpack_decode_init,
     NULL,
-    wavpack_decode_close,
+    NULL,
     wavpack_decode_frame,
 };