Statistics
| Branch: | Revision:

ffmpeg / libavcodec / flac.c @ 625daac4

History | View | Annotate | Download (4 KB)

1
/*
2
 * FLAC common code
3
 * Copyright (c) 2009 Justin Ruggles
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
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
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
#include "libavutil/crc.h"
23
#include "flac.h"
24
#include "flacdata.h"
25

    
26
static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
27

    
28
static int64_t get_utf8(GetBitContext *gb)
29
{
30
    int64_t val;
31
    GET_UTF8(val, get_bits(gb, 8), return -1;)
32
    return val;
33
}
34

    
35
int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
36
                                FLACFrameInfo *fi)
37
{
38
    int bs_code, sr_code, bps_code;
39

    
40
    /* frame sync code */
41
    skip_bits(gb, 16);
42

    
43
    /* block size and sample rate codes */
44
    bs_code = get_bits(gb, 4);
45
    sr_code = get_bits(gb, 4);
46

    
47
    /* channels and decorrelation */
48
    fi->ch_mode = get_bits(gb, 4);
49
    if (fi->ch_mode < FLAC_MAX_CHANNELS) {
50
        fi->channels = fi->ch_mode + 1;
51
        fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
52
    } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
53
        fi->channels = 2;
54
    } else {
55
        av_log(avctx, AV_LOG_ERROR, "invalid channel mode: %d\n", fi->ch_mode);
56
        return -1;
57
    }
58

    
59
    /* bits per sample */
60
    bps_code = get_bits(gb, 3);
61
    if (bps_code == 3 || bps_code == 7) {
62
        av_log(avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n",
63
               bps_code);
64
        return -1;
65
    }
66
    fi->bps = sample_size_table[bps_code];
67

    
68
    /* reserved bit */
69
    if (get_bits1(gb)) {
70
        av_log(avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
71
        return -1;
72
    }
73

    
74
    /* sample or frame count */
75
    if (get_utf8(gb) < 0) {
76
        av_log(avctx, AV_LOG_ERROR, "utf8 fscked\n");
77
        return -1;
78
    }
79

    
80
    /* blocksize */
81
    if (bs_code == 0) {
82
        av_log(avctx, AV_LOG_ERROR, "reserved blocksize code: 0\n");
83
        return -1;
84
    } else if (bs_code == 6) {
85
        fi->blocksize = get_bits(gb, 8) + 1;
86
    } else if (bs_code == 7) {
87
        fi->blocksize = get_bits(gb, 16) + 1;
88
    } else {
89
        fi->blocksize = ff_flac_blocksize_table[bs_code];
90
    }
91

    
92
    /* sample rate */
93
    if (sr_code < 12) {
94
        fi->samplerate = ff_flac_sample_rate_table[sr_code];
95
    } else if (sr_code == 12) {
96
        fi->samplerate = get_bits(gb, 8) * 1000;
97
    } else if (sr_code == 13) {
98
        fi->samplerate = get_bits(gb, 16);
99
    } else if (sr_code == 14) {
100
        fi->samplerate = get_bits(gb, 16) * 10;
101
    } else {
102
        av_log(avctx, AV_LOG_ERROR, "illegal sample rate code %d\n",
103
               sr_code);
104
        return -1;
105
    }
106

    
107
    /* header CRC-8 check */
108
    skip_bits(gb, 8);
109
    if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
110
               get_bits_count(gb)/8)) {
111
        av_log(avctx, AV_LOG_ERROR, "header crc mismatch\n");
112
        return -1;
113
    }
114

    
115
    return 0;
116
}
117

    
118
int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
119
{
120
    /* Technically, there is no limit to FLAC frame size, but an encoder
121
       should not write a frame that is larger than if verbatim encoding mode
122
       were to be used. */
123

    
124
    int count;
125

    
126
    count = 16;                  /* frame header */
127
    count += ch * ((7+bps+7)/8); /* subframe headers */
128
    if (ch == 2) {
129
        /* for stereo, need to account for using decorrelation */
130
        count += (( 2*bps+1) * blocksize + 7) / 8;
131
    } else {
132
        count += ( ch*bps    * blocksize + 7) / 8;
133
    }
134
    count += 2; /* frame footer */
135

    
136
    return count;
137
}