]> git.sesse.net Git - ffmpeg/blob - libavformat/flacdec.c
rtpdec_vp9: Update header parsing to spec draft 02
[ffmpeg] / libavformat / flacdec.c
1 /*
2  * Raw FLAC demuxer
3  * Copyright (c) 2001 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "flac_picture.h"
25 #include "internal.h"
26 #include "rawdec.h"
27 #include "oggdec.h"
28 #include "vorbiscomment.h"
29 #include "replaygain.h"
30
31 static int flac_read_header(AVFormatContext *s)
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->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
40     st->codecpar->codec_id = AV_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 (!s->pb->eof_reached && !metadata_last) {
52         avio_read(s->pb, header, 4);
53         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_PICTURE:
60         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
61             buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
62             if (!buffer) {
63                 return AVERROR(ENOMEM);
64             }
65             if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
66                 av_freep(&buffer);
67                 return AVERROR(EIO);
68             }
69             break;
70         /* skip metadata block for unsupported types */
71         default:
72             ret = avio_skip(s->pb, metadata_size);
73             if (ret < 0)
74                 return ret;
75         }
76
77         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
78             uint32_t samplerate;
79             uint64_t samples;
80
81             /* STREAMINFO can only occur once */
82             if (found_streaminfo) {
83                 av_freep(&buffer);
84                 return AVERROR_INVALIDDATA;
85             }
86             if (metadata_size != FLAC_STREAMINFO_SIZE) {
87                 av_freep(&buffer);
88                 return AVERROR_INVALIDDATA;
89             }
90             found_streaminfo = 1;
91             st->codecpar->extradata      = buffer;
92             st->codecpar->extradata_size = metadata_size;
93             buffer = NULL;
94
95             /* get sample rate and sample count from STREAMINFO header;
96              * other parameters will be extracted by the parser */
97             samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
98             samples    = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
99
100             /* set time base and duration */
101             if (samplerate > 0) {
102                 avpriv_set_pts_info(st, 64, 1, samplerate);
103                 if (samples > 0)
104                     st->duration = samples;
105             }
106         } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
107             uint8_t isrc[13];
108             uint64_t start;
109             const uint8_t *offset;
110             int i, chapters, track, ti;
111             if (metadata_size < 431)
112                 return AVERROR_INVALIDDATA;
113             offset = buffer + 395;
114             chapters = bytestream_get_byte(&offset) - 1;
115             if (chapters <= 0)
116                 return AVERROR_INVALIDDATA;
117             for (i = 0; i < chapters; i++) {
118                 if (offset + 36 - buffer > metadata_size)
119                     return AVERROR_INVALIDDATA;
120                 start = bytestream_get_be64(&offset);
121                 track = bytestream_get_byte(&offset);
122                 bytestream_get_buffer(&offset, isrc, 12);
123                 isrc[12] = 0;
124                 offset += 14;
125                 ti = bytestream_get_byte(&offset);
126                 if (ti <= 0) return AVERROR_INVALIDDATA;
127                 offset += ti * 12;
128                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
129             }
130         } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
131             ret = ff_flac_parse_picture(s, buffer, metadata_size);
132             av_freep(&buffer);
133             if (ret < 0) {
134                 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
135                 return ret;
136             }
137         } else {
138             /* STREAMINFO must be the first block */
139             if (!found_streaminfo) {
140                 av_freep(&buffer);
141                 return AVERROR_INVALIDDATA;
142             }
143             /* process supported blocks other than STREAMINFO */
144             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
145                 AVDictionaryEntry *chmask;
146
147                 ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
148                 if (ret < 0) {
149                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
150                 } else if (ret > 0) {
151                     s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
152                 }
153
154                 /* parse the channels mask if present */
155                 chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
156                 if (chmask) {
157                     uint64_t mask = strtol(chmask->value, NULL, 0);
158                     if (!mask || mask & ~0x3ffffULL) {
159                         av_log(s, AV_LOG_WARNING,
160                                "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
161                     } else {
162                         st->codecpar->channel_layout = mask;
163                         av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
164                     }
165                 }
166             }
167             av_freep(&buffer);
168         }
169     }
170
171     ret = ff_replaygain_export(st, s->metadata);
172     if (ret < 0)
173         return ret;
174
175     return 0;
176 }
177
178 static int flac_probe(AVProbeData *p)
179 {
180     if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
181         return 0;
182     return AVPROBE_SCORE_EXTENSION;
183 }
184
185 AVInputFormat ff_flac_demuxer = {
186     .name           = "flac",
187     .long_name      = NULL_IF_CONFIG_SMALL("raw FLAC"),
188     .read_probe     = flac_probe,
189     .read_header    = flac_read_header,
190     .read_packet    = ff_raw_read_partial_packet,
191     .flags          = AVFMT_GENERIC_INDEX,
192     .extensions     = "flac",
193     .raw_codec_id   = AV_CODEC_ID_FLAC,
194 };