00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavcodec/flac.h"
00023 #include "avformat.h"
00024 #include "raw.h"
00025 #include "id3v2.h"
00026 #include "oggdec.h"
00027 #include "vorbiscomment.h"
00028
00029 static int flac_read_header(AVFormatContext *s,
00030 AVFormatParameters *ap)
00031 {
00032 uint8_t buf[ID3v2_HEADER_SIZE];
00033 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
00034 uint8_t header[4];
00035 uint8_t *buffer=NULL;
00036 AVStream *st = av_new_stream(s, 0);
00037 if (!st)
00038 return AVERROR(ENOMEM);
00039 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00040 st->codec->codec_id = CODEC_ID_FLAC;
00041 st->need_parsing = AVSTREAM_PARSE_FULL;
00042
00043
00044
00045 ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
00046 if (ret == ID3v2_HEADER_SIZE && ff_id3v2_match(buf)) {
00047 int len = ff_id3v2_tag_len(buf);
00048 url_fseek(s->pb, len - ID3v2_HEADER_SIZE, SEEK_CUR);
00049 } else {
00050 url_fseek(s->pb, 0, SEEK_SET);
00051 }
00052
00053
00054 if (get_le32(s->pb) != MKTAG('f','L','a','C')) {
00055 url_fseek(s->pb, -4, SEEK_CUR);
00056 return 0;
00057 }
00058
00059
00060 while (!url_feof(s->pb) && !metadata_last) {
00061 get_buffer(s->pb, header, 4);
00062 ff_flac_parse_block_header(header, &metadata_last, &metadata_type,
00063 &metadata_size);
00064 switch (metadata_type) {
00065
00066 case FLAC_METADATA_TYPE_STREAMINFO:
00067 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
00068 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00069 if (!buffer) {
00070 return AVERROR(ENOMEM);
00071 }
00072 if (get_buffer(s->pb, buffer, metadata_size) != metadata_size) {
00073 av_freep(&buffer);
00074 return AVERROR(EIO);
00075 }
00076 break;
00077
00078 default:
00079 ret = url_fseek(s->pb, metadata_size, SEEK_CUR);
00080 if (ret < 0)
00081 return ret;
00082 }
00083
00084 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
00085 FLACStreaminfo si;
00086
00087 if (found_streaminfo) {
00088 av_freep(&buffer);
00089 return AVERROR_INVALIDDATA;
00090 }
00091 if (metadata_size != FLAC_STREAMINFO_SIZE) {
00092 av_freep(&buffer);
00093 return AVERROR_INVALIDDATA;
00094 }
00095 found_streaminfo = 1;
00096 st->codec->extradata = buffer;
00097 st->codec->extradata_size = metadata_size;
00098 buffer = NULL;
00099
00100
00101 ff_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
00102
00103
00104 if (si.samplerate > 0) {
00105 av_set_pts_info(st, 64, 1, si.samplerate);
00106 if (si.samples > 0)
00107 st->duration = si.samples;
00108 }
00109 } else {
00110
00111 if (!found_streaminfo) {
00112 av_freep(&buffer);
00113 return AVERROR_INVALIDDATA;
00114 }
00115
00116 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
00117 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
00118 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
00119 }
00120 }
00121 av_freep(&buffer);
00122 }
00123 }
00124
00125 return 0;
00126 }
00127
00128 static int flac_probe(AVProbeData *p)
00129 {
00130 uint8_t *bufptr = p->buf;
00131 uint8_t *end = p->buf + p->buf_size;
00132
00133 if(ff_id3v2_match(bufptr))
00134 bufptr += ff_id3v2_tag_len(bufptr);
00135
00136 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
00137 else return AVPROBE_SCORE_MAX/2;
00138 }
00139
00140 AVInputFormat flac_demuxer = {
00141 "flac",
00142 NULL_IF_CONFIG_SMALL("raw FLAC"),
00143 0,
00144 flac_probe,
00145 flac_read_header,
00146 ff_raw_read_partial_packet,
00147 .flags= AVFMT_GENERIC_INDEX,
00148 .extensions = "flac",
00149 .value = CODEC_ID_FLAC,
00150 .metadata_conv = ff_vorbiscomment_metadata_conv,
00151 };