FFmpeg  4.4.9
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 "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 deinit(AVFormatContext *s)
51 {
52  ChromaprintMuxContext *const cpr = s->priv_data;
53 
54  if (cpr->ctx) {
56  chromaprint_free(cpr->ctx);
58  }
59 }
60 
62 {
63  ChromaprintMuxContext *cpr = s->priv_data;
64  AVStream *st;
65 
67  cpr->ctx = chromaprint_new(cpr->algorithm);
69 
70  if (!cpr->ctx) {
71  av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
72  return AVERROR(ENOMEM);
73  }
74 
75  if (cpr->silence_threshold != -1) {
76 #if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
77  if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
78  av_log(s, AV_LOG_ERROR, "Failed to set silence threshold. Setting silence_threshold requires -algorithm 3 option.\n");
79  goto fail;
80  }
81 #else
82  av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
83  "version 0.7.0 or later.\n");
84  goto fail;
85 #endif
86  }
87 
88  if (s->nb_streams != 1) {
89  av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
90  goto fail;
91  }
92 
93  st = s->streams[0];
94 
95  if (st->codecpar->channels > 2) {
96  av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
97  goto fail;
98  }
99 
100  if (st->codecpar->sample_rate < 1000) {
101  av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
102  goto fail;
103  }
104 
105  if (!chromaprint_start(cpr->ctx, st->codecpar->sample_rate, st->codecpar->channels)) {
106  av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
107  goto fail;
108  }
109 
110  return 0;
111 fail:
112  return AVERROR(EINVAL);
113 }
114 
116 {
117  ChromaprintMuxContext *cpr = s->priv_data;
118  return chromaprint_feed(cpr->ctx, (const int16_t *)pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
119 }
120 
122 {
123  ChromaprintMuxContext *cpr = s->priv_data;
124  AVIOContext *pb = s->pb;
125  void *fp = NULL;
126  char *enc_fp = NULL;
127  int size, enc_size, ret = AVERROR(EINVAL);
128 
129  if (!chromaprint_finish(cpr->ctx)) {
130  av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
131  goto fail;
132  }
133 
134  if (!chromaprint_get_raw_fingerprint(cpr->ctx, (uint32_t **)&fp, &size)) {
135  av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
136  goto fail;
137  }
138 
139  switch (cpr->fp_format) {
140  case FINGERPRINT_RAW:
141  avio_write(pb, fp, size * 4); //fp points to array of uint32_t
142  break;
144  case FINGERPRINT_BASE64:
145  if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
146  cpr->fp_format == FINGERPRINT_BASE64)) {
147  av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
148  goto fail;
149  }
150  avio_write(pb, enc_fp, enc_size);
151  break;
152  }
153 
154  ret = 0;
155 fail:
156  if (fp)
157  chromaprint_dealloc(fp);
158  if (enc_fp)
159  chromaprint_dealloc(enc_fp);
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 
183  .name = "chromaprint",
184  .long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
185  .priv_data_size = sizeof(ChromaprintMuxContext),
187  .write_header = write_header,
188  .write_packet = write_packet,
189  .write_trailer = write_trailer,
190  .deinit = deinit,
191  .flags = AVFMT_NOTIMESTAMPS,
192  .priv_class = &chromaprint_class,
193 };
Main libavformat public API header.
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:230
#define s(width, name)
Definition: cbs_vp9.c:257
#define fail()
Definition: checkasm.h:133
FingerprintFormat
Definition: chromaprint.c:32
@ FINGERPRINT_COMPRESSED
Definition: chromaprint.c:34
@ FINGERPRINT_BASE64
Definition: chromaprint.c:35
@ FINGERPRINT_RAW
Definition: chromaprint.c:33
static void deinit(AVFormatContext *s)
Definition: chromaprint.c:50
AVOutputFormat ff_chromaprint_muxer
Definition: chromaprint.c:182
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: chromaprint.c:115
static const AVOption options[]
Definition: chromaprint.c:165
static const AVClass chromaprint_class
Definition: chromaprint.c:175
#define FLAGS
Definition: chromaprint.c:164
static int write_header(AVFormatContext *s)
Definition: chromaprint.c:61
static int write_trailer(AVFormatContext *s)
Definition: chromaprint.c:121
#define OFFSET(x)
Definition: chromaprint.c:163
#define AV_NE(be, le)
Definition: common.h:50
#define NULL
Definition: coverity.c:32
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:314
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
common internal api header.
int ff_unlock_avformat(void)
Definition: utils.c:84
int ff_lock_avformat(void)
Definition: utils.c:79
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVOptions.
#define fp
Definition: regdef.h:44
Describe the class of an AVClass context structure.
Definition: log.h:67
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
int channels
Audio only.
Definition: codec_par.h:166
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
Bytestream IO Context.
Definition: avio.h:161
AVOption.
Definition: opt.h:248
const char * name
Definition: avformat.h:491
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
FingerprintFormat fp_format
Definition: chromaprint.c:42
ChromaprintContext ctx
Definition: chromaprint.c:46
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
int size