FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/opt.h"
24 #include "libavcodec/internal.h"
25 #include <chromaprint.h>
26 
27 #define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
28  CHROMAPRINT_VERSION_MINOR, \
29  CHROMAPRINT_VERSION_PATCH)
30 
31 typedef enum FingerprintFormat {
36 
37 typedef struct ChromaprintMuxContext {
38  const AVClass *class;
40  int algorithm;
42  ChromaprintContext ctx;
44 
45 static void cleanup(ChromaprintMuxContext *cpr)
46 {
47  if (cpr->ctx) {
49  chromaprint_free(cpr->ctx);
51  }
52 }
53 
55 {
57  AVStream *st;
58 
60  cpr->ctx = chromaprint_new(cpr->algorithm);
62 
63  if (!cpr->ctx) {
64  av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
65  return AVERROR(ENOMEM);
66  }
67 
68  if (cpr->silence_threshold != -1) {
69 #if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
70  if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
71  av_log(s, AV_LOG_ERROR, "Failed to set silence threshold.\n");
72  goto fail;
73  }
74 #else
75  av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
76  "version 0.7.0 or later.\n");
77  goto fail;
78 #endif
79  }
80 
81  if (s->nb_streams != 1) {
82  av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
83  goto fail;
84  }
85 
86  st = s->streams[0];
87 
88  if (st->codecpar->channels > 2) {
89  av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
90  goto fail;
91  }
92 
93  if (st->codecpar->sample_rate < 1000) {
94  av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
95  goto fail;
96  }
97 
98  if (!chromaprint_start(cpr->ctx, st->codecpar->sample_rate, st->codecpar->channels)) {
99  av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
100  goto fail;
101  }
102 
103  return 0;
104 fail:
105  cleanup(cpr);
106  return AVERROR(EINVAL);
107 }
108 
110 {
112  return chromaprint_feed(cpr->ctx, pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
113 }
114 
116 {
118  AVIOContext *pb = s->pb;
119  void *fp = NULL, *enc_fp = NULL;
120  int size, enc_size, ret = AVERROR(EINVAL);
121 
122  if (!chromaprint_finish(cpr->ctx)) {
123  av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
124  goto fail;
125  }
126 
127  if (!chromaprint_get_raw_fingerprint(cpr->ctx, &fp, &size)) {
128  av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
129  goto fail;
130  }
131 
132  switch (cpr->fp_format) {
133  case FINGERPRINT_RAW:
134  avio_write(pb, fp, size);
135  break;
137  case FINGERPRINT_BASE64:
138  if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
139  cpr->fp_format == FINGERPRINT_BASE64)) {
140  av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
141  goto fail;
142  }
143  avio_write(pb, enc_fp, enc_size);
144  break;
145  }
146 
147  ret = 0;
148 fail:
149  if (fp)
150  chromaprint_dealloc(fp);
151  if (enc_fp)
152  chromaprint_dealloc(enc_fp);
153  cleanup(cpr);
154  return ret;
155 }
156 
157 #define OFFSET(x) offsetof(ChromaprintMuxContext, x)
158 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
159 static const AVOption options[] = {
160  { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
161  { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
162  { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS },
163  { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
164  { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
165  { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
166  { NULL },
167 };
168 
169 static const AVClass chromaprint_class = {
170  .class_name = "chromaprint muxer",
171  .item_name = av_default_item_name,
172  .option = options,
173  .version = LIBAVUTIL_VERSION_INT,
174 };
175 
177  .name = "chromaprint",
178  .long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
179  .priv_data_size = sizeof(ChromaprintMuxContext),
181  .write_header = write_header,
182  .write_packet = write_packet,
183  .write_trailer = write_trailer,
184  .flags = AVFMT_NOTIMESTAMPS,
185  .priv_class = &chromaprint_class,
186 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:147
int avpriv_unlock_avformat(void)
Definition: utils.c:3824
AVOption.
Definition: opt.h:245
#define LIBAVUTIL_VERSION_INT
Definition: version.h:70
int size
Definition: avcodec.h:1581
ChromaprintContext ctx
Definition: chromaprint.c:42
AVOutputFormat ff_chromaprint_muxer
Definition: chromaprint.c:176
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1325
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
static int write_header(AVFormatContext *s)
Definition: chromaprint.c:54
static int write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: chromaprint.c:109
AVOptions.
#define AV_NE(be, le)
Definition: common.h:50
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1393
uint8_t * data
Definition: avcodec.h:1580
ptrdiff_t size
Definition: opengl_enc.c:101
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:204
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
int avpriv_lock_avformat(void)
Definition: utils.c:3815
#define fail()
Definition: checkasm.h:81
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1381
const char * name
Definition: avformat.h:522
Stream structure.
Definition: avformat.h:876
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:484
AVIOContext * pb
I/O context.
Definition: avformat.h:1367
static void cleanup(ChromaprintMuxContext *cpr)
Definition: chromaprint.c:45
#define FLAGS
Definition: chromaprint.c:158
#define fp
Definition: regdef.h:44
static const AVClass chromaprint_class
Definition: chromaprint.c:169
Describe the class of an AVClass context structure.
Definition: log.h:67
#define OFFSET(x)
Definition: chromaprint.c:157
static int write_trailer(AVFormatContext *s)
Definition: chromaprint.c:115
int sample_rate
Audio only.
Definition: avcodec.h:4032
Main libavformat public API header.
common internal api header.
void * priv_data
Format private data.
Definition: avformat.h:1353
FingerprintFormat fp_format
Definition: chromaprint.c:41
int channels
Audio only.
Definition: avcodec.h:4028
static const AVOption options[]
Definition: chromaprint.c:159
AVCodecParameters * codecpar
Definition: avformat.h:1006
FingerprintFormat
Definition: chromaprint.c:31
This structure stores compressed data.
Definition: avcodec.h:1557