2fe25ece63938f7c172422b1fe0ac0b49413b1b8
[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., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, 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 #define GST_BASE_TRANSFORM_CAST(obj)    ((GstBaseTransform *)(obj))
35
36 /**
37  * GST_BASE_TRANSFORM_SINK_NAME:
38  *
39  * The name of the templates for the sink pad.
40  */
41 #define GST_BASE_TRANSFORM_SINK_NAME    "sink"
42 /**
43  * GST_BASE_TRANSFORM_SRC_NAME:
44  *
45  * The name of the templates for the source pad.
46  */
47 #define GST_BASE_TRANSFORM_SRC_NAME     "src"
48
49 /**
50  * GST_BASE_TRANSFORM_SRC_PAD:
51  * @obj: base transform instance
52  *
53  * Gives the pointer to the source #GstPad object of the element.
54  */
55 #define GST_BASE_TRANSFORM_SRC_PAD(obj)         (GST_BASE_TRANSFORM_CAST (obj)->srcpad)
56
57 /**
58  * GST_BASE_TRANSFORM_SINK_PAD:
59  * @obj: base transform instance
60  *
61  * Gives the pointer to the sink #GstPad object of the element.
62  */
63 #define GST_BASE_TRANSFORM_SINK_PAD(obj)        (GST_BASE_TRANSFORM_CAST (obj)->sinkpad)
64
65 /**
66  * GST_BASE_TRANSFORM_FLOW_DROPPED:
67  *
68  * A #GstFlowReturn that can be returned from transform and transform_ip to
69  * indicate that no output buffer was generated.
70  */
71 #define GST_BASE_TRANSFORM_FLOW_DROPPED   GST_FLOW_CUSTOM_SUCCESS
72
73 typedef struct _GstBaseTransform GstBaseTransform;
74 typedef struct _GstBaseTransformClass GstBaseTransformClass;
75 typedef struct _GstBaseTransformPrivate GstBaseTransformPrivate;
76
77 /**
78  * GstBaseTransform:
79  *
80  * The opaque #GstBaseTransform data structure.
81  */
82 struct _GstBaseTransform {
83   GstElement     element;
84
85   /*< protected >*/
86   /* source and sink pads */
87   GstPad        *sinkpad;
88   GstPad        *srcpad;
89
90   /* MT-protected (with STREAM_LOCK) */
91   gboolean       have_segment;
92   GstSegment     segment;
93   /* Default submit_input_buffer places the buffer here,
94    * for consumption by the generate_output method: */
95   GstBuffer      *queued_buf;
96
97   /*< private >*/
98   GstBaseTransformPrivate *priv;
99
100   gpointer       _gst_reserved[GST_PADDING_LARGE-1];
101 };
102
103 /**
104  * GstBaseTransformClass:
105  * @parent_class:   Element parent class
106  * @passthrough_on_same_caps: If set to %TRUE, passthrough mode will be
107  *                            automatically enabled if the caps are the same.
108  *                            Set to %FALSE by default.
109  * @transform_ip_on_passthrough: If set to %TRUE, @transform_ip will be called in
110  *                           passthrough mode. The passed buffer might not be
111  *                           writable. When %FALSE, neither @transform nor
112  *                           @transform_ip will be called in passthrough mode.
113  *                           Set to %TRUE by default.
114  * @transform_caps: Optional.  Given the pad in this direction and the given
115  *                  caps, what caps are allowed on the other pad in this
116  *                  element ?
117  * @fixate_caps:    Optional. Given the pad in this direction and the given
118  *                  caps, fixate the caps on the other pad. The function takes
119  *                  ownership of @othercaps and returns a fixated version of
120  *                  @othercaps. @othercaps is not guaranteed to be writable.
121  * @accept_caps:    Optional.
122  *                  Subclasses can override this method to check if @caps can be
123  *                  handled by the element. The default implementation might not be
124  *                  the most optimal way to check this in all cases.
125  * @set_caps:       allows the subclass to be notified of the actual caps set.
126  * @query:          Optional.
127  *                  Handle a requested query. Subclasses that implement this
128  *                  should must chain up to the parent if they didn't handle the
129  *                  query
130  * @decide_allocation: Setup the allocation parameters for allocating output
131  *                    buffers. The passed in query contains the result of the
132  *                    downstream allocation query. This function is only called
133  *                    when not operating in passthrough mode. The default
134  *                    implementation will remove all memory dependent metadata.
135  *                    If there is a @filter_meta method implementation, it will
136  *                    be called for all metadata API in the downstream query,
137  *                    otherwise the metadata API is removed.
138  * @filter_meta: Return %TRUE if the metadata API should be proposed in the
139  *               upstream allocation query. The default implementation is %NULL
140  *               and will cause all metadata to be removed.
141  * @propose_allocation: Propose buffer allocation parameters for upstream elements.
142  *                      This function must be implemented if the element reads or
143  *                      writes the buffer content. The query that was passed to
144  *                      the decide_allocation is passed in this method (or %NULL
145  *                      when the element is in passthrough mode). The default
146  *                      implementation will pass the query downstream when in
147  *                      passthrough mode and will copy all the filtered metadata
148  *                      API in non-passthrough mode.
149  * @transform_size: Optional. Given the size of a buffer in the given direction
150  *                  with the given caps, calculate the size in bytes of a buffer
151  *                  on the other pad with the given other caps.
152  *                  The default implementation uses get_unit_size and keeps
153  *                  the number of units the same.
154  * @get_unit_size:  Required if the transform is not in-place.
155  *                  get the size in bytes of one unit for the given caps.
156  * @start:          Optional.
157  *                  Called when the element starts processing.
158  *                  Allows opening external resources.
159  * @stop:           Optional.
160  *                  Called when the element stops processing.
161  *                  Allows closing external resources.
162  * @sink_event:     Optional.
163  *                  Event handler on the sink pad. The default implementation
164  *                  handles the event and forwards it downstream.
165  * @src_event:      Optional.
166  *                  Event handler on the source pad. The default implementation
167  *                  handles the event and forwards it upstream.
168  * @prepare_output_buffer: Optional.
169  *                         Subclasses can override this to do their own
170  *                         allocation of output buffers.  Elements that only do
171  *                         analysis can return a subbuffer or even just
172  *                         return a reference to the input buffer (if in
173  *                         passthrough mode). The default implementation will
174  *                         use the negotiated allocator or bufferpool and
175  *                         transform_size to allocate an output buffer or it
176  *                         will return the input buffer in passthrough mode.
177  * @copy_metadata: Optional.
178  *                 Copy the metadata from the input buffer to the output buffer.
179  *                 The default implementation will copy the flags, timestamps and
180  *                 offsets of the buffer.
181  * @transform_meta: Optional. Transform the metadata on the input buffer to the
182  *                  output buffer. By default this method copies all meta without
183  *                  tags. subclasses can implement this method and return %TRUE if
184  *                  the metadata is to be copied.
185  * @before_transform: Optional.
186  *                    This method is called right before the base class will
187  *                    start processing. Dynamic properties or other delayed
188  *                    configuration could be performed in this method.
189  * @transform:      Required if the element does not operate in-place.
190  *                  Transforms one incoming buffer to one outgoing buffer.
191  *                  The function is allowed to change size/timestamp/duration
192  *                  of the outgoing buffer.
193  * @transform_ip:   Required if the element operates in-place.
194  *                  Transform the incoming buffer in-place.
195  * @submit_input_buffer: Function which accepts a new input buffer and pre-processes it.
196  *                  The default implementation performs caps (re)negotiation, then
197  *                  QoS if needed, and places the input buffer into the @queued_buf
198  *                  member variable. If the buffer is dropped due to QoS, it returns
199  *                  GST_BASE_TRANSFORM_FLOW_DROPPED. If this input buffer is not
200  *                  contiguous with any previous input buffer, then @is_discont
201  *                  is set to #TRUE. (Since 1.6)
202  * @generate_output: Called after each new input buffer is submitted repeatedly
203  *                   until it either generates an error or fails to generate an output
204  *                   buffer. The default implementation takes the contents of the
205  *                   @queued_buf variable, generates an output buffer if needed
206  *                   by calling the class @prepare_output_buffer, and then
207  *                   calls either @transform or @transform_ip. Elements that don't
208  *                   do 1-to-1 transformations on input to output buffers can either
209  *                   return GST_BASE_TRANSFORM_FLOW_DROPPED or simply not generate
210  *                   an output buffer until they are ready to do so. (Since 1.6)
211  *                   
212  * Subclasses can override any of the available virtual methods or not, as
213  * needed. At minimum either @transform or @transform_ip need to be overridden.
214  * If the element can overwrite the input data with the results (data is of the
215  * same type and quantity) it should provide @transform_ip.
216  */
217 struct _GstBaseTransformClass {
218   GstElementClass parent_class;
219
220   /*< public >*/
221   gboolean       passthrough_on_same_caps;
222   gboolean       transform_ip_on_passthrough;
223
224   /* virtual methods for subclasses */
225   GstCaps*      (*transform_caps) (GstBaseTransform *trans,
226                                    GstPadDirection direction,
227                                    GstCaps *caps, GstCaps *filter);
228   GstCaps*      (*fixate_caps)    (GstBaseTransform *trans,
229                                    GstPadDirection direction, GstCaps *caps,
230                                    GstCaps *othercaps);
231   gboolean      (*accept_caps)    (GstBaseTransform *trans, GstPadDirection direction,
232                                    GstCaps *caps);
233   gboolean      (*set_caps)       (GstBaseTransform *trans, GstCaps *incaps,
234                                    GstCaps *outcaps);
235   gboolean      (*query)          (GstBaseTransform *trans, GstPadDirection direction,
236                                    GstQuery *query);
237
238   /* decide allocation query for output buffers */
239   gboolean      (*decide_allocation)  (GstBaseTransform *trans, GstQuery *query);
240   gboolean      (*filter_meta)        (GstBaseTransform *trans, GstQuery *query,
241                                        GType api, const GstStructure *params);
242
243   /* propose allocation query parameters for input buffers */
244   gboolean      (*propose_allocation) (GstBaseTransform *trans, GstQuery *decide_query,
245                                        GstQuery *query);
246
247   /* transform size */
248   gboolean      (*transform_size) (GstBaseTransform *trans,
249                                    GstPadDirection direction,
250                                    GstCaps *caps, gsize size,
251                                    GstCaps *othercaps, gsize *othersize);
252
253   gboolean      (*get_unit_size)  (GstBaseTransform *trans, GstCaps *caps,
254                                    gsize *size);
255
256   /* states */
257   gboolean      (*start)        (GstBaseTransform *trans);
258   gboolean      (*stop)         (GstBaseTransform *trans);
259
260   /* sink and src pad event handlers */
261   gboolean      (*sink_event)   (GstBaseTransform *trans, GstEvent *event);
262   gboolean      (*src_event)    (GstBaseTransform *trans, GstEvent *event);
263
264   GstFlowReturn (*prepare_output_buffer) (GstBaseTransform * trans,
265                                           GstBuffer *input, GstBuffer **outbuf);
266
267   /* metadata */
268   gboolean      (*copy_metadata)     (GstBaseTransform *trans, GstBuffer *input,
269                                       GstBuffer *outbuf);
270   gboolean      (*transform_meta)    (GstBaseTransform *trans, GstBuffer *outbuf,
271                                       GstMeta *meta, GstBuffer *inbuf);
272
273   void          (*before_transform)  (GstBaseTransform *trans, GstBuffer *buffer);
274
275   /* transform */
276   GstFlowReturn (*transform)    (GstBaseTransform *trans, GstBuffer *inbuf,
277                                  GstBuffer *outbuf);
278   GstFlowReturn (*transform_ip) (GstBaseTransform *trans, GstBuffer *buf);
279
280   GstFlowReturn (*submit_input_buffer) (GstBaseTransform *trans, gboolean is_discont, GstBuffer *input);
281   GstFlowReturn (*generate_output) (GstBaseTransform *trans, GstBuffer **outbuf);
282
283   /*< private >*/
284   gpointer       _gst_reserved[GST_PADDING_LARGE - 2];
285 };
286
287 GType           gst_base_transform_get_type         (void);
288
289 void            gst_base_transform_set_passthrough  (GstBaseTransform *trans,
290                                                      gboolean passthrough);
291 gboolean        gst_base_transform_is_passthrough   (GstBaseTransform *trans);
292
293 void            gst_base_transform_set_in_place     (GstBaseTransform *trans,
294                                                      gboolean in_place);
295 gboolean        gst_base_transform_is_in_place      (GstBaseTransform *trans);
296
297 void            gst_base_transform_update_qos       (GstBaseTransform *trans,
298                                                      gdouble proportion,
299                                                      GstClockTimeDiff diff,
300                                                      GstClockTime timestamp);
301 void            gst_base_transform_set_qos_enabled  (GstBaseTransform *trans,
302                                                      gboolean enabled);
303 gboolean        gst_base_transform_is_qos_enabled   (GstBaseTransform *trans);
304
305 void            gst_base_transform_set_gap_aware    (GstBaseTransform *trans,
306                                                      gboolean gap_aware);
307
308 void            gst_base_transform_set_prefer_passthrough (GstBaseTransform *trans,
309                                                            gboolean prefer_passthrough);
310
311 GstBufferPool * gst_base_transform_get_buffer_pool  (GstBaseTransform *trans);
312 void            gst_base_transform_get_allocator    (GstBaseTransform *trans,
313                                                      GstAllocator **allocator,
314                                                      GstAllocationParams *params);
315
316 void            gst_base_transform_reconfigure_sink (GstBaseTransform *trans);
317 void            gst_base_transform_reconfigure_src  (GstBaseTransform *trans);
318 gboolean gst_base_transform_update_src_caps (GstBaseTransform *trans,
319                                              GstCaps *updated_caps);
320 #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
321 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstBaseTransform, gst_object_unref)
322 #endif
323
324 G_END_DECLS
325
326 #endif /* __GST_BASE_TRANSFORM_H__ */