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 "internal.h"
00025 #include "rawdec.h"
00026 #include "oggdec.h"
00027 #include "vorbiscomment.h"
00028 #include "libavcodec/bytestream.h"
00029
00030 static int flac_read_header(AVFormatContext *s)
00031 {
00032 int ret, metadata_last=0, metadata_type, metadata_size, found_streaminfo=0;
00033 uint8_t header[4];
00034 uint8_t *buffer=NULL;
00035 AVStream *st = avformat_new_stream(s, NULL);
00036 if (!st)
00037 return AVERROR(ENOMEM);
00038 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00039 st->codec->codec_id = CODEC_ID_FLAC;
00040 st->need_parsing = AVSTREAM_PARSE_FULL;
00041
00042
00043
00044 if (avio_rl32(s->pb) != MKTAG('f','L','a','C')) {
00045 avio_seek(s->pb, -4, SEEK_CUR);
00046 return 0;
00047 }
00048
00049
00050 while (!url_feof(s->pb) && !metadata_last) {
00051 avio_read(s->pb, header, 4);
00052 avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
00053 &metadata_size);
00054 switch (metadata_type) {
00055
00056 case FLAC_METADATA_TYPE_STREAMINFO:
00057 case FLAC_METADATA_TYPE_CUESHEET:
00058 case FLAC_METADATA_TYPE_VORBIS_COMMENT:
00059 buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00060 if (!buffer) {
00061 return AVERROR(ENOMEM);
00062 }
00063 if (avio_read(s->pb, buffer, metadata_size) != metadata_size) {
00064 av_freep(&buffer);
00065 return AVERROR(EIO);
00066 }
00067 break;
00068
00069 default:
00070 ret = avio_skip(s->pb, metadata_size);
00071 if (ret < 0)
00072 return ret;
00073 }
00074
00075 if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
00076 FLACStreaminfo si;
00077
00078 if (found_streaminfo) {
00079 av_freep(&buffer);
00080 return AVERROR_INVALIDDATA;
00081 }
00082 if (metadata_size != FLAC_STREAMINFO_SIZE) {
00083 av_freep(&buffer);
00084 return AVERROR_INVALIDDATA;
00085 }
00086 found_streaminfo = 1;
00087 st->codec->extradata = buffer;
00088 st->codec->extradata_size = metadata_size;
00089 buffer = NULL;
00090
00091
00092 avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
00093
00094
00095 if (si.samplerate > 0) {
00096 avpriv_set_pts_info(st, 64, 1, si.samplerate);
00097 if (si.samples > 0)
00098 st->duration = si.samples;
00099 }
00100 } else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
00101 uint8_t isrc[13];
00102 uint64_t start;
00103 const uint8_t *offset;
00104 int i, chapters, track, ti;
00105 if (metadata_size < 431)
00106 return AVERROR_INVALIDDATA;
00107 offset = buffer + 395;
00108 chapters = bytestream_get_byte(&offset) - 1;
00109 if (chapters <= 0)
00110 return AVERROR_INVALIDDATA;
00111 for (i = 0; i < chapters; i++) {
00112 if (offset + 36 - buffer > metadata_size)
00113 return AVERROR_INVALIDDATA;
00114 start = bytestream_get_be64(&offset);
00115 track = bytestream_get_byte(&offset);
00116 bytestream_get_buffer(&offset, isrc, 12);
00117 isrc[12] = 0;
00118 offset += 14;
00119 ti = bytestream_get_byte(&offset);
00120 if (ti <= 0) return AVERROR_INVALIDDATA;
00121 offset += ti * 12;
00122 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
00123 }
00124 } else {
00125
00126 if (!found_streaminfo) {
00127 av_freep(&buffer);
00128 return AVERROR_INVALIDDATA;
00129 }
00130
00131 if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
00132 if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
00133 av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
00134 }
00135 }
00136 av_freep(&buffer);
00137 }
00138 }
00139
00140 return 0;
00141 }
00142
00143 static int flac_probe(AVProbeData *p)
00144 {
00145 uint8_t *bufptr = p->buf;
00146 uint8_t *end = p->buf + p->buf_size;
00147
00148 if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
00149 else return AVPROBE_SCORE_MAX/2;
00150 }
00151
00152 AVInputFormat ff_flac_demuxer = {
00153 .name = "flac",
00154 .long_name = NULL_IF_CONFIG_SMALL("raw FLAC"),
00155 .read_probe = flac_probe,
00156 .read_header = flac_read_header,
00157 .read_packet = ff_raw_read_partial_packet,
00158 .flags = AVFMT_GENERIC_INDEX,
00159 .extensions = "flac",
00160 .raw_codec_id = CODEC_ID_FLAC,
00161 };