FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/flac.c
Date: 2021-09-24 03:35:22
Exec Total Coverage
Lines: 95 111 85.6%
Branches: 49 60 81.7%

Line Branch Exec Source
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/channel_layout.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/log.h"
25 #include "bytestream.h"
26 #include "get_bits.h"
27 #include "flac.h"
28 #include "flacdata.h"
29
30 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
31
32 static const uint64_t flac_channel_layouts[8] = {
33 AV_CH_LAYOUT_MONO,
34 AV_CH_LAYOUT_STEREO,
35 AV_CH_LAYOUT_SURROUND,
36 AV_CH_LAYOUT_QUAD,
37 AV_CH_LAYOUT_5POINT0,
38 AV_CH_LAYOUT_5POINT1,
39 AV_CH_LAYOUT_6POINT1,
40 AV_CH_LAYOUT_7POINT1
41 };
42
43 9886 static int64_t get_utf8(GetBitContext *gb)
44 {
45 int64_t val;
46
7/8
✓ Branch 1 taken 9845 times.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9845 times.
✓ Branch 6 taken 32 times.
✓ Branch 7 taken 1905 times.
✓ Branch 8 taken 1937 times.
✓ Branch 9 taken 9813 times.
11791 GET_UTF8(val, get_bits(gb, 8), return -1;)
47 9813 return val;
48 }
49
50 10452 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
51 FLACFrameInfo *fi, int log_level_offset)
52 {
53 int bs_code, sr_code, bps_code;
54
55 /* frame sync code */
56
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 10444 times.
10452 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
57 8 av_log(avctx, AV_LOG_ERROR + log_level_offset, "invalid sync code\n");
58 8 return AVERROR_INVALIDDATA;
59 }
60
61 /* variable block size stream code */
62 10444 fi->is_var_size = get_bits1(gb);
63
64 /* block size and sample rate codes */
65 10444 bs_code = get_bits(gb, 4);
66 10444 sr_code = get_bits(gb, 4);
67
68 /* channels and decorrelation */
69 10444 fi->ch_mode = get_bits(gb, 4);
70
2/2
✓ Branch 0 taken 3817 times.
✓ Branch 1 taken 6627 times.
10444 if (fi->ch_mode < FLAC_MAX_CHANNELS) {
71 3817 fi->channels = fi->ch_mode + 1;
72 3817 fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
73
2/2
✓ Branch 0 taken 6394 times.
✓ Branch 1 taken 233 times.
6627 } else if (fi->ch_mode < FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE) {
74 6394 fi->channels = 2;
75 6394 fi->ch_mode -= FLAC_MAX_CHANNELS - 1;
76 } else {
77 233 av_log(avctx, AV_LOG_ERROR + log_level_offset,
78 "invalid channel mode: %d\n", fi->ch_mode);
79 233 return AVERROR_INVALIDDATA;
80 }
81
82 /* bits per sample */
83 10211 bps_code = get_bits(gb, 3);
84
4/4
✓ Branch 0 taken 10163 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 79 times.
✓ Branch 3 taken 10084 times.
10211 if (bps_code == 3 || bps_code == 7) {
85 127 av_log(avctx, AV_LOG_ERROR + log_level_offset,
86 "invalid sample size code (%d)\n",
87 bps_code);
88 127 return AVERROR_INVALIDDATA;
89 }
90 10084 fi->bps = sample_size_table[bps_code];
91
92 /* reserved bit */
93
2/2
✓ Branch 1 taken 198 times.
✓ Branch 2 taken 9886 times.
10084 if (get_bits1(gb)) {
94 198 av_log(avctx, AV_LOG_ERROR + log_level_offset,
95 "broken stream, invalid padding\n");
96 198 return AVERROR_INVALIDDATA;
97 }
98
99 /* sample or frame count */
100 9886 fi->frame_or_sample_num = get_utf8(gb);
101
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 9813 times.
9886 if (fi->frame_or_sample_num < 0) {
102 73 av_log(avctx, AV_LOG_ERROR + log_level_offset,
103 "sample/frame number invalid; utf8 fscked\n");
104 73 return AVERROR_INVALIDDATA;
105 }
106
107 /* blocksize */
108
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 9791 times.
9813 if (bs_code == 0) {
109 22 av_log(avctx, AV_LOG_ERROR + log_level_offset,
110 "reserved blocksize code: 0\n");
111 22 return AVERROR_INVALIDDATA;
112
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 9787 times.
9791 } else if (bs_code == 6) {
113 4 fi->blocksize = get_bits(gb, 8) + 1;
114
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 9697 times.
9787 } else if (bs_code == 7) {
115 90 fi->blocksize = get_bits(gb, 16) + 1;
116 } else {
117 9697 fi->blocksize = ff_flac_blocksize_table[bs_code];
118 }
119
120 /* sample rate */
121
2/2
✓ Branch 0 taken 8718 times.
✓ Branch 1 taken 1073 times.
9791 if (sr_code < 12) {
122 8718 fi->samplerate = ff_flac_sample_rate_table[sr_code];
123
2/2
✓ Branch 0 taken 1050 times.
✓ Branch 1 taken 23 times.
1073 } else if (sr_code == 12) {
124 1050 fi->samplerate = get_bits(gb, 8) * 1000;
125
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 16 times.
23 } else if (sr_code == 13) {
126 7 fi->samplerate = get_bits(gb, 16);
127
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 10 times.
16 } else if (sr_code == 14) {
128 6 fi->samplerate = get_bits(gb, 16) * 10;
129 } else {
130 10 av_log(avctx, AV_LOG_ERROR + log_level_offset,
131 "illegal sample rate code %d\n",
132 sr_code);
133 10 return AVERROR_INVALIDDATA;
134 }
135
136 /* header CRC-8 check */
137 9781 skip_bits(gb, 8);
138
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 9690 times.
9781 if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
139 9781 get_bits_count(gb)/8)) {
140 91 av_log(avctx, AV_LOG_ERROR + log_level_offset,
141 "header crc mismatch\n");
142 91 return AVERROR_INVALIDDATA;
143 }
144
145 9690 return 0;
146 }
147
148 62 int ff_flac_get_max_frame_size(int blocksize, int ch, int bps)
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
154 int count;
155
156 62 count = 16; /* frame header */
157 62 count += ch * ((7+bps+7)/8); /* subframe headers */
158
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 32 times.
62 if (ch == 2) {
159 /* for stereo, need to account for using decorrelation */
160 30 count += (( 2*bps+1) * blocksize + 7) / 8;
161 } else {
162 32 count += ( ch*bps * blocksize + 7) / 8;
163 }
164 62 count += 2; /* frame footer */
165
166 62 return count;
167 }
168
169 80 int ff_flac_is_extradata_valid(AVCodecContext *avctx,
170 enum FLACExtradataFormat *format,
171 uint8_t **streaminfo_start)
172 {
173
2/4
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
80 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
174 av_log(avctx, AV_LOG_ERROR, "extradata NULL or too small.\n");
175 return 0;
176 }
177
1/2
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
80 if (AV_RL32(avctx->extradata) != MKTAG('f','L','a','C')) {
178 /* extradata contains STREAMINFO only */
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
180 av_log(avctx, AV_LOG_WARNING, "extradata contains %d bytes too many.\n",
181 FLAC_STREAMINFO_SIZE-avctx->extradata_size);
182 }
183 80 *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
184 80 *streaminfo_start = avctx->extradata;
185 } else {
186 if (avctx->extradata_size < 8+FLAC_STREAMINFO_SIZE) {
187 av_log(avctx, AV_LOG_ERROR, "extradata too small.\n");
188 return 0;
189 }
190 *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
191 *streaminfo_start = &avctx->extradata[8];
192 }
193 80 return 1;
194 }
195
196 30 void ff_flac_set_channel_layout(AVCodecContext *avctx)
197 {
198
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
199 30 avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
200 else
201 avctx->channel_layout = 0;
202 30 }
203
204 80 int ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
205 const uint8_t *buffer)
206 {
207 GetBitContext gb;
208 80 init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
209
210 80 skip_bits(&gb, 16); /* skip min blocksize */
211 80 s->max_blocksize = get_bits(&gb, 16);
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
213 av_log(avctx, AV_LOG_WARNING, "invalid max blocksize: %d\n",
214 s->max_blocksize);
215 s->max_blocksize = 16;
216 return AVERROR_INVALIDDATA;
217 }
218
219 80 skip_bits(&gb, 24); /* skip min frame size */
220 80 s->max_framesize = get_bits(&gb, 24);
221
222 80 s->samplerate = get_bits(&gb, 20);
223 80 s->channels = get_bits(&gb, 3) + 1;
224 80 s->bps = get_bits(&gb, 5) + 1;
225
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80 times.
80 if (s->bps < 4) {
227 av_log(avctx, AV_LOG_ERROR, "invalid bps: %d\n", s->bps);
228 s->bps = 16;
229 return AVERROR_INVALIDDATA;
230 }
231
232 80 avctx->channels = s->channels;
233 80 avctx->sample_rate = s->samplerate;
234 80 avctx->bits_per_raw_sample = s->bps;
235
236
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 30 times.
80 if (!avctx->channel_layout ||
237
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50 av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
238 30 ff_flac_set_channel_layout(avctx);
239
240 80 s->samples = get_bits64(&gb, 36);
241
242 80 skip_bits_long(&gb, 64); /* md5 sum */
243 80 skip_bits_long(&gb, 64); /* md5 sum */
244
245 80 return 0;
246 }
247