FFmpeg
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 
34 typedef enum FingerprintFormat {
39 
40 typedef struct ChromaprintMuxContext {
41  const AVClass *class;
43  int algorithm;
45 #if CPR_VERSION_INT >= AV_VERSION_INT(1, 4, 0)
46  ChromaprintContext *ctx;
47 #else
48  ChromaprintContext ctx;
49 #endif
51 
52 static void deinit(AVFormatContext *s)
53 {
54  ChromaprintMuxContext *const cpr = s->priv_data;
55 
56  if (cpr->ctx) {
58  chromaprint_free(cpr->ctx);
60  }
61 }
62 
64 {
65  ChromaprintMuxContext *cpr = s->priv_data;
66  AVStream *st;
67 
69  cpr->ctx = chromaprint_new(cpr->algorithm);
71 
72  if (!cpr->ctx) {
73  av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
74  return AVERROR_EXTERNAL;
75  }
76 
77  if (cpr->silence_threshold != -1) {
78 #if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
79  if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
80  av_log(s, AV_LOG_ERROR, "Failed to set silence threshold. Setting silence_threshold requires -algorithm 3 option.\n");
81  return AVERROR_EXTERNAL;
82  }
83 #else
84  av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
85  "version 0.7.0 or later.\n");
86  return AVERROR(ENOSYS);
87 #endif
88  }
89 
90  st = s->streams[0];
91 
92  if (st->codecpar->ch_layout.nb_channels > 2) {
93  av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
94  return AVERROR(EINVAL);
95  }
96 
97  if (st->codecpar->sample_rate < 1000) {
98  av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
99  return AVERROR(EINVAL);
100  }
101 
102  if (!chromaprint_start(cpr->ctx, st->codecpar->sample_rate, st->codecpar->ch_layout.nb_channels)) {
103  av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
104  return AVERROR_EXTERNAL;
105  }
106 
107  return 0;
108 }
109 
111 {
112  ChromaprintMuxContext *cpr = s->priv_data;
113  return chromaprint_feed(cpr->ctx, (const int16_t *)pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
114 }
115 
117 {
118  ChromaprintMuxContext *cpr = s->priv_data;
119  AVIOContext *pb = s->pb;
120  void *fp = NULL;
121  char *enc_fp = NULL;
122  int size, enc_size, ret = AVERROR_EXTERNAL;
123 
124  if (!chromaprint_finish(cpr->ctx)) {
125  av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
126  goto fail;
127  }
128 
129  if (!chromaprint_get_raw_fingerprint(cpr->ctx, (uint32_t **)&fp, &size)) {
130  av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
131  goto fail;
132  }
133 
134  switch (cpr->fp_format) {
135  case FINGERPRINT_RAW:
136  avio_write(pb, fp, size * 4); //fp points to array of uint32_t
137  break;
139  case FINGERPRINT_BASE64:
140  if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
141  cpr->fp_format == FINGERPRINT_BASE64)) {
142  av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
143  goto fail;
144  }
145  avio_write(pb, enc_fp, enc_size);
146  break;
147  }
148 
149  ret = 0;
150 fail:
151  if (fp)
152  chromaprint_dealloc(fp);
153  if (enc_fp)
154  chromaprint_dealloc(enc_fp);
155  return ret;
156 }
157 
158 #define OFFSET(x) offsetof(ChromaprintMuxContext, x)
159 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
160 static const AVOption options[] = {
161  { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
162  { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
163  { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS, .unit = "fp_format" },
164  { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, .unit = "fp_format"},
165  { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, .unit = "fp_format"},
166  { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, .unit = "fp_format"},
167  { NULL },
168 };
169 
170 static const AVClass chromaprint_class = {
171  .class_name = "chromaprint muxer",
172  .item_name = av_default_item_name,
173  .option = options,
174  .version = LIBAVUTIL_VERSION_INT,
175 };
176 
178  .p.name = "chromaprint",
179  .p.long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
180  .priv_data_size = sizeof(ChromaprintMuxContext),
182  .p.video_codec = AV_CODEC_ID_NONE,
183  .p.subtitle_codec = AV_CODEC_ID_NONE,
184  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
186  .init = init,
187  .write_packet = write_packet,
188  .write_trailer = write_trailer,
189  .deinit = deinit,
190  .p.flags = AVFMT_NOTIMESTAMPS,
191  .p.priv_class = &chromaprint_class,
192 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:328
ChromaprintMuxContext::algorithm
int algorithm
Definition: chromaprint.c:43
AVOutputFormat::name
const char * name
Definition: avformat.h:510
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
thread.h
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
init
static av_cold int init(AVFormatContext *s)
Definition: chromaprint.c:63
FingerprintFormat
FingerprintFormat
Definition: chromaprint.c:34
deinit
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:52
AVPacket::data
uint8_t * data
Definition: packet.h:524
AVOption
AVOption.
Definition: opt.h:357
ff_chromaprint_muxer
const FFOutputFormat ff_chromaprint_muxer
Definition: chromaprint.c:177
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#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
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
ff_mutex_unlock
static int ff_mutex_unlock(AVMutex *mutex)
Definition: thread.h:189
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:329
fail
#define fail()
Definition: checkasm.h:182
options
static const AVOption options[]
Definition: chromaprint.c:160
ChromaprintMuxContext::ctx
ChromaprintContext * ctx
Definition: chromaprint.c:46
write_trailer
static int write_trailer(AVFormatContext *s)
Definition: chromaprint.c:116
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AVMutex
#define AVMutex
Definition: thread.h:184
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_NE
#define AV_NE(be, le)
Definition: macros.h:33
ChromaprintMuxContext::silence_threshold
int silence_threshold
Definition: chromaprint.c:42
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:237
FLAGS
#define FLAGS
Definition: chromaprint.c:159
FINGERPRINT_BASE64
@ FINGERPRINT_BASE64
Definition: chromaprint.c:37
FFOutputFormat
Definition: mux.h:61
fp
#define fp
Definition: regdef.h:44
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:525
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:94
AV_MUTEX_INITIALIZER
#define AV_MUTEX_INITIALIZER
Definition: thread.h:185
size
int size
Definition: twinvq_data.h:10344
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:201
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
ff_mutex_lock
static int ff_mutex_lock(AVMutex *mutex)
Definition: thread.h:188
FINGERPRINT_RAW
@ FINGERPRINT_RAW
Definition: chromaprint.c:35
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
chromaprint_class
static const AVClass chromaprint_class
Definition: chromaprint.c:170
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#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
write_packet
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: chromaprint.c:110
OFFSET
#define OFFSET(x)
Definition: chromaprint.c:158
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
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:71
avformat.h
chromaprint_mutex
static AVMutex chromaprint_mutex
Definition: chromaprint.c:32
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:245
FINGERPRINT_COMPRESSED
@ FINGERPRINT_COMPRESSED
Definition: chromaprint.c:36
AVPacket
This structure stores compressed data.
Definition: packet.h:501
ChromaprintMuxContext
Definition: chromaprint.c:40
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ChromaprintMuxContext::fp_format
FingerprintFormat fp_format
Definition: chromaprint.c:44
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:254
mux.h