X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fwavpack.c;h=7358d29735a7ba22db8e08663d5ccd92b289680b;hb=867215336debce9fe38f728ad5c74776a1965785;hp=c8ab858439577e28195e5cc81b0c3082adf7d989;hpb=10c773c12ff2db5894fd8f76f5575cee1793d1c4;p=ffmpeg diff --git a/libavcodec/wavpack.c b/libavcodec/wavpack.c index c8ab8584395..7358d29735a 100644 --- a/libavcodec/wavpack.c +++ b/libavcodec/wavpack.c @@ -24,7 +24,7 @@ #include "unary.h" /** - * @file libavcodec/wavpack.c + * @file * WavPack lossless audio decoder */ @@ -37,6 +37,12 @@ #define WV_HYBRID_BITRATE 0x00000200 #define WV_HYBRID_BALANCE 0x00000400 +#define WV_FLT_SHIFT_ONES 0x01 +#define WV_FLT_SHIFT_SAME 0x02 +#define WV_FLT_SHIFT_SENT 0x04 +#define WV_FLT_ZERO_SENT 0x08 +#define WV_FLT_ZERO_SIGN 0x10 + enum WP_ID_Flags{ WP_IDF_MASK = 0x1F, WP_IDF_IGNORE = 0x20, @@ -61,6 +67,13 @@ enum WP_ID{ WP_ID_CHANINFO }; +typedef struct SavedContext { + int offset; + int size; + int bits_used; + uint32_t crc; +} SavedContext; + #define MAX_TERMS 16 typedef struct Decorr { @@ -97,7 +110,14 @@ typedef struct WavpackContext { int and, or, shift; int post_shift; int hybrid, hybrid_bitrate; + int float_flag; + int float_shift; + int float_max_exp; WvChannel ch[2]; + int samples_left; + int max_samples; + int pos; + SavedContext sc, extra_sc; } WavpackContext; // exponent table copied from WavPack source @@ -341,18 +361,115 @@ static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int return sign ? ~ret : ret; } -static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int hires) +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 float wv_get_value_float(WavpackContext *s, uint32_t *crc, int S) +{ + union { + float f; + uint32_t u; + } value; + + int sign; + int exp = s->float_max_exp; + + if(s->got_extra_bits){ + const int max_bits = 1 + 23 + 8 + 1; + const int left_bits = get_bits_left(&s->gb_extra_bits); + + if(left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits) + return 0.0; + } + + if(S){ + S <<= s->float_shift; + sign = S < 0; + if(sign) + S = -S; + if(S >= 0x1000000){ + if(s->got_extra_bits && get_bits1(&s->gb_extra_bits)){ + S = get_bits(&s->gb_extra_bits, 23); + }else{ + S = 0; + } + exp = 255; + }else if(exp){ + int shift = 23 - av_log2(S); + exp = s->float_max_exp; + if(exp <= shift){ + shift = --exp; + } + exp -= shift; + + if(shift){ + S <<= shift; + if((s->float_flag & WV_FLT_SHIFT_ONES) || + (s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SAME) && get_bits1(&s->gb_extra_bits)) ){ + S |= (1 << shift) - 1; + } else if(s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SENT)){ + S |= get_bits(&s->gb_extra_bits, shift); + } + } + }else{ + exp = s->float_max_exp; + } + S &= 0x7fffff; + }else{ + sign = 0; + exp = 0; + if(s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)){ + if(get_bits1(&s->gb_extra_bits)){ + S = get_bits(&s->gb_extra_bits, 23); + if(s->float_max_exp >= 25) + exp = get_bits(&s->gb_extra_bits, 8); + sign = get_bits1(&s->gb_extra_bits); + }else{ + if(s->float_flag & WV_FLT_ZERO_SIGN) + sign = get_bits1(&s->gb_extra_bits); + } + } + } + + *crc = *crc * 27 + S * 9 + exp * 3 + sign; + + value.u = (sign << 31) | (exp << 23) | S; + return value.f; +} + +static void wv_reset_saved_context(WavpackContext *s) +{ + s->pos = 0; + s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF; +} + +static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int type) { int i, j, count = 0; int last, t; - int A, B, L, L2, R, R2, bit; - int pos = 0; - uint32_t crc = 0xFFFFFFFF; - uint32_t crc_extra_bits = 0xFFFFFFFF; + int A, B, L, L2, R, R2; + int pos = s->pos; + uint32_t crc = s->sc.crc; + uint32_t crc_extra_bits = s->extra_sc.crc; int16_t *dst16 = dst; int32_t *dst32 = dst; + float *dstfl = dst; - s->one = s->zero = s->zeroes = 0; + if(s->samples_left == s->samples) + s->one = s->zero = s->zeroes = 0; do{ L = wv_get_value(s, gb, 0, &last); if(last) break; @@ -377,7 +494,7 @@ static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *d B = s->decorr[i].samplesB[pos]; j = (pos + t) & 7; } - if(hires){ + if(type != SAMPLE_FMT_S16){ L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10); R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10); }else{ @@ -389,13 +506,13 @@ static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *d s->decorr[i].samplesA[j] = L = L2; s->decorr[i].samplesB[j] = R = R2; }else if(t == -1){ - if(hires) + if(type != SAMPLE_FMT_S16) 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; - if(hires) + if(type != SAMPLE_FMT_S16) R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10); else R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10); @@ -403,7 +520,7 @@ static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *d R = R2; s->decorr[i].samplesA[0] = R; }else{ - if(hires) + if(type != SAMPLE_FMT_S16) 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); @@ -415,7 +532,7 @@ static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *d s->decorr[i].samplesA[0] = R; } - if(hires) + if(type != SAMPLE_FMT_S16) L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10); else L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10); @@ -428,54 +545,57 @@ static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *d if(s->joint) L += (R -= (L >> 1)); crc = (crc * 3 + L) * 3 + R; - if(s->extra_bits){ - L <<= s->extra_bits; - R <<= s->extra_bits; - - if(s->got_extra_bits){ - L |= get_bits(&s->gb_extra_bits, s->extra_bits); - crc_extra_bits = crc_extra_bits * 9 + (L&0xffff) * 3 + ((unsigned)L>>16); - R |= get_bits(&s->gb_extra_bits, s->extra_bits); - crc_extra_bits = crc_extra_bits * 9 + (R&0xffff) * 3 + ((unsigned)R>>16); - } + if(type == SAMPLE_FMT_FLT){ + *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L); + *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R); + } else if(type == SAMPLE_FMT_S32){ + *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); } - bit = (L & s->and) | s->or; - if(hires) - *dst32++ = (((L + bit) << s->shift) - bit) << s->post_shift; - else - *dst16++ = (((L + bit) << s->shift) - bit) << s->post_shift; - bit = (R & s->and) | s->or; - if(hires) - *dst32++ = (((R + bit) << s->shift) - bit) << s->post_shift; - else - *dst16++ = (((R + bit) << s->shift) - bit) << s->post_shift; count++; - }while(!last && count < s->samples); + }while(!last && count < s->max_samples); - if(crc != s->CRC){ - 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; + s->samples_left -= count; + if(!s->samples_left){ + if(crc != s->CRC){ + 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; + } + wv_reset_saved_context(s); + }else{ + s->pos = pos; + s->sc.crc = crc; + s->sc.bits_used = get_bits_count(&s->gb); + if(s->got_extra_bits){ + s->extra_sc.crc = crc_extra_bits; + s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits); + } } return count * 2; } -static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int hires) +static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int type) { int i, j, count = 0; int last, t; - int A, S, T, bit; - int pos = 0; - uint32_t crc = 0xFFFFFFFF; - uint32_t crc_extra_bits = 0xFFFFFFFF; + int A, S, T; + int pos = s->pos; + uint32_t crc = s->sc.crc; + uint32_t crc_extra_bits = s->extra_sc.crc; int16_t *dst16 = dst; int32_t *dst32 = dst; + float *dstfl = dst; - s->one = s->zero = s->zeroes = 0; + if(s->samples_left == s->samples) + s->one = s->zero = s->zeroes = 0; do{ T = wv_get_value(s, gb, 0, &last); S = 0; @@ -493,7 +613,7 @@ static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst A = s->decorr[i].samplesA[pos]; j = (pos + t) & 7; } - if(hires) + if(type != SAMPLE_FMT_S16) S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10); else S = T + ((s->decorr[i].weightA * A + 512) >> 10); @@ -502,30 +622,35 @@ static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst } pos = (pos + 1) & 7; crc = crc * 3 + S; - if(s->extra_bits){ - S <<= s->extra_bits; - - if(s->got_extra_bits){ - S |= get_bits(&s->gb_extra_bits, s->extra_bits); - crc_extra_bits = crc_extra_bits * 9 + (S&0xffff) * 3 + ((unsigned)S>>16); - } - } - bit = (S & s->and) | s->or; - if(hires) - *dst32++ = (((S + bit) << s->shift) - bit) << s->post_shift; + if(type == SAMPLE_FMT_FLT) + *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S); + else if(type == SAMPLE_FMT_S32) + *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S); else - *dst16++ = (((S + bit) << s->shift) - bit) << s->post_shift; + *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S); count++; }while(!last && count < s->samples); - if(crc != s->CRC){ - 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; + s->samples_left -= count; + if(!s->samples_left){ + if(crc != s->CRC){ + 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; + } + wv_reset_saved_context(s); + }else{ + s->pos = pos; + s->sc.crc = crc; + s->sc.bits_used = get_bits_count(&s->gb); + if(s->got_extra_bits){ + s->extra_sc.crc = crc_extra_bits; + s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits); + } } return count; } @@ -542,6 +667,8 @@ static av_cold int wavpack_decode_init(AVCodecContext *avctx) avctx->sample_fmt = SAMPLE_FMT_S32; avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO; + wv_reset_saved_context(s); + return 0; } @@ -554,7 +681,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx, WavpackContext *s = avctx->priv_data; void *samples = data; int samplecount; - int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0; + int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0; int got_hybrid = 0; const uint8_t* buf_end = buf + buf_size; int i, j, id, size, ssize, weights, t; @@ -565,11 +692,13 @@ static int wavpack_decode_frame(AVCodecContext *avctx, return 0; } - 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; + if(!s->samples_left){ + 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){ @@ -577,7 +706,10 @@ static int wavpack_decode_frame(AVCodecContext *avctx, return buf_size; } s->frame_flags = AV_RL32(buf); buf += 4; - if((s->frame_flags&0x03) <= 1){ + if(s->frame_flags&0x80){ + bpp = sizeof(float); + avctx->sample_fmt = SAMPLE_FMT_FLT; + } else if((s->frame_flags&0x03) <= 1){ bpp = 2; avctx->sample_fmt = SAMPLE_FMT_S16; } else { @@ -591,10 +723,11 @@ static int wavpack_decode_frame(AVCodecContext *avctx, 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; + s->max_samples = *data_size / (bpp * avctx->channels); + s->max_samples = FFMIN(s->max_samples, s->samples); + if(s->samples_left > 0){ + s->max_samples = FFMIN(s->max_samples, s->samples_left); + buf = buf_end; } // parse metadata blocks @@ -749,7 +882,21 @@ static int wavpack_decode_frame(AVCodecContext *avctx, } buf += 4; break; + case WP_ID_FLOATINFO: + if(size != 4){ + av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size); + buf += ssize; + continue; + } + s->float_flag = buf[0]; + s->float_shift = buf[1]; + s->float_max_exp = buf[2]; + buf += 4; + got_float = 1; + break; case WP_ID_DATA: + s->sc.offset = buf - avpkt->data; + s->sc.size = size * 8; init_get_bits(&s->gb, buf, size * 8); s->data_size = size * 8; buf += size; @@ -761,6 +908,8 @@ static int wavpack_decode_frame(AVCodecContext *avctx, buf += size; continue; } + s->extra_sc.offset = buf - avpkt->data; + s->extra_sc.size = size * 8; init_get_bits(&s->gb_extra_bits, buf, size * 8); s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32); buf += size; @@ -771,42 +920,71 @@ static int wavpack_decode_frame(AVCodecContext *avctx, } if(id & WP_IDF_ODD) buf++; } - if(!got_terms){ - av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n"); - return -1; - } - if(!got_weights){ - av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n"); - return -1; - } - if(!got_samples){ - av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n"); - return -1; - } - if(!got_entropy){ - av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n"); - return -1; - } - if(s->hybrid && !got_hybrid){ - av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n"); - return -1; - } - if(!got_bs){ - av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n"); - return -1; + if(!s->samples_left){ + if(!got_terms){ + av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n"); + return -1; + } + if(!got_weights){ + av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n"); + return -1; + } + if(!got_samples){ + av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n"); + return -1; + } + if(!got_entropy){ + av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n"); + return -1; + } + if(s->hybrid && !got_hybrid){ + av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n"); + return -1; + } + if(!got_bs){ + av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n"); + return -1; + } + if(!got_float && avctx->sample_fmt == SAMPLE_FMT_FLT){ + av_log(avctx, AV_LOG_ERROR, "Float information not found\n"); + return -1; + } + if(s->got_extra_bits && avctx->sample_fmt != SAMPLE_FMT_FLT){ + const int size = get_bits_left(&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; + } + } + s->samples_left = s->samples; + }else{ + init_get_bits(&s->gb, avpkt->data + s->sc.offset, s->sc.size); + skip_bits_long(&s->gb, s->sc.bits_used); + if(s->got_extra_bits){ + init_get_bits(&s->gb_extra_bits, avpkt->data + s->extra_sc.offset, + s->extra_sc.size); + skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used); + } } if(s->stereo_in){ - if(bpp == 2) - samplecount = wv_unpack_stereo(s, &s->gb, samples, 0); + if(avctx->sample_fmt == SAMPLE_FMT_S16) + samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S16); + else if(avctx->sample_fmt == SAMPLE_FMT_S32) + samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S32); else - samplecount = wv_unpack_stereo(s, &s->gb, samples, 1); + samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_FLT); + }else{ - if(bpp == 2) - samplecount = wv_unpack_mono(s, &s->gb, samples, 0); + if(avctx->sample_fmt == SAMPLE_FMT_S16) + samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S16); + else if(avctx->sample_fmt == SAMPLE_FMT_S32) + samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S32); else - samplecount = wv_unpack_mono(s, &s->gb, samples, 1); - if(s->stereo && bpp == 2){ + samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_FLT); + + if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S16){ int16_t *dst = (int16_t*)samples + samplecount * 2; int16_t *src = (int16_t*)samples + samplecount; int cnt = samplecount; @@ -815,7 +993,7 @@ static int wavpack_decode_frame(AVCodecContext *avctx, *--dst = *src; } samplecount *= 2; - }else if(s->stereo){ //32-bit output + }else if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S32){ int32_t *dst = (int32_t*)samples + samplecount * 2; int32_t *src = (int32_t*)samples + samplecount; int cnt = samplecount; @@ -824,21 +1002,31 @@ static int wavpack_decode_frame(AVCodecContext *avctx, *--dst = *src; } samplecount *= 2; + }else if(s->stereo){ + float *dst = (float*)samples + samplecount * 2; + float *src = (float*)samples + samplecount; + int cnt = samplecount; + while(cnt--){ + *--dst = *--src; + *--dst = *src; + } + samplecount *= 2; } } *data_size = samplecount * bpp; - return buf_size; + return s->samples_left > 0 ? 0 : buf_size; } AVCodec wavpack_decoder = { "wavpack", - CODEC_TYPE_AUDIO, + AVMEDIA_TYPE_AUDIO, CODEC_ID_WAVPACK, sizeof(WavpackContext), wavpack_decode_init, NULL, NULL, wavpack_decode_frame, + .capabilities = CODEC_CAP_SUBFRAMES, .long_name = NULL_IF_CONFIG_SMALL("WavPack"), };