FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/flacdec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 134 200 67.0%
Functions: 7 7 100.0%
Branches: 68 128 53.1%

Line Branch Exec Source
1 /*
2 * Raw FLAC demuxer
3 * Copyright (c) 2001 Fabrice Bellard
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/mem.h"
24 #include "libavcodec/avcodec.h"
25 #include "libavcodec/bytestream.h"
26 #include "libavcodec/flac.h"
27 #include "avformat.h"
28 #include "demux.h"
29 #include "flac_picture.h"
30 #include "internal.h"
31 #include "rawdec.h"
32 #include "oggdec.h"
33 #include "replaygain.h"
34
35 #define SEEKPOINT_SIZE 18
36
37 typedef struct FLACDecContext {
38 FFRawDemuxerContext rawctx;
39 int found_seektable;
40
41 AVCodecContext *parser_dec;
42 } FLACDecContext;
43
44 32 static void reset_index_position(int64_t metadata_head_size, AVStream *st)
45 {
46 32 FFStream *const sti = ffstream(st);
47 /* the real seek index offset should be the size of metadata blocks with the offset in the frame blocks */
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 for (int i = 0; i < sti->nb_index_entries; i++)
49 sti->index_entries[i].pos += metadata_head_size;
50 32 }
51
52 static const uint16_t sr_table[16] = {
53 0, 1764, 3528, 3840, 160, 320, 441, 480, 640, 882, 960, 1920, 0, 0, 0, 0
54 };
55
56 32 static int flac_read_header(AVFormatContext *s)
57 {
58 32 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
59 uint8_t header[4];
60 32 uint8_t *buffer=NULL;
61 uint32_t marker;
62 32 FLACDecContext *flac = s->priv_data;
63 32 AVStream *st = avformat_new_stream(s, NULL);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (!st)
65 return AVERROR(ENOMEM);
66 32 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
67 32 st->codecpar->codec_id = AV_CODEC_ID_FLAC;
68 32 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL_RAW;
69 /* the parameters will be extracted from the compressed bitstream */
70
71 /* if fLaC marker is not found, assume there is no header */
72 32 marker = avio_rl32(s->pb);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (marker != MKTAG('f','L','a','C')) {
74 const int sample_rate = 50 * sr_table[(marker >> 16) & 0xF];
75 if (sample_rate)
76 avpriv_set_pts_info(st, 64, 1, sample_rate);
77 avio_seek(s->pb, -4, SEEK_CUR);
78 return 0;
79 }
80
81 /* process metadata blocks */
82
3/4
✓ Branch 1 taken 149 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 117 times.
✓ Branch 4 taken 32 times.
149 while (!avio_feof(s->pb) && !metadata_last) {
83
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117 times.
117 if (avio_read(s->pb, header, 4) != 4)
84 return AVERROR_INVALIDDATA;
85 117 flac_parse_block_header(header, &metadata_last, &metadata_type,
86 &metadata_size);
87
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 32 times.
117 switch (metadata_type) {
88 /* allocate and read metadata block for supported types */
89 85 case FLAC_METADATA_TYPE_STREAMINFO:
90 case FLAC_METADATA_TYPE_CUESHEET:
91 case FLAC_METADATA_TYPE_PICTURE:
92 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
93 case FLAC_METADATA_TYPE_SEEKTABLE:
94 85 buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (!buffer) {
96 return AVERROR(ENOMEM);
97 }
98
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
85 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
99 RETURN_ERROR(AVERROR(EIO));
100 }
101 85 break;
102 /* skip metadata block for unsupported types */
103 32 default:
104 32 ret = avio_skip(s->pb, metadata_size);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
106 return ret;
107 }
108
109
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 85 times.
117 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
110 uint32_t samplerate;
111 uint64_t samples;
112
113 /* STREAMINFO can only occur once */
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (found_streaminfo) {
115 RETURN_ERROR(AVERROR_INVALIDDATA);
116 }
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (metadata_size != FLAC_STREAMINFO_SIZE) {
118 RETURN_ERROR(AVERROR_INVALIDDATA);
119 }
120 32 found_streaminfo = 1;
121 32 st->codecpar->extradata = buffer;
122 32 st->codecpar->extradata_size = metadata_size;
123 32 buffer = NULL;
124
125 /* get sample rate and sample count from STREAMINFO header;
126 * other parameters will be extracted by the parser */
127 32 samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
128 32 samples = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
129
130 /* set time base and duration */
131
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (samplerate > 0) {
132 32 avpriv_set_pts_info(st, 64, 1, samplerate);
133
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (samples > 0)
134 32 st->duration = samples;
135 }
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
137 uint8_t isrc[13];
138 uint64_t start;
139 const uint8_t *offset;
140 int i, chapters, track, ti;
141 if (metadata_size < 431)
142 RETURN_ERROR(AVERROR_INVALIDDATA);
143 offset = buffer + 395;
144 chapters = bytestream_get_byte(&offset) - 1;
145 if (chapters <= 0)
146 RETURN_ERROR(AVERROR_INVALIDDATA);
147 for (i = 0; i < chapters; i++) {
148 if (offset + 36 - buffer > metadata_size)
149 RETURN_ERROR(AVERROR_INVALIDDATA);
150 start = bytestream_get_be64(&offset);
151 track = bytestream_get_byte(&offset);
152 bytestream_get_buffer(&offset, isrc, 12);
153 isrc[12] = 0;
154 offset += 14;
155 ti = bytestream_get_byte(&offset);
156 if (ti <= 0) RETURN_ERROR(AVERROR_INVALIDDATA);
157 offset += ti * 12;
158 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
159 }
160 av_freep(&buffer);
161
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 72 times.
85 } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
162 13 ret = ff_flac_parse_picture(s, &buffer, metadata_size, 1);
163 13 av_freep(&buffer);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (ret < 0) {
165 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
166 return ret;
167 }
168
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 63 times.
72 } else if (metadata_type == FLAC_METADATA_TYPE_SEEKTABLE) {
169 9 const uint8_t *seekpoint = buffer;
170 9 int i, seek_point_count = metadata_size/SEEKPOINT_SIZE;
171 9 flac->found_seektable = 1;
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if ((s->flags&AVFMT_FLAG_FAST_SEEK)) {
173 for(i=0; i<seek_point_count; i++) {
174 int64_t timestamp = bytestream_get_be64(&seekpoint);
175 int64_t pos = bytestream_get_be64(&seekpoint);
176 /* skip number of samples */
177 bytestream_get_be16(&seekpoint);
178 av_add_index_entry(st, pos, timestamp, 0, 0, AVINDEX_KEYFRAME);
179 }
180 }
181 9 av_freep(&buffer);
182 }
183 else {
184
185 /* STREAMINFO must be the first block */
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
63 if (!found_streaminfo) {
187 RETURN_ERROR(AVERROR_INVALIDDATA);
188 }
189 /* process supported blocks other than STREAMINFO */
190
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 32 times.
63 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
191 AVDictionaryEntry *chmask;
192
193 31 ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0) {
195 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
196
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 12 times.
31 } else if (ret > 0) {
197 19 s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
198 }
199
200 /* parse the channels mask if present */
201 31 chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
202
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 29 times.
31 if (chmask) {
203 2 uint64_t mask = strtol(chmask->value, NULL, 0);
204
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!mask || mask & ~0x3ffffULL) {
205 av_log(s, AV_LOG_WARNING,
206 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
207 } else {
208 2 av_channel_layout_from_mask(&st->codecpar->ch_layout, mask);
209 2 av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
210 }
211 }
212 }
213 63 av_freep(&buffer);
214 }
215 }
216
217 32 ret = ff_replaygain_export(st, s->metadata);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
219 return ret;
220
221 32 reset_index_position(avio_tell(s->pb), st);
222 32 return 0;
223
224 fail:
225 av_free(buffer);
226 return ret;
227 }
228
229 16 static int raw_flac_probe(const AVProbeData *p)
230 {
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if ((p->buf[2] & 0xF0) == 0) // blocksize code invalid
232 return 0;
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if ((p->buf[2] & 0x0F) == 0x0F) // sample rate code invalid
234 return 0;
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if ((p->buf[3] & 0xF0) >= FLAC_MAX_CHANNELS + FLAC_CHMODE_MID_SIDE << 4)
236 // channel mode invalid
237 return 0;
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if ((p->buf[3] & 0x06) == 0x06) // bits per sample code invalid
239 return 0;
240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if ((p->buf[3] & 0x01) == 0x01) // reserved bit set
241 return 0;
242 16 return AVPROBE_SCORE_EXTENSION / 4 + 1;
243 }
244
245 7186 static int flac_probe(const AVProbeData *p)
246 {
247
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 7170 times.
7186 if ((AV_RB16(p->buf) & 0xFFFE) == 0xFFF8)
248 16 return raw_flac_probe(p);
249
250 /* file header + metadata header + checked bytes of streaminfo */
251
1/2
✓ Branch 0 taken 7170 times.
✗ Branch 1 not taken.
7170 if (p->buf_size >= 4 + 4 + 13) {
252 7170 int type = p->buf[4] & 0x7f;
253 7170 int size = AV_RB24(p->buf + 5);
254 7170 int min_block_size = AV_RB16(p->buf + 8);
255 7170 int max_block_size = AV_RB16(p->buf + 10);
256 7170 int sample_rate = AV_RB24(p->buf + 18) >> 4;
257
258
2/2
✓ Branch 0 taken 7140 times.
✓ Branch 1 taken 30 times.
7170 if (memcmp(p->buf, "fLaC", 4))
259 7140 return 0;
260
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 if (type == FLAC_METADATA_TYPE_STREAMINFO &&
261
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 size == FLAC_STREAMINFO_SIZE &&
262
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 min_block_size >= 16 &&
263
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 max_block_size >= min_block_size &&
264
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 sample_rate && sample_rate <= 655350)
265 30 return AVPROBE_SCORE_MAX;
266 return AVPROBE_SCORE_EXTENSION;
267 }
268
269 return 0;
270 }
271
272 120 static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
273 int64_t *ppos, int64_t pos_limit)
274 {
275 120 FLACDecContext *flac = s->priv_data;
276 120 FFFormatContext *const si = ffformatcontext(s);
277 120 AVPacket *const pkt = si->parse_pkt;
278 120 AVStream *st = s->streams[stream_index];
279 AVCodecParserContext *parser;
280 int ret;
281 120 int64_t pts = AV_NOPTS_VALUE;
282
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
120 if (avio_seek(s->pb, *ppos, SEEK_SET) < 0)
284 return AV_NOPTS_VALUE;
285
286
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 119 times.
120 if (!flac->parser_dec) {
287 1 flac->parser_dec = avcodec_alloc_context3(NULL);
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!flac->parser_dec)
289 return AV_NOPTS_VALUE;
290
291 1 ret = avcodec_parameters_to_context(flac->parser_dec, st->codecpar);
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
293 return ret;
294 }
295
296 120 parser = av_parser_init(st->codecpar->codec_id);
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (!parser)
298 return AV_NOPTS_VALUE;
299 120 parser->flags |= PARSER_FLAG_USE_CODEC_TS;
300
301 1534 for (;;){
302 uint8_t *data;
303 int size;
304
305 1654 ret = ff_raw_read_partial_packet(s, pkt);
306
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 1618 times.
1654 if (ret < 0){
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (ret == AVERROR(EAGAIN))
308 continue;
309 else {
310 36 av_packet_unref(pkt);
311 av_assert1(!pkt->size);
312 }
313 }
314 1654 av_parser_parse2(parser, flac->parser_dec,
315 1654 &data, &size, pkt->data, pkt->size,
316 pkt->pts, pkt->dts, *ppos);
317
318 1654 av_packet_unref(pkt);
319
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 1425 times.
1654 if (size) {
320
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 118 times.
229 if (parser->pts != AV_NOPTS_VALUE){
321 // seeking may not have started from beginning of a frame
322 // calculate frame start position from next frame backwards
323 111 *ppos = parser->next_frame_offset - size;
324 111 pts = parser->pts;
325 111 break;
326 }
327
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1416 times.
1425 } else if (ret < 0)
328 9 break;
329 }
330 120 av_parser_close(parser);
331 120 return pts;
332 }
333
334 32 static int flac_close(AVFormatContext *s)
335 {
336 32 FLACDecContext *flac = s->priv_data;
337
338 32 avcodec_free_context(&flac->parser_dec);
339
340 32 return 0;
341 }
342
343 26 static int flac_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags) {
344 26 AVStream *const st = s->streams[0];
345 26 FFStream *const sti = ffstream(st);
346 int index;
347 int64_t pos;
348 AVIndexEntry e;
349 26 FLACDecContext *flac = s->priv_data;
350
351
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26 if (!flac->found_seektable || !(s->flags&AVFMT_FLAG_FAST_SEEK)) {
352 26 return -1;
353 }
354
355 index = av_index_search_timestamp(st, timestamp, flags);
356 if (index < 0 || index >= sti->nb_index_entries)
357 return -1;
358
359 e = sti->index_entries[index];
360 pos = avio_seek(s->pb, e.pos, SEEK_SET);
361 if (pos >= 0) {
362 return 0;
363 }
364 return -1;
365 }
366
367 const FFInputFormat ff_flac_demuxer = {
368 .p.name = "flac",
369 .p.long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
370 .p.flags = AVFMT_GENERIC_INDEX,
371 .p.extensions = "flac",
372 .p.priv_class = &ff_raw_demuxer_class,
373 .read_probe = flac_probe,
374 .read_header = flac_read_header,
375 .read_close = flac_close,
376 .read_packet = ff_raw_read_partial_packet,
377 .read_seek = flac_seek,
378 .read_timestamp = flac_read_timestamp,
379 .raw_codec_id = AV_CODEC_ID_FLAC,
380 .priv_data_size = sizeof(FLACDecContext),
381 };
382