FFmpeg
chromaprint.c
Go to the documentation of this file.
1 /*
2  * Chromaprint fingerprinting muxer
3  * Copyright (c) 2015 Rodger Combs
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 {
37 
38 typedef struct ChromaprintMuxContext {
39  const AVClass *class;
41  int algorithm;
43 #if CPR_VERSION_INT >= AV_VERSION_INT(1, 4, 0)
44  ChromaprintContext *ctx;
45 #else
46  ChromaprintContext ctx;
47 #endif
49 
50 static void cleanup(ChromaprintMuxContext *cpr)
51 {
52  if (cpr->ctx) {
54  chromaprint_free(cpr->ctx);
56  }
57 }
58 
60 {
61  ChromaprintMuxContext *cpr = s->priv_data;
62  AVStream *st;
63 
65  cpr->ctx = chromaprint_new(cpr->algorithm);
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.\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 
115 {
116  ChromaprintMuxContext *cpr = s->priv_data;
117  return chromaprint_feed(cpr->ctx, pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
118 }
119 
121 {
122  ChromaprintMuxContext *cpr = s->priv_data;
123  AVIOContext *pb = s->pb;
124  void *fp = NULL, *enc_fp = NULL;
125  int size, enc_size, ret = AVERROR(EINVAL);
126 
127  if (!chromaprint_finish(cpr->ctx)) {
128  av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
129  goto fail;
130  }
131 
132  if (!chromaprint_get_raw_fingerprint(cpr->ctx, &fp, &size)) {
133  av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
134  goto fail;
135  }
136 
137  switch (cpr->fp_format) {
138  case FINGERPRINT_RAW:
139  avio_write(pb, fp, size);
140  break;
142  case FINGERPRINT_BASE64:
143  if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
144  cpr->fp_format == FINGERPRINT_BASE64)) {
145  av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
146  goto fail;
147  }
148  avio_write(pb, enc_fp, enc_size);
149  break;
150  }
151 
152  ret = 0;
153 fail:
154  if (fp)
155  chromaprint_dealloc(fp);
156  if (enc_fp)
157  chromaprint_dealloc(enc_fp);
158  cleanup(cpr);
159  return ret;
160 }
161 
162 #define OFFSET(x) offsetof(ChromaprintMuxContext, x)
163 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
164 static const AVOption options[] = {
165  { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
166  { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
167  { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS },
168  { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
169  { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
170  { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
171  { NULL },
172 };
173 
174 static const AVClass chromaprint_class = {
175  .class_name = "chromaprint muxer",
176  .item_name = av_default_item_name,
177  .option = options,
178  .version = LIBAVUTIL_VERSION_INT,
179 };
180 
182  .name = "chromaprint",
183  .long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
184  .priv_data_size = sizeof(ChromaprintMuxContext),
186  .write_header = write_header,
187  .write_packet = write_packet,
188  .write_trailer = write_trailer,
189  .flags = AVFMT_NOTIMESTAMPS,
190  .priv_class = &chromaprint_class,
191 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: avcodec.h:463
ChromaprintMuxContext::algorithm
int algorithm
Definition: chromaprint.c:41
AVOutputFormat::name
const char * name
Definition: avformat.h:496
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:467
write_header
static int write_header(AVFormatContext *s)
Definition: chromaprint.c:59
FingerprintFormat
FingerprintFormat
Definition: chromaprint.c:32
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
ff_unlock_avformat
int ff_unlock_avformat(void)
Definition: utils.c:88
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: avcodec.h:464
fail
#define fail()
Definition: checkasm.h:120
options
static const AVOption options[]
Definition: chromaprint.c:164
ChromaprintMuxContext::ctx
ChromaprintContext * ctx
Definition: chromaprint.c:44
write_trailer
static int write_trailer(AVFormatContext *s)
Definition: chromaprint.c:120
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
s
#define s(width, name)
Definition: cbs_vp9.c:257
ff_chromaprint_muxer
AVOutputFormat ff_chromaprint_muxer
Definition: chromaprint.c:181
ChromaprintMuxContext::silence_threshold
int silence_threshold
Definition: chromaprint.c:40
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
FLAGS
#define FLAGS
Definition: chromaprint.c:163
FINGERPRINT_BASE64
@ FINGERPRINT_BASE64
Definition: chromaprint.c:35
fp
#define fp
Definition: regdef.h:44
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:4067
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
size
int size
Definition: twinvq_data.h:11134
ff_lock_avformat
int ff_lock_avformat(void)
Definition: utils.c:83
AV_NE
#define AV_NE(be, le)
Definition: common.h:50
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:218
FINGERPRINT_RAW
@ FINGERPRINT_RAW
Definition: chromaprint.c:33
AVOutputFormat
Definition: avformat.h:495
chromaprint_class
static const AVClass chromaprint_class
Definition: chromaprint.c:174
write_packet
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: chromaprint.c:114
OFFSET
#define OFFSET(x)
Definition: chromaprint.c:162
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:870
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
avformat.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:223
cleanup
static void cleanup(ChromaprintMuxContext *cpr)
Definition: chromaprint.c:50
FINGERPRINT_COMPRESSED
@ FINGERPRINT_COMPRESSED
Definition: chromaprint.c:34
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
ChromaprintMuxContext
Definition: chromaprint.c:38
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ChromaprintMuxContext::fp_format
FingerprintFormat fp_format
Definition: chromaprint.c:42
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:232