Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / ext / ofa / gstofa.c
1 /* GStreamer
2  *
3  * gstofa.c
4  * 
5  * Copyright (C) 2006 M. Derezynski
6  * Copyright (C) 2008 Eric Buehl
7  * Copyright (C) 2008 Sebastian Dröge <slomo@circular-chaos.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA  02110-1301 USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <gst/gst.h>
30 #include <ofa1/ofa.h>
31 #include "gstofa.h"
32
33 #define PAD_CAPS \
34         "audio/x-raw-int, " \
35         "rate = (int) [ 1, MAX ], " \
36         "channels = (int) [ 1, 2 ], " \
37         "endianness = (int) { LITTLE_ENDIAN, BIG_ENDIAN }, " \
38         "width = (int) { 16 }, " \
39         "depth = (int) { 16 }, " \
40         "signed = (boolean) true"
41
42 GST_DEBUG_CATEGORY_STATIC (gst_ofa_debug);
43 #define GST_CAT_DEFAULT gst_ofa_debug
44
45 enum
46 {
47   PROP_0,
48   PROP_FINGERPRINT,
49 };
50
51
52 GST_BOILERPLATE (GstOFA, gst_ofa, GstAudioFilter, GST_TYPE_AUDIO_FILTER);
53
54 static void gst_ofa_finalize (GObject * object);
55 static void gst_ofa_get_property (GObject * object, guint prop_id,
56     GValue * value, GParamSpec * pspec);
57 static GstFlowReturn gst_ofa_transform_ip (GstBaseTransform * trans,
58     GstBuffer * buf);
59 static gboolean gst_ofa_event (GstBaseTransform * trans, GstEvent * event);
60
61 static void
62 gst_ofa_base_init (gpointer g_class)
63 {
64   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
65   GstAudioFilterClass *audio_filter_class = (GstAudioFilterClass *) g_class;
66   GstCaps *caps;
67
68   gst_element_class_set_details_simple (gstelement_class, "OFA",
69       "MusicIP Fingerprinting element",
70       "Find a music fingerprint using MusicIP's libofa",
71       "Milosz Derezynski <internalerror@gmail.com>, Eric Buehl <eric.buehl@gmail.com>");
72
73   caps = gst_caps_from_string (PAD_CAPS);
74   gst_audio_filter_class_add_pad_templates (audio_filter_class, caps);
75   gst_caps_unref (caps);
76 }
77
78 static void
79 gst_ofa_finalize (GObject * object)
80 {
81   GstOFA *ofa = GST_OFA (object);
82
83   if (ofa->adapter) {
84     g_object_unref (ofa->adapter);
85     ofa->adapter = NULL;
86   }
87
88   g_free (ofa->fingerprint);
89   ofa->fingerprint = NULL;
90
91   G_OBJECT_CLASS (parent_class)->finalize (object);
92 }
93
94 static void
95 gst_ofa_class_init (GstOFAClass * klass)
96 {
97   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98   GstBaseTransformClass *gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (klass);
99
100   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_ofa_get_property);
101
102   g_object_class_install_property (gobject_class, PROP_FINGERPRINT,
103       g_param_spec_string ("fingerprint", "Resulting fingerprint",
104           "Resulting fingerprint", NULL,
105           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
106
107   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_ofa_finalize);
108
109   gstbasetrans_class->transform_ip = GST_DEBUG_FUNCPTR (gst_ofa_transform_ip);
110   gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_ofa_event);
111   gstbasetrans_class->passthrough_on_same_caps = TRUE;
112 }
113
114 static void
115 create_fingerprint (GstOFA * ofa)
116 {
117   GstBuffer *buf;
118   GstAudioFilter *ofa_filter = GST_AUDIO_FILTER (ofa);
119   gint rate = ofa_filter->format.rate;
120   gint channels = ofa_filter->format.channels;
121   gint endianness =
122       ofa_filter->format.bigend ? OFA_BIG_ENDIAN : OFA_LITTLE_ENDIAN;
123   GstTagList *tags;
124   guint available;
125
126   available = gst_adapter_available (ofa->adapter);
127
128   if (available == 0) {
129     GST_WARNING_OBJECT (ofa, "No data to take fingerprint from");
130     ofa->record = FALSE;
131     return;
132   }
133
134   if (GST_AUDIO_FILTER (ofa)->format.bigend)
135     endianness = OFA_BIG_ENDIAN;
136   else
137     endianness = OFA_LITTLE_ENDIAN;
138
139
140   GST_DEBUG_OBJECT (ofa, "Generating fingerprint for %u samples",
141       available / 2);
142
143   buf = gst_adapter_take_buffer (ofa->adapter, available);
144
145   ofa->fingerprint = g_strdup (ofa_create_print (GST_BUFFER_DATA (buf),
146           endianness, GST_BUFFER_SIZE (buf) / 2, rate,
147           (channels == 2) ? 1 : 0));
148
149   if (ofa->fingerprint) {
150     GST_INFO_OBJECT (ofa, "Generated fingerprint: %s", ofa->fingerprint);
151   } else {
152     GST_WARNING_OBJECT (ofa, "Failed to generate fingerprint");
153   }
154
155   gst_buffer_unref (buf);
156
157   if (ofa->fingerprint) {
158     tags = gst_tag_list_new ();
159     gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE,
160         GST_TAG_OFA_FINGERPRINT, ofa->fingerprint, NULL);
161     gst_element_found_tags (GST_ELEMENT (ofa), tags);
162
163     g_object_notify (G_OBJECT (ofa), "fingerprint");
164   }
165
166   ofa->record = FALSE;
167 }
168
169 static gboolean
170 gst_ofa_event (GstBaseTransform * trans, GstEvent * event)
171 {
172   GstOFA *ofa = GST_OFA (trans);
173
174   switch (GST_EVENT_TYPE (event)) {
175     case GST_EVENT_FLUSH_STOP:
176     case GST_EVENT_NEWSEGMENT:
177       GST_DEBUG_OBJECT (ofa, "Got %s event, clearing buffer",
178           GST_EVENT_TYPE_NAME (event));
179       gst_adapter_clear (ofa->adapter);
180       ofa->record = TRUE;
181       g_free (ofa->fingerprint);
182       ofa->fingerprint = NULL;
183       break;
184     case GST_EVENT_EOS:
185       /* we got to the end of the stream but never generated a fingerprint
186        * (probably under 135 seconds)
187        */
188       if (!ofa->fingerprint && ofa->record)
189         create_fingerprint (ofa);
190       break;
191     default:
192       break;
193   }
194
195   return TRUE;
196 }
197
198 static void
199 gst_ofa_init (GstOFA * ofa, GstOFAClass * g_class)
200 {
201   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (ofa), TRUE);
202
203   ofa->fingerprint = NULL;
204   ofa->record = TRUE;
205
206   ofa->adapter = gst_adapter_new ();
207 }
208
209 static GstFlowReturn
210 gst_ofa_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
211 {
212   GstOFA *ofa = GST_OFA (trans);
213   GstAudioFilter *ofa_filter = GST_AUDIO_FILTER (ofa);
214   guint64 nframes;
215   GstClockTime duration;
216   gint rate = ofa_filter->format.rate;
217   gint channels = ofa_filter->format.channels;
218
219   g_return_val_if_fail (rate > 0 && channels > 0, GST_FLOW_NOT_NEGOTIATED);
220
221   if (!ofa->record)
222     return GST_FLOW_OK;
223
224   gst_adapter_push (ofa->adapter, gst_buffer_copy (buf));
225
226   nframes = gst_adapter_available (ofa->adapter) / (channels * 2);
227   duration = GST_FRAMES_TO_CLOCK_TIME (nframes, rate);
228
229   if (duration >= 135 * GST_SECOND && ofa->fingerprint == NULL)
230     create_fingerprint (ofa);
231
232   return GST_FLOW_OK;
233 }
234
235 static void
236 gst_ofa_get_property (GObject * object, guint prop_id, GValue * value,
237     GParamSpec * pspec)
238 {
239   GstOFA *ofa = GST_OFA (object);
240
241   switch (prop_id) {
242     case PROP_FINGERPRINT:
243       g_value_set_string (value, ofa->fingerprint);
244       break;
245     default:
246       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247       break;
248   }
249 }
250
251
252 static gboolean
253 plugin_init (GstPlugin * plugin)
254 {
255   gboolean ret;
256
257   int major, minor, rev;
258
259   GST_DEBUG_CATEGORY_INIT (gst_ofa_debug, "ofa", 0, "ofa element");
260
261   ofa_get_version (&major, &minor, &rev);
262
263   GST_DEBUG ("libofa %d.%d.%d", major, minor, rev);
264
265   ret = gst_element_register (plugin, "ofa", GST_RANK_NONE, GST_TYPE_OFA);
266
267   if (ret) {
268     /* TODO: get this into core */
269     gst_tag_register (GST_TAG_OFA_FINGERPRINT, GST_TAG_FLAG_META,
270         G_TYPE_STRING, "ofa fingerprint", "OFA fingerprint", NULL);
271   }
272
273   return ret;
274 }
275
276 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
277     GST_VERSION_MINOR,
278     "ofa",
279     "Calculate MusicIP fingerprint from audio files",
280     plugin_init, VERSION, "GPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)