Bug Summary

File:root/firefox-clang/media/ffvpx/libavcodec/flacdec.c
Warning:line 310, column 14
Assigned value is uninitialized

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name flacdec.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/media/ffvpx/libavcodec -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/media/ffvpx/libavcodec -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -include libavutil_visibility.h -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D HAVE_AV_CONFIG_H -D ASSERT_LEVEL=2 -I /root/firefox-clang/media/ffvpx/libavcodec -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/media/ffvpx/libavcodec -I /root/firefox-clang/modules/fdlibm/inexact-math-override -I /root/firefox-clang/third_party/khronos/vulkan-headers/include -I /root/firefox-clang/media/mozva -I /root/firefox-clang/media/libopus/include -I /root/firefox-clang/media/libvorbis -I /root/firefox-clang/media/libvpx -I /root/firefox-clang/media/ffvpx -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=tautological-type-limit-compare -Wno-range-loop-analysis -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-unknown-warning-option -Wno-character-conversion -Wno-parentheses -Wno-pointer-sign -Wno-sign-compare -Wno-switch -Wno-type-limits -Wno-unused-function -Wno-deprecated-declarations -Wno-absolute-value -Wno-incompatible-pointer-types -Wno-string-conversion -Wno-visibility -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-01-224014-2642839-1 -x c /root/firefox-clang/media/ffvpx/libavcodec/flacdec.c
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
51typedef 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_CHANNELS8]; ///< 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
74static int allocate_buffers(FLACContext *s);
75
76static void flac_set_bps(FLACContext *s)
77{
78 enum AVSampleFormat req = s->avctx->request_sample_fmt;
79 int need32 = s->stream_info.bps > 16;
80 int want32 = av_get_bytes_per_sample(req) > 2;
81 int planar = av_sample_fmt_is_planar(req);
82
83 if (need32 || want32) {
84 if (planar)
85 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
86 else
87 s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
88 s->sample_shift = 32 - s->stream_info.bps;
89 } else {
90 if (planar)
91 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
92 else
93 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
94 s->sample_shift = 16 - s->stream_info.bps;
95 }
96}
97
98static av_cold__attribute__((cold)) int flac_decode_init(AVCodecContext *avctx)
99{
100 uint8_t *streaminfo;
101 int ret;
102 FLACContext *s = avctx->priv_data;
103 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 if (!avctx->extradata)
108 return 0;
109
110 if (!ff_flac_is_extradata_valid(avctx, &streaminfo))
111 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
112
113 /* initialize based on the demuxer-supplied streamdata header */
114 ret = ff_flac_parse_streaminfo(avctx, &s->stream_info, streaminfo);
115 if (ret < 0)
116 return ret;
117 ret = allocate_buffers(s);
118 if (ret < 0)
119 return ret;
120 flac_set_bps(s);
121 ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
122 s->stream_info.channels);
123 s->got_streaminfo = 1;
124
125 return 0;
126}
127
128static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
129{
130 av_log(avctx, AV_LOG_DEBUG48, " Max Blocksize: %d\n", s->max_blocksize);
131 av_log(avctx, AV_LOG_DEBUG48, " Max Framesize: %d\n", s->max_framesize);
132 av_log(avctx, AV_LOG_DEBUG48, " Samplerate: %d\n", s->samplerate);
133 av_log(avctx, AV_LOG_DEBUG48, " Channels: %d\n", s->channels);
134 av_log(avctx, AV_LOG_DEBUG48, " Bits: %d\n", s->bps);
135}
136
137static int allocate_buffers(FLACContext *s)
138{
139 int buf_size;
140 int ret;
141
142 av_assert0(s->stream_info.max_blocksize)do { if (!(s->stream_info.max_blocksize)) { av_log(((void*
)0), 0, "Assertion %s failed at %s:%d\n", "s->stream_info.max_blocksize"
, "/root/firefox-clang/media/ffvpx/libavcodec/flacdec.c", 142
); abort(); } } while (0)
;
143
144 buf_size = av_samples_get_buffer_size(NULL((void*)0), s->stream_info.channels,
145 s->stream_info.max_blocksize,
146 AV_SAMPLE_FMT_S32P, 0);
147 if (buf_size < 0)
148 return buf_size;
149
150 av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
151 if (!s->decoded_buffer) {
152 memset(s->decoded, 0, sizeof(s->decoded));
153 return AVERROR(ENOMEM)(-(12));
154 }
155
156 ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL((void*)0),
157 s->decoded_buffer,
158 s->stream_info.channels,
159 s->stream_info.max_blocksize,
160 AV_SAMPLE_FMT_S32P, 0);
161 if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) {
162 buf_size = av_samples_get_buffer_size(NULL((void*)0), 1,
163 s->stream_info.max_blocksize,
164 AV_SAMPLE_FMT_S64P, 0);
165 if (buf_size < 0)
166 return buf_size;
167
168 av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size);
169 if (!s->decoded_buffer_33bps) {
170 s->decoded_33bps = NULL((void*)0);
171 return AVERROR(ENOMEM)(-(12));
172 }
173
174 ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL((void*)0),
175 s->decoded_buffer_33bps,
176 1,
177 s->stream_info.max_blocksize,
178 AV_SAMPLE_FMT_S64P, 0);
179
180 }
181 return ret < 0 ? ret : 0;
182}
183
184/**
185 * Parse the STREAMINFO from an inline header.
186 * @param s the flac decoding context
187 * @param buf input buffer, starting with the "fLaC" marker
188 * @param buf_size buffer size
189 * @return non-zero if metadata is invalid
190 */
191static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
192{
193 int metadata_type, metadata_size, ret;
194
195 if (buf_size < FLAC_STREAMINFO_SIZE34+8) {
196 /* need more data */
197 return 0;
198 }
199 flac_parse_block_header(&buf[4], NULL((void*)0), &metadata_type, &metadata_size);
200 if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
201 metadata_size != FLAC_STREAMINFO_SIZE34) {
202 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
203 }
204 ret = ff_flac_parse_streaminfo(s->avctx, &s->stream_info, &buf[8]);
205 if (ret < 0)
206 return ret;
207 ret = allocate_buffers(s);
208 if (ret < 0)
209 return ret;
210 flac_set_bps(s);
211 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
212 s->stream_info.channels);
213 s->got_streaminfo = 1;
214
215 return 0;
216}
217
218/**
219 * Determine the size of an inline header.
220 * @param buf input buffer, starting with the "fLaC" marker
221 * @param buf_size buffer size
222 * @return number of bytes in the header, or 0 if more data is needed
223 */
224static int get_metadata_size(const uint8_t *buf, int buf_size)
225{
226 int metadata_last, metadata_size;
227 const uint8_t *buf_end = buf + buf_size;
228
229 buf += 4;
230 do {
231 if (buf_end - buf < 4)
232 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
233 flac_parse_block_header(buf, &metadata_last, NULL((void*)0), &metadata_size);
234 buf += 4;
235 if (buf_end - buf < metadata_size) {
236 /* need more data in order to read the complete header */
237 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
238 }
239 buf += metadata_size;
240 } while (!metadata_last);
241
242 return buf_size - (buf_end - buf);
243}
244
245static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
246{
247 GetBitContext gb = s->gb;
248 int i, tmp, partition, method_type, rice_order;
249 int rice_bits, rice_esc;
250 int samples;
251
252 method_type = get_bits(&gb, 2);
253 rice_order = get_bits(&gb, 4);
254
255 samples = s->blocksize >> rice_order;
256 rice_bits = 4 + method_type;
257 rice_esc = (1 << rice_bits) - 1;
258
259 decoded += pred_order;
260 i = pred_order;
261
262 if (method_type > 1) {
263 av_log(s->avctx, AV_LOG_ERROR16, "illegal residual coding method %d\n",
264 method_type);
265 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
266 }
267
268 if (samples << rice_order != s->blocksize) {
269 av_log(s->avctx, AV_LOG_ERROR16, "invalid rice order: %i blocksize %i\n",
270 rice_order, s->blocksize);
271 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
272 }
273
274 if (pred_order > samples) {
275 av_log(s->avctx, AV_LOG_ERROR16, "invalid predictor order: %i > %i\n",
276 pred_order, samples);
277 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
278 }
279
280 for (partition = 0; partition < (1 << rice_order); partition++) {
281 tmp = get_bits(&gb, rice_bits);
282 if (tmp == rice_esc) {
283 tmp = get_bits(&gb, 5);
284 for (; i < samples; i++)
285 *decoded++ = get_sbits_long(&gb, tmp);
286 } else {
287 int real_limit = (tmp > 1) ? (INT_MAX2147483647 >> (tmp - 1)) + 2 : INT_MAX2147483647;
288 for (; i < samples; i++) {
289 int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1);
290 if (v == 0x80000000){
291 av_log(s->avctx, AV_LOG_ERROR16, "invalid residual\n");
292 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
293 }
294
295 *decoded++ = v;
296 }
297 }
298 i= 0;
299 }
300
301 s->gb = gb;
302
303 return 0;
304}
305
306static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
307 int pred_order, int bps)
308{
309 const int blocksize = s->blocksize;
310 unsigned av_uninit(a)a=a, av_uninit(b)b=b, av_uninit(c)c=c, av_uninit(d)d=d;
49
Assigned value is uninitialized
311 int i;
312 int ret;
313
314 /* warm up samples */
315 for (i = 0; i < pred_order; i++) {
316 decoded[i] = get_sbits_long(&s->gb, bps);
317 }
318
319 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
320 return ret;
321
322 if (pred_order > 0)
323 a = decoded[pred_order-1];
324 if (pred_order > 1)
325 b = a - decoded[pred_order-2];
326 if (pred_order > 2)
327 c = b - decoded[pred_order-2] + decoded[pred_order-3];
328 if (pred_order > 3)
329 d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4];
330
331 switch (pred_order) {
332 case 0:
333 break;
334 case 1:
335 for (i = pred_order; i < blocksize; i++)
336 decoded[i] = a += decoded[i];
337 break;
338 case 2:
339 for (i = pred_order; i < blocksize; i++)
340 decoded[i] = a += b += decoded[i];
341 break;
342 case 3:
343 for (i = pred_order; i < blocksize; i++)
344 decoded[i] = a += b += c += decoded[i];
345 break;
346 case 4:
347 for (i = pred_order; i < blocksize; i++)
348 decoded[i] = a += b += c += d += decoded[i];
349 break;
350 default:
351 av_log(s->avctx, AV_LOG_ERROR16, "illegal pred order %d\n", pred_order);
352 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
353 }
354
355 return 0;
356}
357
358#define DECODER_SUBFRAME_FIXED_WIDE(residual){ const int blocksize = s->blocksize; int ret; if ((ret = decode_residuals
(s, residual, pred_order)) < 0) return ret; switch (pred_order
) { case 0: for (int i = pred_order; i < blocksize; i++) decoded
[i] = residual[i]; break; case 1: for (int i = pred_order; i <
blocksize; i++) decoded[i] = (uint64_t)residual[i] + (uint64_t
)decoded[i-1]; break; case 2: for (int i = pred_order; i <
blocksize; i++) decoded[i] = (uint64_t)residual[i] + 2*(uint64_t
)decoded[i-1] - (uint64_t)decoded[i-2]; break; case 3: for (int
i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t
)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded
[i-2] + (uint64_t)decoded[i-3]; break; case 4: for (int i = pred_order
; i < blocksize; i++) 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]; break; default: av_log
(s->avctx, 16, "illegal pred order %d\n", pred_order); return
(-(int)(('I') | (('N') << 8) | (('D') << 16) | (
(unsigned)('A') << 24))); } return 0; }
{ \
359 const int blocksize = s->blocksize; \
360 int ret; \
361 \
362 if ((ret = decode_residuals(s, residual, pred_order)) < 0) \
363 return ret; \
364 \
365 switch (pred_order) { \
366 case 0: \
367 for (int i = pred_order; i < blocksize; i++) \
368 decoded[i] = residual[i]; \
369 break; \
370 case 1: \
371 for (int i = pred_order; i < blocksize; i++) \
372 decoded[i] = (uint64_t)residual[i] + (uint64_t)decoded[i-1];\
373 break; \
374 case 2: \
375 for (int i = pred_order; i < blocksize; i++) \
376 decoded[i] = (uint64_t)residual[i] + 2*(uint64_t)decoded[i-1] - (uint64_t)decoded[i-2]; \
377 break; \
378 case 3: \
379 for (int i = pred_order; i < blocksize; i++) \
380 decoded[i] = (uint64_t)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[i-2] + (uint64_t)decoded[i-3]; \
381 break; \
382 case 4: \
383 for (int i = pred_order; i < blocksize; i++) \
384 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]; \
385 break; \
386 default: \
387 av_log(s->avctx, AV_LOG_ERROR16, "illegal pred order %d\n", pred_order); \
388 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
; \
389 } \
390 return 0; \
391}
392
393static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded,
394 int pred_order, int bps)
395{
396 /* warm up samples */
397 for (int i = 0; i < pred_order; i++) {
398 decoded[i] = get_sbits_long(&s->gb, bps);
399 }
400 DECODER_SUBFRAME_FIXED_WIDE(decoded){ const int blocksize = s->blocksize; int ret; if ((ret = decode_residuals
(s, decoded, pred_order)) < 0) return ret; switch (pred_order
) { case 0: for (int i = pred_order; i < blocksize; i++) decoded
[i] = decoded[i]; break; case 1: for (int i = pred_order; i <
blocksize; i++) decoded[i] = (uint64_t)decoded[i] + (uint64_t
)decoded[i-1]; break; case 2: for (int i = pred_order; i <
blocksize; i++) decoded[i] = (uint64_t)decoded[i] + 2*(uint64_t
)decoded[i-1] - (uint64_t)decoded[i-2]; break; case 3: for (int
i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t
)decoded[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[
i-2] + (uint64_t)decoded[i-3]; break; case 4: for (int i = pred_order
; i < blocksize; i++) decoded[i] = (uint64_t)decoded[i] + 4
*(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t
)decoded[i-3] - (uint64_t)decoded[i-4]; break; default: av_log
(s->avctx, 16, "illegal pred order %d\n", pred_order); return
(-(int)(('I') | (('N') << 8) | (('D') << 16) | (
(unsigned)('A') << 24))); } return 0; }
;
401}
402
403
404static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded,
405 int32_t *residual, int pred_order)
406{
407 /* warm up samples */ \
408 for (int i = 0; i < pred_order; i++) { \
409 decoded[i] = get_sbits64(&s->gb, 33); \
410 } \
411 DECODER_SUBFRAME_FIXED_WIDE(residual){ const int blocksize = s->blocksize; int ret; if ((ret = decode_residuals
(s, residual, pred_order)) < 0) return ret; switch (pred_order
) { case 0: for (int i = pred_order; i < blocksize; i++) decoded
[i] = residual[i]; break; case 1: for (int i = pred_order; i <
blocksize; i++) decoded[i] = (uint64_t)residual[i] + (uint64_t
)decoded[i-1]; break; case 2: for (int i = pred_order; i <
blocksize; i++) decoded[i] = (uint64_t)residual[i] + 2*(uint64_t
)decoded[i-1] - (uint64_t)decoded[i-2]; break; case 3: for (int
i = pred_order; i < blocksize; i++) decoded[i] = (uint64_t
)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded
[i-2] + (uint64_t)decoded[i-3]; break; case 4: for (int i = pred_order
; i < blocksize; i++) 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]; break; default: av_log
(s->avctx, 16, "illegal pred order %d\n", pred_order); return
(-(int)(('I') | (('N') << 8) | (('D') << 16) | (
(unsigned)('A') << 24))); } return 0; }
;
412}
413
414static void lpc_analyze_remodulate(SUINT32uint32_t *decoded, const int coeffs[32],
415 int order, int qlevel, int len, int bps)
416{
417 int i, j;
418 int ebps = 1 << (bps-1);
419 unsigned sigma = 0;
420
421 for (i = order; i < len; i++)
422 sigma |= decoded[i] + ebps;
423
424 if (sigma < 2*ebps)
425 return;
426
427 for (i = len - 1; i >= order; i--) {
428 int64_t p = 0;
429 for (j = 0; j < order; j++)
430 p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j];
431 decoded[i] -= p >> qlevel;
432 }
433 for (i = order; i < len; i++, decoded++) {
434 int32_t p = 0;
435 for (j = 0; j < order; j++)
436 p += coeffs[j] * (uint32_t)decoded[j];
437 decoded[j] += p >> qlevel;
438 }
439}
440
441static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
442 int bps)
443{
444 int i, ret;
445 int coeff_prec, qlevel;
446 int coeffs[32];
447
448 /* warm up samples */
449 for (i = 0; i < pred_order; i++) {
450 decoded[i] = get_sbits_long(&s->gb, bps);
451 }
452
453 coeff_prec = get_bits(&s->gb, 4) + 1;
454 if (coeff_prec == 16) {
455 av_log(s->avctx, AV_LOG_ERROR16, "invalid coeff precision\n");
456 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
457 }
458 qlevel = get_sbits(&s->gb, 5);
459 if (qlevel < 0) {
460 av_log(s->avctx, AV_LOG_ERROR16, "qlevel %d not supported, maybe buggy stream\n",
461 qlevel);
462 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
463 }
464
465 for (i = 0; i < pred_order; i++) {
466 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
467 }
468
469 if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
470 return ret;
471
472 if ( ( s->buggy_lpc && s->stream_info.bps <= 16)
473 || ( !s->buggy_lpc && bps <= 16
474 && bps + coeff_prec + av_log2(pred_order)(31 - __builtin_clz((pred_order)|1)) <= 32)) {
475 s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
476 } else {
477 s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
478 if (s->stream_info.bps <= 16)
479 lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
480 }
481
482 return 0;
483}
484
485static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded,
486 int32_t *residual, int pred_order)
487{
488 int i, ret;
489 int coeff_prec, qlevel;
490 int coeffs[32];
491
492 /* warm up samples */
493 for (i = 0; i < pred_order; i++) {
494 decoded[i] = get_sbits64(&s->gb, 33);
495 }
496
497 coeff_prec = get_bits(&s->gb, 4) + 1;
498 if (coeff_prec == 16) {
499 av_log(s->avctx, AV_LOG_ERROR16, "invalid coeff precision\n");
500 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
501 }
502 qlevel = get_sbits(&s->gb, 5);
503 if (qlevel < 0) {
504 av_log(s->avctx, AV_LOG_ERROR16, "qlevel %d not supported, maybe buggy stream\n",
505 qlevel);
506 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
507 }
508
509 for (i = 0; i < pred_order; i++) {
510 coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
511 }
512
513 if ((ret = decode_residuals(s, residual, pred_order)) < 0)
514 return ret;
515
516 s->dsp.lpc33(decoded, residual, coeffs, pred_order, qlevel, s->blocksize);
517
518 return 0;
519}
520
521static inline int decode_subframe(FLACContext *s, int channel)
522{
523 int32_t *decoded = s->decoded[channel];
524 int type, wasted = 0;
525 int bps = s->stream_info.bps;
526 int i, ret;
527
528 if (channel
30.1
'channel' is equal to 0
== 0) {
31
Taking true branch
529 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
32
Assuming field 'ch_mode' is not equal to FLAC_CHMODE_RIGHT_SIDE
33
Taking false branch
530 bps++;
531 } else {
532 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
533 bps++;
534 }
535
536 if (get_bits1(&s->gb)) {
34
Assuming the condition is false
35
Taking false branch
537 av_log(s->avctx, AV_LOG_ERROR16, "invalid subframe padding\n");
538 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
539 }
540 type = get_bits(&s->gb, 6);
541
542 if (get_bits1(&s->gb)) {
36
Assuming the condition is false
37
Taking false branch
543 int left = get_bits_left(&s->gb);
544 if ( left <= 0 ||
545 (left < bps && !show_bits_long(&s->gb, left)) ||
546 !show_bits_long(&s->gb, bps-1)) {
547 av_log(s->avctx, AV_LOG_ERROR16,
548 "Invalid number of wasted bits > available bits (%d) - left=%d\n",
549 bps, left);
550 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
551 }
552 wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
553 bps -= wasted;
554 }
555
556//FIXME use av_log2 for types
557 if (type == 0) {
38
Assuming 'type' is not equal to 0
39
Taking false branch
558 if (bps < 33) {
559 int32_t tmp = get_sbits_long(&s->gb, bps);
560 for (i = 0; i < s->blocksize; i++)
561 decoded[i] = tmp;
562 } else {
563 int64_t tmp = get_sbits64(&s->gb, 33);
564 for (i = 0; i < s->blocksize; i++)
565 s->decoded_33bps[i] = tmp;
566 }
567 } else if (type == 1) {
40
Assuming 'type' is not equal to 1
568 if (bps < 33) {
569 for (i = 0; i < s->blocksize; i++)
570 decoded[i] = get_sbits_long(&s->gb, bps);
571 } else {
572 for (i = 0; i < s->blocksize; i++)
573 s->decoded_33bps[i] = get_sbits64(&s->gb, 33);
574 }
575 } else if ((type >= 8) && (type <= 12)) {
41
Assuming 'type' is >= 8
42
Assuming 'type' is <= 12
43
Taking true branch
576 int order = type & ~0x8;
577 if (bps < 33) {
44
Assuming 'bps' is < 33
45
Taking true branch
578 if (bps + order <= 32) {
46
Assuming the condition is true
47
Taking true branch
579 if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0)
48
Calling 'decode_subframe_fixed'
580 return ret;
581 } else {
582 if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0)
583 return ret;
584 }
585 } else {
586 if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0)
587 return ret;
588 }
589 } else if (type >= 32) {
590 if (bps < 33) {
591 if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
592 return ret;
593 } else {
594 if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0)
595 return ret;
596 }
597 } else {
598 av_log(s->avctx, AV_LOG_ERROR16, "invalid coding type\n");
599 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
600 }
601
602 if (wasted) {
603 if (wasted+bps == 33) {
604 s->dsp.wasted33(s->decoded_33bps, decoded, wasted, s->blocksize);
605 } else if (wasted < 32) {
606 s->dsp.wasted32(decoded, wasted, s->blocksize);
607 }
608 }
609
610 return 0;
611}
612
613static int decode_frame(FLACContext *s)
614{
615 int i, ret;
616 GetBitContext *gb = &s->gb;
617 FLACFrameInfo fi;
618
619 if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
11
Assuming the condition is false
620 av_log(s->avctx, AV_LOG_ERROR16, "invalid frame header\n");
621 return ret;
622 }
623
624 if ( s->stream_info.channels
12
Assuming field 'channels' is 0
625 && fi.channels != s->stream_info.channels
626 && s->got_streaminfo) {
627 s->stream_info.channels = fi.channels;
628 ff_flac_set_channel_layout(s->avctx, fi.channels);
629 ret = allocate_buffers(s);
630 if (ret < 0)
631 return ret;
632 }
633 s->stream_info.channels = fi.channels;
634 ff_flac_set_channel_layout(s->avctx, fi.channels);
635 s->ch_mode = fi.ch_mode;
636
637 if (!s->stream_info.bps && !fi.bps) {
13
Assuming field 'bps' is not equal to 0
638 av_log(s->avctx, AV_LOG_ERROR16, "bps not found in STREAMINFO or frame header\n");
639 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
640 }
641 if (!fi.bps) {
14
Assuming field 'bps' is not equal to 0
642 fi.bps = s->stream_info.bps;
643 } else if (s->stream_info.bps
14.1
Field 'bps' is not equal to 0
&& fi.bps != s->stream_info.bps) {
15
Assuming 'fi.bps' is equal to 's->stream_info.bps'
16
Taking false branch
644 av_log(s->avctx, AV_LOG_ERROR16, "switching bps mid-stream is not "
645 "supported\n");
646 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
647 }
648
649 if (!s->stream_info.bps
16.1
Field 'bps' is not equal to 0
) {
17
Taking false branch
650 s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
651 flac_set_bps(s);
652 }
653
654 if (!s->stream_info.max_blocksize)
18
Assuming field 'max_blocksize' is not equal to 0
19
Taking false branch
655 s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE65535;
656 if (fi.blocksize > s->stream_info.max_blocksize) {
20
Assuming field 'blocksize' is <= field 'max_blocksize'
21
Taking false branch
657 av_log(s->avctx, AV_LOG_ERROR16, "blocksize %d > %d\n", fi.blocksize,
658 s->stream_info.max_blocksize);
659 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
660 }
661 s->blocksize = fi.blocksize;
662
663 if (!s->stream_info.samplerate && !fi.samplerate) {
22
Assuming field 'samplerate' is not equal to 0
664 av_log(s->avctx, AV_LOG_ERROR16, "sample rate not found in STREAMINFO"
665 " or frame header\n");
666 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
667 }
668 if (fi.samplerate == 0)
23
Assuming field 'samplerate' is not equal to 0
24
Taking false branch
669 fi.samplerate = s->stream_info.samplerate;
670 s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
671
672 if (!s->got_streaminfo || !s->decoded_buffer) {
25
Assuming field 'got_streaminfo' is not equal to 0
26
Assuming field 'decoded_buffer' is non-null
27
Taking false branch
673 ret = allocate_buffers(s);
674 if (ret < 0)
675 return ret;
676 s->got_streaminfo = 1;
677 dump_headers(s->avctx, &s->stream_info);
678 }
679 ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
680 s->stream_info.channels);
681
682// dump_headers(s->avctx, &s->stream_info);
683
684 /* subframes */
685 for (i = 0; i < s->stream_info.channels; i++) {
28
Assuming 'i' is < field 'channels'
29
Loop condition is true. Entering loop body
686 if ((ret = decode_subframe(s, i)) < 0)
30
Calling 'decode_subframe'
687 return ret;
688 }
689
690 align_get_bits(gb);
691
692 /* frame footer */
693 skip_bits(gb, 16); /* data crc */
694
695 return 0;
696}
697
698static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len)
699{
700 int i;
701 if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) {
702 for (i = 0; i < len; i++)
703 decoded[1][i] = decoded[0][i] - (uint64_t)decoded_33bps[i];
704 } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) {
705 for (i = 0; i < len; i++)
706 decoded[0][i] = decoded[1][i] + (uint64_t)decoded_33bps[i];
707 } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) {
708 for (i = 0; i < len; i++) {
709 uint64_t a = decoded[0][i];
710 int64_t b = decoded_33bps[i];
711 a -= b >> 1;
712 decoded[0][i] = (a + b);
713 decoded[1][i] = a;
714 }
715 }
716}
717
718static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
719 int *got_frame_ptr, AVPacket *avpkt)
720{
721 const uint8_t *buf = avpkt->data;
722 int buf_size = avpkt->size;
723 FLACContext *s = avctx->priv_data;
724 int bytes_read = 0;
725 int ret;
726
727 *got_frame_ptr = 0;
728
729 if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
1
Assuming 'buf_size' is > 5
2
Assuming the condition is false
730 av_log(s->avctx, AV_LOG_DEBUG48, "skipping flac header packet 1\n");
731 return buf_size;
732 }
733
734 if (buf_size
2.1
'buf_size' is > 0
> 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
3
Assuming the condition is false
4
Taking false branch
735 av_log(s->avctx, AV_LOG_DEBUG48, "skipping vorbis comment\n");
736 return buf_size;
737 }
738
739 /* check that there is at least the smallest decodable amount of data.
740 this amount corresponds to the smallest valid FLAC frame possible.
741 FF F8 69 02 00 00 9A 00 00 34 */
742 if (buf_size < FLAC_MIN_FRAME_SIZE10)
5
Assuming 'buf_size' is >= FLAC_MIN_FRAME_SIZE
6
Taking false branch
743 return buf_size;
744
745 /* check for inline header */
746 if (AV_RB32(buf)av_bswap32((((const union unaligned_32 *) (buf))->l)) == MKBETAG('f','L','a','C')(('C') | (('a') << 8) | (('L') << 16) | ((unsigned
)('f') << 24))
) {
7
Assuming the condition is false
8
Taking false branch
747 if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
748 av_log(s->avctx, AV_LOG_ERROR16, "invalid header\n");
749 return ret;
750 }
751 return get_metadata_size(buf, buf_size);
752 }
753
754 /* decode frame */
755 if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
9
Taking false branch
756 return ret;
757 if ((ret = decode_frame(s)) < 0) {
10
Calling 'decode_frame'
758 av_log(s->avctx, AV_LOG_ERROR16, "decode_frame() failed\n");
759 return ret;
760 }
761 bytes_read = get_bits_count(&s->gb)/8;
762
763 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK(1<<0)|AV_EF_COMPLIANT(1<<17))) &&
764 av_crc(av_crc_get_table(AV_CRC_16_ANSI),
765 0, buf, bytes_read)) {
766 av_log(s->avctx, AV_LOG_ERROR16, "CRC error at PTS %"PRId64"l" "d""\n", avpkt->pts);
767 if (s->avctx->err_recognition & AV_EF_EXPLODE(1<<3))
768 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
769 }
770
771 /* get output buffer */
772 frame->nb_samples = s->blocksize;
773 if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
774 return ret;
775
776 if (s->stream_info.bps == 32 && s->ch_mode > 0) {
777 decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize);
778 s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels,
779 s->blocksize, s->sample_shift);
780 } else {
781 s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
782 s->stream_info.channels,
783 s->blocksize, s->sample_shift);
784 }
785
786 if (bytes_read > buf_size) {
787 av_log(s->avctx, AV_LOG_ERROR16, "overread: %d\n", bytes_read - buf_size);
788 return AVERROR_INVALIDDATA(-(int)(('I') | (('N') << 8) | (('D') << 16) | ((
unsigned)('A') << 24)))
;
789 }
790 if (bytes_read < buf_size) {
791 av_log(s->avctx, AV_LOG_DEBUG48, "underread: %d orig size: %d\n",
792 buf_size - bytes_read, buf_size);
793 }
794
795 *got_frame_ptr = 1;
796
797 return bytes_read;
798}
799
800static av_cold__attribute__((cold)) int flac_decode_close(AVCodecContext *avctx)
801{
802 FLACContext *s = avctx->priv_data;
803
804 av_freep(&s->decoded_buffer);
805 av_freep(&s->decoded_buffer_33bps);
806
807 return 0;
808}
809
810static const AVOption options[] = {
811{ "use_buggy_lpc", "emulate old buggy lavc behavior", offsetof(FLACContext, buggy_lpc)__builtin_offsetof(FLACContext, buggy_lpc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM(1 << 1) | AV_OPT_FLAG_AUDIO_PARAM(1 << 3) },
812{ NULL((void*)0) },
813};
814
815static const AVClass flac_decoder_class = {
816 .class_name = "FLAC decoder",
817 .item_name = av_default_item_name,
818 .option = options,
819 .version = LIBAVUTIL_VERSION_INT((60)<<16 | (29)<<8 | (100)),
820};
821
822const FFCodec ff_flac_decoder = {
823 .p.name = "flac",
824 CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)").p.long_name = "FLAC (Free Lossless Audio Codec)",
825 .p.type = AVMEDIA_TYPE_AUDIO,
826 .p.id = AV_CODEC_ID_FLAC,
827 .priv_data_size = sizeof(FLACContext),
828 .init = flac_decode_init,
829 .close = flac_decode_close,
830 FF_CODEC_DECODE_CB(flac_decode_frame).is_decoder = 1, .cb_type = FF_CODEC_CB_TYPE_DECODE, .cb.decode
= (flac_decode_frame)
,
831 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF(1 << 10) |
832 AV_CODEC_CAP_DR1(1 << 1) |
833 AV_CODEC_CAP_FRAME_THREADS(1 << 12),
834 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P,GCC diagnostic push GCC diagnostic ignored "-Wdeprecated-declarations"
.p.sample_fmts = ((((const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16
, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
}))) GCC diagnostic pop
835 AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P)GCC diagnostic push GCC diagnostic ignored "-Wdeprecated-declarations"
.p.sample_fmts = ((((const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16
, AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE
}))) GCC diagnostic pop
,
836 .p.priv_class = &flac_decoder_class,
837};