]> git.sesse.net Git - ffmpeg/blob - libavformat/flacdec.c
sbgdec: replace EOVERFLOW with ENOMEM.
[ffmpeg] / libavformat / flacdec.c
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 "libavcodec/flac.h"
23 #include "avformat.h"
24 #include "internal.h"
25 #include "rawdec.h"
26 #include "oggdec.h"
27 #include "vorbiscomment.h"
28 #include "libavcodec/bytestream.h"
29
30 static int flac_read_header(AVFormatContext *s,
31                              AVFormatParameters *ap)
32 {
33     int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
34     uint8_t header[4];
35     uint8_t *buffer=NULL;
36     AVStream *st = avformat_new_stream(s, NULL);
37     if (!st)
38         return AVERROR(ENOMEM);
39     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
40     st->codec->codec_id = CODEC_ID_FLAC;
41     st->need_parsing = AVSTREAM_PARSE_FULL;
42     /* the parameters will be extracted from the compressed bitstream */
43
44     /* if fLaC marker is not found, assume there is no header */
45     if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
46         avio_seek(s->pb, -4, SEEK_CUR);
47         return 0;
48     }
49
50     /* process metadata blocks */
51     while (!url_feof(s->pb) && !metadata_last) {
52         avio_read(s->pb, header, 4);
53         avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
54                                    &metadata_size);
55         switch (metadata_type) {
56         /* allocate and read metadata block for supported types */
57         case FLAC_METADATA_TYPE_STREAMINFO:
58         case FLAC_METADATA_TYPE_CUESHEET:
59         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
60             buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
61             if (!buffer) {
62                 return AVERROR(ENOMEM);
63             }
64             if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
65                 av_freep(&buffer);
66                 return AVERROR(EIO);
67             }
68             break;
69         /* skip metadata block for unsupported types */
70         default:
71             ret = avio_skip(s->pb, metadata_size);
72             if (ret < 0)
73                 return ret;
74         }
75
76         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
77             FLACStreaminfo si;
78             /* STREAMINFO can only occur once */
79             if (found_streaminfo) {
80                 av_freep(&buffer);
81                 return AVERROR_INVALIDDATA;
82             }
83             if (metadata_size != FLAC_STREAMINFO_SIZE) {
84                 av_freep(&buffer);
85                 return AVERROR_INVALIDDATA;
86             }
87             found_streaminfo = 1;
88             st->codec->extradata      = buffer;
89             st->codec->extradata_size = metadata_size;
90             buffer = NULL;
91
92             /* get codec params from STREAMINFO header */
93             avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
94
95             /* set time base and duration */
96             if (si.samplerate > 0) {
97                 avpriv_set_pts_info(st, 64, 1, si.samplerate);
98                 if (si.samples > 0)
99                     st->duration = si.samples;
100             }
101         } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
102             uint8_t isrc[13];
103             uint64_t start;
104             const uint8_t *offset;
105             int i, j, chapters, track, ti;
106             if (metadata_size < 431)
107                 return AVERROR_INVALIDDATA;
108             offset = buffer + 395;
109             chapters = bytestream_get_byte(&offset) - 1;
110             if (chapters <= 0)
111                 return AVERROR_INVALIDDATA;
112             for (i = 0; i < chapters; i++) {
113                 if (offset + 36 - buffer > metadata_size)
114                     return AVERROR_INVALIDDATA;
115                 start = bytestream_get_be64(&offset);
116                 track = bytestream_get_byte(&offset);
117                 bytestream_get_buffer(&offset, isrc, 12);
118                 isrc[12] = 0;
119                 offset += 14;
120                 ti = bytestream_get_byte(&offset);
121                 if (ti <= 0) return AVERROR_INVALIDDATA;
122                 for (j = 0; j < ti; j++)
123                     offset += 12;
124                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
125             }
126         } else {
127             /* STREAMINFO must be the first block */
128             if (!found_streaminfo) {
129                 av_freep(&buffer);
130                 return AVERROR_INVALIDDATA;
131             }
132             /* process supported blocks other than STREAMINFO */
133             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
134                 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
135                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
136                 }
137             }
138             av_freep(&buffer);
139         }
140     }
141
142     return 0;
143 }
144
145 static int flac_probe(AVProbeData *p)
146 {
147     uint8_t *bufptr = p->buf;
148     uint8_t *end    = p->buf + p->buf_size;
149
150     if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
151     else                                            return AVPROBE_SCORE_MAX/2;
152 }
153
154 AVInputFormat ff_flac_demuxer = {
155     .name           = "flac",
156     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
157     .read_probe     = flac_probe,
158     .read_header    = flac_read_header,
159     .read_packet    = ff_raw_read_partial_packet,
160     .flags= AVFMT_GENERIC_INDEX,
161     .extensions = "flac",
162     .value = CODEC_ID_FLAC,
163 };