#include "oggdec.h"
#include "vorbiscomment.h"
#include "replaygain.h"
-#include "libavcodec/bytestream.h"
static int flac_read_header(AVFormatContext *s)
{
AVStream *st = avformat_new_stream(s, NULL);
if (!st)
return AVERROR(ENOMEM);
- st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
- st->codec->codec_id = AV_CODEC_ID_FLAC;
+ st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
+ st->codecpar->codec_id = AV_CODEC_ID_FLAC;
st->need_parsing = AVSTREAM_PARSE_FULL;
/* the parameters will be extracted from the compressed bitstream */
/* process metadata blocks */
while (!s->pb->eof_reached && !metadata_last) {
avio_read(s->pb, header, 4);
- avpriv_flac_parse_block_header(header, &metadata_last, &metadata_type,
+ flac_parse_block_header(header, &metadata_last, &metadata_type,
&metadata_size);
switch (metadata_type) {
/* allocate and read metadata block for supported types */
case FLAC_METADATA_TYPE_CUESHEET:
case FLAC_METADATA_TYPE_PICTURE:
case FLAC_METADATA_TYPE_VORBIS_COMMENT:
- buffer = av_mallocz(metadata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+ buffer = av_mallocz(metadata_size + AV_INPUT_BUFFER_PADDING_SIZE);
if (!buffer) {
return AVERROR(ENOMEM);
}
}
if (metadata_type == FLAC_METADATA_TYPE_STREAMINFO) {
- FLACStreaminfo si;
+ uint32_t samplerate;
+ uint64_t samples;
+
/* STREAMINFO can only occur once */
if (found_streaminfo) {
av_freep(&buffer);
return AVERROR_INVALIDDATA;
}
found_streaminfo = 1;
- st->codec->extradata = buffer;
- st->codec->extradata_size = metadata_size;
+ st->codecpar->extradata = buffer;
+ st->codecpar->extradata_size = metadata_size;
buffer = NULL;
- /* get codec params from STREAMINFO header */
- avpriv_flac_parse_streaminfo(st->codec, &si, st->codec->extradata);
+ /* get sample rate and sample count from STREAMINFO header;
+ * other parameters will be extracted by the parser */
+ samplerate = AV_RB24(st->codecpar->extradata + 10) >> 4;
+ samples = (AV_RB64(st->codecpar->extradata + 13) >> 24) & ((1ULL << 36) - 1);
/* set time base and duration */
- if (si.samplerate > 0) {
- avpriv_set_pts_info(st, 64, 1, si.samplerate);
- if (si.samples > 0)
- st->duration = si.samples;
+ if (samplerate > 0) {
+ avpriv_set_pts_info(st, 64, 1, samplerate);
+ if (samples > 0)
+ st->duration = samples;
}
} else if (metadata_type == FLAC_METADATA_TYPE_CUESHEET) {
uint8_t isrc[13];
if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
AVDictionaryEntry *chmask;
- if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
+ ret = ff_vorbis_comment(s, &s->metadata, buffer, metadata_size, 1);
+ if (ret < 0) {
av_log(s, AV_LOG_WARNING, "error parsing VorbisComment metadata\n");
+ } else if (ret > 0) {
+ s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
}
/* parse the channels mask if present */
av_log(s, AV_LOG_WARNING,
"Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
} else {
- st->codec->channel_layout = mask;
+ st->codecpar->channel_layout = mask;
av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
}
}
static int flac_probe(AVProbeData *p)
{
- if (p->buf_size < 4 || memcmp(p->buf, "fLaC", 4))
- return 0;
- return AVPROBE_SCORE_EXTENSION;
+ /* file header + metadata header + checked bytes of streaminfo */
+ if (p->buf_size >= 4 + 4 + 13) {
+ int type = p->buf[4] & 0x7f;
+ int size = AV_RB24(p->buf + 5);
+ int min_block_size = AV_RB16(p->buf + 8);
+ int max_block_size = AV_RB16(p->buf + 10);
+ int sample_rate = AV_RB24(p->buf + 18) >> 4;
+
+ if (!memcmp(p->buf, "fLaC", 4) &&
+ type == FLAC_METADATA_TYPE_STREAMINFO &&
+ size == FLAC_STREAMINFO_SIZE &&
+ min_block_size >= 16 &&
+ max_block_size >= min_block_size &&
+ sample_rate && sample_rate <= 655350)
+ return AVPROBE_SCORE_MAX;
+ }
+
+ return 0;
}
AVInputFormat ff_flac_demuxer = {