afe0a754fcebf0894e0ef59d342e57bf6d7db83d
[platform/upstream/gstreamer.git] / docs / random / metadata
1 The point of the metadata is to provide some context for each buffer.  In
2 the case of audio data, for instance, it would provide the samplerate, bit
3 depth, and channel count.
4
5 The trick is that there may be multiple types of metadata ganged onto a
6 single buffer.  This is why they're going to be a GList.  This does mean
7 extra overhead in all cases, but I think it's minimal.  The GList type
8 uses a chunk allocater so we're not wasting too much memory or time when
9 adding to the list.
10
11 The trick is dealing with these structs as they pass through a pipeline,
12 since they have potentially different mutability properties.  For
13 instance, if you've got a mp3 decoder connected to a tee, which sends the
14 buffers off to both the decoder and a spectrum analyzer (and then a
15 visualization element).  The preferred setup would be where every time a
16 audio/raw metadata comes down the pipe (indicating a potential change in
17 audio format), the audiosink and spectrum would just save off pointers.
18
19 So when exactly does this metadata go away (deallocated)?  Well, that
20 means metadata has to be refcounted.  But that gets rather hairy.  OK, in
21 the simple case you create a metadata struct, it comes with refcount set
22 to 1.  You pass it through, it stays one, eventually someone drops the
23 last reference on the buffer it's tied to, you free the metadata too.
24 Easy.  What if you tee?  You could go through and for every metadata in
25 the buffer, increment the refcount by the same as the buffer.  So in the
26 above case (tee'd), the audiosink and spectrum would get the buffer with a
27 refcount of 2, and it'd have a metadata with refcount 2.  Do they ref it
28 each themselves, then unref the buffer?  Or do they remove the metadata?
29 Removing the metadata would require a buffer CoW, which would suck, so
30 yes, they'd just ref the metadata.
31
32 But....  what if they're all in different threads?  Then we're off into
33 the magical world of mutexes.  Everything with a refcount in a threaded
34 world must be mutexed, else you can do atomic increment and atomic
35 dec&test.  Can this be done from C easily?  Perhaps it needs to be found
36 from kernel includes via autoconf?
37
38
39
40
41 The goal in designing the way metadata will be defined and used is to keep
42 it as simple as possible.  The basis for accomplishing this is the fact
43 that in order to actually use (rather than just pass) the metadata, you
44 have to know what the fields are, which means you have to have compiled in
45 support for that metadata at build time.  Therefore, if you're using
46 metadata, you must have build-time access to the necessary include file
47 that defines it.
48
49 So, given that you've got an include file, it would be nice if the whole
50 thing could be contained there.  This would limit the need to be linked
51 against something, or have load-time requirements as to that has to be
52 loaded before you are.
53
54 Given that really all metadata is is a region of memory of a given size
55 with a certain signature, this isn't all that hard.  First you lay out the
56 struct that defines the metadata.  Then you set up #defines that expand to
57 the size of the struct in question, as well as the four-cc code that
58 defines the type.
59
60 The work is done by a few #defines, a la the #defines used in all Gtk
61 objects.  The first is a NEW() method that allocates the memory for the
62 metadata and fills in all the normal fields (type, size, utility
63 functions).  Because of the way it's defined (as a #define, no less),
64 you'll have to invoke it as META_NEW(meta), since it can't return()
65 anything.
66
67 Another #define will check to make sure a meta is indeed that type by
68 verifying the type code and size.  Theoretically, meta types can overlap
69 with the same fourcc code, as long as they have different sizes.  But I
70 probably ought to have a global public registry so people writing things
71 don't conflict.  MSFT got that right, at least.
72
73 So, a hairy problem is what to do when there are utility functions
74 associated with one of these things.  One option is to not bother with
75 them.  This is very likely a possible solution, since metadata is supposed
76 to be flat memory of a given size.  Not much to do to either free or copy
77 it, is there?