2 * Chromaprint fingerprinting muxer
3 * Copyright (c) 2015 Rodger Combs
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/opt.h"
24 #include "libavcodec/internal.h"
25 #include <chromaprint.h>
27 #define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
28 CHROMAPRINT_VERSION_MINOR, \
29 CHROMAPRINT_VERSION_PATCH)
31 typedef enum FingerprintFormat {
33 FINGERPRINT_COMPRESSED,
37 typedef struct ChromaprintMuxContext {
39 int silence_threshold;
41 FingerprintFormat fp_format;
42 ChromaprintContext ctx;
43 } ChromaprintMuxContext;
45 static void cleanup(ChromaprintMuxContext *cpr)
48 avpriv_lock_avformat();
49 chromaprint_free(cpr->ctx);
50 avpriv_unlock_avformat();
54 static int write_header(AVFormatContext *s)
56 ChromaprintMuxContext *cpr = s->priv_data;
59 avpriv_lock_avformat();
60 cpr->ctx = chromaprint_new(cpr->algorithm);
61 avpriv_unlock_avformat();
64 av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
65 return AVERROR(ENOMEM);
68 if (cpr->silence_threshold != -1) {
69 #if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
70 if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
71 av_log(s, AV_LOG_ERROR, "Failed to set silence threshold.\n");
75 av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
76 "version 0.7.0 or later.\n");
81 if (s->nb_streams != 1) {
82 av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
88 if (st->codecpar->channels > 2) {
89 av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
93 if (st->codecpar->sample_rate < 1000) {
94 av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
98 if (!chromaprint_start(cpr->ctx, st->codecpar->sample_rate, st->codecpar->channels)) {
99 av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
106 return AVERROR(EINVAL);
109 static int write_packet(AVFormatContext *s, AVPacket *pkt)
111 ChromaprintMuxContext *cpr = s->priv_data;
112 return chromaprint_feed(cpr->ctx, pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
115 static int write_trailer(AVFormatContext *s)
117 ChromaprintMuxContext *cpr = s->priv_data;
118 AVIOContext *pb = s->pb;
119 void *fp = NULL, *enc_fp = NULL;
120 int size, enc_size, ret = AVERROR(EINVAL);
122 if (!chromaprint_finish(cpr->ctx)) {
123 av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
127 if (!chromaprint_get_raw_fingerprint(cpr->ctx, &fp, &size)) {
128 av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
132 switch (cpr->fp_format) {
133 case FINGERPRINT_RAW:
134 avio_write(pb, fp, size);
136 case FINGERPRINT_COMPRESSED:
137 case FINGERPRINT_BASE64:
138 if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
139 cpr->fp_format == FINGERPRINT_BASE64)) {
140 av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
143 avio_write(pb, enc_fp, enc_size);
150 chromaprint_dealloc(fp);
152 chromaprint_dealloc(enc_fp);
157 #define OFFSET(x) offsetof(ChromaprintMuxContext, x)
158 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
159 static const AVOption options[] = {
160 { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
161 { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
162 { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS },
163 { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
164 { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
165 { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
169 static const AVClass chromaprint_class = {
170 .class_name = "chromaprint muxer",
171 .item_name = av_default_item_name,
173 .version = LIBAVUTIL_VERSION_INT,
176 AVOutputFormat ff_chromaprint_muxer = {
177 .name = "chromaprint",
178 .long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
179 .priv_data_size = sizeof(ChromaprintMuxContext),
180 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
181 .write_header = write_header,
182 .write_packet = write_packet,
183 .write_trailer = write_trailer,
184 .flags = AVFMT_NOTIMESTAMPS,
185 .priv_class = &chromaprint_class,