Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * FLAC (Free Lossless Audio Codec) decoder | ||
3 | * Copyright (c) 2003 Alex Beregszaszi | ||
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 | /** | ||
23 | * @file | ||
24 | * FLAC (Free Lossless Audio Codec) decoder | ||
25 | * @author Alex Beregszaszi | ||
26 | * @see http://flac.sourceforge.net/ | ||
27 | * | ||
28 | * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed | ||
29 | * through, starting from the initial 'fLaC' signature; or by passing the | ||
30 | * 34-byte streaminfo structure through avctx->extradata[_size] followed | ||
31 | * by data starting with the 0xFFF8 marker. | ||
32 | */ | ||
33 | |||
34 | #include <limits.h> | ||
35 | |||
36 | #include "libavutil/avassert.h" | ||
37 | #include "libavutil/crc.h" | ||
38 | #include "libavutil/mem.h" | ||
39 | #include "libavutil/opt.h" | ||
40 | #include "avcodec.h" | ||
41 | #include "codec_internal.h" | ||
42 | #include "get_bits.h" | ||
43 | #include "golomb.h" | ||
44 | #include "flac.h" | ||
45 | #include "flacdsp.h" | ||
46 | #include "flac_parse.h" | ||
47 | #include "thread.h" | ||
48 | #include "unary.h" | ||
49 | |||
50 | |||
51 | typedef struct FLACContext { | ||
52 | AVClass *class; | ||
53 | FLACStreaminfo stream_info; | ||
54 | |||
55 | AVCodecContext *avctx; ///< parent AVCodecContext | ||
56 | GetBitContext gb; ///< GetBitContext initialized to start at the current frame | ||
57 | |||
58 | int blocksize; ///< number of samples in the current frame | ||
59 | int sample_shift; ///< shift required to make output samples 16-bit or 32-bit | ||
60 | int ch_mode; ///< channel decorrelation type in the current frame | ||
61 | int got_streaminfo; ///< indicates if the STREAMINFO has been read | ||
62 | |||
63 | int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples | ||
64 | uint8_t *decoded_buffer; | ||
65 | unsigned int decoded_buffer_size; | ||
66 | int64_t *decoded_33bps; ///< decoded samples for a 33 bps subframe | ||
67 | uint8_t *decoded_buffer_33bps; | ||
68 | unsigned int decoded_buffer_size_33bps; | ||
69 | int buggy_lpc; ///< use workaround for old lavc encoded files | ||
70 | |||
71 | FLACDSPContext dsp; | ||
72 | } FLACContext; | ||
73 | |||
74 | static int allocate_buffers(FLACContext *s); | ||
75 | |||
76 | 237 | static void flac_set_bps(FLACContext *s) | |
77 | { | ||
78 | 237 | enum AVSampleFormat req = s->avctx->request_sample_fmt; | |
79 | 237 | int need32 = s->stream_info.bps > 16; | |
80 | 237 | int want32 = av_get_bytes_per_sample(req) > 2; | |
81 | 237 | int planar = av_sample_fmt_is_planar(req); | |
82 | |||
83 |
3/4✓ Branch 0 taken 219 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 219 times.
|
237 | if (need32 || want32) { |
84 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (planar) |
85 | ✗ | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P; | |
86 | else | ||
87 | 18 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32; | |
88 | 18 | s->sample_shift = 32 - s->stream_info.bps; | |
89 | } else { | ||
90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 219 times.
|
219 | if (planar) |
91 | ✗ | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | |
92 | else | ||
93 | 219 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16; | |
94 | 219 | s->sample_shift = 16 - s->stream_info.bps; | |
95 | } | ||
96 | 237 | } | |
97 | |||
98 | 237 | static av_cold int flac_decode_init(AVCodecContext *avctx) | |
99 | { | ||
100 | uint8_t *streaminfo; | ||
101 | int ret; | ||
102 | 237 | FLACContext *s = avctx->priv_data; | |
103 | 237 | s->avctx = avctx; | |
104 | |||
105 | /* for now, the raw FLAC header is allowed to be passed to the decoder as | ||
106 | frame data instead of extradata. */ | ||
107 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 221 times.
|
237 | if (!avctx->extradata) |
108 | 16 | return 0; | |
109 | |||
110 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 221 times.
|
221 | if (!ff_flac_is_extradata_valid(avctx, &streaminfo)) |
111 | ✗ | return AVERROR_INVALIDDATA; | |
112 | |||
113 | /* initialize based on the demuxer-supplied streamdata header */ | ||
114 | 221 | ret = ff_flac_parse_streaminfo(avctx, &s->stream_info, streaminfo); | |
115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
|
221 | if (ret < 0) |
116 | ✗ | return ret; | |
117 | 221 | ret = allocate_buffers(s); | |
118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
|
221 | if (ret < 0) |
119 | ✗ | return ret; | |
120 | 221 | flac_set_bps(s); | |
121 | 221 | ff_flacdsp_init(&s->dsp, avctx->sample_fmt, | |
122 | s->stream_info.channels); | ||
123 | 221 | s->got_streaminfo = 1; | |
124 | |||
125 | 221 | return 0; | |
126 | } | ||
127 | |||
128 | 16 | static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s) | |
129 | { | ||
130 | 16 | av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize); | |
131 | 16 | av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize); | |
132 | 16 | av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate); | |
133 | 16 | av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels); | |
134 | 16 | av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps); | |
135 | 16 | } | |
136 | |||
137 | 237 | static int allocate_buffers(FLACContext *s) | |
138 | { | ||
139 | int buf_size; | ||
140 | int ret; | ||
141 | |||
142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
|
237 | av_assert0(s->stream_info.max_blocksize); |
143 | |||
144 | 237 | buf_size = av_samples_get_buffer_size(NULL, s->stream_info.channels, | |
145 | s->stream_info.max_blocksize, | ||
146 | AV_SAMPLE_FMT_S32P, 0); | ||
147 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
|
237 | if (buf_size < 0) |
148 | ✗ | return buf_size; | |
149 | |||
150 | 237 | av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size); | |
151 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 237 times.
|
237 | if (!s->decoded_buffer) |
152 | ✗ | return AVERROR(ENOMEM); | |
153 | |||
154 | 237 | ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL, | |
155 | 237 | s->decoded_buffer, | |
156 | s->stream_info.channels, | ||
157 | s->stream_info.max_blocksize, | ||
158 | AV_SAMPLE_FMT_S32P, 0); | ||
159 |
4/6✓ Branch 0 taken 237 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 235 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
237 | if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) { |
160 | 2 | buf_size = av_samples_get_buffer_size(NULL, 1, | |
161 | s->stream_info.max_blocksize, | ||
162 | AV_SAMPLE_FMT_S64P, 0); | ||
163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (buf_size < 0) |
164 | ✗ | return buf_size; | |
165 | |||
166 | 2 | av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size); | |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->decoded_buffer_33bps) |
168 | ✗ | return AVERROR(ENOMEM); | |
169 | |||
170 | 2 | ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL, | |
171 | 2 | s->decoded_buffer_33bps, | |
172 | 1, | ||
173 | s->stream_info.max_blocksize, | ||
174 | AV_SAMPLE_FMT_S64P, 0); | ||
175 | |||
176 | } | ||
177 | 237 | return ret < 0 ? ret : 0; | |
178 | } | ||
179 | |||
180 | /** | ||
181 | * Parse the STREAMINFO from an inline header. | ||
182 | * @param s the flac decoding context | ||
183 | * @param buf input buffer, starting with the "fLaC" marker | ||
184 | * @param buf_size buffer size | ||
185 | * @return non-zero if metadata is invalid | ||
186 | */ | ||
187 | ✗ | static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size) | |
188 | { | ||
189 | int metadata_type, metadata_size, ret; | ||
190 | |||
191 | ✗ | if (buf_size < FLAC_STREAMINFO_SIZE+8) { | |
192 | /* need more data */ | ||
193 | ✗ | return 0; | |
194 | } | ||
195 | ✗ | flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size); | |
196 | ✗ | if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO || | |
197 | ✗ | metadata_size != FLAC_STREAMINFO_SIZE) { | |
198 | ✗ | return AVERROR_INVALIDDATA; | |
199 | } | ||
200 | ✗ | ret = ff_flac_parse_streaminfo(s->avctx, &s->stream_info, &buf[8]); | |
201 | ✗ | if (ret < 0) | |
202 | ✗ | return ret; | |
203 | ✗ | ret = allocate_buffers(s); | |
204 | ✗ | if (ret < 0) | |
205 | ✗ | return ret; | |
206 | ✗ | flac_set_bps(s); | |
207 | ✗ | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, | |
208 | s->stream_info.channels); | ||
209 | ✗ | s->got_streaminfo = 1; | |
210 | |||
211 | ✗ | return 0; | |
212 | } | ||
213 | |||
214 | /** | ||
215 | * Determine the size of an inline header. | ||
216 | * @param buf input buffer, starting with the "fLaC" marker | ||
217 | * @param buf_size buffer size | ||
218 | * @return number of bytes in the header, or 0 if more data is needed | ||
219 | */ | ||
220 | ✗ | static int get_metadata_size(const uint8_t *buf, int buf_size) | |
221 | { | ||
222 | int metadata_last, metadata_size; | ||
223 | ✗ | const uint8_t *buf_end = buf + buf_size; | |
224 | |||
225 | ✗ | buf += 4; | |
226 | do { | ||
227 | ✗ | if (buf_end - buf < 4) | |
228 | ✗ | return AVERROR_INVALIDDATA; | |
229 | ✗ | flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size); | |
230 | ✗ | buf += 4; | |
231 | ✗ | if (buf_end - buf < metadata_size) { | |
232 | /* need more data in order to read the complete header */ | ||
233 | ✗ | return AVERROR_INVALIDDATA; | |
234 | } | ||
235 | ✗ | buf += metadata_size; | |
236 | ✗ | } while (!metadata_last); | |
237 | |||
238 | ✗ | return buf_size - (buf_end - buf); | |
239 | } | ||
240 | |||
241 | 10540 | static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order) | |
242 | { | ||
243 | 10540 | GetBitContext gb = s->gb; | |
244 | int i, tmp, partition, method_type, rice_order; | ||
245 | int rice_bits, rice_esc; | ||
246 | int samples; | ||
247 | |||
248 | 10540 | method_type = get_bits(&gb, 2); | |
249 | 10540 | rice_order = get_bits(&gb, 4); | |
250 | |||
251 | 10540 | samples = s->blocksize >> rice_order; | |
252 | 10540 | rice_bits = 4 + method_type; | |
253 | 10540 | rice_esc = (1 << rice_bits) - 1; | |
254 | |||
255 | 10540 | decoded += pred_order; | |
256 | 10540 | i = pred_order; | |
257 | |||
258 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10540 times.
|
10540 | if (method_type > 1) { |
259 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n", | |
260 | method_type); | ||
261 | ✗ | return AVERROR_INVALIDDATA; | |
262 | } | ||
263 | |||
264 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10540 times.
|
10540 | if (samples << rice_order != s->blocksize) { |
265 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n", | |
266 | rice_order, s->blocksize); | ||
267 | ✗ | return AVERROR_INVALIDDATA; | |
268 | } | ||
269 | |||
270 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10540 times.
|
10540 | if (pred_order > samples) { |
271 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", | |
272 | pred_order, samples); | ||
273 | ✗ | return AVERROR_INVALIDDATA; | |
274 | } | ||
275 | |||
276 |
2/2✓ Branch 0 taken 137670 times.
✓ Branch 1 taken 10539 times.
|
148209 | for (partition = 0; partition < (1 << rice_order); partition++) { |
277 | 137670 | tmp = get_bits(&gb, rice_bits); | |
278 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 137670 times.
|
137670 | if (tmp == rice_esc) { |
279 | ✗ | tmp = get_bits(&gb, 5); | |
280 | ✗ | for (; i < samples; i++) | |
281 | ✗ | *decoded++ = get_sbits_long(&gb, tmp); | |
282 | } else { | ||
283 |
2/2✓ Branch 0 taken 131787 times.
✓ Branch 1 taken 5883 times.
|
137670 | int real_limit = (tmp > 1) ? (INT_MAX >> (tmp - 1)) + 2 : INT_MAX; |
284 |
2/2✓ Branch 0 taken 57998762 times.
✓ Branch 1 taken 137669 times.
|
58136431 | for (; i < samples; i++) { |
285 | 57998762 | int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1); | |
286 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 57998761 times.
|
57998762 | if (v == 0x80000000){ |
287 | 1 | av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n"); | |
288 | 1 | return AVERROR_INVALIDDATA; | |
289 | } | ||
290 | |||
291 | 57998761 | *decoded++ = v; | |
292 | } | ||
293 | } | ||
294 | 137669 | i= 0; | |
295 | } | ||
296 | |||
297 | 10539 | s->gb = gb; | |
298 | |||
299 | 10539 | return 0; | |
300 | } | ||
301 | |||
302 | 935 | static int decode_subframe_fixed(FLACContext *s, int32_t *decoded, | |
303 | int pred_order, int bps) | ||
304 | { | ||
305 | 935 | const int blocksize = s->blocksize; | |
306 | 935 | unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d); | |
307 | int i; | ||
308 | int ret; | ||
309 | |||
310 | /* warm up samples */ | ||
311 |
2/2✓ Branch 0 taken 2662 times.
✓ Branch 1 taken 935 times.
|
3597 | for (i = 0; i < pred_order; i++) { |
312 | 2662 | decoded[i] = get_sbits_long(&s->gb, bps); | |
313 | } | ||
314 | |||
315 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
|
935 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) |
316 | ✗ | return ret; | |
317 | |||
318 |
2/2✓ Branch 0 taken 837 times.
✓ Branch 1 taken 98 times.
|
935 | if (pred_order > 0) |
319 | 837 | a = decoded[pred_order-1]; | |
320 |
2/2✓ Branch 0 taken 721 times.
✓ Branch 1 taken 214 times.
|
935 | if (pred_order > 1) |
321 | 721 | b = a - decoded[pred_order-2]; | |
322 |
2/2✓ Branch 0 taken 591 times.
✓ Branch 1 taken 344 times.
|
935 | if (pred_order > 2) |
323 | 591 | c = b - decoded[pred_order-2] + decoded[pred_order-3]; | |
324 |
2/2✓ Branch 0 taken 513 times.
✓ Branch 1 taken 422 times.
|
935 | if (pred_order > 3) |
325 | 513 | d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4]; | |
326 | |||
327 |
5/6✓ Branch 0 taken 98 times.
✓ Branch 1 taken 116 times.
✓ Branch 2 taken 130 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 513 times.
✗ Branch 5 not taken.
|
935 | switch (pred_order) { |
328 | 98 | case 0: | |
329 | 98 | break; | |
330 | 116 | case 1: | |
331 |
2/2✓ Branch 0 taken 505740 times.
✓ Branch 1 taken 116 times.
|
505856 | for (i = pred_order; i < blocksize; i++) |
332 | 505740 | decoded[i] = a += decoded[i]; | |
333 | 116 | break; | |
334 | 130 | case 2: | |
335 |
2/2✓ Branch 0 taken 549640 times.
✓ Branch 1 taken 130 times.
|
549770 | for (i = pred_order; i < blocksize; i++) |
336 | 549640 | decoded[i] = a += b += decoded[i]; | |
337 | 130 | break; | |
338 | 78 | case 3: | |
339 |
2/2✓ Branch 0 taken 117270 times.
✓ Branch 1 taken 78 times.
|
117348 | for (i = pred_order; i < blocksize; i++) |
340 | 117270 | decoded[i] = a += b += c += decoded[i]; | |
341 | 78 | break; | |
342 | 513 | case 4: | |
343 |
2/2✓ Branch 0 taken 587484 times.
✓ Branch 1 taken 513 times.
|
587997 | for (i = pred_order; i < blocksize; i++) |
344 | 587484 | decoded[i] = a += b += c += d += decoded[i]; | |
345 | 513 | break; | |
346 | ✗ | default: | |
347 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); | |
348 | ✗ | return AVERROR_INVALIDDATA; | |
349 | } | ||
350 | |||
351 | 935 | return 0; | |
352 | } | ||
353 | |||
354 | #define DECODER_SUBFRAME_FIXED_WIDE(residual) { \ | ||
355 | const int blocksize = s->blocksize; \ | ||
356 | int ret; \ | ||
357 | \ | ||
358 | if ((ret = decode_residuals(s, residual, pred_order)) < 0) \ | ||
359 | return ret; \ | ||
360 | \ | ||
361 | switch (pred_order) { \ | ||
362 | case 0: \ | ||
363 | for (int i = pred_order; i < blocksize; i++) \ | ||
364 | decoded[i] = residual[i]; \ | ||
365 | break; \ | ||
366 | case 1: \ | ||
367 | for (int i = pred_order; i < blocksize; i++) \ | ||
368 | decoded[i] = (uint64_t)residual[i] + (uint64_t)decoded[i-1];\ | ||
369 | break; \ | ||
370 | case 2: \ | ||
371 | for (int i = pred_order; i < blocksize; i++) \ | ||
372 | decoded[i] = (uint64_t)residual[i] + 2*(uint64_t)decoded[i-1] - (uint64_t)decoded[i-2]; \ | ||
373 | break; \ | ||
374 | case 3: \ | ||
375 | for (int i = pred_order; i < blocksize; i++) \ | ||
376 | decoded[i] = (uint64_t)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[i-2] + (uint64_t)decoded[i-3]; \ | ||
377 | break; \ | ||
378 | case 4: \ | ||
379 | for (int i = pred_order; i < blocksize; i++) \ | ||
380 | decoded[i] = (uint64_t)residual[i] + 4*(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t)decoded[i-3] - (uint64_t)decoded[i-4]; \ | ||
381 | break; \ | ||
382 | default: \ | ||
383 | av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); \ | ||
384 | return AVERROR_INVALIDDATA; \ | ||
385 | } \ | ||
386 | return 0; \ | ||
387 | } | ||
388 | |||
389 | ✗ | static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded, | |
390 | int pred_order, int bps) | ||
391 | { | ||
392 | /* warm up samples */ | ||
393 | ✗ | for (int i = 0; i < pred_order; i++) { | |
394 | ✗ | decoded[i] = get_sbits_long(&s->gb, bps); | |
395 | } | ||
396 | ✗ | DECODER_SUBFRAME_FIXED_WIDE(decoded); | |
397 | } | ||
398 | |||
399 | |||
400 | ✗ | static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded, | |
401 | int32_t *residual, int pred_order) | ||
402 | { | ||
403 | /* warm up samples */ \ | ||
404 | ✗ | for (int i = 0; i < pred_order; i++) { \ | |
405 | ✗ | decoded[i] = get_sbits64(&s->gb, 33); \ | |
406 | } \ | ||
407 | ✗ | DECODER_SUBFRAME_FIXED_WIDE(residual); | |
408 | } | ||
409 | |||
410 | 1982 | static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32], | |
411 | int order, int qlevel, int len, int bps) | ||
412 | { | ||
413 | int i, j; | ||
414 | 1982 | int ebps = 1 << (bps-1); | |
415 | 1982 | unsigned sigma = 0; | |
416 | |||
417 |
2/2✓ Branch 0 taken 8704229 times.
✓ Branch 1 taken 1982 times.
|
8706211 | for (i = order; i < len; i++) |
418 | 8704229 | sigma |= decoded[i] + ebps; | |
419 | |||
420 |
1/2✓ Branch 0 taken 1982 times.
✗ Branch 1 not taken.
|
1982 | if (sigma < 2*ebps) |
421 | 1982 | return; | |
422 | |||
423 | ✗ | for (i = len - 1; i >= order; i--) { | |
424 | ✗ | int64_t p = 0; | |
425 | ✗ | for (j = 0; j < order; j++) | |
426 | ✗ | p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j]; | |
427 | ✗ | decoded[i] -= p >> qlevel; | |
428 | } | ||
429 | ✗ | for (i = order; i < len; i++, decoded++) { | |
430 | ✗ | int32_t p = 0; | |
431 | ✗ | for (j = 0; j < order; j++) | |
432 | ✗ | p += coeffs[j] * (uint32_t)decoded[j]; | |
433 | ✗ | decoded[j] += p >> qlevel; | |
434 | } | ||
435 | } | ||
436 | |||
437 | 9603 | static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order, | |
438 | int bps) | ||
439 | { | ||
440 | int i, ret; | ||
441 | int coeff_prec, qlevel; | ||
442 | int coeffs[32]; | ||
443 | |||
444 | /* warm up samples */ | ||
445 |
2/2✓ Branch 0 taken 41824 times.
✓ Branch 1 taken 9603 times.
|
51427 | for (i = 0; i < pred_order; i++) { |
446 | 41824 | decoded[i] = get_sbits_long(&s->gb, bps); | |
447 | } | ||
448 | |||
449 | 9603 | coeff_prec = get_bits(&s->gb, 4) + 1; | |
450 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9603 times.
|
9603 | if (coeff_prec == 16) { |
451 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n"); | |
452 | ✗ | return AVERROR_INVALIDDATA; | |
453 | } | ||
454 | 9603 | qlevel = get_sbits(&s->gb, 5); | |
455 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9603 times.
|
9603 | if (qlevel < 0) { |
456 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n", | |
457 | qlevel); | ||
458 | ✗ | return AVERROR_INVALIDDATA; | |
459 | } | ||
460 | |||
461 |
2/2✓ Branch 0 taken 41824 times.
✓ Branch 1 taken 9603 times.
|
51427 | for (i = 0; i < pred_order; i++) { |
462 | 41824 | coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); | |
463 | } | ||
464 | |||
465 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9602 times.
|
9603 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) |
466 | 1 | return ret; | |
467 | |||
468 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 9602 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
9602 | if ( ( s->buggy_lpc && s->stream_info.bps <= 16) |
469 |
3/4✓ Branch 0 taken 9602 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6916 times.
✓ Branch 3 taken 2686 times.
|
9602 | || ( !s->buggy_lpc && bps <= 16 |
470 |
2/2✓ Branch 0 taken 6173 times.
✓ Branch 1 taken 743 times.
|
6916 | && bps + coeff_prec + av_log2(pred_order) <= 32)) { |
471 | 6173 | s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize); | |
472 | } else { | ||
473 | 3429 | s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize); | |
474 |
2/2✓ Branch 0 taken 1982 times.
✓ Branch 1 taken 1447 times.
|
3429 | if (s->stream_info.bps <= 16) |
475 | 1982 | lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps); | |
476 | } | ||
477 | |||
478 | 9602 | return 0; | |
479 | } | ||
480 | |||
481 | 2 | static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded, | |
482 | int32_t *residual, int pred_order) | ||
483 | { | ||
484 | int i, ret; | ||
485 | int coeff_prec, qlevel; | ||
486 | int coeffs[32]; | ||
487 | |||
488 | /* warm up samples */ | ||
489 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (i = 0; i < pred_order; i++) { |
490 | 16 | decoded[i] = get_sbits64(&s->gb, 33); | |
491 | } | ||
492 | |||
493 | 2 | coeff_prec = get_bits(&s->gb, 4) + 1; | |
494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (coeff_prec == 16) { |
495 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n"); | |
496 | ✗ | return AVERROR_INVALIDDATA; | |
497 | } | ||
498 | 2 | qlevel = get_sbits(&s->gb, 5); | |
499 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (qlevel < 0) { |
500 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n", | |
501 | qlevel); | ||
502 | ✗ | return AVERROR_INVALIDDATA; | |
503 | } | ||
504 | |||
505 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
|
18 | for (i = 0; i < pred_order; i++) { |
506 | 16 | coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); | |
507 | } | ||
508 | |||
509 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = decode_residuals(s, residual, pred_order)) < 0) |
510 | ✗ | return ret; | |
511 | |||
512 | 2 | s->dsp.lpc33(decoded, residual, coeffs, pred_order, qlevel, s->blocksize); | |
513 | |||
514 | 2 | return 0; | |
515 | } | ||
516 | |||
517 | 16374 | static inline int decode_subframe(FLACContext *s, int channel) | |
518 | { | ||
519 | 16374 | int32_t *decoded = s->decoded[channel]; | |
520 | 16374 | int type, wasted = 0; | |
521 | 16374 | int bps = s->stream_info.bps; | |
522 | int i, ret; | ||
523 | |||
524 |
2/2✓ Branch 0 taken 6113 times.
✓ Branch 1 taken 10261 times.
|
16374 | if (channel == 0) { |
525 |
2/2✓ Branch 0 taken 642 times.
✓ Branch 1 taken 5471 times.
|
6113 | if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE) |
526 | 642 | bps++; | |
527 | } else { | ||
528 |
4/4✓ Branch 0 taken 8602 times.
✓ Branch 1 taken 1659 times.
✓ Branch 2 taken 907 times.
✓ Branch 3 taken 7695 times.
|
10261 | if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE) |
529 | 2566 | bps++; | |
530 | } | ||
531 | |||
532 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16374 times.
|
16374 | if (get_bits1(&s->gb)) { |
533 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n"); | |
534 | ✗ | return AVERROR_INVALIDDATA; | |
535 | } | ||
536 | 16374 | type = get_bits(&s->gb, 6); | |
537 | |||
538 |
2/2✓ Branch 1 taken 5246 times.
✓ Branch 2 taken 11128 times.
|
16374 | if (get_bits1(&s->gb)) { |
539 | 5246 | int left = get_bits_left(&s->gb); | |
540 |
2/4✓ Branch 0 taken 5246 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5246 times.
|
5246 | if ( left <= 0 || |
541 |
1/4✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5246 times.
|
5246 | (left < bps && !show_bits_long(&s->gb, left)) || |
542 | 5246 | !show_bits_long(&s->gb, bps-1)) { | |
543 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
544 | "Invalid number of wasted bits > available bits (%d) - left=%d\n", | ||
545 | bps, left); | ||
546 | ✗ | return AVERROR_INVALIDDATA; | |
547 | } | ||
548 | 5246 | wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb)); | |
549 | 5246 | bps -= wasted; | |
550 | } | ||
551 | |||
552 | //FIXME use av_log2 for types | ||
553 |
2/2✓ Branch 0 taken 5833 times.
✓ Branch 1 taken 10541 times.
|
16374 | if (type == 0) { |
554 |
1/2✓ Branch 0 taken 5833 times.
✗ Branch 1 not taken.
|
5833 | if (bps < 33) { |
555 | 5833 | int32_t tmp = get_sbits_long(&s->gb, bps); | |
556 |
2/2✓ Branch 0 taken 35956224 times.
✓ Branch 1 taken 5833 times.
|
35962057 | for (i = 0; i < s->blocksize; i++) |
557 | 35956224 | decoded[i] = tmp; | |
558 | } else { | ||
559 | ✗ | int64_t tmp = get_sbits64(&s->gb, 33); | |
560 | ✗ | for (i = 0; i < s->blocksize; i++) | |
561 | ✗ | s->decoded_33bps[i] = tmp; | |
562 | } | ||
563 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10540 times.
|
10541 | } else if (type == 1) { |
564 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (bps < 33) { |
565 | ✗ | for (i = 0; i < s->blocksize; i++) | |
566 | ✗ | decoded[i] = get_sbits_long(&s->gb, bps); | |
567 | } else { | ||
568 |
2/2✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 1 times.
|
4609 | for (i = 0; i < s->blocksize; i++) |
569 | 4608 | s->decoded_33bps[i] = get_sbits64(&s->gb, 33); | |
570 | } | ||
571 |
3/4✓ Branch 0 taken 10540 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
✓ Branch 3 taken 9605 times.
|
11475 | } else if ((type >= 8) && (type <= 12)) { |
572 | 935 | int order = type & ~0x8; | |
573 |
1/2✓ Branch 0 taken 935 times.
✗ Branch 1 not taken.
|
935 | if (bps < 33) { |
574 |
1/2✓ Branch 0 taken 935 times.
✗ Branch 1 not taken.
|
935 | if (bps + order <= 32) { |
575 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 935 times.
|
935 | if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0) |
576 | ✗ | return ret; | |
577 | } else { | ||
578 | ✗ | if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0) | |
579 | ✗ | return ret; | |
580 | } | ||
581 | } else { | ||
582 | ✗ | if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0) | |
583 | ✗ | return ret; | |
584 | } | ||
585 |
1/2✓ Branch 0 taken 9605 times.
✗ Branch 1 not taken.
|
9605 | } else if (type >= 32) { |
586 |
2/2✓ Branch 0 taken 9603 times.
✓ Branch 1 taken 2 times.
|
9605 | if (bps < 33) { |
587 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9602 times.
|
9603 | if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0) |
588 | 1 | return ret; | |
589 | } else { | ||
590 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0) |
591 | ✗ | return ret; | |
592 | } | ||
593 | } else { | ||
594 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n"); | |
595 | ✗ | return AVERROR_INVALIDDATA; | |
596 | } | ||
597 | |||
598 |
2/2✓ Branch 0 taken 5246 times.
✓ Branch 1 taken 11127 times.
|
16373 | if (wasted) { |
599 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5240 times.
|
5246 | if (wasted+bps == 33) { |
600 | 6 | s->dsp.wasted33(s->decoded_33bps, decoded, wasted, s->blocksize); | |
601 |
1/2✓ Branch 0 taken 5240 times.
✗ Branch 1 not taken.
|
5240 | } else if (wasted < 32) { |
602 | 5240 | s->dsp.wasted32(decoded, wasted, s->blocksize); | |
603 | } | ||
604 | } | ||
605 | |||
606 | 16373 | return 0; | |
607 | } | ||
608 | |||
609 | 6113 | static int decode_frame(FLACContext *s) | |
610 | { | ||
611 | int i, ret; | ||
612 | 6113 | GetBitContext *gb = &s->gb; | |
613 | FLACFrameInfo fi; | ||
614 | |||
615 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6113 times.
|
6113 | if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) { |
616 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n"); | |
617 | ✗ | return ret; | |
618 | } | ||
619 | |||
620 |
2/2✓ Branch 0 taken 6097 times.
✓ Branch 1 taken 16 times.
|
6113 | if ( s->stream_info.channels |
621 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6097 times.
|
6097 | && fi.channels != s->stream_info.channels |
622 | ✗ | && s->got_streaminfo) { | |
623 | ✗ | s->stream_info.channels = fi.channels; | |
624 | ✗ | ff_flac_set_channel_layout(s->avctx, fi.channels); | |
625 | ✗ | ret = allocate_buffers(s); | |
626 | ✗ | if (ret < 0) | |
627 | ✗ | return ret; | |
628 | } | ||
629 | 6113 | s->stream_info.channels = fi.channels; | |
630 | 6113 | ff_flac_set_channel_layout(s->avctx, fi.channels); | |
631 | 6113 | s->ch_mode = fi.ch_mode; | |
632 | |||
633 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6097 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
6113 | if (!s->stream_info.bps && !fi.bps) { |
634 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n"); | |
635 | ✗ | return AVERROR_INVALIDDATA; | |
636 | } | ||
637 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6113 times.
|
6113 | if (!fi.bps) { |
638 | ✗ | fi.bps = s->stream_info.bps; | |
639 |
3/4✓ Branch 0 taken 6097 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6097 times.
|
6113 | } else if (s->stream_info.bps && fi.bps != s->stream_info.bps) { |
640 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not " | |
641 | "supported\n"); | ||
642 | ✗ | return AVERROR_INVALIDDATA; | |
643 | } | ||
644 | |||
645 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6097 times.
|
6113 | if (!s->stream_info.bps) { |
646 | 16 | s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps; | |
647 | 16 | flac_set_bps(s); | |
648 | } | ||
649 | |||
650 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6097 times.
|
6113 | if (!s->stream_info.max_blocksize) |
651 | 16 | s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE; | |
652 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6113 times.
|
6113 | if (fi.blocksize > s->stream_info.max_blocksize) { |
653 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize, | |
654 | s->stream_info.max_blocksize); | ||
655 | ✗ | return AVERROR_INVALIDDATA; | |
656 | } | ||
657 | 6113 | s->blocksize = fi.blocksize; | |
658 | |||
659 |
3/4✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6097 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
|
6113 | if (!s->stream_info.samplerate && !fi.samplerate) { |
660 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO" | |
661 | " or frame header\n"); | ||
662 | ✗ | return AVERROR_INVALIDDATA; | |
663 | } | ||
664 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6113 times.
|
6113 | if (fi.samplerate == 0) |
665 | ✗ | fi.samplerate = s->stream_info.samplerate; | |
666 | 6113 | s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate; | |
667 | |||
668 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6097 times.
|
6113 | if (!s->got_streaminfo) { |
669 | 16 | ret = allocate_buffers(s); | |
670 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | if (ret < 0) |
671 | ✗ | return ret; | |
672 | 16 | s->got_streaminfo = 1; | |
673 | 16 | dump_headers(s->avctx, &s->stream_info); | |
674 | } | ||
675 | 6113 | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, | |
676 | s->stream_info.channels); | ||
677 | |||
678 | // dump_headers(s->avctx, &s->stream_info); | ||
679 | |||
680 | /* subframes */ | ||
681 |
2/2✓ Branch 0 taken 16374 times.
✓ Branch 1 taken 6112 times.
|
22486 | for (i = 0; i < s->stream_info.channels; i++) { |
682 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 16373 times.
|
16374 | if ((ret = decode_subframe(s, i)) < 0) |
683 | 1 | return ret; | |
684 | } | ||
685 | |||
686 | 6112 | align_get_bits(gb); | |
687 | |||
688 | /* frame footer */ | ||
689 | 6112 | skip_bits(gb, 16); /* data crc */ | |
690 | |||
691 | 6112 | return 0; | |
692 | } | ||
693 | |||
694 | 9 | static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len) | |
695 | { | ||
696 | int i; | ||
697 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) { |
698 | ✗ | for (i = 0; i < len; i++) | |
699 | ✗ | decoded[1][i] = decoded[0][i] - (uint64_t)decoded_33bps[i]; | |
700 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) { |
701 |
2/2✓ Branch 0 taken 34917 times.
✓ Branch 1 taken 8 times.
|
34925 | for (i = 0; i < len; i++) |
702 | 34917 | decoded[0][i] = decoded[1][i] + (uint64_t)decoded_33bps[i]; | |
703 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) { |
704 |
2/2✓ Branch 0 taken 4608 times.
✓ Branch 1 taken 1 times.
|
4609 | for (i = 0; i < len; i++) { |
705 | 4608 | uint64_t a = decoded[0][i]; | |
706 | 4608 | int64_t b = decoded_33bps[i]; | |
707 | 4608 | a -= b >> 1; | |
708 | 4608 | decoded[0][i] = (a + b); | |
709 | 4608 | decoded[1][i] = a; | |
710 | } | ||
711 | } | ||
712 | 9 | } | |
713 | |||
714 | 6113 | static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
715 | int *got_frame_ptr, AVPacket *avpkt) | ||
716 | { | ||
717 | 6113 | const uint8_t *buf = avpkt->data; | |
718 | 6113 | int buf_size = avpkt->size; | |
719 | 6113 | FLACContext *s = avctx->priv_data; | |
720 | 6113 | int bytes_read = 0; | |
721 | int ret; | ||
722 | |||
723 | 6113 | *got_frame_ptr = 0; | |
724 | |||
725 |
2/4✓ Branch 0 taken 6113 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6113 times.
|
6113 | if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) { |
726 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n"); | |
727 | ✗ | return buf_size; | |
728 | } | ||
729 | |||
730 |
2/4✓ Branch 0 taken 6113 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6113 times.
|
6113 | if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) { |
731 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n"); | |
732 | ✗ | return buf_size; | |
733 | } | ||
734 | |||
735 | /* check that there is at least the smallest decodable amount of data. | ||
736 | this amount corresponds to the smallest valid FLAC frame possible. | ||
737 | FF F8 69 02 00 00 9A 00 00 34 */ | ||
738 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6113 times.
|
6113 | if (buf_size < FLAC_MIN_FRAME_SIZE) |
739 | ✗ | return buf_size; | |
740 | |||
741 | /* check for inline header */ | ||
742 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6113 times.
|
6113 | if (AV_RB32(buf) == MKBETAG('f','L','a','C')) { |
743 | ✗ | if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) { | |
744 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid header\n"); | |
745 | ✗ | return ret; | |
746 | } | ||
747 | ✗ | return get_metadata_size(buf, buf_size); | |
748 | } | ||
749 | |||
750 | /* decode frame */ | ||
751 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6113 times.
|
6113 | if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0) |
752 | ✗ | return ret; | |
753 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6112 times.
|
6113 | if ((ret = decode_frame(s)) < 0) { |
754 | 1 | av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n"); | |
755 | 1 | return ret; | |
756 | } | ||
757 | 6112 | bytes_read = get_bits_count(&s->gb)/8; | |
758 | |||
759 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6112 times.
|
6112 | if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) && |
760 | ✗ | av_crc(av_crc_get_table(AV_CRC_16_ANSI), | |
761 | 0, buf, bytes_read)) { | ||
762 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts); | |
763 | ✗ | if (s->avctx->err_recognition & AV_EF_EXPLODE) | |
764 | ✗ | return AVERROR_INVALIDDATA; | |
765 | } | ||
766 | |||
767 | /* get output buffer */ | ||
768 | 6112 | frame->nb_samples = s->blocksize; | |
769 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6112 times.
|
6112 | if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) |
770 | ✗ | return ret; | |
771 | |||
772 |
4/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6101 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 2 times.
|
6112 | if (s->stream_info.bps == 32 && s->ch_mode > 0) { |
773 | 9 | decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize); | |
774 | 9 | s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels, | |
775 | s->blocksize, s->sample_shift); | ||
776 | } else { | ||
777 | 6103 | s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, | |
778 | s->stream_info.channels, | ||
779 | s->blocksize, s->sample_shift); | ||
780 | } | ||
781 | |||
782 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6112 times.
|
6112 | if (bytes_read > buf_size) { |
783 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size); | |
784 | ✗ | return AVERROR_INVALIDDATA; | |
785 | } | ||
786 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6112 times.
|
6112 | if (bytes_read < buf_size) { |
787 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n", | |
788 | buf_size - bytes_read, buf_size); | ||
789 | } | ||
790 | |||
791 | 6112 | *got_frame_ptr = 1; | |
792 | |||
793 | 6112 | return bytes_read; | |
794 | } | ||
795 | |||
796 | 237 | static av_cold int flac_decode_close(AVCodecContext *avctx) | |
797 | { | ||
798 | 237 | FLACContext *s = avctx->priv_data; | |
799 | |||
800 | 237 | av_freep(&s->decoded_buffer); | |
801 | 237 | av_freep(&s->decoded_buffer_33bps); | |
802 | |||
803 | 237 | return 0; | |
804 | } | ||
805 | |||
806 | static const AVOption options[] = { | ||
807 | { "use_buggy_lpc", "emulate old buggy lavc behavior", offsetof(FLACContext, buggy_lpc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM }, | ||
808 | { NULL }, | ||
809 | }; | ||
810 | |||
811 | static const AVClass flac_decoder_class = { | ||
812 | .class_name = "FLAC decoder", | ||
813 | .item_name = av_default_item_name, | ||
814 | .option = options, | ||
815 | .version = LIBAVUTIL_VERSION_INT, | ||
816 | }; | ||
817 | |||
818 | const FFCodec ff_flac_decoder = { | ||
819 | .p.name = "flac", | ||
820 | CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"), | ||
821 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
822 | .p.id = AV_CODEC_ID_FLAC, | ||
823 | .priv_data_size = sizeof(FLACContext), | ||
824 | .init = flac_decode_init, | ||
825 | .close = flac_decode_close, | ||
826 | FF_CODEC_DECODE_CB(flac_decode_frame), | ||
827 | .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | | ||
828 | AV_CODEC_CAP_DR1 | | ||
829 | AV_CODEC_CAP_FRAME_THREADS, | ||
830 | .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16, | ||
831 | AV_SAMPLE_FMT_S16P, | ||
832 | AV_SAMPLE_FMT_S32, | ||
833 | AV_SAMPLE_FMT_S32P, | ||
834 | AV_SAMPLE_FMT_NONE }, | ||
835 | .p.priv_class = &flac_decoder_class, | ||
836 | }; | ||
837 |