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