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
29
31
41 };
42
44 {
47 return val;
48 }
49
52 {
53 int bs_code, sr_code, bps_code;
54
55 /* frame sync code */
56 if ((
get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
58 return -1;
59 }
60
61 /* variable block size stream code */
63
64 /* block size and sample rate codes */
67
68 /* channels and decorrelation */
74 fi->channels = 2;
76 } else {
78 "invalid channel mode: %d\n", fi->
ch_mode);
79 return -1;
80 }
81
82 /* bits per sample */
84 if (bps_code == 3 || bps_code == 7) {
86 "invalid sample size code (%d)\n",
87 bps_code);
88 return -1;
89 }
91
92 /* reserved bit */
95 "broken stream, invalid padding\n");
96 return -1;
97 }
98
99 /* sample or frame count */
103 "sample/frame number invalid; utf8 fscked\n");
104 return -1;
105 }
106
107 /* blocksize */
108 if (bs_code == 0) {
110 "reserved blocksize code: 0\n");
111 return -1;
112 } else if (bs_code == 6) {
114 } else if (bs_code == 7) {
116 } else {
118 }
119
120 /* sample rate */
121 if (sr_code < 12) {
123 } else if (sr_code == 12) {
124 fi->samplerate =
get_bits(gb, 8) * 1000;
125 } else if (sr_code == 13) {
127 } else if (sr_code == 14) {
128 fi->samplerate =
get_bits(gb, 16) * 10;
129 } else {
131 "illegal sample rate code %d\n",
132 sr_code);
133 return -1;
134 }
135
136 /* header CRC-8 check */
141 "header crc mismatch\n");
142 return -1;
143 }
144
145 return 0;
146 }
147
149 {
150 /* Technically, there is no limit to FLAC frame size, but an encoder
151 should not write a frame that is larger than if verbatim encoding mode
152 were to be used. */
153
155
156 count = 16; /* frame header */
157 count += ch * ((7+bps+7)/8); /* subframe headers */
158 if (ch == 2) {
159 /* for stereo, need to account for using decorrelation */
160 count += (( 2*bps+1) * blocksize + 7) / 8;
161 } else {
162 count += ( ch*bps * blocksize + 7) / 8;
163 }
164 count += 2; /* frame footer */
165
167 }
168
172 {
175 return 0;
176 }
178 /* extradata contains STREAMINFO only */
182 }
185 } else {
188 return 0;
189 }
191 *streaminfo_start = &avctx->
extradata[8];
192 }
193 return 1;
194 }
195
197 {
200 else
202 }
203
206 {
209
210 skip_bits(&gb, 16);
/* skip min blocksize */
211 s->max_blocksize =
get_bits(&gb, 16);
214 s->max_blocksize);
215 s->max_blocksize = 16;
216 }
217
218 skip_bits(&gb, 24);
/* skip min frame size */
220
224
229
231
234 }
235
237 int *last,
int *type,
int *
size)
238 {
239 int tmp = bytestream_get_byte(&block_header);
240 if (last)
241 *last = tmp & 0x80;
242 if (type)
243 *type = tmp & 0x7F;
244 if (size)
245 *size = bytestream_get_be24(&block_header);
246 }