~ubuntu-dev/mplayer/ubuntu-feisty

« back to all changes in this revision

Viewing changes to libavcodec/flac.c

  • Committer: William Grant
  • Date: 2007-02-03 03:16:07 UTC
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: william.grant@ubuntu.org.au-20070203031607-08gc2ompbz6spt9i
Update to 1.0rc1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * FLAC (Free Lossless Audio Codec) decoder
3
3
 * Copyright (c) 2003 Alex Beregszaszi
4
4
 *
5
 
 * This library is free software; you can redistribute it and/or
 
5
 * This file is part of FFmpeg.
 
6
 *
 
7
 * FFmpeg is free software; you can redistribute it and/or
6
8
 * modify it under the terms of the GNU Lesser General Public
7
9
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
 
10
 * version 2.1 of the License, or (at your option) any later version.
9
11
 *
10
 
 * This library is distributed in the hope that it will be useful,
 
12
 * FFmpeg is distributed in the hope that it will be useful,
11
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
15
 * Lesser General Public License for more details.
14
16
 *
15
17
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
 
18
 * License along with FFmpeg; if not, write to the Free Software
17
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
20
 */
19
21
 
33
35
 
34
36
#include <limits.h>
35
37
 
 
38
#define ALT_BITSTREAM_READER
36
39
#include "avcodec.h"
37
40
#include "bitstream.h"
38
41
#include "golomb.h"
85
88
256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
86
89
};
87
90
 
88
 
static int64_t get_utf8(GetBitContext *gb)
89
 
{
90
 
    uint64_t val;
91
 
    int ones=0, bytes;
92
 
 
93
 
    while(get_bits1(gb))
94
 
        ones++;
95
 
 
96
 
    if     (ones==0) bytes=0;
97
 
    else if(ones==1) return -1;
98
 
    else             bytes= ones - 1;
99
 
 
100
 
    val= get_bits(gb, 7-ones);
101
 
    while(bytes--){
102
 
        const int tmp = get_bits(gb, 8);
103
 
 
104
 
        if((tmp>>6) != 2)
105
 
            return -1;
106
 
        val<<=6;
107
 
        val|= tmp&0x3F;
108
 
    }
 
91
static int64_t get_utf8(GetBitContext *gb){
 
92
    int64_t val;
 
93
    GET_UTF8(val, get_bits(gb, 8), return -1;)
109
94
    return val;
110
95
}
111
96
 
112
 
#if 0
113
 
static int skip_utf8(GetBitContext *gb)
114
 
{
115
 
    int ones=0, bytes;
116
 
 
117
 
    while(get_bits1(gb))
118
 
        ones++;
119
 
 
120
 
    if     (ones==0) bytes=0;
121
 
    else if(ones==1) return -1;
122
 
    else             bytes= ones - 1;
123
 
 
124
 
    skip_bits(gb, 7-ones);
125
 
    while(bytes--){
126
 
        const int tmp = get_bits(gb, 8);
127
 
 
128
 
        if((tmp>>6) != 2)
129
 
            return -1;
130
 
    }
131
 
    return 0;
132
 
}
133
 
#endif
134
 
 
135
97
static void metadata_streaminfo(FLACContext *s);
136
98
static void dump_headers(FLACContext *s);
137
99
 
296
258
 
297
259
static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
298
260
{
299
 
    int sum, i, j;
 
261
    int i, j;
300
262
    int coeff_prec, qlevel;
301
263
    int coeffs[pred_order];
302
264
 
334
296
    if (decode_residuals(s, channel, pred_order) < 0)
335
297
        return -1;
336
298
 
337
 
    for (i = pred_order; i < s->blocksize; i++)
338
 
    {
339
 
        sum = 0;
340
 
        for (j = 0; j < pred_order; j++)
341
 
            sum += coeffs[j] * s->decoded[channel][i-j-1];
342
 
        s->decoded[channel][i] += sum >> qlevel;
 
299
    if (s->bps > 16) {
 
300
        int64_t sum;
 
301
        for (i = pred_order; i < s->blocksize; i++)
 
302
        {
 
303
            sum = 0;
 
304
            for (j = 0; j < pred_order; j++)
 
305
                sum += (int64_t)coeffs[j] * s->decoded[channel][i-j-1];
 
306
            s->decoded[channel][i] += sum >> qlevel;
 
307
        }
 
308
    } else {
 
309
        int sum;
 
310
        for (i = pred_order; i < s->blocksize; i++)
 
311
        {
 
312
            sum = 0;
 
313
            for (j = 0; j < pred_order; j++)
 
314
                sum += coeffs[j] * s->decoded[channel][i-j-1];
 
315
            s->decoded[channel][i] += sum >> qlevel;
 
316
        }
343
317
    }
344
318
 
345
319
    return 0;
538
512
    return 0;
539
513
}
540
514
 
 
515
static inline int16_t shift_to_16_bits(int32_t data, int bps)
 
516
{
 
517
    if (bps == 24) {
 
518
        return (data >> 8);
 
519
    } else if (bps == 20) {
 
520
        return (data >> 4);
 
521
    } else {
 
522
        return data;
 
523
    }
 
524
}
 
525
 
541
526
static int flac_decode_frame(AVCodecContext *avctx,
542
527
                            void *data, int *data_size,
543
528
                            uint8_t *buf, int buf_size)
674
659
    }
675
660
    }
676
661
#else
 
662
#define DECORRELATE(left, right)\
 
663
            assert(s->channels == 2);\
 
664
            for (i = 0; i < s->blocksize; i++)\
 
665
            {\
 
666
                int a= s->decoded[0][i];\
 
667
                int b= s->decoded[1][i];\
 
668
                *(samples++) = (left ) >> (16 - s->bps);\
 
669
                *(samples++) = (right) >> (16 - s->bps);\
 
670
            }\
 
671
            break;
 
672
 
677
673
    switch(s->decorrelation)
678
674
    {
679
675
        case INDEPENDENT:
680
676
            for (j = 0; j < s->blocksize; j++)
681
677
            {
682
678
                for (i = 0; i < s->channels; i++)
683
 
                    *(samples++) = s->decoded[i][j];
 
679
                    *(samples++) = shift_to_16_bits(s->decoded[i][j], s->bps);
684
680
            }
685
681
            break;
686
682
        case LEFT_SIDE:
687
 
            assert(s->channels == 2);
688
 
            for (i = 0; i < s->blocksize; i++)
689
 
            {
690
 
                *(samples++) = s->decoded[0][i];
691
 
                *(samples++) = s->decoded[0][i] - s->decoded[1][i];
692
 
            }
693
 
            break;
 
683
            DECORRELATE(a,a-b)
694
684
        case RIGHT_SIDE:
695
 
            assert(s->channels == 2);
696
 
            for (i = 0; i < s->blocksize; i++)
697
 
            {
698
 
                *(samples++) = s->decoded[0][i] + s->decoded[1][i];
699
 
                *(samples++) = s->decoded[1][i];
700
 
            }
701
 
            break;
 
685
            DECORRELATE(a+b,b)
702
686
        case MID_SIDE:
703
 
            assert(s->channels == 2);
704
 
            for (i = 0; i < s->blocksize; i++)
705
 
            {
706
 
                int mid, side;
707
 
                mid = s->decoded[0][i];
708
 
                side = s->decoded[1][i];
709
 
 
710
 
#if 1 //needs to be checked but IMHO it should be binary identical
711
 
                mid -= side>>1;
712
 
                *(samples++) = mid + side;
713
 
                *(samples++) = mid;
714
 
#else
715
 
 
716
 
                mid <<= 1;
717
 
                if (side & 1)
718
 
                    mid++;
719
 
                *(samples++) = (mid + side) >> 1;
720
 
                *(samples++) = (mid - side) >> 1;
721
 
#endif
722
 
            }
723
 
            break;
 
687
            DECORRELATE( (a-=b>>1) + b, a)
724
688
    }
725
689
#endif
726
690