basetransform: refine metadata filter and transform
[platform/upstream/gstreamer.git] / libs / gst / base / gstbasetransform.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2005 Wim Taymans <wim@fluendo.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifndef __GST_BASE_TRANSFORM_H__
22 #define __GST_BASE_TRANSFORM_H__
23
24 #include <gst/gst.h>
25
26 G_BEGIN_DECLS
27
28 #define GST_TYPE_BASE_TRANSFORM            (gst_base_transform_get_type())
29 #define GST_BASE_TRANSFORM(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_TRANSFORM,GstBaseTransform))
30 #define GST_BASE_TRANSFORM_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_TRANSFORM,GstBaseTransformClass))
31 #define GST_BASE_TRANSFORM_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_BASE_TRANSFORM,GstBaseTransformClass))
32 #define GST_IS_BASE_TRANSFORM(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_TRANSFORM))
33 #define GST_IS_BASE_TRANSFORM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_TRANSFORM))
34 /* since 0.10.4 */
35 #define GST_BASE_TRANSFORM_CAST(obj)    ((GstBaseTransform *)(obj))
36
37 /**
38  * GST_BASE_TRANSFORM_SINK_NAME:
39  *
40  * The name of the templates for the sink pad.
41  */
42 #define GST_BASE_TRANSFORM_SINK_NAME    "sink"
43 /**
44  * GST_BASE_TRANSFORM_SRC_NAME:
45  *
46  * The name of the templates for the source pad.
47  */
48 #define GST_BASE_TRANSFORM_SRC_NAME     "src"
49
50 /**
51  * GST_BASE_TRANSFORM_SRC_PAD:
52  * @obj: base transform instance
53  *
54  * Gives the pointer to the source #GstPad object of the element.
55  *
56  * Since: 0.10.4
57  */
58 #define GST_BASE_TRANSFORM_SRC_PAD(obj)         (GST_BASE_TRANSFORM_CAST (obj)->srcpad)
59
60 /**
61  * GST_BASE_TRANSFORM_SINK_PAD:
62  * @obj: base transform instance
63  *
64  * Gives the pointer to the sink #GstPad object of the element.
65  *
66  * Since: 0.10.4
67  */
68 #define GST_BASE_TRANSFORM_SINK_PAD(obj)        (GST_BASE_TRANSFORM_CAST (obj)->sinkpad)
69
70 /**
71  * GST_BASE_TRANSFORM_FLOW_DROPPED:
72  *
73  * A #GstFlowReturn that can be returned from transform and transform_ip to
74  * indicate that no output buffer was generated.
75  *
76  * Since: 0.10.13
77  */
78 #define GST_BASE_TRANSFORM_FLOW_DROPPED   GST_FLOW_CUSTOM_SUCCESS
79
80 typedef struct _GstBaseTransform GstBaseTransform;
81 typedef struct _GstBaseTransformClass GstBaseTransformClass;
82 typedef struct _GstBaseTransformPrivate GstBaseTransformPrivate;
83
84 /**
85  * GstBaseTransform:
86  *
87  * The opaque #GstBaseTransform data structure.
88  */
89 struct _GstBaseTransform {
90   GstElement     element;
91
92   /*< protected >*/
93   /* source and sink pads */
94   GstPad        *sinkpad;
95   GstPad        *srcpad;
96
97   /* Set by sub-class */
98   gboolean       passthrough;
99   gboolean       always_in_place;
100
101   GstCaps       *cache_caps1;
102   gsize          cache_caps1_size;
103   GstCaps       *cache_caps2;
104   gsize          cache_caps2_size;
105   gboolean       have_same_caps;
106
107   gboolean       negotiated;
108
109   gboolean       have_segment;
110
111   /* MT-protected (with STREAM_LOCK) */
112   GstSegment     segment;
113
114   /*< private >*/
115   GstBaseTransformPrivate *priv;
116
117   gpointer       _gst_reserved[GST_PADDING_LARGE];
118 };
119
120 /**
121  * GstBaseTransformClass:
122  * @parent_class:   Element parent class
123  * @passthrough_on_same_caps: If set to TRUE, passthrough mode will be
124  *                            automatically enabled if the caps are the same.
125  * @transform_caps: Optional.  Given the pad in this direction and the given
126  *                  caps, what caps are allowed on the other pad in this
127  *                  element ?
128  * @fixate_caps:    Optional. Given the pad in this direction and the given
129  *                  caps, fixate the caps on the other pad. The function takes
130  *                  ownership of @othercaps and returns a fixated version of
131  *                  @othercaps. @othercaps is not guaranteed to be writable.
132  * @accept_caps:    Optional. Since 0.10.30
133  *                  Subclasses can override this method to check if @caps can be
134  *                  handled by the element. The default implementation might not be
135  *                  the most optimal way to check this in all cases.
136  * @set_caps:       allows the subclass to be notified of the actual caps set.
137  * @query:          Optional Since 0.10.36
138  *                  Handle a requested query. Subclasses that implement this
139  *                  should must chain up to the parent if they didn't handle the
140  *                  query
141  * @decide_allocation: Setup the allocation parameters for allocating output
142  *                    buffers. The passed in query contains the result of the
143  *                    downstream allocation query. This function is only called
144  *                    when not operating in passthrough mode. The default
145  *                    implementation will remove all memory dependent metadata.
146  *                    If there is ia @filter_meta method implementation, it will
147  *                    be called for all metadata API in the downstream query,
148  *                    otherwise the metadata API is removed.
149  * @filter_meta: Return TRUE if the metadata API should be proposed in the
150  *               upstream allocation query. The default implementation is NULL
151  *               and will cause all metadata to be removed.
152  * @propose_allocation: Propose buffer allocation parameters for upstream elements.
153  *                      This function must be implemented if the element reads or
154  *                      writes the buffer content. The query that was passed to
155  *                      the decide_allocation is passed in this method (or NULL
156  *                      when the element is in passthrough mode). The default
157  *                      implementation will pass the query downstream when in
158  *                      passthrough mode and will copy all the filtered metadata
159  *                      API in non-passthrough mode.
160  * @transform_size: Optional. Given the size of a buffer in the given direction
161  *                  with the given caps, calculate the size in bytes of a buffer
162  *                  on the other pad with the given other caps.
163  *                  The default implementation uses get_unit_size and keeps
164  *                  the number of units the same.
165  * @get_unit_size:  Required if the transform is not in-place.
166  *                  get the size in bytes of one unit for the given caps.
167  * @start:          Optional.
168  *                  Called when the element starts processing.
169  *                  Allows opening external resources.
170  * @stop:           Optional.
171  *                  Called when the element stops processing.
172  *                  Allows closing external resources.
173  * @sink_event:     Optional.
174  *                  Event handler on the sink pad. The default implementation
175  *                  handles the event and forwards it downstream.
176  * @src_event:      Optional.
177  *                  Event handler on the source pad. The default implementation
178  *                  handles the event and forwards it upstream.
179  * @prepare_output_buffer: Optional.
180  *                         Subclasses can override this to do their own
181  *                         allocation of output buffers.  Elements that only do
182  *                         analysis can return a subbuffer or even just
183  *                         return a reference to the input buffer (if in
184  *                         passthrough mode). The default implementation will
185  *                         use the negotiated allocator or bufferpool and
186  *                         transform_size to allocate an output buffer or it
187  *                         will return the input buffer in passthrough mode.
188  * @copy_metadata: Optional.
189  *                 Copy the metadata from the input buffer to the output buffer.
190  *                 The default implementation will copy the flags, timestamps and
191  *                 offsets of the buffer.
192  * @transform_meta: Optional. Transform the metadata on the input buffer to the
193  *                  output buffer. By default this method is NULL and no
194  *                  metadata is copied. subclasses can implement this method and
195  *                  return TRUE if the metadata is to be copied.
196  * @before_transform: Optional. Since 0.10.22
197  *                    This method is called right before the base class will
198  *                    start processing. Dynamic properties or other delayed
199  *                    configuration could be performed in this method.
200  * @transform:      Required if the element does not operate in-place.
201  *                  Transforms one incoming buffer to one outgoing buffer.
202  *                  The function is allowed to change size/timestamp/duration
203  *                  of the outgoing buffer.
204  * @transform_ip:   Required if the element operates in-place.
205  *                  Transform the incoming buffer in-place.
206  *
207  * Subclasses can override any of the available virtual methods or not, as
208  * needed. At minimum either @transform or @transform_ip need to be overridden.
209  * If the element can overwrite the input data with the results (data is of the
210  * same type and quantity) it should provide @transform_ip.
211  */
212 struct _GstBaseTransformClass {
213   GstElementClass parent_class;
214
215   /*< public >*/
216   gboolean       passthrough_on_same_caps;
217
218   /* virtual methods for subclasses */
219   GstCaps*      (*transform_caps) (GstBaseTransform *trans,
220                                    GstPadDirection direction,
221                                    GstCaps *caps, GstCaps *filter);
222   GstCaps*      (*fixate_caps)    (GstBaseTransform *trans,
223                                    GstPadDirection direction, GstCaps *caps,
224                                    GstCaps *othercaps);
225   gboolean      (*accept_caps)    (GstBaseTransform *trans, GstPadDirection direction,
226                                    GstCaps *caps);
227   gboolean      (*set_caps)       (GstBaseTransform *trans, GstCaps *incaps,
228                                    GstCaps *outcaps);
229   gboolean      (*query)          (GstBaseTransform *trans, GstPadDirection direction,
230                                    GstQuery *query);
231
232   /* decide allocation query for output buffers */
233   gboolean      (*decide_allocation)  (GstBaseTransform *trans, GstQuery *query);
234   gboolean      (*filter_meta)        (GstBaseTransform *trans, GstQuery *query, GType api);
235
236   /* propose allocation query parameters for input buffers */
237   gboolean      (*propose_allocation) (GstBaseTransform *trans, GstQuery *decide_query,
238                                        GstQuery *query);
239
240   /* transform size */
241   gboolean      (*transform_size) (GstBaseTransform *trans,
242                                    GstPadDirection direction,
243                                    GstCaps *caps, gsize size,
244                                    GstCaps *othercaps, gsize *othersize);
245
246   gboolean      (*get_unit_size)  (GstBaseTransform *trans, GstCaps *caps,
247                                    gsize *size);
248
249   /* states */
250   gboolean      (*start)        (GstBaseTransform *trans);
251   gboolean      (*stop)         (GstBaseTransform *trans);
252
253   /* sink and src pad event handlers */
254   gboolean      (*sink_event)   (GstBaseTransform *trans, GstEvent *event);
255   gboolean      (*src_event)    (GstBaseTransform *trans, GstEvent *event);
256
257   GstFlowReturn (*prepare_output_buffer) (GstBaseTransform * trans,
258                                           GstBuffer *input, GstBuffer **outbuf);
259
260   /* metadata */
261   gboolean      (*copy_metadata)     (GstBaseTransform *trans, GstBuffer *input,
262                                       GstBuffer *outbuf);
263   gboolean      (*transform_meta)    (GstBaseTransform *trans, GstBuffer *outbuf,
264                                       GstMeta *meta, GstBuffer *inbuf);
265
266   void          (*before_transform)  (GstBaseTransform *trans, GstBuffer *buffer);
267
268   /* transform */
269   GstFlowReturn (*transform)    (GstBaseTransform *trans, GstBuffer *inbuf,
270                                  GstBuffer *outbuf);
271   GstFlowReturn (*transform_ip) (GstBaseTransform *trans, GstBuffer *buf);
272
273   /*< private >*/
274   gpointer       _gst_reserved[GST_PADDING_LARGE];
275 };
276
277 GType           gst_base_transform_get_type         (void);
278
279 void            gst_base_transform_set_passthrough  (GstBaseTransform *trans,
280                                                      gboolean passthrough);
281 gboolean        gst_base_transform_is_passthrough   (GstBaseTransform *trans);
282
283 void            gst_base_transform_set_in_place     (GstBaseTransform *trans,
284                                                      gboolean in_place);
285 gboolean        gst_base_transform_is_in_place      (GstBaseTransform *trans);
286
287 void            gst_base_transform_update_qos       (GstBaseTransform *trans,
288                                                      gdouble proportion,
289                                                      GstClockTimeDiff diff,
290                                                      GstClockTime timestamp);
291 void            gst_base_transform_set_qos_enabled  (GstBaseTransform *trans,
292                                                      gboolean enabled);
293 gboolean        gst_base_transform_is_qos_enabled   (GstBaseTransform *trans);
294
295 void            gst_base_transform_set_gap_aware    (GstBaseTransform *trans,
296                                                      gboolean gap_aware);
297
298 void            gst_base_transform_reconfigure_sink (GstBaseTransform *trans);
299 void            gst_base_transform_reconfigure_src  (GstBaseTransform *trans);
300 G_END_DECLS
301
302 #endif /* __GST_BASE_TRANSFORM_H__ */