flacdec: give a more accurate error message when validating channel
[FFMpeg-mirror/lagarith.git] / libavformat / flacdec.c
blobc8432145ce79947e199e827c1bb1f8e8d7706254
1 /*
2 * Raw FLAC demuxer
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"
23 #include "avformat.h"
24 #include "raw.h"
25 #include "id3v2.h"
26 #include "oggdec.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;
33 uint8_t header[4];
34 uint8_t *buffer=NULL;
35 AVStream *st = av_new_stream(s, 0);
36 if (!st)
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);
48 } else {
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);
55 return 0;
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,
62 &metadata_size);
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);
68 if (!buffer) {
69 return AVERROR_NOMEM;
71 if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
72 av_freep(&buffer);
73 return AVERROR_IO;
75 break;
76 /* skip metadata block for unsupported types */
77 default:
78 ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
79 if (ret < 0)
80 return ret;
83 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
84 FLACStreaminfo si;
85 /* STREAMINFO can only occur once */
86 if (found_streaminfo) {
87 av_freep(&buffer);
88 return AVERROR_INVALIDDATA;
90 if (metadata_size != FLAC_STREAMINFO_SIZE) {
91 av_freep(&buffer);
92 return AVERROR_INVALIDDATA;
94 found_streaminfo = 1;
95 st->codec->extradata = buffer;
96 st->codec->extradata_size = metadata_size;
97 buffer = NULL;
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);
105 if (si.samples > 0)
106 st->duration = si.samples;
108 } else {
109 /* STREAMINFO must be the first block */
110 if (!found_streaminfo) {
111 av_freep(&buffer);
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");
120 av_freep(&buffer);
124 return 0;
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 = {
140 "flac",
141 NULL_IF_CONFIG_SMALL("raw FLAC"),
143 flac_probe,
144 flac_read_header,
145 ff_raw_read_partial_packet,
146 .flags= AVFMT_GENERIC_INDEX,
147 .extensions = "flac",
148 .value = CODEC_ID_FLAC,