]> git.sesse.net Git - ffmpeg/blob - libavcodec/flac.c
Merge commit '0c00fd80ee4791bd70b634084307fc9f179e0412'
[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 "libavutil/log.h"
24 #include "bytestream.h"
25 #include "get_bits.h"
26 #include "flac.h"
27 #include "flacdata.h"
28
29 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
30
31 static int64_t get_utf8(GetBitContext *gb)
32 {
33     int64_t val;
34     GET_UTF8(val, get_bits(gb, 8), return -1;)
35     return val;
36 }
37
38 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
39                                 FLACFrameInfo *fi, int log_level_offset)
40 {
41     int bs_code, sr_code, bps_code;
42
43     /* frame sync code */
44     if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
45         av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
46         return -1;
47     }
48
49     /* variable block size stream code */
50     fi->is_var_size = get_bits1(gb);
51
52     /* block size and sample rate codes */
53     bs_code = get_bits(gb, 4);
54     sr_code = get_bits(gb, 4);
55
56     /* channels and decorrelation */
57     fi->ch_mode = get_bits(gb, 4);
58     if (fi->ch_mode < FLAC_MAX_CHANNELS) {
59         fi->channels = fi->ch_mode + 1;
60         fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
61     } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
62         fi->channels = 2;
63         fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
64     } else {
65         av_log(avctx, AV_LOG_ERROR + log_level_offset,
66                "invalid channel mode: %d\n", fi->ch_mode);
67         return -1;
68     }
69
70     /* bits per sample */
71     bps_code = get_bits(gb, 3);
72     if (bps_code == 3 || bps_code == 7) {
73         av_log(avctx, AV_LOG_ERROR + log_level_offset,
74                "invalid sample size code (%d)\n",
75                bps_code);
76         return -1;
77     }
78     fi->bps = sample_size_table[bps_code];
79
80     /* reserved bit */
81     if (get_bits1(gb)) {
82         av_log(avctx, AV_LOG_ERROR + log_level_offset,
83                "broken stream, invalid padding\n");
84         return -1;
85     }
86
87     /* sample or frame count */
88     fi->frame_or_sample_num = get_utf8(gb);
89     if (fi->frame_or_sample_num < 0) {
90         av_log(avctx, AV_LOG_ERROR + log_level_offset,
91                "sample/frame number invalid; utf8 fscked\n");
92         return -1;
93     }
94
95     /* blocksize */
96     if (bs_code == 0) {
97         av_log(avctx, AV_LOG_ERROR + log_level_offset,
98                "reserved blocksize code: 0\n");
99         return -1;
100     } else if (bs_code == 6) {
101         fi->blocksize = get_bits(gb, 8) + 1;
102     } else if (bs_code == 7) {
103         fi->blocksize = get_bits(gb, 16) + 1;
104     } else {
105         fi->blocksize = ff_flac_blocksize_table[bs_code];
106     }
107
108     /* sample rate */
109     if (sr_code < 12) {
110         fi->samplerate = ff_flac_sample_rate_table[sr_code];
111     } else if (sr_code == 12) {
112         fi->samplerate = get_bits(gb, 8) * 1000;
113     } else if (sr_code == 13) {
114         fi->samplerate = get_bits(gb, 16);
115     } else if (sr_code == 14) {
116         fi->samplerate = get_bits(gb, 16) * 10;
117     } else {
118         av_log(avctx, AV_LOG_ERROR + log_level_offset,
119                "illegal sample rate code %d\n",
120                sr_code);
121         return -1;
122     }
123
124     /* header CRC-8 check */
125     skip_bits(gb, 8);
126     if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
127                get_bits_count(gb)/8)) {
128         av_log(avctx, AV_LOG_ERROR + log_level_offset,
129                "header crc mismatch\n");
130         return -1;
131     }
132
133     return 0;
134 }
135
136 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
137 {
138     /* Technically, there is no limit to FLAC frame size, but an encoder
139        should not write a frame that is larger than if verbatim encoding mode
140        were to be used. */
141
142     int count;
143
144     count = 16;                  /* frame header */
145     count += ch * ((7+bps+7)/8); /* subframe headers */
146     if (ch == 2) {
147         /* for stereo, need to account for using decorrelation */
148         count += (( 2*bps+1) * blocksize + 7) / 8;
149     } else {
150         count += ( ch*bps    * blocksize + 7) / 8;
151     }
152     count += 2; /* frame footer */
153
154     return count;
155 }
156
157 int avpriv_flac_is_extradata_valid(AVCodecContext *avctx,
158                                enum FLACExtradataFormat *format,
159                                uint8_t **streaminfo_start)
160 {
161     if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
162         av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
163         return 0;
164     }
165     if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
166         /* extradata contains STREAMINFO only */
167         if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
168             av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
169                    FLAC_STREAMINFO_SIZE-avctx->extradata_size);
170         }
171         *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
172         *streaminfo_start = avctx->extradata;
173     } else {
174         if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
175             av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
176             return 0;
177         }
178         *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
179         *streaminfo_start = &avctx->extradata[8];
180     }
181     return 1;
182 }
183
184 void avpriv_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
185                               const uint8_t *buffer)
186 {
187     GetBitContext gb;
188     init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
189
190     skip_bits(&gb, 16); /* skip min blocksize */
191     s->max_blocksize = get_bits(&gb, 16);
192     if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
193         av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
194                s->max_blocksize);
195         s->max_blocksize = 16;
196     }
197
198     skip_bits(&gb, 24); /* skip min frame size */
199     s->max_framesize = get_bits_long(&gb, 24);
200
201     s->samplerate = get_bits_long(&gb, 20);
202     s->channels = get_bits(&gb, 3) + 1;
203     s->bps = get_bits(&gb, 5) + 1;
204
205     avctx->channels = s->channels;
206     avctx->sample_rate = s->samplerate;
207     avctx->bits_per_raw_sample = s->bps;
208
209     s->samples  = get_bits_long(&gb, 32) << 4;
210     s->samples |= get_bits(&gb, 4);
211
212     skip_bits_long(&gb, 64); /* md5 sum */
213     skip_bits_long(&gb, 64); /* md5 sum */
214 }
215
216 void avpriv_flac_parse_block_header(const uint8_t *block_header,
217                                 int *last, int *type, int *size)
218 {
219     int tmp = bytestream_get_byte(&block_header);
220     if (last)
221         *last = tmp & 0x80;
222     if (type)
223         *type = tmp & 0x7F;
224     if (size)
225         *size = bytestream_get_be24(&block_header);
226 }