~phatforge/libav/fixup

« back to all changes in this revision

Viewing changes to libavcodec/wavpack.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2011-04-30 14:27:42 UTC
  • mfrom: (1.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110430142742-quvblxk1tj6adlh5
Tags: 4:0.7~b1-1ubuntu1
* Merge from debian. Remaining changes:
  - don't build against libfaad, libdirac, librtmp and libopenjpeg
    (all in universe)
  - explicitly --enable-pic on powerpc, cf. LP #654666
  - different arm configure bits that should probably better be
    merged into debian
* Cherry-picked from git: 
  - install doc/APIChanges and refer to them in NEWS.Debian (Closes: #623682)
  - don't try to install non-existing documentation, fixes FTBFS on powerpc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * WavPack lossless audio decoder
3
 
 * Copyright (c) 2006 Konstantin Shishkov
4
 
 *
5
 
 * This file is part of FFmpeg.
6
 
 *
7
 
 * FFmpeg is free software; you can redistribute it and/or
 
3
 * Copyright (c) 2006,2011 Konstantin Shishkov
 
4
 *
 
5
 * This file is part of Libav.
 
6
 *
 
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.
11
11
 *
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.
16
16
 *
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
20
20
 */
21
21
#define ALT_BITSTREAM_READER_LE
22
22
#include "avcodec.h"
23
23
#include "get_bits.h"
24
24
#include "unary.h"
 
25
#include "libavutil/audioconvert.h"
25
26
 
26
27
/**
27
28
 * @file
91
92
    int bitrate_acc, bitrate_delta;
92
93
} WvChannel;
93
94
 
94
 
typedef struct WavpackContext {
 
95
typedef struct WavpackFrameContext {
95
96
    AVCodecContext *avctx;
96
97
    int frame_flags;
97
98
    int stereo, stereo_in;
118
119
    int max_samples;
119
120
    int pos;
120
121
    SavedContext sc, extra_sc;
 
122
} WavpackFrameContext;
 
123
 
 
124
#define WV_MAX_FRAME_DECODERS 14
 
125
 
 
126
typedef struct WavpackContext {
 
127
    AVCodecContext *avctx;
 
128
 
 
129
    WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
 
130
    int fdec_num;
 
131
 
 
132
    int multichannel;
 
133
    int mkv_mode;
 
134
    int block;
 
135
    int samples;
 
136
    int samples_left;
 
137
    int ch_offset;
121
138
} WavpackContext;
122
139
 
123
140
// exponent table copied from WavPack source
224
241
    return res;
225
242
}
226
243
 
227
 
static void update_error_limit(WavpackContext *ctx)
 
244
static void update_error_limit(WavpackFrameContext *ctx)
228
245
{
229
246
    int i, br[2], sl[2];
230
247
 
258
275
    }
259
276
}
260
277
 
261
 
static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int *last)
 
278
static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb, int channel, int *last)
262
279
{
263
280
    int t, t2;
264
281
    int sign, base, add, ret;
361
378
    return sign ? ~ret : ret;
362
379
}
363
380
 
364
 
static inline int wv_get_value_integer(WavpackContext *s, uint32_t *crc, int S)
 
381
static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc, int S)
365
382
{
366
383
    int bit;
367
384
 
377
394
    return (((S + bit) << s->shift) - bit) << s->post_shift;
378
395
}
379
396
 
380
 
static float wv_get_value_float(WavpackContext *s, uint32_t *crc, int S)
 
397
static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
381
398
{
382
399
    union {
383
400
        float    f;
450
467
    return value.f;
451
468
}
452
469
 
453
 
static void wv_reset_saved_context(WavpackContext *s)
 
470
static void wv_reset_saved_context(WavpackFrameContext *s)
454
471
{
455
472
    s->pos = 0;
456
473
    s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
457
474
}
458
475
 
459
 
static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
 
476
static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb, void *dst, const int type)
460
477
{
461
478
    int i, j, count = 0;
462
479
    int last, t;
467
484
    int16_t *dst16 = dst;
468
485
    int32_t *dst32 = dst;
469
486
    float   *dstfl = dst;
 
487
    const int channel_pad = s->avctx->channels - 2;
470
488
 
471
489
    if(s->samples_left == s->samples)
472
490
        s->one = s->zero = s->zeroes = 0;
494
512
                    B = s->decorr[i].samplesB[pos];
495
513
                    j = (pos + t) & 7;
496
514
                }
497
 
                if(type != SAMPLE_FMT_S16){
 
515
                if(type != AV_SAMPLE_FMT_S16){
498
516
                    L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
499
517
                    R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
500
518
                }else{
506
524
                s->decorr[i].samplesA[j] = L = L2;
507
525
                s->decorr[i].samplesB[j] = R = R2;
508
526
            }else if(t == -1){
509
 
                if(type != SAMPLE_FMT_S16)
 
527
                if(type != AV_SAMPLE_FMT_S16)
510
528
                    L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
511
529
                else
512
530
                    L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
513
531
                UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
514
532
                L = L2;
515
 
                if(type != SAMPLE_FMT_S16)
 
533
                if(type != AV_SAMPLE_FMT_S16)
516
534
                    R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
517
535
                else
518
536
                    R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
520
538
                R = R2;
521
539
                s->decorr[i].samplesA[0] = R;
522
540
            }else{
523
 
                if(type != SAMPLE_FMT_S16)
 
541
                if(type != AV_SAMPLE_FMT_S16)
524
542
                    R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
525
543
                else
526
544
                    R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
532
550
                    s->decorr[i].samplesA[0] = R;
533
551
                }
534
552
 
535
 
                if(type != SAMPLE_FMT_S16)
 
553
                if(type != AV_SAMPLE_FMT_S16)
536
554
                    L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
537
555
                else
538
556
                    L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
546
564
            L += (R -= (L >> 1));
547
565
        crc = (crc * 3 + L) * 3 + R;
548
566
 
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;
555
575
        } else {
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;
558
579
        }
559
580
        count++;
560
581
    }while(!last && count < s->max_samples);
582
603
    return count * 2;
583
604
}
584
605
 
585
 
static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
 
606
static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb, void *dst, const int type)
586
607
{
587
608
    int i, j, count = 0;
588
609
    int last, t;
593
614
    int16_t *dst16 = dst;
594
615
    int32_t *dst32 = dst;
595
616
    float   *dstfl = dst;
 
617
    const int channel_stride = s->avctx->channels;
596
618
 
597
619
    if(s->samples_left == s->samples)
598
620
        s->one = s->zero = s->zeroes = 0;
613
635
                A = s->decorr[i].samplesA[pos];
614
636
                j = (pos + t) & 7;
615
637
            }
616
 
            if(type != SAMPLE_FMT_S16)
 
638
            if(type != AV_SAMPLE_FMT_S16)
617
639
                S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
618
640
            else
619
641
                S = T + ((s->decorr[i].weightA * A + 512) >> 10);
623
645
        pos = (pos + 1) & 7;
624
646
        crc = crc * 3 + S;
625
647
 
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);
630
 
        else
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;
 
654
        }else{
 
655
            *dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
 
656
            dst16 += channel_stride;
 
657
        }
632
658
        count++;
633
 
    }while(!last && count < s->samples);
 
659
    }while(!last && count < s->max_samples);
634
660
 
635
661
    s->samples_left -= count;
636
662
    if(!s->samples_left){
655
681
    return count;
656
682
}
657
683
 
 
684
static av_cold int wv_alloc_frame_context(WavpackContext *c)
 
685
{
 
686
 
 
687
    if(c->fdec_num == WV_MAX_FRAME_DECODERS)
 
688
        return -1;
 
689
 
 
690
    c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
 
691
    if(!c->fdec[c->fdec_num])
 
692
        return -1;
 
693
    c->fdec_num++;
 
694
    c->fdec[c->fdec_num - 1]->avctx = c->avctx;
 
695
    wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
 
696
 
 
697
    return 0;
 
698
}
 
699
 
658
700
static av_cold int wavpack_decode_init(AVCodecContext *avctx)
659
701
{
660
702
    WavpackContext *s = avctx->priv_data;
661
703
 
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;
666
707
    else
667
 
        avctx->sample_fmt = SAMPLE_FMT_S32;
668
 
    avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
669
 
 
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;
 
711
 
 
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 */
 
715
    s->mkv_mode = 0;
 
716
    if(s->multichannel && avctx->extradata && avctx->extradata_size == 2){
 
717
        int ver = AV_RL16(avctx->extradata);
 
718
        if(ver >= 0x402 && ver <= 0x410)
 
719
            s->mkv_mode = 1;
 
720
    }
 
721
 
 
722
    s->fdec_num = 0;
671
723
 
672
724
    return 0;
673
725
}
674
726
 
675
 
static int wavpack_decode_frame(AVCodecContext *avctx,
676
 
                            void *data, int *data_size,
677
 
                            AVPacket *avpkt)
 
727
static av_cold int wavpack_decode_end(AVCodecContext *avctx)
678
728
{
679
 
    const uint8_t *buf = avpkt->data;
680
 
    int buf_size = avpkt->size;
681
729
    WavpackContext *s = avctx->priv_data;
 
730
    int i;
 
731
 
 
732
    for(i = 0; i < s->fdec_num; i++)
 
733
        av_freep(&s->fdec[i]);
 
734
    s->fdec_num = 0;
 
735
 
 
736
    return 0;
 
737
}
 
738
 
 
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)
 
742
{
 
743
    WavpackContext *wc = avctx->priv_data;
 
744
    WavpackFrameContext *s;
682
745
    void *samples = data;
683
746
    int samplecount;
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;
688
 
    int bpp;
 
752
    int bpp, chan, chmask;
689
753
 
690
754
    if (buf_size == 0){
691
755
        *data_size = 0;
692
756
        return 0;
693
757
    }
694
758
 
 
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");
 
761
        return -1;
 
762
    }
 
763
 
 
764
    s = wc->fdec[block_no];
 
765
    if(!s){
 
766
        av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);
 
767
        return -1;
 
768
    }
 
769
 
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;
701
776
    }
702
777
 
703
 
    s->samples = AV_RL32(buf); buf += 4;
704
 
    if(!s->samples){
705
 
        *data_size = 0;
706
 
        return buf_size;
 
778
    if(!wc->mkv_mode){
 
779
        s->samples = AV_RL32(buf); buf += 4;
 
780
        if(!s->samples){
 
781
            *data_size = 0;
 
782
            return buf_size;
 
783
        }
 
784
    }else{
 
785
        s->samples = wc->samples;
707
786
    }
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){
713
792
        bpp = 2;
714
 
        avctx->sample_fmt = SAMPLE_FMT_S16;
 
793
        avctx->sample_fmt = AV_SAMPLE_FMT_S16;
715
794
    } else {
716
795
        bpp = 4;
717
 
        avctx->sample_fmt = SAMPLE_FMT_S32;
 
796
        avctx->sample_fmt = AV_SAMPLE_FMT_S32;
718
797
    }
 
798
    samples = (uint8_t*)samples + bpp * wc->ch_offset;
 
799
 
 
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;
 
807
    if(wc->mkv_mode)
 
808
        buf += 4; //skip block size;
 
809
 
 
810
    wc->ch_offset += 1 + s->stereo;
725
811
 
726
812
    s->max_samples = *data_size / (bpp * avctx->channels);
727
813
    s->max_samples = FFMIN(s->max_samples, s->samples);
895
981
            got_float = 1;
896
982
            break;
897
983
        case WP_ID_DATA:
898
 
            s->sc.offset = buf - avpkt->data;
 
984
            s->sc.offset = buf - orig_buf;
899
985
            s->sc.size   = size * 8;
900
986
            init_get_bits(&s->gb, buf, size * 8);
901
987
            s->data_size = size * 8;
908
994
                buf += size;
909
995
                continue;
910
996
            }
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);
915
1001
            buf += size;
916
1002
            s->got_extra_bits = 1;
917
1003
            break;
 
1004
        case WP_ID_CHANINFO:
 
1005
            if(size <= 1){
 
1006
                av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");
 
1007
                return -1;
 
1008
            }
 
1009
            chan = *buf++;
 
1010
            switch(size - 2){
 
1011
            case 0:
 
1012
                chmask = *buf;
 
1013
                break;
 
1014
            case 1:
 
1015
                chmask = AV_RL16(buf);
 
1016
                break;
 
1017
            case 2:
 
1018
                chmask = AV_RL24(buf);
 
1019
                break;
 
1020
            case 3:
 
1021
                chmask = AV_RL32(buf);
 
1022
                break;
 
1023
            case 5:
 
1024
                chan |= (buf[1] & 0xF) << 8;
 
1025
                chmask = AV_RL24(buf + 2);
 
1026
                break;
 
1027
            default:
 
1028
                av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
 
1029
                chan = avctx->channels;
 
1030
                chmask = avctx->channel_layout;
 
1031
            }
 
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);
 
1035
                return -1;
 
1036
            }
 
1037
            if(!avctx->channel_layout)
 
1038
                avctx->channel_layout = chmask;
 
1039
            buf += size - 1;
 
1040
            break;
918
1041
        default:
919
1042
            buf += size;
920
1043
        }
945
1068
            av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
946
1069
            return -1;
947
1070
        }
948
 
        if(!got_float && avctx->sample_fmt == SAMPLE_FMT_FLT){
 
1071
        if(!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT){
949
1072
            av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
950
1073
            return -1;
951
1074
        }
952
 
        if(s->got_extra_bits && avctx->sample_fmt != SAMPLE_FMT_FLT){
 
1075
        if(s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT){
953
1076
            const int size = get_bits_left(&s->gb_extra_bits);
954
1077
            const int wanted = s->samples * s->extra_bits << s->stereo_in;
955
1078
            if(size < wanted){
959
1082
        }
960
1083
        s->samples_left = s->samples;
961
1084
    }else{
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);
968
1091
        }
969
1092
    }
970
1093
 
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);
976
1099
        else
977
 
            samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_FLT);
978
 
 
 
1100
            samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
 
1101
        samplecount >>= 1;
979
1102
    }else{
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;
 
1104
 
 
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);
984
1109
        else
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);
986
1111
 
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;
991
 
            while(cnt--){
992
 
                *--dst = *--src;
993
 
                *--dst = *src;
994
 
            }
995
 
            samplecount *= 2;
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;
1000
 
            while(cnt--){
1001
 
                *--dst = *--src;
1002
 
                *--dst = *src;
1003
 
            }
1004
 
            samplecount *= 2;
 
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;
 
1116
            while(cnt--){
 
1117
                *dst = *src;
 
1118
                src += channel_stride;
 
1119
                dst += channel_stride;
 
1120
            }
 
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;
 
1125
            while(cnt--){
 
1126
                *dst = *src;
 
1127
                src += channel_stride;
 
1128
                dst += channel_stride;
 
1129
            }
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;
1009
1134
            while(cnt--){
1010
 
                *--dst = *--src;
1011
 
                *--dst = *src;
1012
 
            }
1013
 
            samplecount *= 2;
1014
 
        }
1015
 
    }
1016
 
    *data_size = samplecount * bpp;
1017
 
 
1018
 
    return s->samples_left > 0 ? 0 : buf_size;
1019
 
}
1020
 
 
1021
 
AVCodec wavpack_decoder = {
 
1135
                *dst = *src;
 
1136
                src += channel_stride;
 
1137
                dst += channel_stride;
 
1138
            }
 
1139
        }
 
1140
    }
 
1141
 
 
1142
    wc->samples_left = s->samples_left;
 
1143
 
 
1144
    return samplecount * bpp;
 
1145
}
 
1146
 
 
1147
static int wavpack_decode_frame(AVCodecContext *avctx,
 
1148
                            void *data, int *data_size,
 
1149
                            AVPacket *avpkt)
 
1150
{
 
1151
    WavpackContext *s = avctx->priv_data;
 
1152
    const uint8_t *buf = avpkt->data;
 
1153
    int buf_size = avpkt->size;
 
1154
    int frame_size;
 
1155
    int samplecount = 0;
 
1156
 
 
1157
    s->block = 0;
 
1158
    s->samples_left = 0;
 
1159
    s->ch_offset = 0;
 
1160
 
 
1161
    if(s->mkv_mode){
 
1162
        s->samples = AV_RL32(buf); buf += 4;
 
1163
    }
 
1164
    while(buf_size > 0){
 
1165
        if(!s->multichannel){
 
1166
            frame_size = buf_size;
 
1167
        }else{
 
1168
            if(!s->mkv_mode){
 
1169
                frame_size = AV_RL32(buf) - 12; buf += 4; buf_size -= 4;
 
1170
            }else{
 
1171
                if(buf_size < 12) //MKV files can have zero flags after last block
 
1172
                    break;
 
1173
                frame_size = AV_RL32(buf + 8) + 12;
 
1174
            }
 
1175
        }
 
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);
 
1179
            return -1;
 
1180
        }
 
1181
        if((samplecount = wavpack_decode_block(avctx, s->block, data,
 
1182
                                               data_size, buf, frame_size)) < 0)
 
1183
            return -1;
 
1184
        s->block++;
 
1185
        buf += frame_size; buf_size -= frame_size;
 
1186
    }
 
1187
    *data_size = samplecount * avctx->channels;
 
1188
 
 
1189
    return s->samples_left > 0 ? 0 : avpkt->size;
 
1190
}
 
1191
 
 
1192
AVCodec ff_wavpack_decoder = {
1022
1193
    "wavpack",
1023
1194
    AVMEDIA_TYPE_AUDIO,
1024
1195
    CODEC_ID_WAVPACK,
1025
1196
    sizeof(WavpackContext),
1026
1197
    wavpack_decode_init,
1027
1198
    NULL,
1028
 
    NULL,
 
1199
    wavpack_decode_end,
1029
1200
    wavpack_decode_frame,
1030
1201
    .capabilities = CODEC_CAP_SUBFRAMES,
1031
1202
    .long_name = NULL_IF_CONFIG_SMALL("WavPack"),