1 /*
2 * Chromaprint fingerprinting muxer
3 * Copyright (c) 2015 rcombs
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "avformat.h"
23 #include "internal.h"
24 #include "libavutil/opt.h"
25 #include "libavcodec/internal.h"
26 #include <chromaprint.h>
27
28 #define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
29 CHROMAPRINT_VERSION_MINOR, \
30 CHROMAPRINT_VERSION_PATCH)
31
32 typedef enum FingerprintFormat {
33 FINGERPRINT_RAW,
34 FINGERPRINT_COMPRESSED,
35 FINGERPRINT_BASE64,
36 } FingerprintFormat;
37
38 typedef struct ChromaprintMuxContext {
39 const AVClass *class;
40 int silence_threshold;
41 int algorithm;
42 FingerprintFormat fp_format;
43 #if CPR_VERSION_INT >= AV_VERSION_INT(1, 4, 0)
44 ChromaprintContext *ctx;
45 #else
46 ChromaprintContext ctx;
47 #endif
48 } ChromaprintMuxContext;
49
cleanup(ChromaprintMuxContext * cpr)50 static void cleanup(ChromaprintMuxContext *cpr)
51 {
52 if (cpr->ctx) {
53 ff_lock_avformat();
54 chromaprint_free(cpr->ctx);
55 ff_unlock_avformat();
56 }
57 }
58
write_header(AVFormatContext * s)59 static int write_header(AVFormatContext *s)
60 {
61 ChromaprintMuxContext *cpr = s->priv_data;
62 AVStream *st;
63
64 ff_lock_avformat();
65 cpr->ctx = chromaprint_new(cpr->algorithm);
66 ff_unlock_avformat();
67
68 if (!cpr->ctx) {
69 av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
70 return AVERROR(ENOMEM);
71 }
72
73 if (cpr->silence_threshold != -1) {
74 #if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
75 if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
76 av_log(s, AV_LOG_ERROR, "Failed to set silence threshold. Setting silence_threshold requires -algorithm 3 option.\n");
77 goto fail;
78 }
79 #else
80 av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
81 "version 0.7.0 or later.\n");
82 goto fail;
83 #endif
84 }
85
86 if (s->nb_streams != 1) {
87 av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
88 goto fail;
89 }
90
91 st = s->streams[0];
92
93 if (st->codecpar->channels > 2) {
94 av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
95 goto fail;
96 }
97
98 if (st->codecpar->sample_rate < 1000) {
99 av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
100 goto fail;
101 }
102
103 if (!chromaprint_start(cpr->ctx, st->codecpar->sample_rate, st->codecpar->channels)) {
104 av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
105 goto fail;
106 }
107
108 return 0;
109 fail:
110 cleanup(cpr);
111 return AVERROR(EINVAL);
112 }
113
write_packet(AVFormatContext * s,AVPacket * pkt)114 static int write_packet(AVFormatContext *s, AVPacket *pkt)
115 {
116 ChromaprintMuxContext *cpr = s->priv_data;
117 return chromaprint_feed(cpr->ctx, (const int16_t *)pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
118 }
119
write_trailer(AVFormatContext * s)120 static int write_trailer(AVFormatContext *s)
121 {
122 ChromaprintMuxContext *cpr = s->priv_data;
123 AVIOContext *pb = s->pb;
124 void *fp = NULL;
125 char *enc_fp = NULL;
126 int size, enc_size, ret = AVERROR(EINVAL);
127
128 if (!chromaprint_finish(cpr->ctx)) {
129 av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
130 goto fail;
131 }
132
133 if (!chromaprint_get_raw_fingerprint(cpr->ctx, (uint32_t **)&fp, &size)) {
134 av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
135 goto fail;
136 }
137
138 switch (cpr->fp_format) {
139 case FINGERPRINT_RAW:
140 avio_write(pb, fp, size * 4); //fp points to array of uint32_t
141 break;
142 case FINGERPRINT_COMPRESSED:
143 case FINGERPRINT_BASE64:
144 if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
145 cpr->fp_format == FINGERPRINT_BASE64)) {
146 av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
147 goto fail;
148 }
149 avio_write(pb, enc_fp, enc_size);
150 break;
151 }
152
153 ret = 0;
154 fail:
155 if (fp)
156 chromaprint_dealloc(fp);
157 if (enc_fp)
158 chromaprint_dealloc(enc_fp);
159 cleanup(cpr);
160 return ret;
161 }
162
163 #define OFFSET(x) offsetof(ChromaprintMuxContext, x)
164 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
165 static const AVOption options[] = {
166 { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
167 { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
168 { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS, "fp_format" },
169 { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
170 { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
171 { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
172 { NULL },
173 };
174
175 static const AVClass chromaprint_class = {
176 .class_name = "chromaprint muxer",
177 .item_name = av_default_item_name,
178 .option = options,
179 .version = LIBAVUTIL_VERSION_INT,
180 };
181
182 AVOutputFormat ff_chromaprint_muxer = {
183 .name = "chromaprint",
184 .long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
185 .priv_data_size = sizeof(ChromaprintMuxContext),
186 .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
187 .write_header = write_header,
188 .write_packet = write_packet,
189 .write_trailer = write_trailer,
190 .flags = AVFMT_NOTIMESTAMPS,
191 .priv_class = &chromaprint_class,
192 };
193