]> git.sesse.net Git - ffmpeg/blob - libavcodec/flac.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / flac.c
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, int log_level_offset)
37 {
38     int bs_code, sr_code, bps_code;
39
40     /* frame sync code */
41     if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
42         av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
43         return -1;
44     }
45
46     /* variable block size stream code */
47     fi->is_var_size = get_bits1(gb);
48
49     /* block size and sample rate codes */
50     bs_code = get_bits(gb, 4);
51     sr_code = get_bits(gb, 4);
52
53     /* channels and decorrelation */
54     fi->ch_mode = get_bits(gb, 4);
55     if (fi->ch_mode < FLAC_MAX_CHANNELS) {
56         fi->channels = fi->ch_mode + 1;
57         fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
58     } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
59         fi->channels = 2;
60         fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
61     } else {
62         av_log(avctx, AV_LOG_ERROR + log_level_offset,
63                "invalid channel mode: %d\n", fi->ch_mode);
64         return -1;
65     }
66
67     /* bits per sample */
68     bps_code = get_bits(gb, 3);
69     if (bps_code == 3 || bps_code == 7) {
70         av_log(avctx, AV_LOG_ERROR + log_level_offset,
71                "invalid sample size code (%d)\n",
72                bps_code);
73         return -1;
74     }
75     fi->bps = sample_size_table[bps_code];
76
77     /* reserved bit */
78     if (get_bits1(gb)) {
79         av_log(avctx, AV_LOG_ERROR + log_level_offset,
80                "broken stream, invalid padding\n");
81         return -1;
82     }
83
84     /* sample or frame count */
85     fi->frame_or_sample_num = get_utf8(gb);
86     if (fi->frame_or_sample_num < 0) {
87         av_log(avctx, AV_LOG_ERROR + log_level_offset,
88                "sample/frame number invalid; utf8 fscked\n");
89         return -1;
90     }
91
92     /* blocksize */
93     if (bs_code == 0) {
94         av_log(avctx, AV_LOG_ERROR + log_level_offset,
95                "reserved blocksize code: 0\n");
96         return -1;
97     } else if (bs_code == 6) {
98         fi->blocksize = get_bits(gb, 8) + 1;
99     } else if (bs_code == 7) {
100         fi->blocksize = get_bits(gb, 16) + 1;
101     } else {
102         fi->blocksize = ff_flac_blocksize_table[bs_code];
103     }
104
105     /* sample rate */
106     if (sr_code < 12) {
107         fi->samplerate = ff_flac_sample_rate_table[sr_code];
108     } else if (sr_code == 12) {
109         fi->samplerate = get_bits(gb, 8) * 1000;
110     } else if (sr_code == 13) {
111         fi->samplerate = get_bits(gb, 16);
112     } else if (sr_code == 14) {
113         fi->samplerate = get_bits(gb, 16) * 10;
114     } else {
115         av_log(avctx, AV_LOG_ERROR + log_level_offset,
116                "illegal sample rate code %d\n",
117                sr_code);
118         return -1;
119     }
120
121     /* header CRC-8 check */
122     skip_bits(gb, 8);
123     if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
124                get_bits_count(gb)/8)) {
125         av_log(avctx, AV_LOG_ERROR + log_level_offset,
126                "header crc mismatch\n");
127         return -1;
128     }
129
130     return 0;
131 }
132
133 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
134 {
135     /* Technically, there is no limit to FLAC frame size, but an encoder
136        should not write a frame that is larger than if verbatim encoding mode
137        were to be used. */
138
139     int count;
140
141     count = 16;                  /* frame header */
142     count += ch * ((7+bps+7)/8); /* subframe headers */
143     if (ch == 2) {
144         /* for stereo, need to account for using decorrelation */
145         count += (( 2*bps+1) * blocksize + 7) / 8;
146     } else {
147         count += ( ch*bps    * blocksize + 7) / 8;
148     }
149     count += 2; /* frame footer */
150
151     return count;
152 }