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 "libavcodec/flac.h"
28 static int flac_read_header(AVFormatContext
*s
,
29 AVFormatParameters
*ap
)
31 uint8_t buf
[ID3v2_HEADER_SIZE
];
32 int ret
, metadata_last
=0, metadata_type
, metadata_size
, found_streaminfo
=0;
35 AVStream
*st
= av_new_stream(s
, 0);
37 return AVERROR(ENOMEM
);
38 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
39 st
->codec
->codec_id
= CODEC_ID_FLAC
;
40 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
41 /* the parameters will be extracted from the compressed bitstream */
43 /* skip ID3v2 header if found */
44 ret
= get_buffer(s
->pb
, buf
, ID3v2_HEADER_SIZE
);
45 if (ret
== ID3v2_HEADER_SIZE
&& ff_id3v2_match(buf
)) {
46 int len
= ff_id3v2_tag_len(buf
);
47 url_fseek(s
->pb
, len
- ID3v2_HEADER_SIZE
, SEEK_CUR
);
49 url_fseek(s
->pb
, 0, SEEK_SET
);
52 /* if fLaC marker is not found, assume there is no header */
53 if (get_le32(s
->pb
) != MKTAG('f','L','a','C')) {
54 url_fseek(s
->pb
, -4, SEEK_CUR
);
58 /* process metadata blocks */
59 while (!url_feof(s
->pb
) && !metadata_last
) {
60 get_buffer(s
->pb
, header
, 4);
61 ff_flac_parse_block_header(header
, &metadata_last
, &metadata_type
,
63 switch (metadata_type
) {
64 /* allocate and read metadata block for supported types */
65 case FLAC_METADATA_TYPE_STREAMINFO
:
66 case FLAC_METADATA_TYPE_VORBIS_COMMENT
:
67 buffer
= av_mallocz(metadata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
71 if (get_buffer(s
->pb
, buffer
, metadata_size
) != metadata_size
) {
76 /* skip metadata block for unsupported types */
78 ret
= url_fseek(s
->pb
, metadata_size
, SEEK_CUR
);
83 if (metadata_type
== FLAC_METADATA_TYPE_STREAMINFO
) {
85 /* STREAMINFO can only occur once */
86 if (found_streaminfo
) {
88 return AVERROR_INVALIDDATA
;
90 if (metadata_size
!= FLAC_STREAMINFO_SIZE
) {
92 return AVERROR_INVALIDDATA
;
95 st
->codec
->extradata
= buffer
;
96 st
->codec
->extradata_size
= metadata_size
;
99 /* get codec params from STREAMINFO header */
100 ff_flac_parse_streaminfo(st
->codec
, &si
, st
->codec
->extradata
);
102 /* set time base and duration */
103 if (si
.samplerate
> 0) {
104 av_set_pts_info(st
, 64, 1, si
.samplerate
);
106 st
->duration
= si
.samples
;
109 /* STREAMINFO must be the first block */
110 if (!found_streaminfo
) {
112 return AVERROR_INVALIDDATA
;
114 /* process supported blocks other than STREAMINFO */
115 if (metadata_type
== FLAC_METADATA_TYPE_VORBIS_COMMENT
) {
116 if (vorbis_comment(s
, buffer
, metadata_size
)) {
117 av_log(s
, AV_LOG_WARNING
, "error parsing VorbisComment metadata\n");
127 static int flac_probe(AVProbeData
*p
)
129 uint8_t *bufptr
= p
->buf
;
130 uint8_t *end
= p
->buf
+ p
->buf_size
;
132 if(ff_id3v2_match(bufptr
))
133 bufptr
+= ff_id3v2_tag_len(bufptr
);
135 if(bufptr
> end
-4 || memcmp(bufptr
, "fLaC", 4)) return 0;
136 else return AVPROBE_SCORE_MAX
/2;
139 AVInputFormat flac_demuxer
= {
141 NULL_IF_CONFIG_SMALL("raw FLAC"),
145 ff_raw_read_partial_packet
,
146 .flags
= AVFMT_GENERIC_INDEX
,
147 .extensions
= "flac",
148 .value
= CODEC_ID_FLAC
,
149 .metadata_conv
= ff_vorbiscomment_metadata_conv
,