FFmpeg
Loading...
Searching...
No Matches
chromaprint.c
Go to the documentation of this file.
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 "mux.h"
24#include "libavutil/opt.h"
25#include "libavutil/thread.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
33
39
40typedef struct ChromaprintMuxContext {
41 const AVClass *class;
44 /* FingerprintFormat, use int for AVOption */
46#if CPR_VERSION_INT >= AV_VERSION_INT(1, 4, 0)
47 ChromaprintContext *ctx;
48#else
49 ChromaprintContext ctx;
50#endif
52
54{
55 ChromaprintMuxContext *const cpr = s->priv_data;
56
57 if (cpr->ctx) {
59 chromaprint_free(cpr->ctx);
61 }
62}
63
65{
66 ChromaprintMuxContext *cpr = s->priv_data;
67 AVStream *st;
68
70 cpr->ctx = chromaprint_new(cpr->algorithm);
72
73 if (!cpr->ctx) {
74 av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
75 return AVERROR_EXTERNAL;
76 }
77
78 if (cpr->silence_threshold != -1) {
79#if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
80 if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
81 av_log(s, AV_LOG_ERROR, "Failed to set silence threshold. Setting silence_threshold requires -algorithm 3 option.\n");
82 return AVERROR_EXTERNAL;
83 }
84#else
85 av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
86 "version 0.7.0 or later.\n");
87 return AVERROR(ENOSYS);
88#endif
89 }
90
91 st = s->streams[0];
92
93 if (st->codecpar->ch_layout.nb_channels > 2) {
94 av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
95 return AVERROR(EINVAL);
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 return AVERROR(EINVAL);
101 }
102
103 if (!chromaprint_start(cpr->ctx, st->codecpar->sample_rate, st->codecpar->ch_layout.nb_channels)) {
104 av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
105 return AVERROR_EXTERNAL;
106 }
107
108 return 0;
109}
110
112{
113 ChromaprintMuxContext *cpr = s->priv_data;
114 return chromaprint_feed(cpr->ctx, (const int16_t *)pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
115}
116
118{
119 ChromaprintMuxContext *cpr = s->priv_data;
120 AVIOContext *pb = s->pb;
121 void *fp = NULL;
122 char *enc_fp = NULL;
123 int size, enc_size, ret = AVERROR_EXTERNAL;
124
125 if (!chromaprint_finish(cpr->ctx)) {
126 av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
127 goto fail;
128 }
129
130 if (!chromaprint_get_raw_fingerprint(cpr->ctx, (uint32_t **)&fp, &size)) {
131 av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
132 goto fail;
133 }
134
135 switch (cpr->fp_format) {
136 case FINGERPRINT_RAW:
137 avio_write(pb, fp, size * 4); //fp points to array of uint32_t
138 break;
141 if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
142 cpr->fp_format == FINGERPRINT_BASE64)) {
143 av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
144 goto fail;
145 }
146 avio_write(pb, enc_fp, enc_size);
147 break;
148 }
149
150 ret = 0;
151fail:
152 if (fp)
153 chromaprint_dealloc(fp);
154 if (enc_fp)
155 chromaprint_dealloc(enc_fp);
156 return ret;
157}
158
159#define OFFSET(x) offsetof(ChromaprintMuxContext, x)
160#define FLAGS AV_OPT_FLAG_ENCODING_PARAM
161static const AVOption options[] = {
162 { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
163 { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
164 { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS, .unit = "fp_format" },
165 { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, .unit = "fp_format"},
166 { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, .unit = "fp_format"},
167 { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, .unit = "fp_format"},
168 { NULL },
169};
170
172 .class_name = "chromaprint muxer",
173 .item_name = av_default_item_name,
174 .option = options,
175 .version = LIBAVUTIL_VERSION_INT,
176};
177
179 .p.name = "chromaprint",
180 .p.long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
181 .priv_data_size = sizeof(ChromaprintMuxContext),
183 .p.video_codec = AV_CODEC_ID_NONE,
184 .p.subtitle_codec = AV_CODEC_ID_NONE,
185 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
187 .init = init,
188 .write_packet = write_packet,
189 .write_trailer = write_trailer,
190 .deinit = deinit,
191 .p.flags = AVFMT_NOTIMESTAMPS,
192 .p.priv_class = &chromaprint_class,
193};
const FFOutputFormat ff_chromaprint_muxer
Main libavformat public API header.
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
#define s(width, name)
Definition cbs_vp9.c:198
FingerprintFormat
Definition chromaprint.c:34
@ FINGERPRINT_COMPRESSED
Definition chromaprint.c:36
@ FINGERPRINT_BASE64
Definition chromaprint.c:37
@ FINGERPRINT_RAW
Definition chromaprint.c:35
static void deinit(AVFormatContext *s)
Definition chromaprint.c:53
static int write_packet(AVFormatContext *s, AVPacket *pkt)
static const AVClass chromaprint_class
static AVMutex chromaprint_mutex
Definition chromaprint.c:32
static int write_trailer(AVFormatContext *s)
#define OFFSET(x)
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:204
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_CODEC_ID_PCM_S16LE
Definition codec_id.h:330
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_PCM_S16BE
Definition codec_id.h:331
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define AV_MUTEX_INITIALIZER
Definition thread.h:185
static int ff_mutex_unlock(AVMutex *mutex)
Definition thread.h:189
static int ff_mutex_lock(AVMutex *mutex)
Definition thread.h:188
#define AVMutex
Definition thread.h:184
#define AV_NE(be, le)
Definition macros.h:33
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition mux.h:50
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition mux.h:59
AVOptions.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
ChromaprintContext * ctx
Definition chromaprint.c:47
#define av_log(a,...)
int size
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101