Merging gst-plugins-bad
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / chromaprint / gstchromaprint.c
1 /* GStreamer chromaprint audio fingerprinting element
2  * Copyright (C) 2006 M. Derezynski
3  * Copyright (C) 2008 Eric Buehl
4  * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
5  * Copyright (C) 2011 Lukáš Lalinský <lalinsky@gmail.com>
6  * Copyright (C) 2012 Collabora Ltd. <tim.muller@collabora.co.uk>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 /**
25  * SECTION:element-chromaprint
26  * @title: chromaprint
27  *
28  * The chromaprint element calculates an acoustic fingerprint for an
29  * audio stream which can be used to identify a song and look up
30  * further metadata from the [Acoustid](http://acoustid.org/) and Musicbrainz
31  * databases.
32  *
33  * ## Example launch line
34  * |[
35  * gst-launch-1.0 -m uridecodebin uri=file:///path/to/song.ogg ! audioconvert ! chromaprint ! fakesink
36  * ]|
37  *
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "gstchromaprint.h"
45
46 #define DEFAULT_MAX_DURATION 120
47
48 #define PAD_CAPS \
49         "audio/x-raw, " \
50         "format = (string) " GST_AUDIO_NE(S16) ", "\
51         "rate = (int) [ 1, MAX ], " \
52         "channels = (int) [ 1, 2 ]"
53
54 GST_DEBUG_CATEGORY_STATIC (gst_chromaprint_debug);
55 #define GST_CAT_DEFAULT gst_chromaprint_debug
56
57 enum
58 {
59   PROP_0,
60   PROP_FINGERPRINT,
61   PROP_MAX_DURATION
62 };
63
64 static gboolean chromaprint_element_init (GstPlugin * plugin);
65
66 #define parent_class gst_chromaprint_parent_class
67 G_DEFINE_TYPE (GstChromaprint, gst_chromaprint, GST_TYPE_AUDIO_FILTER);
68 GST_ELEMENT_REGISTER_DEFINE_CUSTOM (chromaprint, chromaprint_element_init);
69
70 static void gst_chromaprint_finalize (GObject * object);
71 static void gst_chromaprint_set_property (GObject * object, guint prop_id,
72     const GValue * value, GParamSpec * pspec);
73 static void gst_chromaprint_get_property (GObject * object, guint prop_id,
74     GValue * value, GParamSpec * pspec);
75 static GstFlowReturn gst_chromaprint_transform_ip (GstBaseTransform * trans,
76     GstBuffer * buf);
77 static gboolean gst_chromaprint_sink_event (GstBaseTransform * trans,
78     GstEvent * event);
79
80 static void
81 gst_chromaprint_class_init (GstChromaprintClass * klass)
82 {
83   GObjectClass *gobject_class;
84   GstBaseTransformClass *gstbasetrans_class;
85   GstCaps *caps;
86
87   gobject_class = G_OBJECT_CLASS (klass);
88   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
89
90   gobject_class->set_property = gst_chromaprint_set_property;
91   gobject_class->get_property = gst_chromaprint_get_property;
92
93   g_object_class_install_property (gobject_class, PROP_FINGERPRINT,
94       g_param_spec_string ("fingerprint", "Resulting fingerprint",
95           "Resulting fingerprint", NULL, G_PARAM_READABLE));
96
97   g_object_class_install_property (gobject_class, PROP_MAX_DURATION,
98       g_param_spec_uint ("duration", "Duration limit",
99           "Number of seconds of audio to use for fingerprinting",
100           0, G_MAXUINT, DEFAULT_MAX_DURATION,
101           G_PARAM_READABLE | G_PARAM_WRITABLE));
102
103   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_chromaprint_finalize);
104
105   gstbasetrans_class->transform_ip =
106       GST_DEBUG_FUNCPTR (gst_chromaprint_transform_ip);
107   gstbasetrans_class->sink_event =
108       GST_DEBUG_FUNCPTR (gst_chromaprint_sink_event);
109   gstbasetrans_class->passthrough_on_same_caps = TRUE;
110
111   gst_element_class_set_static_metadata (GST_ELEMENT_CLASS (klass),
112       "Chromaprint fingerprinting element",
113       "Filter/Analyzer/Audio",
114       "Find an audio fingerprint using the Chromaprint library",
115       "Lukáš Lalinský <lalinsky@gmail.com>");
116
117   caps = gst_caps_from_string (PAD_CAPS);
118   gst_audio_filter_class_add_pad_templates (GST_AUDIO_FILTER_CLASS (klass),
119       caps);
120   gst_caps_unref (caps);
121 }
122
123 static void
124 gst_chromaprint_reset (GstChromaprint * chromaprint)
125 {
126   if (chromaprint->fingerprint) {
127     chromaprint_dealloc (chromaprint->fingerprint);
128     chromaprint->fingerprint = NULL;
129   }
130
131   chromaprint->nsamples = 0;
132   chromaprint->duration = 0;
133   chromaprint->record = TRUE;
134 }
135
136 static void
137 gst_chromaprint_create_fingerprint (GstChromaprint * chromaprint)
138 {
139   GstTagList *tags;
140
141   if (chromaprint->duration <= 3)
142     return;
143
144   GST_DEBUG_OBJECT (chromaprint,
145       "Generating fingerprint based on %d seconds of audio",
146       chromaprint->duration);
147
148   chromaprint_finish (chromaprint->context);
149   chromaprint_get_fingerprint (chromaprint->context, &chromaprint->fingerprint);
150   chromaprint->record = FALSE;
151
152   g_object_notify ((GObject *) chromaprint, "fingerprint");
153
154   tags = gst_tag_list_new (GST_TAG_CHROMAPRINT_FINGERPRINT,
155       chromaprint->fingerprint, NULL);
156
157   gst_pad_push_event (GST_BASE_TRANSFORM_SRC_PAD (chromaprint),
158       gst_event_new_tag (tags));
159 }
160
161 static void
162 gst_chromaprint_init (GstChromaprint * chromaprint)
163 {
164   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (chromaprint), TRUE);
165
166   chromaprint->context = chromaprint_new (CHROMAPRINT_ALGORITHM_DEFAULT);
167   chromaprint->fingerprint = NULL;
168   chromaprint->max_duration = DEFAULT_MAX_DURATION;
169   gst_chromaprint_reset (chromaprint);
170 }
171
172 static void
173 gst_chromaprint_finalize (GObject * object)
174 {
175   GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
176
177   chromaprint->record = FALSE;
178
179   if (chromaprint->context) {
180     chromaprint_free (chromaprint->context);
181     chromaprint->context = NULL;
182   }
183
184   if (chromaprint->fingerprint) {
185     chromaprint_dealloc (chromaprint->fingerprint);
186     chromaprint->fingerprint = NULL;
187   }
188
189   G_OBJECT_CLASS (parent_class)->finalize (object);
190 }
191
192 static GstFlowReturn
193 gst_chromaprint_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
194 {
195   GstChromaprint *chromaprint = GST_CHROMAPRINT (trans);
196   GstAudioFilter *filter = GST_AUDIO_FILTER (trans);
197   GstMapInfo map_info;
198   guint nsamples;
199   gint rate, channels;
200
201   rate = GST_AUDIO_INFO_RATE (&filter->info);
202   channels = GST_AUDIO_INFO_CHANNELS (&filter->info);
203
204   if (G_UNLIKELY (rate <= 0 || channels <= 0))
205     return GST_FLOW_NOT_NEGOTIATED;
206
207   if (!chromaprint->record)
208     return GST_FLOW_OK;
209
210   if (!gst_buffer_map (buf, &map_info, GST_MAP_READ))
211     return GST_FLOW_ERROR;
212
213   nsamples = map_info.size / (channels * 2);
214
215   if (nsamples == 0)
216     goto end;
217
218   if (chromaprint->nsamples == 0) {
219     chromaprint_start (chromaprint->context, rate, channels);
220   }
221   chromaprint->nsamples += nsamples;
222   chromaprint->duration = chromaprint->nsamples / rate;
223
224   chromaprint_feed (chromaprint->context, (gint16 *) map_info.data,
225       map_info.size / sizeof (guint16));
226
227   if (chromaprint->duration >= chromaprint->max_duration
228       && !chromaprint->fingerprint) {
229     gst_chromaprint_create_fingerprint (chromaprint);
230   }
231
232 end:
233   gst_buffer_unmap (buf, &map_info);
234
235   return GST_FLOW_OK;
236 }
237
238 static gboolean
239 gst_chromaprint_sink_event (GstBaseTransform * trans, GstEvent * event)
240 {
241   GstChromaprint *chromaprint = GST_CHROMAPRINT (trans);
242
243   switch (GST_EVENT_TYPE (event)) {
244     case GST_EVENT_FLUSH_STOP:
245     case GST_EVENT_SEGMENT:
246       GST_DEBUG_OBJECT (trans, "Got %s event, clearing buffer",
247           GST_EVENT_TYPE_NAME (event));
248       gst_chromaprint_reset (chromaprint);
249       break;
250     case GST_EVENT_EOS:
251       if (!chromaprint->fingerprint) {
252         gst_chromaprint_create_fingerprint (chromaprint);
253       }
254       break;
255     default:
256       break;
257   }
258
259   return GST_BASE_TRANSFORM_CLASS (parent_class)->sink_event (trans, event);
260 }
261
262 static void
263 gst_chromaprint_set_property (GObject * object, guint prop_id,
264     const GValue * value, GParamSpec * pspec)
265 {
266   GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
267
268   switch (prop_id) {
269     case PROP_MAX_DURATION:
270       chromaprint->max_duration = g_value_get_uint (value);
271       break;
272     default:
273       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274       break;
275   }
276 }
277
278 static void
279 gst_chromaprint_get_property (GObject * object, guint prop_id,
280     GValue * value, GParamSpec * pspec)
281 {
282   GstChromaprint *chromaprint = GST_CHROMAPRINT (object);
283
284   switch (prop_id) {
285     case PROP_FINGERPRINT:
286       g_value_set_string (value, chromaprint->fingerprint);
287       break;
288     case PROP_MAX_DURATION:
289       g_value_set_uint (value, chromaprint->max_duration);
290       break;
291     default:
292       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293       break;
294   }
295 }
296
297 static gboolean
298 chromaprint_element_init (GstPlugin * plugin)
299 {
300   gboolean ret;
301
302   GST_DEBUG_CATEGORY_INIT (gst_chromaprint_debug, "chromaprint",
303       0, "chromaprint element");
304
305   GST_INFO ("libchromaprint %s", chromaprint_get_version ());
306
307   ret = gst_element_register (plugin, "chromaprint", GST_RANK_NONE,
308       GST_TYPE_CHROMAPRINT);
309
310   if (ret) {
311     gst_tag_register (GST_TAG_CHROMAPRINT_FINGERPRINT, GST_TAG_FLAG_META,
312         G_TYPE_STRING, "chromaprint fingerprint", "Chromaprint fingerprint",
313         NULL);
314   }
315
316   return ret;
317 }
318
319 static gboolean
320 plugin_init (GstPlugin * plugin)
321 {
322   return GST_ELEMENT_REGISTER (chromaprint, plugin);
323 }
324
325 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
326     GST_VERSION_MINOR,
327     chromaprint,
328     "Calculate Chromaprint fingerprint from audio files",
329     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)