3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
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"
28 #include "avio_internal.h"
30 #include "flac_picture.h"
34 #include "replaygain.h"
36 #define SEEKPOINT_SIZE 18
38 typedef struct FLACDecContext
{
39 FFRawDemuxerContext rawctx
;
42 AVCodecContext
*parser_dec
;
45 static void reset_index_position(int64_t metadata_head_size
, AVStream
*st
)
47 FFStream
*const sti
= ffstream(st
);
48 /* the real seek index offset should be the size of metadata blocks with the offset in the frame blocks */
49 for (int i
= 0; i
< sti
->nb_index_entries
; i
++)
50 sti
->index_entries
[i
].pos
+= metadata_head_size
;
53 static const uint16_t sr_table
[16] = {
54 0, 1764, 3528, 3840, 160, 320, 441, 480, 640, 882, 960, 1920, 0, 0, 0, 0
57 static int flac_read_header(AVFormatContext
*s
)
59 int ret
, metadata_last
=0, metadata_type
, metadata_size
, found_streaminfo
=0;
63 FLACDecContext
*flac
= s
->priv_data
;
64 AVStream
*st
= avformat_new_stream(s
, NULL
);
66 return AVERROR(ENOMEM
);
67 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
68 st
->codecpar
->codec_id
= AV_CODEC_ID_FLAC
;
69 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_FULL_RAW
;
70 /* the parameters will be extracted from the compressed bitstream */
72 /* if fLaC marker is not found, assume there is no header */
73 marker
= avio_rl32(s
->pb
);
74 if (marker
!= MKTAG('f','L','a','C')) {
75 const int sample_rate
= 50 * sr_table
[(marker
>> 16) & 0xF];
77 avpriv_set_pts_info(st
, 64, 1, sample_rate
);
78 avio_seek(s
->pb
, -4, SEEK_CUR
);
82 /* process metadata blocks */
83 while (!avio_feof(s
->pb
) && !metadata_last
) {
84 ret
= avio_read(s
->pb
, header
, 4);
87 } else if (ret
!= 4) {
91 flac_parse_block_header(header
, &metadata_last
, &metadata_type
,
93 switch (metadata_type
) {
94 /* allocate and read metadata block for supported types */
95 case FLAC_METADATA_TYPE_STREAMINFO
:
96 case FLAC_METADATA_TYPE_CUESHEET
:
97 case FLAC_METADATA_TYPE_PICTURE
:
98 case FLAC_METADATA_TYPE_VORBIS_COMMENT
:
99 case FLAC_METADATA_TYPE_SEEKTABLE
:
100 buffer
= av_mallocz(metadata_size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
102 return AVERROR(ENOMEM
);
104 ret
= ffio_read_size(s
->pb
, buffer
, metadata_size
);
109 /* skip metadata block for unsupported types */
111 ret
= avio_skip(s
->pb
, metadata_size
);
116 if (metadata_type
== FLAC_METADATA_TYPE_STREAMINFO
) {
120 /* STREAMINFO can only occur once */
121 if (found_streaminfo
) {
122 RETURN_ERROR(AVERROR_INVALIDDATA
);
124 if (metadata_size
!= FLAC_STREAMINFO_SIZE
) {
125 RETURN_ERROR(AVERROR_INVALIDDATA
);
127 found_streaminfo
= 1;
128 st
->codecpar
->extradata
= buffer
;
129 st
->codecpar
->extradata_size
= metadata_size
;
132 /* get sample rate and sample count from STREAMINFO header;
133 * other parameters will be extracted by the parser */
134 samplerate
= AV_RB24(st
->codecpar
->extradata
+ 10) >> 4;
135 samples
= (AV_RB64(st
->codecpar
->extradata
+ 13) >> 24) & ((1ULL << 36) - 1);
137 /* set time base and duration */
138 if (samplerate
> 0) {
139 avpriv_set_pts_info(st
, 64, 1, samplerate
);
141 st
->duration
= samples
;
143 } else if (metadata_type
== FLAC_METADATA_TYPE_CUESHEET
) {
146 const uint8_t *offset
;
147 int i
, chapters
, track
, ti
;
148 if (metadata_size
< 431)
149 RETURN_ERROR(AVERROR_INVALIDDATA
);
150 offset
= buffer
+ 395;
151 chapters
= bytestream_get_byte(&offset
) - 1;
153 RETURN_ERROR(AVERROR_INVALIDDATA
);
154 for (i
= 0; i
< chapters
; i
++) {
155 if (offset
+ 36 - buffer
> metadata_size
)
156 RETURN_ERROR(AVERROR_INVALIDDATA
);
157 start
= bytestream_get_be64(&offset
);
158 track
= bytestream_get_byte(&offset
);
159 bytestream_get_buffer(&offset
, isrc
, 12);
162 ti
= bytestream_get_byte(&offset
);
163 if (ti
<= 0) RETURN_ERROR(AVERROR_INVALIDDATA
);
165 avpriv_new_chapter(s
, track
, st
->time_base
, start
, AV_NOPTS_VALUE
, isrc
);
168 } else if (metadata_type
== FLAC_METADATA_TYPE_PICTURE
) {
169 ret
= ff_flac_parse_picture(s
, &buffer
, metadata_size
, 1);
172 av_log(s
, AV_LOG_ERROR
, "Error parsing attached picture.\n");
175 } else if (metadata_type
== FLAC_METADATA_TYPE_SEEKTABLE
) {
176 const uint8_t *seekpoint
= buffer
;
177 int i
, seek_point_count
= metadata_size
/SEEKPOINT_SIZE
;
178 flac
->found_seektable
= 1;
179 if ((s
->flags
&AVFMT_FLAG_FAST_SEEK
)) {
180 for(i
=0; i
<seek_point_count
; i
++) {
181 int64_t timestamp
= bytestream_get_be64(&seekpoint
);
182 int64_t pos
= bytestream_get_be64(&seekpoint
);
183 /* skip number of samples */
184 bytestream_get_be16(&seekpoint
);
185 av_add_index_entry(st
, pos
, timestamp
, 0, 0, AVINDEX_KEYFRAME
);
192 /* STREAMINFO must be the first block */
193 if (!found_streaminfo
) {
194 RETURN_ERROR(AVERROR_INVALIDDATA
);
196 /* process supported blocks other than STREAMINFO */
197 if (metadata_type
== FLAC_METADATA_TYPE_VORBIS_COMMENT
) {
198 AVDictionaryEntry
*chmask
;
200 ret
= ff_vorbis_comment(s
, &s
->metadata
, buffer
, metadata_size
, 1);
202 av_log(s
, AV_LOG_WARNING
, "error parsing VorbisComment metadata\n");
203 } else if (ret
> 0) {
204 s
->event_flags
|= AVFMT_EVENT_FLAG_METADATA_UPDATED
;
207 /* parse the channels mask if present */
208 chmask
= av_dict_get(s
->metadata
, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL
, 0);
210 uint64_t mask
= strtol(chmask
->value
, NULL
, 0);
211 if (!mask
|| mask
& ~0x3ffffULL
) {
212 av_log(s
, AV_LOG_WARNING
,
213 "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
215 av_channel_layout_from_mask(&st
->codecpar
->ch_layout
, mask
);
216 av_dict_set(&s
->metadata
, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL
, 0);
224 ret
= ff_replaygain_export(st
, s
->metadata
);
228 reset_index_position(avio_tell(s
->pb
), st
);
236 static int raw_flac_probe(const AVProbeData
*p
)
238 if ((p
->buf
[2] & 0xF0) == 0) // blocksize code invalid
240 if ((p
->buf
[2] & 0x0F) == 0x0F) // sample rate code invalid
242 if ((p
->buf
[3] & 0xF0) >= FLAC_MAX_CHANNELS
+ FLAC_CHMODE_MID_SIDE
<< 4)
243 // channel mode invalid
245 if ((p
->buf
[3] & 0x06) == 0x06) // bits per sample code invalid
247 if ((p
->buf
[3] & 0x01) == 0x01) // reserved bit set
249 return AVPROBE_SCORE_EXTENSION
/ 4 + 1;
252 static int flac_probe(const AVProbeData
*p
)
254 if ((AV_RB16(p
->buf
) & 0xFFFE) == 0xFFF8)
255 return raw_flac_probe(p
);
257 /* file header + metadata header + checked bytes of streaminfo */
258 if (p
->buf_size
>= 4 + 4 + 13) {
259 int type
= p
->buf
[4] & 0x7f;
260 int size
= AV_RB24(p
->buf
+ 5);
261 int min_block_size
= AV_RB16(p
->buf
+ 8);
262 int max_block_size
= AV_RB16(p
->buf
+ 10);
263 int sample_rate
= AV_RB24(p
->buf
+ 18) >> 4;
265 if (memcmp(p
->buf
, "fLaC", 4))
267 if (type
== FLAC_METADATA_TYPE_STREAMINFO
&&
268 size
== FLAC_STREAMINFO_SIZE
&&
269 min_block_size
>= 16 &&
270 max_block_size
>= min_block_size
&&
271 sample_rate
&& sample_rate
<= 655350)
272 return AVPROBE_SCORE_MAX
;
273 return AVPROBE_SCORE_EXTENSION
;
279 av_unused
static int64_t flac_read_timestamp(AVFormatContext
*s
, int stream_index
,
280 int64_t *ppos
, int64_t pos_limit
)
282 FLACDecContext
*flac
= s
->priv_data
;
283 FFFormatContext
*const si
= ffformatcontext(s
);
284 AVPacket
*const pkt
= si
->parse_pkt
;
285 AVStream
*st
= s
->streams
[stream_index
];
286 AVCodecParserContext
*parser
;
288 int64_t pts
= AV_NOPTS_VALUE
;
290 if (avio_seek(s
->pb
, *ppos
, SEEK_SET
) < 0)
291 return AV_NOPTS_VALUE
;
293 if (!flac
->parser_dec
) {
294 flac
->parser_dec
= avcodec_alloc_context3(NULL
);
295 if (!flac
->parser_dec
)
296 return AV_NOPTS_VALUE
;
298 ret
= avcodec_parameters_to_context(flac
->parser_dec
, st
->codecpar
);
303 parser
= av_parser_init(st
->codecpar
->codec_id
);
305 return AV_NOPTS_VALUE
;
306 parser
->flags
|= PARSER_FLAG_USE_CODEC_TS
;
312 ret
= ff_raw_read_partial_packet(s
, pkt
);
314 if (ret
== AVERROR(EAGAIN
))
317 av_packet_unref(pkt
);
318 av_assert1(!pkt
->size
);
321 av_parser_parse2(parser
, flac
->parser_dec
,
322 &data
, &size
, pkt
->data
, pkt
->size
,
323 pkt
->pts
, pkt
->dts
, *ppos
);
325 av_packet_unref(pkt
);
327 if (parser
->pts
!= AV_NOPTS_VALUE
){
328 // seeking may not have started from beginning of a frame
329 // calculate frame start position from next frame backwards
330 *ppos
= parser
->next_frame_offset
- size
;
337 av_parser_close(parser
);
341 static int flac_close(AVFormatContext
*s
)
343 FLACDecContext
*flac
= s
->priv_data
;
345 avcodec_free_context(&flac
->parser_dec
);
350 static int flac_seek(AVFormatContext
*s
, int stream_index
, int64_t timestamp
, int flags
) {
351 AVStream
*const st
= s
->streams
[0];
352 FFStream
*const sti
= ffstream(st
);
356 FLACDecContext
*flac
= s
->priv_data
;
358 if (!flac
->found_seektable
|| !(s
->flags
&AVFMT_FLAG_FAST_SEEK
)) {
362 index
= av_index_search_timestamp(st
, timestamp
, flags
);
363 if (index
< 0 || index
>= sti
->nb_index_entries
)
366 e
= sti
->index_entries
[index
];
367 pos
= avio_seek(s
->pb
, e
.pos
, SEEK_SET
);
374 const FFInputFormat ff_flac_demuxer
= {
376 .p
.long_name
= NULL_IF_CONFIG_SMALL("raw FLAC"),
377 .p
.flags
= AVFMT_GENERIC_INDEX
,
378 .p
.extensions
= "flac",
379 .p
.priv_class
= &ff_raw_demuxer_class
,
380 .read_probe
= flac_probe
,
381 .read_header
= flac_read_header
,
382 .read_close
= flac_close
,
383 .read_packet
= ff_raw_read_partial_packet
,
384 .read_seek
= flac_seek
,
385 .read_timestamp
= flac_read_timestamp
,
386 .raw_codec_id
= AV_CODEC_ID_FLAC
,
387 .priv_data_size
= sizeof(FLACDecContext
),
388 .flags_internal
= FF_INFMT_FLAG_ID3V2_AUTO
,