add default bufferpool, clean up some code, add bufferpool testing to fakesrc
[platform/upstream/gstreamer.git] / gst / gstbuffer.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstbuffer.h: Header for GstBuffer object
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #ifndef __GST_BUFFER_H__
25 #define __GST_BUFFER_H__
26
27 #include <gst/gstdata.h>
28
29 G_BEGIN_DECLS
30
31 typedef struct _GstBuffer GstBuffer;
32 typedef struct _GstBufferPool GstBufferPool;
33
34 extern GType _gst_buffer_type;
35 extern GType _gst_buffer_pool_type;
36
37 #define GST_TYPE_BUFFER                         (_gst_buffer_type)
38 #define GST_TYPE_BUFFER_POOL                    (_gst_buffer_pool_type)
39
40 #define GST_BUFFER(buf)                         ((GstBuffer *)(buf))
41 #define GST_BUFFER_POOL(pool)                   ((GstBufferPool *)(pool))
42 #define GST_IS_BUFFER(buf)                      (GST_DATA_TYPE(buf) == GST_TYPE_BUFFER)
43 #define GST_IS_BUFFER_POOL(buf)                 (GST_DATA_TYPE(buf) == GST_TYPE_BUFFER_POOL)
44
45 #define GST_BUFFER_REFCOUNT(buf)                GST_DATA_REFCOUNT(buf)
46 #define GST_BUFFER_REFCOUNT_VALUE(buf)          GST_DATA_REFCOUNT_VALUE(buf)
47 #define GST_BUFFER_COPY_FUNC(buf)               GST_DATA_COPY_FUNC(buf)
48 #define GST_BUFFER_FREE_FUNC(buf)               GST_DATA_FREE_FUNC(buf)
49
50 #define GST_BUFFER_FLAGS(buf)                   GST_DATA_FLAGS(buf)
51 #define GST_BUFFER_FLAG_IS_SET(buf,flag)        GST_DATA_FLAG_IS_SET (buf, flag)
52 #define GST_BUFFER_FLAG_SET(buf,flag)           GST_DATA_FLAG_SET (buf, flag)
53 #define GST_BUFFER_FLAG_UNSET(buf,flag)         GST_DATA_FLAG_UNSET (buf, flag)
54
55 #define GST_BUFFER_DATA(buf)                    (GST_BUFFER(buf)->data)
56 #define GST_BUFFER_SIZE(buf)                    (GST_BUFFER(buf)->size)
57 #define GST_BUFFER_MAXSIZE(buf)                 (GST_BUFFER(buf)->maxsize)
58 #define GST_BUFFER_TIMESTAMP(buf)               (GST_BUFFER(buf)->timestamp)
59 #define GST_BUFFER_OFFSET(buf)                  (GST_BUFFER(buf)->offset)
60 #define GST_BUFFER_BUFFERPOOL(buf)              (GST_BUFFER(buf)->pool)
61 #define GST_BUFFER_POOL_PRIVATE(buf)            (GST_BUFFER(buf)->pool_private)
62
63 typedef enum {
64   GST_BUFFER_READONLY   = GST_DATA_FLAG_LAST,
65   GST_BUFFER_SUBBUFFER,
66   GST_BUFFER_ORIGINAL,
67   GST_BUFFER_DONTFREE,
68   GST_BUFFER_DISCONTINOUS,
69   GST_BUFFER_KEY_UNIT,
70   GST_BUFFER_PREROLL,
71   GST_BUFFER_FLAG_LAST  = GST_DATA_FLAG_LAST + 8
72 } GstBufferFlag;
73
74 struct _GstBuffer {
75   GstData                data_type;
76
77   /* pointer to data and its size */
78   guint8                *data;                  /* pointer to buffer data */
79   guint                  size;                  /* size of buffer data */
80   guint64                maxsize;               /* max size of this buffer */
81
82   guint64                timestamp;             
83   guint64                offset;
84
85   /* this is a pointer to the buffer pool (if any) */
86   GstBufferPool         *pool;
87   /* pointer to pool private data of parent buffer in case of a subbuffer */
88   gpointer               pool_private;
89 };
90
91 /* bufferpools */
92
93 typedef GstBuffer*      (*GstBufferPoolBufferNewFunction)       (GstBufferPool *pool, guint64 offset, guint size, gpointer user_data);
94 typedef GstBuffer*      (*GstBufferPoolBufferCopyFunction)      (GstBufferPool *pool, const GstBuffer *buffer, gpointer user_data);
95 typedef void            (*GstBufferPoolBufferFreeFunction)      (GstBufferPool *pool, GstBuffer *buffer, gpointer user_data);
96
97 struct _GstBufferPool {
98   GstData                               data;
99
100   gboolean                              active;
101
102   GstBufferPoolBufferNewFunction        buffer_new;
103   GstBufferPoolBufferCopyFunction       buffer_copy;
104   GstBufferPoolBufferFreeFunction       buffer_free;
105
106   gpointer                              user_data;
107 };
108
109
110 /*< private >*/
111 void            _gst_buffer_initialize          (void);
112
113 /* functions used by subclasses and bufferpools */
114 void            gst_buffer_default_free         (GstBuffer *buffer);
115 GstBuffer*      gst_buffer_default_copy         (GstBuffer *buffer);
116
117 void            gst_buffer_print_stats          (void);
118
119 /* allocation */
120 GstBuffer*      gst_buffer_new                  (void);
121 GstBuffer*      gst_buffer_new_and_alloc        (guint size);
122
123 /* creating a new buffer from a pool */
124 GstBuffer*      gst_buffer_new_from_pool        (GstBufferPool *pool, guint64 offset, guint size);
125
126 /* refcounting */
127 #define         gst_buffer_ref(buf)             GST_BUFFER (gst_data_ref (GST_DATA (buf)))
128 #define         gst_buffer_ref_by_count(buf,c)  GST_BUFFER (gst_data_ref_by_count (GST_DATA (buf), c))
129 #define         gst_buffer_unref(buf)           gst_data_unref (GST_DATA (buf))
130 /* copy buffer */
131 #define         gst_buffer_copy(buffer)         GST_BUFFER (gst_data_copy (GST_DATA (buffer)))
132 #define         gst_buffer_copy_on_write(buffer) GST_BUFFER (gst_data_copy_on_write (GST_DATA (buffer)))
133 #define         gst_buffer_free(buffer)         gst_data_free (GST_DATA (buffer))
134
135 /* creating a subbuffer */
136 GstBuffer*      gst_buffer_create_sub           (GstBuffer *parent, guint offset, guint size);
137
138 /* merge, span, or append two buffers, intelligently */
139 GstBuffer*      gst_buffer_merge                (GstBuffer *buf1, GstBuffer *buf2);
140 gboolean        gst_buffer_is_span_fast         (GstBuffer *buf1, GstBuffer *buf2);
141 GstBuffer*      gst_buffer_span                 (GstBuffer *buf1, guint32 offset, GstBuffer *buf2, guint32 len);
142
143
144 /* creating a new buffer pools */
145 GstBufferPool*  gst_buffer_pool_new                     (GstDataFreeFunction free,
146                                                          GstDataCopyFunction copy,
147                                                          GstBufferPoolBufferNewFunction buffer_new,
148                                                          GstBufferPoolBufferCopyFunction buffer_copy,
149                                                          GstBufferPoolBufferFreeFunction buffer_free,
150                                                          gpointer user_data);
151
152 /* function used by subclasses and bufferpools */
153 void            gst_buffer_pool_default_free            (GstBufferPool *pool);
154
155 gboolean        gst_buffer_pool_is_active               (GstBufferPool *pool);
156 void            gst_buffer_pool_set_active              (GstBufferPool *pool, gboolean active);
157
158 #define         gst_buffer_pool_ref(pool)               GST_BUFFER_POOL (gst_data_ref (GST_DATA (pool)))
159 #define         gst_buffer_pool_ref_by_count(pool,c)    GST_BUFFER_POOL (gst_data_ref_by_count (GST_DATA (pool), c))
160 #define         gst_buffer_pool_unref(pool)             gst_data_unref (GST_DATA (pool))
161
162 /* bufferpool operations */
163 #define         gst_buffer_pool_copy(pool)              GST_BUFFER_POOL (gst_data_copy (GST_DATA (pool)))
164 #define         gst_buffer_pool_copy_on_write(pool)     GST_BUFFER_POOL (gst_data_copy_on_write (GST_DATA (pool)))
165 #define         gst_buffer_pool_free(pool)              gst_data_free (GST_DATA (pool))
166
167 void            gst_buffer_pool_set_user_data           (GstBufferPool *pool, gpointer user_data);
168 gpointer        gst_buffer_pool_get_user_data           (GstBufferPool *pool);
169
170 /* a default pool */
171 GstBufferPool*  gst_buffer_pool_get_default             (guint size, guint numbuffers);
172
173 G_END_DECLS
174
175
176 #endif /* __GST_BUFFER_H__ */