[FFmpeg-devel] [PATCH] lavf: add chromaprint muxer

Rodger Combs rodger.combs at gmail.com
Sun Oct 4 00:24:06 CEST 2015


---
 Changelog                 |   1 +
 configure                 |   4 +
 doc/muxers.texi           |  35 +++++++++
 libavformat/Makefile      |   1 +
 libavformat/allformats.c  |   1 +
 libavformat/chromaprint.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++
 libavformat/version.h     |   4 +-
 7 files changed, 230 insertions(+), 2 deletions(-)
 create mode 100644 libavformat/chromaprint.c

diff --git a/Changelog b/Changelog
index bbb9ed0..4b18d00 100644
--- a/Changelog
+++ b/Changelog
@@ -14,6 +14,7 @@ version <next>:
 - chromakey filter
 - maskedmerge filter
 - Screenpresso SPV1 decoding
+- chromaprint fingerprinting muxer
 
 
 version 2.8:
diff --git a/configure b/configure
index 3ee1ed2..3b1aea7 100755
--- a/configure
+++ b/configure
@@ -196,6 +196,7 @@ Individual component options:
 External library support:
   --enable-avisynth        enable reading of AviSynth script files [no]
   --disable-bzlib          disable bzlib [autodetect]
+  --enable-chromaprint     enable audio fingerprinting with chromaprint [no]
   --enable-fontconfig      enable fontconfig, useful for drawtext filter [no]
   --enable-frei0r          enable frei0r video filtering [no]
   --enable-gnutls          enable gnutls, needed for https support
@@ -1367,6 +1368,7 @@ EXAMPLE_LIST="
 EXTERNAL_LIBRARY_LIST="
     avisynth
     bzlib
+    chromaprint
     crystalhd
     decklink
     frei0r
@@ -2507,6 +2509,7 @@ vc1_parser_select="mpegvideo startcode vc1_decoder"
 mjpeg2jpeg_bsf_select="jpegtables"
 
 # external libraries
+chromaprint_muxer_deps="chromaprint"
 libaacplus_encoder_deps="libaacplus"
 libcelt_decoder_deps="libcelt"
 libdcadec_decoder_deps="libdcadec"
@@ -5255,6 +5258,7 @@ enabled avfoundation_indev && { check_lib2 CoreGraphics/CoreGraphics.h CGGetActi
 enabled avisynth          && { { check_lib2 "windows.h" LoadLibrary; } ||
                                { check_lib2 "dlfcn.h" dlopen -ldl; } ||
                                die "ERROR: LoadLibrary/dlopen not found for avisynth"; }
+enabled chromaprint       && require chromaprint chromaprint.h chromaprint_get_version -lchromaprint
 enabled decklink          && { check_header DeckLinkAPI.h || die "ERROR: DeckLinkAPI.h header not found"; }
 enabled frei0r            && { check_header frei0r.h || die "ERROR: frei0r.h header not found"; }
 enabled gnutls            && require_pkg_config gnutls gnutls/gnutls.h gnutls_global_init
diff --git a/doc/muxers.texi b/doc/muxers.texi
index 86ca4ad..91d131f 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -37,6 +37,41 @@ ID3v2.3 and ID3v2.4) are supported. The default is version 4.
 
 @end table
 
+ at anchor{chromaprint}
+ at section chromaprint
+
+Chromaprint fingerprinter
+
+This muxer feeds audio data to the Chromaprint library, which generates
+a fingerprint for the provided audio data. It takes a single signed
+native-endian 16-bit raw audio stream.
+
+ at subsection Options
+
+ at table @option
+ at item silence_threshold
+Threshold for detecting silence, ranges from 0 to 32767. -1 for default
+(required for use with the AcoustID service).
+
+ at item algorithm
+Algorithm index to fingerprint with.
+
+ at item fp_format
+Format to output the fingerprint as. Accepts the following options:
+ at table @samp
+ at item raw
+Binary raw fingerprint
+
+ at item compressed
+Binary compressed fingerprint
+
+ at item base64
+Base64 compressed fingerprint
+
+ at end table
+
+ at end table
+
 @anchor{crc}
 @section crc
 
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 466da51..c9bf20f 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -487,6 +487,7 @@ OBJS-$(CONFIG_YUV4MPEGPIPE_MUXER)        += yuv4mpegenc.o
 OBJS-$(CONFIG_YUV4MPEGPIPE_DEMUXER)      += yuv4mpegdec.o
 
 # external libraries
+OBJS-$(CONFIG_CHROMAPRINT_MUXER)         += chromaprint.o
 OBJS-$(CONFIG_LIBGME_DEMUXER)            += libgme.o
 OBJS-$(CONFIG_LIBMODPLUG_DEMUXER)        += libmodplug.o
 OBJS-$(CONFIG_LIBNUT_DEMUXER)            += libnut.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index 0a24ac7..0ccde9d 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -391,6 +391,7 @@ void av_register_all(void)
     REGISTER_PROTOCOL(UNIX,             unix);
 
     /* external libraries */
+    REGISTER_MUXER   (CHROMAPRINT,      chromaprint);
     REGISTER_DEMUXER (LIBGME,           libgme);
     REGISTER_DEMUXER (LIBMODPLUG,       libmodplug);
     REGISTER_MUXDEMUX(LIBNUT,           libnut);
diff --git a/libavformat/chromaprint.c b/libavformat/chromaprint.c
new file mode 100644
index 0000000..4d67f43
--- /dev/null
+++ b/libavformat/chromaprint.c
@@ -0,0 +1,186 @@
+/*
+ * Chromaprint fingerprinting muxer
+ * Copyright (c) 2015 Rodger Combs
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avformat.h"
+#include "libavutil/opt.h"
+#include "libavcodec/internal.h"
+#include <chromaprint.h>
+
+#define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
+                                       CHROMAPRINT_VERSION_MINOR, \
+                                       CHROMAPRINT_VERSION_PATCH)
+
+typedef enum FingerprintFormat {
+    FINGERPRINT_RAW,
+    FINGERPRINT_COMPRESSED,
+    FINGERPRINT_BASE64,
+} FingerprintFormat;
+
+typedef struct ChromaprintMuxContext {
+    const AVClass *class;
+    int silence_threshold;
+    int algorithm;
+    FingerprintFormat fp_format;
+    ChromaprintContext ctx;
+} ChromaprintMuxContext;
+
+static void cleanup(ChromaprintMuxContext *cpr)
+{
+    if (cpr->ctx) {
+        avpriv_lock_avformat();
+        chromaprint_free(cpr->ctx);
+        avpriv_unlock_avformat();
+    }
+}
+
+static int write_header(AVFormatContext *s)
+{
+    ChromaprintMuxContext *cpr = s->priv_data;
+    AVStream *st;
+
+    avpriv_lock_avformat();
+    cpr->ctx = chromaprint_new(cpr->algorithm);
+    avpriv_unlock_avformat();
+
+    if (!cpr->ctx) {
+        av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    if (cpr->silence_threshold != -1) {
+#if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
+        if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
+            av_log(s, AV_LOG_ERROR, "Failed to set silence threshold.\n");
+            goto fail;
+        }
+#else
+        av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
+                                "version 0.7.0 or later.\n");
+        goto fail;
+#endif
+    }
+
+    if (s->nb_streams != 1) {
+        av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
+        goto fail;
+    }
+
+    st = s->streams[0];
+
+    if (st->codec->channels > 2) {
+        av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
+        goto fail;
+    }
+
+    if (st->codec->sample_rate < 1000) {
+        av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
+        goto fail;
+    }
+
+    if (!chromaprint_start(cpr->ctx, st->codec->sample_rate, st->codec->channels)) {
+        av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
+        goto fail;
+    }
+
+    return 0;
+fail:
+    cleanup(cpr);
+    return AVERROR(EINVAL);
+}
+
+static int write_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    ChromaprintMuxContext *cpr = s->priv_data;
+    return chromaprint_feed(cpr->ctx, pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
+}
+
+static int write_trailer(AVFormatContext *s)
+{
+    ChromaprintMuxContext *cpr = s->priv_data;
+    AVIOContext *pb = s->pb;
+    void *fp = NULL, *enc_fp = NULL;
+    int size, enc_size, ret = AVERROR(EINVAL);
+
+    if (!chromaprint_finish(cpr->ctx)) {
+        av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
+        goto fail;
+    }
+
+    if (!chromaprint_get_raw_fingerprint(cpr->ctx, &fp, &size)) {
+        av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
+        goto fail;
+    }
+
+    switch (cpr->fp_format) {
+    case FINGERPRINT_RAW:
+        avio_write(pb, fp, size);
+        break;
+    case FINGERPRINT_COMPRESSED:
+    case FINGERPRINT_BASE64:
+        if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
+                                            cpr->fp_format == FINGERPRINT_BASE64)) {
+            av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
+            goto fail;
+        }
+        avio_write(pb, enc_fp, enc_size);
+        break;
+    }
+
+    ret = 0;
+fail:
+    if (fp)
+        chromaprint_dealloc(fp);
+    if (enc_fp)
+        chromaprint_dealloc(enc_fp);
+    cleanup(cpr);
+    return ret;
+}
+
+#define OFFSET(x) offsetof(ChromaprintMuxContext, x)
+#define FLAGS AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
+    { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
+    { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS },
+    { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
+    { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
+    { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
+    { NULL },
+};
+
+static const AVClass chromaprint_class = {
+    .class_name = "chromaprint muxer",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+AVOutputFormat ff_chromaprint_muxer = {
+    .name              = "chromaprint",
+    .long_name         = NULL_IF_CONFIG_SMALL("Chromaprint"),
+    .priv_data_size    = sizeof(ChromaprintMuxContext),
+    .audio_codec       = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
+    .write_header      = write_header,
+    .write_packet      = write_packet,
+    .write_trailer     = write_trailer,
+    .flags             = AVFMT_NOTIMESTAMPS,
+    .priv_class        = &chromaprint_class,
+};
diff --git a/libavformat/version.h b/libavformat/version.h
index 4c60e50..f7a2e09 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -30,8 +30,8 @@
 #include "libavutil/version.h"
 
 #define LIBAVFORMAT_VERSION_MAJOR  57
-#define LIBAVFORMAT_VERSION_MINOR   2
-#define LIBAVFORMAT_VERSION_MICRO 102
+#define LIBAVFORMAT_VERSION_MINOR   3
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.6.0



More information about the ffmpeg-devel mailing list