]> git.sesse.net Git - ffmpeg/blob - libavformat/flacdec.c
Make MMX registers parametrized in the YSCALEYUV2PACKEDX_YA macro
[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 "raw.h"
25 #include "id3v2.h"
26 #include "oggdec.h"
27
28 static int flac_read_header(AVFormatContext *s,
29                              AVFormatParameters *ap)
30 {
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 */
42
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);
50     }
51
52     /* if fLaC marker is not found, assume there is no header */
53     if (get_le32(s->pb) != MKTAG('f','L','a','C'))
54         return 0;
55
56     /* process metadata blocks */
57     while (!url_feof(s->pb) && !metadata_last) {
58         get_buffer(s->pb, header, 4);
59         ff_flac_parse_block_header(header, &metadata_last, &metadata_type,
60                                    &metadata_size);
61         switch (metadata_type) {
62         /* allocate and read metadata block for supported types */
63         case FLAC_METADATA_TYPE_STREAMINFO:
64         case FLAC_METADATA_TYPE_VORBIS_COMMENT:
65             buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
66             if (!buffer) {
67                 return AVERROR_NOMEM;
68             }
69             if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
70                 av_freep(&buffer);
71                 return AVERROR_IO;
72             }
73             break;
74         /* skip metadata block for unsupported types */
75         default:
76             ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
77             if (ret < 0)
78                 return ret;
79         }
80
81         if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
82             FLACStreaminfo si;
83             /* STREAMINFO can only occur once */
84             if (found_streaminfo) {
85                 av_freep(&buffer);
86                 return AVERROR_INVALIDDATA;
87             }
88             if (metadata_size != FLAC_STREAMINFO_SIZE) {
89                 av_freep(&buffer);
90                 return AVERROR_INVALIDDATA;
91             }
92             found_streaminfo = 1;
93             st->codec->extradata      = buffer;
94             st->codec->extradata_size = metadata_size;
95             buffer = NULL;
96
97             /* get codec params from STREAMINFO header */
98             ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
99
100             /* set time base and duration */
101             if (si.samplerate > 0) {
102                 av_set_pts_info(st, 64, 1, si.samplerate);
103                 if (si.samples > 0)
104                     st->duration = si.samples;
105             }
106         } else {
107             /* STREAMINFO must be the first block */
108             if (!found_streaminfo) {
109                 av_freep(&buffer);
110                 return AVERROR_INVALIDDATA;
111             }
112             /* process supported blocks other than STREAMINFO */
113             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
114                 if (vorbis_comment(s, buffer, metadata_size)) {
115                     av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
116                 }
117             }
118             av_freep(&buffer);
119         }
120     }
121
122     return 0;
123 }
124
125 static int flac_probe(AVProbeData *p)
126 {
127     uint8_t *bufptr = p->buf;
128     uint8_t *end    = p->buf + p->buf_size;
129
130     if(ff_id3v2_match(bufptr))
131         bufptr += ff_id3v2_tag_len(bufptr);
132
133     if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
134     else                                            return AVPROBE_SCORE_MAX/2;
135 }
136
137 AVInputFormat flac_demuxer = {
138     "flac",
139     NULL_IF_CONFIG_SMALL("raw FLAC"),
140     0,
141     flac_probe,
142     flac_read_header,
143     ff_raw_read_partial_packet,
144     .flags= AVFMT_GENERIC_INDEX,
145     .extensions = "flac",
146     .value = CODEC_ID_FLAC,
147 };