Branch data Line data Source code
1 : : /* GStreamer
2 : : * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 : : *
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.
8 : : *
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.
13 : : *
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.
18 : : */
19 : :
20 : : #include <gst/gst.h>
21 : :
22 : : #include "gstindexers.h"
23 : :
24 : : #define GST_TYPE_MEM_INDEX \
25 : : (gst_index_get_type ())
26 : : #define GST_MEM_INDEX(obj) \
27 : : (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_MEM_INDEX, GstMemIndex))
28 : : #define GST_MEM_INDEX_CLASS(klass) \
29 : : (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_MEM_INDEX, GstMemIndexClass))
30 : : #define GST_IS_MEM_INDEX(obj) \
31 : : (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_MEM_INDEX))
32 : : #define GST_IS_MEM_INDEX_CLASS(klass) \
33 : : (GST_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_MEM_INDEX))
34 : :
35 : : /*
36 : : * Object model:
37 : : *
38 : : * All entries are simply added to a GList first. Then we build
39 : : * an index to each entry for each id/format
40 : : *
41 : : *
42 : : * memindex
43 : : * -----------------------------...
44 : : * ! !
45 : : * id1 id2
46 : : * ------------
47 : : * ! !
48 : : * format1 format2
49 : : * ! !
50 : : * GTree GTree
51 : : *
52 : : *
53 : : * The memindex creates a MemIndexId object for each writer id, a
54 : : * Hashtable is kept to map the id to the MemIndexId
55 : : *
56 : : * The MemIndexId keeps a MemIndexFormatIndex for each format the
57 : : * specific writer wants indexed.
58 : : *
59 : : * The MemIndexFormatIndex keeps all the values of the particular
60 : : * format in a GTree, The values of the GTree point back to the entry.
61 : : *
62 : : * Finding a value for an id/format requires locating the correct GTree,
63 : : * then do a lookup in the Tree to get the required value.
64 : : */
65 : :
66 : : typedef struct
67 : : {
68 : : GstFormat format;
69 : : gint offset;
70 : : GTree *tree;
71 : : }
72 : : GstMemIndexFormatIndex;
73 : :
74 : : typedef struct
75 : : {
76 : : gint id;
77 : : GHashTable *format_index;
78 : : }
79 : : GstMemIndexId;
80 : :
81 : : typedef struct _GstMemIndex GstMemIndex;
82 : : typedef struct _GstMemIndexClass GstMemIndexClass;
83 : :
84 : : struct _GstMemIndex
85 : : {
86 : : GstIndex parent;
87 : :
88 : : GList *associations;
89 : :
90 : : GHashTable *id_index;
91 : : };
92 : :
93 : : struct _GstMemIndexClass
94 : : {
95 : : GstIndexClass parent_class;
96 : : };
97 : :
98 : : /* Index signals and args */
99 : : enum
100 : : {
101 : : LAST_SIGNAL
102 : : };
103 : :
104 : : enum
105 : : {
106 : : ARG_0,
107 : : /* FILL ME */
108 : : };
109 : :
110 : : static void gst_mem_index_finalize (GObject * object);
111 : :
112 : : static void gst_mem_index_add_entry (GstIndex * index, GstIndexEntry * entry);
113 : : static GstIndexEntry *gst_mem_index_get_assoc_entry (GstIndex * index, gint id,
114 : : GstIndexLookupMethod method, GstAssocFlags flags,
115 : : GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data);
116 : :
117 : : #define CLASS(mem_index) GST_MEM_INDEX_CLASS (G_OBJECT_GET_CLASS (mem_index))
118 : :
119 : : /*static guint gst_mem_index_signals[LAST_SIGNAL] = { 0 }; */
120 : :
121 : : GType gst_mem_index_get_type (void);
122 : :
123 [ + - ]: 4 : G_DEFINE_TYPE (GstMemIndex, gst_mem_index, GST_TYPE_INDEX);
124 : :
125 : : static void
126 : 0 : gst_mem_index_class_init (GstMemIndexClass * klass)
127 : : {
128 : : GObjectClass *gobject_class;
129 : : GstIndexClass *gstindex_class;
130 : :
131 : 0 : gobject_class = (GObjectClass *) klass;
132 : 0 : gstindex_class = (GstIndexClass *) klass;
133 : :
134 : 0 : gobject_class->finalize = gst_mem_index_finalize;
135 : :
136 : 0 : gstindex_class->add_entry = GST_DEBUG_FUNCPTR (gst_mem_index_add_entry);
137 : 0 : gstindex_class->get_assoc_entry =
138 : 0 : GST_DEBUG_FUNCPTR (gst_mem_index_get_assoc_entry);
139 : 0 : }
140 : :
141 : : static void
142 : 0 : gst_mem_index_init (GstMemIndex * index)
143 : : {
144 [ # # ]: 0 : GST_DEBUG ("created new mem index");
145 : :
146 : 0 : index->associations = NULL;
147 : 0 : index->id_index = g_hash_table_new (g_int_hash, g_int_equal);
148 : 0 : }
149 : :
150 : : static void
151 : 0 : gst_mem_index_free_format (gpointer key, gpointer value, gpointer user_data)
152 : : {
153 : 0 : GstMemIndexFormatIndex *index = (GstMemIndexFormatIndex *) value;
154 : :
155 [ # # ]: 0 : if (index->tree) {
156 : 0 : g_tree_destroy (index->tree);
157 : : }
158 : :
159 : 0 : g_slice_free (GstMemIndexFormatIndex, index);
160 : 0 : }
161 : :
162 : : static void
163 : 0 : gst_mem_index_free_id (gpointer key, gpointer value, gpointer user_data)
164 : : {
165 : 0 : GstMemIndexId *id_index = (GstMemIndexId *) value;
166 : :
167 [ # # ]: 0 : if (id_index->format_index) {
168 : 0 : g_hash_table_foreach (id_index->format_index, gst_mem_index_free_format,
169 : : NULL);
170 : 0 : g_hash_table_destroy (id_index->format_index);
171 : 0 : id_index->format_index = NULL;
172 : : }
173 : :
174 : 0 : g_slice_free (GstMemIndexId, id_index);
175 : 0 : }
176 : :
177 : : static void
178 : 0 : gst_mem_index_finalize (GObject * object)
179 : : {
180 : 0 : GstMemIndex *memindex = GST_MEM_INDEX (object);
181 : :
182 : : /* Delete the trees referencing the associations first */
183 [ # # ]: 0 : if (memindex->id_index) {
184 : 0 : g_hash_table_foreach (memindex->id_index, gst_mem_index_free_id, NULL);
185 : 0 : g_hash_table_destroy (memindex->id_index);
186 : 0 : memindex->id_index = NULL;
187 : : }
188 : :
189 : : /* Then delete the associations themselves */
190 [ # # ]: 0 : if (memindex->associations) {
191 : 0 : g_list_foreach (memindex->associations, (GFunc) gst_index_entry_free, NULL);
192 : 0 : g_list_free (memindex->associations);
193 : 0 : memindex->associations = NULL;
194 : : }
195 : :
196 : 0 : G_OBJECT_CLASS (gst_mem_index_parent_class)->finalize (object);
197 : 0 : }
198 : :
199 : : static void
200 : 0 : gst_mem_index_add_id (GstIndex * index, GstIndexEntry * entry)
201 : : {
202 : 0 : GstMemIndex *memindex = GST_MEM_INDEX (index);
203 : : GstMemIndexId *id_index;
204 : :
205 : 0 : id_index = g_hash_table_lookup (memindex->id_index, &entry->id);
206 : :
207 [ # # ]: 0 : if (!id_index) {
208 : 0 : id_index = g_slice_new0 (GstMemIndexId);
209 : :
210 : 0 : id_index->id = entry->id;
211 : 0 : id_index->format_index = g_hash_table_new (g_int_hash, g_int_equal);
212 : 0 : g_hash_table_insert (memindex->id_index, &id_index->id, id_index);
213 : : }
214 : 0 : }
215 : :
216 : : static gint
217 : 0 : mem_index_compare (gconstpointer a, gconstpointer b, gpointer user_data)
218 : : {
219 : 0 : GstMemIndexFormatIndex *index = user_data;
220 : : gint64 val1, val2;
221 : : gint64 diff;
222 : :
223 : 0 : val1 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) a), index->offset);
224 : 0 : val2 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) b), index->offset);
225 : :
226 : 0 : diff = (val2 - val1);
227 : :
228 [ # # ][ # # ]: 0 : return (diff == 0 ? 0 : (diff > 0 ? 1 : -1));
229 : : }
230 : :
231 : : static void
232 : 0 : gst_mem_index_index_format (GstMemIndexId * id_index, GstIndexEntry * entry,
233 : : gint assoc)
234 : : {
235 : : GstMemIndexFormatIndex *index;
236 : : GstFormat *format;
237 : :
238 : 0 : format = &GST_INDEX_ASSOC_FORMAT (entry, assoc);
239 : :
240 : 0 : index = g_hash_table_lookup (id_index->format_index, format);
241 : :
242 [ # # ]: 0 : if (!index) {
243 : 0 : index = g_slice_new0 (GstMemIndexFormatIndex);
244 : :
245 : 0 : index->format = *format;
246 : 0 : index->offset = assoc;
247 : 0 : index->tree = g_tree_new_with_data (mem_index_compare, index);
248 : :
249 : 0 : g_hash_table_insert (id_index->format_index, &index->format, index);
250 : : }
251 : :
252 : 0 : g_tree_insert (index->tree, entry, entry);
253 : 0 : }
254 : :
255 : : static void
256 : 0 : gst_mem_index_add_association (GstIndex * index, GstIndexEntry * entry)
257 : : {
258 : 0 : GstMemIndex *memindex = GST_MEM_INDEX (index);
259 : : GstMemIndexId *id_index;
260 : :
261 : 0 : memindex->associations = g_list_prepend (memindex->associations, entry);
262 : :
263 : 0 : id_index = g_hash_table_lookup (memindex->id_index, &entry->id);
264 [ # # ]: 0 : if (id_index) {
265 : : gint i;
266 : :
267 [ # # ]: 0 : for (i = 0; i < GST_INDEX_NASSOCS (entry); i++) {
268 : 0 : gst_mem_index_index_format (id_index, entry, i);
269 : : }
270 : : }
271 : 0 : }
272 : :
273 : : static void
274 : 0 : gst_mem_index_add_object (GstIndex * index, GstIndexEntry * entry)
275 : : {
276 : 0 : }
277 : :
278 : : static void
279 : 0 : gst_mem_index_add_format (GstIndex * index, GstIndexEntry * entry)
280 : : {
281 : 0 : }
282 : :
283 : : static void
284 : 0 : gst_mem_index_add_entry (GstIndex * index, GstIndexEntry * entry)
285 : : {
286 [ # # ]: 0 : GST_LOG_OBJECT (index, "added this entry");
287 : :
288 [ # # # # : 0 : switch (entry->type) {
# ]
289 : : case GST_INDEX_ENTRY_ID:
290 : 0 : gst_mem_index_add_id (index, entry);
291 : 0 : break;
292 : : case GST_INDEX_ENTRY_ASSOCIATION:
293 : 0 : gst_mem_index_add_association (index, entry);
294 : 0 : break;
295 : : case GST_INDEX_ENTRY_OBJECT:
296 : 0 : gst_mem_index_add_object (index, entry);
297 : 0 : break;
298 : : case GST_INDEX_ENTRY_FORMAT:
299 : 0 : gst_mem_index_add_format (index, entry);
300 : 0 : break;
301 : : default:
302 : 0 : break;
303 : : }
304 : 0 : }
305 : :
306 : : typedef struct
307 : : {
308 : : gint64 value;
309 : : GstMemIndexFormatIndex *index;
310 : : gboolean exact;
311 : : GstIndexEntry *lower;
312 : : gint64 low_diff;
313 : : GstIndexEntry *higher;
314 : : gint64 high_diff;
315 : : }
316 : : GstMemIndexSearchData;
317 : :
318 : : static gint
319 : 0 : mem_index_search (gconstpointer a, gconstpointer b)
320 : : {
321 : 0 : GstMemIndexSearchData *data = (GstMemIndexSearchData *) b;
322 : 0 : GstMemIndexFormatIndex *index = data->index;
323 : : gint64 val1, val2;
324 : : gint64 diff;
325 : :
326 : 0 : val1 = GST_INDEX_ASSOC_VALUE (((GstIndexEntry *) a), index->offset);
327 : 0 : val2 = data->value;
328 : :
329 : 0 : diff = (val1 - val2);
330 [ # # ]: 0 : if (diff == 0)
331 : 0 : return 0;
332 : :
333 : : /* exact matching, don't update low/high */
334 [ # # ]: 0 : if (data->exact)
335 [ # # ]: 0 : return (diff > 0 ? 1 : -1);
336 : :
337 [ # # ]: 0 : if (diff < 0) {
338 [ # # ]: 0 : if (diff > data->low_diff) {
339 : 0 : data->low_diff = diff;
340 : 0 : data->lower = (GstIndexEntry *) a;
341 : : }
342 : 0 : diff = -1;
343 : : } else {
344 [ # # ]: 0 : if (diff < data->high_diff) {
345 : 0 : data->high_diff = diff;
346 : 0 : data->higher = (GstIndexEntry *) a;
347 : : }
348 : 0 : diff = 1;
349 : : }
350 : :
351 : 0 : return diff;
352 : : }
353 : :
354 : : static GstIndexEntry *
355 : 0 : gst_mem_index_get_assoc_entry (GstIndex * index, gint id,
356 : : GstIndexLookupMethod method,
357 : : GstAssocFlags flags,
358 : : GstFormat format, gint64 value, GCompareDataFunc func, gpointer user_data)
359 : : {
360 : 0 : GstMemIndex *memindex = GST_MEM_INDEX (index);
361 : : GstMemIndexId *id_index;
362 : : GstMemIndexFormatIndex *format_index;
363 : : GstIndexEntry *entry;
364 : : GstMemIndexSearchData data;
365 : :
366 : 0 : id_index = g_hash_table_lookup (memindex->id_index, &id);
367 [ # # ]: 0 : if (!id_index)
368 : 0 : return NULL;
369 : :
370 : 0 : format_index = g_hash_table_lookup (id_index->format_index, &format);
371 [ # # ]: 0 : if (!format_index)
372 : 0 : return NULL;
373 : :
374 : 0 : data.value = value;
375 : 0 : data.index = format_index;
376 : 0 : data.exact = (method == GST_INDEX_LOOKUP_EXACT);
377 : :
378 : : /* setup data for low/high checks if we are not looking
379 : : * for an exact match */
380 [ # # ]: 0 : if (!data.exact) {
381 : 0 : data.low_diff = G_MININT64;
382 : 0 : data.lower = NULL;
383 : 0 : data.high_diff = G_MAXINT64;
384 : 0 : data.higher = NULL;
385 : : }
386 : :
387 : 0 : entry = g_tree_search (format_index->tree, mem_index_search, &data);
388 : :
389 : : /* get the low/high values if we're not exact */
390 [ # # ][ # # ]: 0 : if (entry == NULL && !data.exact) {
391 [ # # ]: 0 : if (method == GST_INDEX_LOOKUP_BEFORE)
392 : 0 : entry = data.lower;
393 [ # # ]: 0 : else if (method == GST_INDEX_LOOKUP_AFTER) {
394 : 0 : entry = data.higher;
395 : : }
396 : : }
397 : :
398 [ # # ][ # # ]: 0 : if (entry && ((GST_INDEX_ASSOC_FLAGS (entry) & flags) != flags)) {
399 [ # # ]: 0 : if (method != GST_INDEX_LOOKUP_EXACT) {
400 : 0 : GList *l_entry = g_list_find (memindex->associations, entry);
401 : :
402 : 0 : entry = NULL;
403 : :
404 [ # # ]: 0 : while (l_entry) {
405 : 0 : entry = (GstIndexEntry *) l_entry->data;
406 : :
407 [ # # ][ # # ]: 0 : if (entry->id == id && (GST_INDEX_ASSOC_FLAGS (entry) & flags) == flags)
408 : 0 : break;
409 : :
410 [ # # ]: 0 : if (method == GST_INDEX_LOOKUP_BEFORE)
411 [ # # ]: 0 : l_entry = g_list_next (l_entry);
412 [ # # ]: 0 : else if (method == GST_INDEX_LOOKUP_AFTER) {
413 [ # # ]: 0 : l_entry = g_list_previous (l_entry);
414 : : }
415 : : }
416 : : } else {
417 : 0 : entry = NULL;
418 : : }
419 : : }
420 : :
421 : 0 : return entry;
422 : : }
423 : :
424 : : gboolean
425 : 4 : gst_mem_index_plugin_init (GstPlugin * plugin)
426 : : {
427 : : GstIndexFactory *factory;
428 : :
429 : 4 : factory = gst_index_factory_new ("memindex",
430 : : "A index that stores entries in memory", gst_mem_index_get_type ());
431 : :
432 [ - + ]: 4 : if (factory == NULL) {
433 : 0 : g_warning ("failed to create memindex factory");
434 : 0 : return FALSE;
435 : : }
436 : :
437 : 4 : GST_PLUGIN_FEATURE (factory)->plugin_name = plugin->desc.name;
438 : 4 : GST_PLUGIN_FEATURE (factory)->loaded = TRUE;
439 : :
440 : 4 : gst_registry_add_feature (gst_registry_get_default (),
441 : 4 : GST_PLUGIN_FEATURE (factory));
442 : :
443 : 4 : return TRUE;
444 : : }
|