2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #define GST_TYPE_MEM_INDEX \
23 (gst_index_get_type ())
24 #define GST_MEM_INDEX(obj) \
25 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MEM_INDEX, GstMemIndex))
26 #define GST_MEM_INDEX_CLASS(klass) \
27 (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MEM_INDEX, GstMemIndexClass))
28 #define GST_IS_MEM_INDEX(obj) \
29 (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MEM_INDEX))
30 #define GST_IS_MEM_INDEX_CLASS(klass) \
31 (GST_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MEM_INDEX))
36 * All entries are simply added to a GList first. Then we build
37 * an index to each entry for each id/format
41 * -----------------------------...
51 * The memindex creates a MemIndexId object for each writer id, a
52 * Hashtable is kept to map the id to the MemIndexId
54 * The MemIndexId keeps a MemIndexFormatIndex for each format the
55 * specific writer wants indexed.
57 * The MemIndexFormatIndex keeps all the values of the particular
58 * format in a GTree, The values of the GTree point back to the entry.
60 * Finding a value for an id/format requires locating the correct GTree,
61 * then do a lookup in the Tree to get the required value.
70 GstMemIndexFormatIndex;
75 GHashTable *format_index;
79 typedef struct _GstMemIndex GstMemIndex;
80 typedef struct _GstMemIndexClass GstMemIndexClass;
91 struct _GstMemIndexClass
93 GstIndexClass parent_class;
96 static void gst_mem_index_finalize (GObject * object);
98 static void gst_mem_index_add_entry (GstIndex * index, GstIndexEntry * entry);
99 static GstIndexEntry *gst_mem_index_get_assoc_entry (GstIndex * index, gint id,
100 GstIndexLookupMethod method, GstIndexAssociationFlags flags,
101 GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data);
103 #define CLASS(mem_index) GST_MEM_INDEX_CLASS (G_OBJECT_GET_CLASS (mem_index))
105 static GType gst_mem_index_get_type (void);
107 G_DEFINE_TYPE (GstMemIndex, gst_mem_index, GST_TYPE_INDEX);
110 gst_mem_index_class_init (GstMemIndexClass * klass)
112 GObjectClass *gobject_class;
113 GstIndexClass *gstindex_class;
115 gobject_class = (GObjectClass *) klass;
116 gstindex_class = (GstIndexClass *) klass;
118 gobject_class->finalize = gst_mem_index_finalize;
120 gstindex_class->add_entry = GST_DEBUG_FUNCPTR (gst_mem_index_add_entry);
121 gstindex_class->get_assoc_entry =
122 GST_DEBUG_FUNCPTR (gst_mem_index_get_assoc_entry);
126 gst_mem_index_init (GstMemIndex * index)
128 GST_DEBUG ("created new mem index");
130 index->associations = NULL;
131 index->id_index = g_hash_table_new (g_int_hash, g_int_equal);
135 gst_mem_index_free_format (gpointer key, gpointer value, gpointer user_data)
137 GstMemIndexFormatIndex *index = (GstMemIndexFormatIndex *) value;
140 g_tree_destroy (index->tree);
143 g_slice_free (GstMemIndexFormatIndex, index);
147 gst_mem_index_free_id (gpointer key, gpointer value, gpointer user_data)
149 GstMemIndexId *id_index = (GstMemIndexId *) value;
151 if (id_index->format_index) {
152 g_hash_table_foreach (id_index->format_index, gst_mem_index_free_format,
154 g_hash_table_destroy (id_index->format_index);
155 id_index->format_index = NULL;
158 g_slice_free (GstMemIndexId, id_index);
162 gst_mem_index_finalize (GObject * object)
164 GstMemIndex *memindex = GST_MEM_INDEX (object);
166 /* Delete the trees referencing the associations first */
167 if (memindex->id_index) {
168 g_hash_table_foreach (memindex->id_index, gst_mem_index_free_id, NULL);
169 g_hash_table_destroy (memindex->id_index);
170 memindex->id_index = NULL;
173 /* Then delete the associations themselves */
174 if (memindex->associations) {
175 g_list_foreach (memindex->associations, (GFunc) gst_index_entry_free, NULL);
176 g_list_free (memindex->associations);
177 memindex->associations = NULL;
180 G_OBJECT_CLASS (gst_mem_index_parent_class)->finalize (object);
184 gst_mem_index_add_id (GstIndex * index, GstIndexEntry * entry)
186 GstMemIndex *memindex = GST_MEM_INDEX (index);
187 GstMemIndexId *id_index;
189 id_index = g_hash_table_lookup (memindex->id_index, &entry->id);
192 id_index = g_slice_new0 (GstMemIndexId);
194 id_index->id = entry->id;
195 id_index->format_index = g_hash_table_new (g_int_hash, g_int_equal);
196 g_hash_table_insert (memindex->id_index, &id_index->id, id_index);
201 mem_index_compare (gconstpointer a, gconstpointer b, gpointer user_data)
203 GstMemIndexFormatIndex *index = user_data;
207 val1 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) a), index->offset);
208 val2 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) b), index->offset);
210 diff = (val2 - val1);
212 return (diff == 0 ? 0 : (diff > 0 ? 1 : -1));
216 gst_mem_index_index_format (GstMemIndexId * id_index, GstIndexEntry * entry,
219 GstMemIndexFormatIndex *index;
222 format = &GST_INDEX_ASSOC_FORMAT (entry, assoc);
224 index = g_hash_table_lookup (id_index->format_index, format);
227 index = g_slice_new0 (GstMemIndexFormatIndex);
229 index->format = *format;
230 index->offset = assoc;
231 index->tree = g_tree_new_with_data (mem_index_compare, index);
233 g_hash_table_insert (id_index->format_index, &index->format, index);
236 g_tree_insert (index->tree, entry, entry);
240 gst_mem_index_add_association (GstIndex * index, GstIndexEntry * entry)
242 GstMemIndex *memindex = GST_MEM_INDEX (index);
243 GstMemIndexId *id_index;
245 memindex->associations = g_list_prepend (memindex->associations, entry);
247 id_index = g_hash_table_lookup (memindex->id_index, &entry->id);
251 for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
252 gst_mem_index_index_format (id_index, entry, i);
258 gst_mem_index_add_object (GstIndex * index, GstIndexEntry * entry)
263 gst_mem_index_add_format (GstIndex * index, GstIndexEntry * entry)
268 gst_mem_index_add_entry (GstIndex * index, GstIndexEntry * entry)
270 GST_LOG_OBJECT (index, "added this entry");
272 switch (entry->type) {
273 case GST_INDEX_ENTRY_ID:
274 gst_mem_index_add_id (index, entry);
276 case GST_INDEX_ENTRY_ASSOCIATION:
277 gst_mem_index_add_association (index, entry);
279 case GST_INDEX_ENTRY_OBJECT:
280 gst_mem_index_add_object (index, entry);
282 case GST_INDEX_ENTRY_FORMAT:
283 gst_mem_index_add_format (index, entry);
293 GstMemIndexFormatIndex *index;
295 GstIndexEntry *lower;
297 GstIndexEntry *higher;
300 GstMemIndexSearchData;
303 mem_index_search (gconstpointer a, gconstpointer b)
305 GstMemIndexSearchData *data = (GstMemIndexSearchData *) b;
306 GstMemIndexFormatIndex *index = data->index;
310 val1 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) a), index->offset);
313 diff = (val1 - val2);
317 /* exact matching, don't update low/high */
319 return (diff > 0 ? 1 : -1);
322 if (diff > data->low_diff) {
323 data->low_diff = diff;
324 data->lower = (GstIndexEntry *) a;
328 if (diff < data->high_diff) {
329 data->high_diff = diff;
330 data->higher = (GstIndexEntry *) a;
338 static GstIndexEntry *
339 gst_mem_index_get_assoc_entry (GstIndex * index, gint id,
340 GstIndexLookupMethod method,
341 GstIndexAssociationFlags flags,
342 GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
344 GstMemIndex *memindex = GST_MEM_INDEX (index);
345 GstMemIndexId *id_index;
346 GstMemIndexFormatIndex *format_index;
347 GstIndexEntry *entry;
348 GstMemIndexSearchData data;
350 id_index = g_hash_table_lookup (memindex->id_index, &id);
354 format_index = g_hash_table_lookup (id_index->format_index, &format);
359 data.index = format_index;
360 data.exact = (method == GST_INDEX_LOOKUP_EXACT);
362 /* setup data for low/high checks if we are not looking
363 * for an exact match */
365 data.low_diff = G_MININT64;
367 data.high_diff = G_MAXINT64;
371 entry = g_tree_search (format_index->tree, mem_index_search, &data);
373 /* get the low/high values if we're not exact */
374 if (entry == NULL && !data.exact) {
375 if (method == GST_INDEX_LOOKUP_BEFORE)
377 else if (method == GST_INDEX_LOOKUP_AFTER) {
382 if (entry && ((GST_INDEX_ASSOC_FLAGS (entry) & flags) != flags)) {
383 if (method != GST_INDEX_LOOKUP_EXACT) {
384 GList *l_entry = g_list_find (memindex->associations, entry);
389 entry = (GstIndexEntry *) l_entry->data;
391 if (entry->id == id && (GST_INDEX_ASSOC_FLAGS (entry) & flags) == flags)
394 if (method == GST_INDEX_LOOKUP_BEFORE)
395 l_entry = g_list_next (l_entry);
396 else if (method == GST_INDEX_LOOKUP_AFTER) {
397 l_entry = g_list_previous (l_entry);
410 gst_mem_index_plugin_init (GstPlugin * plugin)
412 GstIndexFactory *factory;
414 factory = gst_index_factory_new ("memindex",
415 "A index that stores entries in memory", gst_mem_index_get_type ());
417 if (factory == NULL) {
418 g_warning ("failed to create memindex factory");
422 GST_PLUGIN_FEATURE (factory)->plugin_name = plugin->desc.name;
423 GST_PLUGIN_FEATURE (factory)->loaded = TRUE;
425 gst_registry_add_feature (gst_registry_get_default (),
426 GST_PLUGIN_FEATURE (factory));