]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/flacdec.c
Ignore all generated example binaries
[ffmpeg] / libavformat / flacdec.c
index abb36d9377de8323a9dca44933119757a36c9616..2f4ac56040fa016cdb135823853ca7fd33f19eb2 100644 (file)
 
 #include "libavcodec/flac.h"
 #include "avformat.h"
-#include "id3v2.h"
+#include "flac_picture.h"
 #include "internal.h"
 #include "rawdec.h"
 #include "oggdec.h"
 #include "vorbiscomment.h"
-#include "libavcodec/bytestream.h"
-
-static int parse_picture(AVFormatContext *s, uint8_t *buf, int buf_size)
-{
-    const CodecMime *mime = ff_id3v2_mime_tags;
-    enum  CodecID      id = CODEC_ID_NONE;
-    uint8_t mimetype[64], *desc = NULL, *data = NULL;
-    AVIOContext *pb = NULL;
-    AVStream *st;
-    int type, width, height;
-    int len, ret = 0;
-
-    pb = avio_alloc_context(buf, buf_size, 0, NULL, NULL, NULL, NULL);
-    if (!pb)
-        return AVERROR(ENOMEM);
-
-    /* read the picture type */
-    type      = avio_rb32(pb);
-    if (type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types) || type < 0) {
-        av_log(s, AV_LOG_ERROR, "Invalid picture type: %d.\n", type);
-        if (s->error_recognition & AV_EF_EXPLODE) {
-            ret = AVERROR_INVALIDDATA;
-            goto fail;
-        }
-        type = 0;
-    }
-
-    /* picture mimetype */
-    len  = avio_rb32(pb);
-    if (len <= 0 ||
-        avio_read(pb, mimetype, FFMIN(len, sizeof(mimetype) - 1)) != len) {
-        av_log(s, AV_LOG_ERROR, "Could not read mimetype from an attached "
-               "picture.\n");
-        if (s->error_recognition & AV_EF_EXPLODE)
-            ret = AVERROR_INVALIDDATA;
-        goto fail;
-    }
-    mimetype[len] = 0;
-
-    while (mime->id != CODEC_ID_NONE) {
-        if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
-            id = mime->id;
-            break;
-        }
-        mime++;
-    }
-    if (id == CODEC_ID_NONE) {
-        av_log(s, AV_LOG_ERROR, "Unknown attached picture mimetype: %s.\n",
-               mimetype);
-        if (s->error_recognition & AV_EF_EXPLODE)
-            ret = AVERROR_INVALIDDATA;
-        goto fail;
-    }
-
-    /* picture description */
-    len = avio_rb32(pb);
-    if (len > 0) {
-        if (!(desc = av_malloc(len + 1))) {
-            ret = AVERROR(ENOMEM);
-            goto fail;
-        }
-
-        if (avio_read(pb, desc, len) != len) {
-            av_log(s, AV_LOG_ERROR, "Error reading attached picture description.\n");
-            if (s->error_recognition & AV_EF_EXPLODE)
-                ret = AVERROR(EIO);
-            goto fail;
-        }
-        desc[len] = 0;
-    }
-
-    /* picture metadata */
-    width  = avio_rb32(pb);
-    height = avio_rb32(pb);
-    avio_skip(pb, 8);
-
-    /* picture data */
-    len = avio_rb32(pb);
-    if (len <= 0) {
-        av_log(s, AV_LOG_ERROR, "Invalid attached picture size: %d.\n", len);
-        if (s->error_recognition & AV_EF_EXPLODE)
-            ret = AVERROR_INVALIDDATA;
-        goto fail;
-    }
-    if (!(data = av_malloc(len))) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-    if (avio_read(pb, data, len) != len) {
-        av_log(s, AV_LOG_ERROR, "Error reading attached picture data.\n");
-        if (s->error_recognition & AV_EF_EXPLODE)
-            ret = AVERROR(EIO);
-        goto fail;
-    }
-
-    st = avformat_new_stream(s, NULL);
-    if (!st) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    av_init_packet(&st->attached_pic);
-    st->attached_pic.data         = data;
-    st->attached_pic.size         = len;
-    st->attached_pic.destruct     = av_destruct_packet;
-    st->attached_pic.stream_index = st->index;
-    st->attached_pic.flags       |= AV_PKT_FLAG_KEY;
-
-    st->disposition      |= AV_DISPOSITION_ATTACHED_PIC;
-    st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-    st->codec->codec_id   = id;
-    st->codec->width      = width;
-    st->codec->height     = height;
-    av_dict_set(&st->metadata, "comment", ff_id3v2_picture_types[type], 0);
-    if (desc)
-        av_dict_set(&st->metadata, "title",   desc, AV_DICT_DONT_STRDUP_VAL);
-
-    av_freep(&pb);
-
-    return 0;
-
-fail:
-    av_freep(&desc);
-    av_freep(&data);
-    av_freep(&pb);
-    return ret;
-
-}
+#include "replaygain.h"
 
 static int flac_read_header(AVFormatContext *s)
 {
@@ -163,8 +36,8 @@ 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 = 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 */
 
@@ -177,7 +50,7 @@ static int flac_read_header(AVFormatContext *s)
     /* 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 */
@@ -185,7 +58,7 @@ static int flac_read_header(AVFormatContext *s)
         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);
             }
@@ -202,7 +75,9 @@ static int flac_read_header(AVFormatContext *s)
         }
 
         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);
@@ -213,18 +88,20 @@ static int flac_read_header(AVFormatContext *s)
                 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];
@@ -251,7 +128,7 @@ static int flac_read_header(AVFormatContext *s)
                 avpriv_new_chapter(s, track, st->time_base, start, AV_NOPTS_VALUE, isrc);
             }
         } else if (metadata_type == FLAC_METADATA_TYPE_PICTURE) {
-            ret = parse_picture(s, buffer, metadata_size);
+            ret = ff_flac_parse_picture(s, buffer, metadata_size);
             av_freep(&buffer);
             if (ret < 0) {
                 av_log(s, AV_LOG_ERROR, "Error parsing attached picture.\n");
@@ -265,24 +142,59 @@ static int flac_read_header(AVFormatContext *s)
             }
             /* process supported blocks other than STREAMINFO */
             if (metadata_type == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
-                if (ff_vorbis_comment(s, &s->metadata, buffer, metadata_size)) {
+                AVDictionaryEntry *chmask;
+
+                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 */
+                chmask = av_dict_get(s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
+                if (chmask) {
+                    uint64_t mask = strtol(chmask->value, NULL, 0);
+                    if (!mask || mask & ~0x3ffffULL) {
+                        av_log(s, AV_LOG_WARNING,
+                               "Invalid value of WAVEFORMATEXTENSIBLE_CHANNEL_MASK\n");
+                    } else {
+                        st->codecpar->channel_layout = mask;
+                        av_dict_set(&s->metadata, "WAVEFORMATEXTENSIBLE_CHANNEL_MASK", NULL, 0);
+                    }
                 }
             }
             av_freep(&buffer);
         }
     }
 
+    ret = ff_replaygain_export(st, s->metadata);
+    if (ret < 0)
+        return ret;
+
     return 0;
 }
 
 static int flac_probe(AVProbeData *p)
 {
-    uint8_t *bufptr = p->buf;
-    uint8_t *end    = p->buf + p->buf_size;
+    /* 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;
+    }
 
-    if(bufptr > end-4 || memcmp(bufptr, "fLaC", 4)) return 0;
-    else                                            return AVPROBE_SCORE_MAX/2;
+    return 0;
 }
 
 AVInputFormat ff_flac_demuxer = {
@@ -293,5 +205,5 @@ AVInputFormat ff_flac_demuxer = {
     .read_packet    = ff_raw_read_partial_packet,
     .flags          = AVFMT_GENERIC_INDEX,
     .extensions     = "flac",
-    .raw_codec_id   = CODEC_ID_FLAC,
+    .raw_codec_id   = AV_CODEC_ID_FLAC,
 };