2
2
* WavPack lossless audio decoder
3
* Copyright (c) 2006 Konstantin Shishkov
5
* This file is part of FFmpeg.
7
* FFmpeg is free software; you can redistribute it and/or
3
* Copyright (c) 2006,2011 Konstantin Shishkov
5
* This file is part of Libav.
7
* Libav is free software; you can redistribute it and/or
8
8
* modify it under the terms of the GNU Lesser General Public
9
9
* License as published by the Free Software Foundation; either
10
10
* version 2.1 of the License, or (at your option) any later version.
12
* FFmpeg is distributed in the hope that it will be useful,
12
* Libav is distributed in the hope that it will be useful,
13
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
15
* Lesser General Public License for more details.
17
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
18
* License along with Libav; if not, write to the Free Software
19
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
21
#define ALT_BITSTREAM_READER_LE
22
22
#include "avcodec.h"
23
23
#include "get_bits.h"
25
#include "libavutil/audioconvert.h"
546
564
L += (R -= (L >> 1));
547
565
crc = (crc * 3 + L) * 3 + R;
549
if(type == SAMPLE_FMT_FLT){
567
if(type == AV_SAMPLE_FMT_FLT){
550
568
*dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
551
569
*dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
552
} else if(type == SAMPLE_FMT_S32){
570
dstfl += channel_pad;
571
} else if(type == AV_SAMPLE_FMT_S32){
553
572
*dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
554
573
*dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
574
dst32 += channel_pad;
556
576
*dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
557
577
*dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
578
dst16 += channel_pad;
560
581
}while(!last && count < s->max_samples);
623
645
pos = (pos + 1) & 7;
624
646
crc = crc * 3 + S;
626
if(type == SAMPLE_FMT_FLT)
627
*dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
628
else if(type == SAMPLE_FMT_S32)
629
*dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
631
*dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
648
if(type == AV_SAMPLE_FMT_FLT){
649
*dstfl = wv_get_value_float(s, &crc_extra_bits, S);
650
dstfl += channel_stride;
651
}else if(type == AV_SAMPLE_FMT_S32){
652
*dst32 = wv_get_value_integer(s, &crc_extra_bits, S);
653
dst32 += channel_stride;
655
*dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
656
dst16 += channel_stride;
633
}while(!last && count < s->samples);
659
}while(!last && count < s->max_samples);
635
661
s->samples_left -= count;
636
662
if(!s->samples_left){
684
static av_cold int wv_alloc_frame_context(WavpackContext *c)
687
if(c->fdec_num == WV_MAX_FRAME_DECODERS)
690
c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
691
if(!c->fdec[c->fdec_num])
694
c->fdec[c->fdec_num - 1]->avctx = c->avctx;
695
wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
658
700
static av_cold int wavpack_decode_init(AVCodecContext *avctx)
660
702
WavpackContext *s = avctx->priv_data;
662
704
s->avctx = avctx;
663
s->stereo = (avctx->channels == 2);
664
705
if(avctx->bits_per_coded_sample <= 16)
665
avctx->sample_fmt = SAMPLE_FMT_S16;
706
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
667
avctx->sample_fmt = SAMPLE_FMT_S32;
668
avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
670
wv_reset_saved_context(s);
708
avctx->sample_fmt = AV_SAMPLE_FMT_S32;
709
if(avctx->channels <= 2 && !avctx->channel_layout)
710
avctx->channel_layout = (avctx->channels==2) ? AV_CH_LAYOUT_STEREO : AV_CH_LAYOUT_MONO;
712
s->multichannel = avctx->channels > 2;
713
/* lavf demuxer does not provide extradata, Matroska stores 0x403
714
there, use this to detect decoding mode for multichannel */
716
if(s->multichannel && avctx->extradata && avctx->extradata_size == 2){
717
int ver = AV_RL16(avctx->extradata);
718
if(ver >= 0x402 && ver <= 0x410)
675
static int wavpack_decode_frame(AVCodecContext *avctx,
676
void *data, int *data_size,
727
static av_cold int wavpack_decode_end(AVCodecContext *avctx)
679
const uint8_t *buf = avpkt->data;
680
int buf_size = avpkt->size;
681
729
WavpackContext *s = avctx->priv_data;
732
for(i = 0; i < s->fdec_num; i++)
733
av_freep(&s->fdec[i]);
739
static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
740
void *data, int *data_size,
741
const uint8_t *buf, int buf_size)
743
WavpackContext *wc = avctx->priv_data;
744
WavpackFrameContext *s;
682
745
void *samples = data;
684
747
int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;
685
748
int got_hybrid = 0;
749
const uint8_t* orig_buf = buf;
686
750
const uint8_t* buf_end = buf + buf_size;
687
751
int i, j, id, size, ssize, weights, t;
752
int bpp, chan, chmask;
690
754
if (buf_size == 0){
759
if(block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0){
760
av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
764
s = wc->fdec[block_no];
766
av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);
695
770
if(!s->samples_left){
696
771
memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
697
772
memset(s->ch, 0, sizeof(s->ch));
700
775
s->got_extra_bits = 0;
703
s->samples = AV_RL32(buf); buf += 4;
779
s->samples = AV_RL32(buf); buf += 4;
785
s->samples = wc->samples;
708
787
s->frame_flags = AV_RL32(buf); buf += 4;
709
788
if(s->frame_flags&0x80){
710
789
bpp = sizeof(float);
711
avctx->sample_fmt = SAMPLE_FMT_FLT;
790
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
712
791
} else if((s->frame_flags&0x03) <= 1){
714
avctx->sample_fmt = SAMPLE_FMT_S16;
793
avctx->sample_fmt = AV_SAMPLE_FMT_S16;
717
avctx->sample_fmt = SAMPLE_FMT_S32;
796
avctx->sample_fmt = AV_SAMPLE_FMT_S32;
798
samples = (uint8_t*)samples + bpp * wc->ch_offset;
800
s->stereo = !(s->frame_flags & WV_MONO);
719
801
s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
720
802
s->joint = s->frame_flags & WV_JOINT_STEREO;
721
803
s->hybrid = s->frame_flags & WV_HYBRID_MODE;
722
804
s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
723
805
s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
724
806
s->CRC = AV_RL32(buf); buf += 4;
808
buf += 4; //skip block size;
810
wc->ch_offset += 1 + s->stereo;
726
812
s->max_samples = *data_size / (bpp * avctx->channels);
727
813
s->max_samples = FFMIN(s->max_samples, s->samples);
911
s->extra_sc.offset = buf - avpkt->data;
997
s->extra_sc.offset = buf - orig_buf;
912
998
s->extra_sc.size = size * 8;
913
999
init_get_bits(&s->gb_extra_bits, buf, size * 8);
914
1000
s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
916
1002
s->got_extra_bits = 1;
1004
case WP_ID_CHANINFO:
1006
av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");
1015
chmask = AV_RL16(buf);
1018
chmask = AV_RL24(buf);
1021
chmask = AV_RL32(buf);
1024
chan |= (buf[1] & 0xF) << 8;
1025
chmask = AV_RL24(buf + 2);
1028
av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
1029
chan = avctx->channels;
1030
chmask = avctx->channel_layout;
1032
if(chan != avctx->channels){
1033
av_log(avctx, AV_LOG_ERROR, "Block reports total %d channels, decoder believes it's %d channels\n",
1034
chan, avctx->channels);
1037
if(!avctx->channel_layout)
1038
avctx->channel_layout = chmask;
960
1083
s->samples_left = s->samples;
962
init_get_bits(&s->gb, avpkt->data + s->sc.offset, s->sc.size);
1085
init_get_bits(&s->gb, orig_buf + s->sc.offset, s->sc.size);
963
1086
skip_bits_long(&s->gb, s->sc.bits_used);
964
1087
if(s->got_extra_bits){
965
init_get_bits(&s->gb_extra_bits, avpkt->data + s->extra_sc.offset,
1088
init_get_bits(&s->gb_extra_bits, orig_buf + s->extra_sc.offset,
966
1089
s->extra_sc.size);
967
1090
skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used);
971
1094
if(s->stereo_in){
972
if(avctx->sample_fmt == SAMPLE_FMT_S16)
973
samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S16);
974
else if(avctx->sample_fmt == SAMPLE_FMT_S32)
975
samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S32);
1095
if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)
1096
samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
1097
else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1098
samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
977
samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_FLT);
1100
samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
980
if(avctx->sample_fmt == SAMPLE_FMT_S16)
981
samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S16);
982
else if(avctx->sample_fmt == SAMPLE_FMT_S32)
983
samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S32);
1103
const int channel_stride = avctx->channels;
1105
if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)
1106
samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
1107
else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1108
samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
985
samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_FLT);
1110
samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
987
if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S16){
988
int16_t *dst = (int16_t*)samples + samplecount * 2;
989
int16_t *src = (int16_t*)samples + samplecount;
990
int cnt = samplecount;
996
}else if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S32){
997
int32_t *dst = (int32_t*)samples + samplecount * 2;
998
int32_t *src = (int32_t*)samples + samplecount;
999
int cnt = samplecount;
1112
if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16){
1113
int16_t *dst = (int16_t*)samples + 1;
1114
int16_t *src = (int16_t*)samples;
1115
int cnt = samplecount;
1118
src += channel_stride;
1119
dst += channel_stride;
1121
}else if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32){
1122
int32_t *dst = (int32_t*)samples + 1;
1123
int32_t *src = (int32_t*)samples;
1124
int cnt = samplecount;
1127
src += channel_stride;
1128
dst += channel_stride;
1005
1130
}else if(s->stereo){
1006
float *dst = (float*)samples + samplecount * 2;
1007
float *src = (float*)samples + samplecount;
1131
float *dst = (float*)samples + 1;
1132
float *src = (float*)samples;
1008
1133
int cnt = samplecount;
1016
*data_size = samplecount * bpp;
1018
return s->samples_left > 0 ? 0 : buf_size;
1021
AVCodec wavpack_decoder = {
1136
src += channel_stride;
1137
dst += channel_stride;
1142
wc->samples_left = s->samples_left;
1144
return samplecount * bpp;
1147
static int wavpack_decode_frame(AVCodecContext *avctx,
1148
void *data, int *data_size,
1151
WavpackContext *s = avctx->priv_data;
1152
const uint8_t *buf = avpkt->data;
1153
int buf_size = avpkt->size;
1155
int samplecount = 0;
1158
s->samples_left = 0;
1162
s->samples = AV_RL32(buf); buf += 4;
1164
while(buf_size > 0){
1165
if(!s->multichannel){
1166
frame_size = buf_size;
1169
frame_size = AV_RL32(buf) - 12; buf += 4; buf_size -= 4;
1171
if(buf_size < 12) //MKV files can have zero flags after last block
1173
frame_size = AV_RL32(buf + 8) + 12;
1176
if(frame_size < 0 || frame_size > buf_size){
1177
av_log(avctx, AV_LOG_ERROR, "Block %d has invalid size (size %d vs. %d bytes left)\n",
1178
s->block, frame_size, buf_size);
1181
if((samplecount = wavpack_decode_block(avctx, s->block, data,
1182
data_size, buf, frame_size)) < 0)
1185
buf += frame_size; buf_size -= frame_size;
1187
*data_size = samplecount * avctx->channels;
1189
return s->samples_left > 0 ? 0 : avpkt->size;
1192
AVCodec ff_wavpack_decoder = {
1023
1194
AVMEDIA_TYPE_AUDIO,
1024
1195
CODEC_ID_WAVPACK,
1025
1196
sizeof(WavpackContext),
1026
1197
wavpack_decode_init,
1029
1200
wavpack_decode_frame,
1030
1201
.capabilities = CODEC_CAP_SUBFRAMES,
1031
1202
.long_name = NULL_IF_CONFIG_SMALL("WavPack"),