basetrans: improve fixate_caps function
[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 /**
81  * GST_BASE_TRANSFORM_LOCK:
82  * @obj: base transform instance
83  *
84  * Obtain a lock to protect the transform function from concurrent access.
85  *
86  * Since: 0.10.13
87  */
88 #define GST_BASE_TRANSFORM_LOCK(obj)   g_mutex_lock (&GST_BASE_TRANSFORM_CAST (obj)->transform_lock)
89
90 /**
91  * GST_BASE_TRANSFORM_UNLOCK:
92  * @obj: base transform instance
93  *
94  * Release the lock that protects the transform function from concurrent access.
95  *
96  * Since: 0.10.13
97  */
98 #define GST_BASE_TRANSFORM_UNLOCK(obj) g_mutex_unlock (&GST_BASE_TRANSFORM_CAST (obj)->transform_lock)
99
100 typedef struct _GstBaseTransform GstBaseTransform;
101 typedef struct _GstBaseTransformClass GstBaseTransformClass;
102 typedef struct _GstBaseTransformPrivate GstBaseTransformPrivate;
103
104 /**
105  * GstBaseTransform:
106  *
107  * The opaque #GstBaseTransform data structure.
108  */
109 struct _GstBaseTransform {
110   GstElement     element;
111
112   /*< protected >*/
113   /* source and sink pads */
114   GstPad        *sinkpad;
115   GstPad        *srcpad;
116
117   /* Set by sub-class */
118   gboolean       passthrough;
119   gboolean       always_in_place;
120
121   GstCaps       *cache_caps1;
122   gsize          cache_caps1_size;
123   GstCaps       *cache_caps2;
124   gsize          cache_caps2_size;
125   gboolean       have_same_caps;
126
127   gboolean       negotiated;
128
129   gboolean       have_segment;
130
131   /* MT-protected (with STREAM_LOCK) */
132   GstSegment     segment;
133
134   GMutex         transform_lock;
135
136   /*< private >*/
137   GstBaseTransformPrivate *priv;
138
139   gpointer       _gst_reserved[GST_PADDING_LARGE];
140 };
141
142 /**
143  * GstBaseTransformClass:
144  * @parent_class:   Element parent class
145  * @passthrough_on_same_caps: If set to TRUE, passthrough mode will be
146  *                            automatically enabled if the caps are the same.
147  * @transform_caps: Optional.  Given the pad in this direction and the given
148  *                  caps, what caps are allowed on the other pad in this
149  *                  element ?
150  * @fixate_caps:    Optional. Given the pad in this direction and the given
151  *                  caps, fixate the caps on the other pad. The function takes
152  *                  ownership of @othercaps and returns a fixated version of
153  *                  @othercaps. @othercaps is not guaranteed to be writable.
154  * @accept_caps:    Optional. Since 0.10.30
155  *                  Subclasses can override this method to check if @caps can be
156  *                  handled by the element. The default implementation might not be
157  *                  the most optimal way to check this in all cases.
158  * @set_caps:       allows the subclass to be notified of the actual caps set.
159  * @query:          Optional Since 0.10.36
160  *                  Handle a requested query. Subclasses that implement this
161  *                  should must chain up to the parent if they didn't handle the
162  *                  query
163  * @propose_allocation: Propose buffer allocation parameters for upstream elements.
164  *                      This function must be implemented if the element reads or
165  *                      writes the buffer content. The default implementation is NULL.
166  * @decide_allocation: Setup the allocation parameters for allocating output
167  *                    buffers. The passed in query contains the result of the
168  *                    downstream allocation query. This function is only called
169  *                    when not operating in passthrough mode. The default
170  *                    implementation is NULL.
171  * @transform_size: Optional. Given the size of a buffer in the given direction
172  *                  with the given caps, calculate the size in bytes of a buffer
173  *                  on the other pad with the given other caps.
174  *                  The default implementation uses get_unit_size and keeps
175  *                  the number of units the same.
176  * @get_unit_size:  Required if the transform is not in-place.
177  *                  get the size in bytes of one unit for the given caps.
178  * @start:          Optional.
179  *                  Called when the element starts processing.
180  *                  Allows opening external resources.
181  * @stop:           Optional.
182  *                  Called when the element stops processing.
183  *                  Allows closing external resources.
184  * @sink_event:     Optional.
185  *                  Event handler on the sink pad. The default implementation
186  *                  handles the event and forwards it downstream.
187  * @src_event:      Optional.
188  *                  Event handler on the source pad. The default implementation
189  *                  handles the event and forwards it upstream.
190  * @prepare_output_buffer: Optional.
191  *                         Subclasses can override this to do their own
192  *                         allocation of output buffers.  Elements that only do
193  *                         analysis can return a subbuffer or even just
194  *                         return a reference to the input buffer (if in
195  *                         passthrough mode). The default implementation will
196  *                         use the negotiated allocator or bufferpool and
197  *                         transform_size to allocate an output buffer or it
198  *                         will return the input buffer in passthrough mode.
199  * @copy_metadata: Optional.
200  *                 Copy the metadata from the input buffer to the output buffer.
201  *                 The default implementation will copy the flags, timestamps and
202  *                 offsets of the buffer.
203  * @before_transform: Optional. Since 0.10.22
204  *                    This method is called right before the base class will
205  *                    start processing. Dynamic properties or other delayed
206  *                    configuration could be performed in this method.
207  * @transform:      Required if the element does not operate in-place.
208  *                  Transforms one incoming buffer to one outgoing buffer.
209  *                  The function is allowed to change size/timestamp/duration
210  *                  of the outgoing buffer.
211  * @transform_ip:   Required if the element operates in-place.
212  *                  Transform the incoming buffer in-place.
213  *
214  * Subclasses can override any of the available virtual methods or not, as
215  * needed. At minimum either @transform or @transform_ip need to be overridden.
216  * If the element can overwrite the input data with the results (data is of the
217  * same type and quantity) it should provide @transform_ip.
218  */
219 struct _GstBaseTransformClass {
220   GstElementClass parent_class;
221
222   /*< public >*/
223   gboolean       passthrough_on_same_caps;
224
225   /* virtual methods for subclasses */
226   GstCaps*      (*transform_caps) (GstBaseTransform *trans,
227                                    GstPadDirection direction,
228                                    GstCaps *caps, GstCaps *filter);
229   GstCaps*      (*fixate_caps)    (GstBaseTransform *trans,
230                                    GstPadDirection direction, GstCaps *caps,
231                                    GstCaps *othercaps);
232   gboolean      (*accept_caps)    (GstBaseTransform *trans, GstPadDirection direction,
233                                    GstCaps *caps);
234   gboolean      (*set_caps)       (GstBaseTransform *trans, GstCaps *incaps,
235                                    GstCaps *outcaps);
236   gboolean      (*query)          (GstBaseTransform *trans, GstPadDirection direction,
237                                    GstQuery *query);
238
239   /* propose allocation query parameters for input buffers */
240   gboolean      (*propose_allocation) (GstBaseTransform *trans, GstQuery *query);
241   /* decide allocation query for output buffers */
242   gboolean      (*decide_allocation) (GstBaseTransform *trans, GstQuery *query);
243
244   /* transform size */
245   gboolean      (*transform_size) (GstBaseTransform *trans,
246                                    GstPadDirection direction,
247                                    GstCaps *caps, gsize size,
248                                    GstCaps *othercaps, gsize *othersize);
249
250   gboolean      (*get_unit_size)  (GstBaseTransform *trans, GstCaps *caps,
251                                    gsize *size);
252
253   /* states */
254   gboolean      (*start)        (GstBaseTransform *trans);
255   gboolean      (*stop)         (GstBaseTransform *trans);
256
257   /* sink and src pad event handlers */
258   gboolean      (*sink_event)   (GstBaseTransform *trans, GstEvent *event);
259   gboolean      (*src_event)    (GstBaseTransform *trans, GstEvent *event);
260
261   GstFlowReturn (*prepare_output_buffer) (GstBaseTransform * trans,
262                                           GstBuffer *input, GstBuffer **outbuf);
263
264   gboolean      (*copy_metadata)     (GstBaseTransform * trans, GstBuffer *input,
265                                       GstBuffer *outbuf);
266
267   void          (*before_transform)  (GstBaseTransform *trans, GstBuffer *buffer);
268
269   /* transform */
270   GstFlowReturn (*transform)    (GstBaseTransform *trans, GstBuffer *inbuf,
271                                  GstBuffer *outbuf);
272   GstFlowReturn (*transform_ip) (GstBaseTransform *trans, GstBuffer *buf);
273
274   /*< private >*/
275   gpointer       _gst_reserved[GST_PADDING_LARGE];
276 };
277
278 GType           gst_base_transform_get_type         (void);
279
280 void            gst_base_transform_set_passthrough  (GstBaseTransform *trans,
281                                                      gboolean passthrough);
282 gboolean        gst_base_transform_is_passthrough   (GstBaseTransform *trans);
283
284 void            gst_base_transform_set_in_place     (GstBaseTransform *trans,
285                                                      gboolean in_place);
286 gboolean        gst_base_transform_is_in_place      (GstBaseTransform *trans);
287
288 void            gst_base_transform_update_qos       (GstBaseTransform *trans,
289                                                      gdouble proportion,
290                                                      GstClockTimeDiff diff,
291                                                      GstClockTime timestamp);
292 void            gst_base_transform_set_qos_enabled  (GstBaseTransform *trans,
293                                                      gboolean enabled);
294 gboolean        gst_base_transform_is_qos_enabled   (GstBaseTransform *trans);
295
296 void            gst_base_transform_set_gap_aware    (GstBaseTransform *trans,
297                                                      gboolean gap_aware);
298
299 void            gst_base_transform_suggest          (GstBaseTransform *trans,
300                                                      GstCaps *caps, gsize size);
301 void            gst_base_transform_reconfigure      (GstBaseTransform *trans);
302 G_END_DECLS
303
304 #endif /* __GST_BASE_TRANSFORM_H__ */