Merge remote-tracking branch 'origin/0.10'
[platform/upstream/gstreamer.git] / libs / gst / base / gstbaseparse.h
1 /* GStreamer
2  * Copyright (C) 2008 Nokia Corporation. All rights reserved.
3  *
4  * Contact: Stefan Kost <stefan.kost@nokia.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifndef __GST_BASE_PARSE_H__
23 #define __GST_BASE_PARSE_H__
24
25 #include <gst/gst.h>
26
27 G_BEGIN_DECLS
28
29 #define GST_TYPE_BASE_PARSE            (gst_base_parse_get_type())
30 #define GST_BASE_PARSE(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_BASE_PARSE,GstBaseParse))
31 #define GST_BASE_PARSE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_BASE_PARSE,GstBaseParseClass))
32 #define GST_BASE_PARSE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_BASE_PARSE,GstBaseParseClass))
33 #define GST_IS_BASE_PARSE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_BASE_PARSE))
34 #define GST_IS_BASE_PARSE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_BASE_PARSE))
35 #define GST_BASE_PARSE_CAST(obj)       ((GstBaseParse *)(obj))
36
37 /**
38  * GST_BASE_PARSE_SRC_PAD:
39  * @obj: base parse instance
40  *
41  * Gives the pointer to the source #GstPad object of the element.
42  *
43  * Since: 0.10.33
44  */
45 #define GST_BASE_PARSE_SRC_PAD(obj)    (GST_BASE_PARSE_CAST (obj)->srcpad)
46
47 /**
48  * GST_BASE_PARSE_SINK_PAD:
49  * @obj: base parse instance
50  *
51  * Gives the pointer to the sink #GstPad object of the element.
52  *
53  * Since: 0.10.33
54  */
55 #define GST_BASE_PARSE_SINK_PAD(obj)    (GST_BASE_PARSE_CAST (obj)->sinkpad)
56
57 /**
58  * GST_BASE_PARSE_FLOW_DROPPED:
59  *
60  * A #GstFlowReturn that can be returned from parse_frame to
61  * indicate that no output buffer was generated, or from pre_push_frame to
62  * to forego pushing buffer.
63  *
64  * Since: 0.10.33
65  */
66 #define GST_BASE_PARSE_FLOW_DROPPED     GST_FLOW_CUSTOM_SUCCESS
67
68 /* not public API, use accessor macros below */
69 #define GST_BASE_PARSE_FLAG_LOST_SYNC (1 << 0)
70 #define GST_BASE_PARSE_FLAG_DRAINING  (1 << 1)
71
72 /**
73  * GST_BASE_PARSE_LOST_SYNC:
74  * @parse: base parse instance
75  *
76  * Obtains current sync status.
77  *
78  * Since: 0.10.33
79  */
80 #define GST_BASE_PARSE_LOST_SYNC(parse) (!!(GST_BASE_PARSE_CAST(parse)->flags & GST_BASE_PARSE_FLAG_LOST_SYNC))
81
82 /**
83  * GST_BASE_PARSE_DRAINING:
84  * @parse: base parse instance
85  *
86  * Obtains current drain status (ie. whether EOS has been received and
87  * the parser is now processing the frames at the end of the stream)
88  *
89  * Since: 0.10.33
90  */
91 #define GST_BASE_PARSE_DRAINING(parse)  (!!(GST_BASE_PARSE_CAST(parse)->flags & GST_BASE_PARSE_FLAG_DRAINING))
92
93 /**
94  * GstBaseParseFrameFlags:
95  * @GST_BASE_PARSE_FRAME_FLAG_NONE: no flag
96  * @GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME: set by baseclass if current frame
97  *   is passed for processing to the subclass for the first time
98  *   (and not set on subsequent calls with same data).
99  * @GST_BASE_PARSE_FRAME_FLAG_NO_FRAME: set to indicate this buffer should not be
100  *   counted as frame, e.g. if this frame is dependent on a previous one.
101  *   As it is not counted as a frame, bitrate increases but frame to time
102  *   conversions are maintained.
103  * @GST_BASE_PARSE_FRAME_FLAG_CLIP: @pre_push_frame can set this to indicate
104  *    that regular segment clipping can still be performed (as opposed to
105  *    any custom one having been done).
106  * @GST_BASE_PARSE_FRAME_FLAG_DROP: indicates to @finish_frame that the
107  *    the frame should be dropped (and might be handled internall by subclass)
108  * @GST_BASE_PARSE_FRAME_FLAG_QUEUE: indicates to @finish_frame that the
109  *    the frame should be queued for now and processed fully later
110  *    when the first non-queued frame is finished
111  *
112  * Flags to be used in a #GstBaseParseFrame.
113  *
114  * Since: 0.10.33
115  */
116 typedef enum {
117   GST_BASE_PARSE_FRAME_FLAG_NONE         = 0,
118   GST_BASE_PARSE_FRAME_FLAG_NEW_FRAME    = (1 << 0),
119   GST_BASE_PARSE_FRAME_FLAG_NO_FRAME     = (1 << 1),
120   GST_BASE_PARSE_FRAME_FLAG_CLIP         = (1 << 2),
121   GST_BASE_PARSE_FRAME_FLAG_DROP         = (1 << 3),
122   GST_BASE_PARSE_FRAME_FLAG_QUEUE        = (1 << 4)
123 } GstBaseParseFrameFlags;
124
125 /**
126  * GstBaseParseFrame:
127  * @buffer: input data to be parsed for frames.
128  * @out_buffer: (optional) (replacement) output data.
129  * @offset: media specific offset of input frame
130  *   Note that a converter may have a different one on the frame's buffer.
131  * @overhead: subclass can set this to indicates the metadata overhead
132  *   for the given frame, which is then used to enable more accurate bitrate
133  *   computations. If this is -1, it is assumed that this frame should be
134  *   skipped in bitrate calculation.
135  * @flags: a combination of input and output #GstBaseParseFrameFlags that
136  *  convey additional context to subclass or allow subclass to tune
137  *  subsequent #GstBaseParse actions.
138  *
139  * Frame (context) data passed to each frame parsing virtual methods.  In
140  * addition to providing the data to be checked for a valid frame or an already
141  * identified frame, it conveys additional metadata or control information
142  * from and to the subclass w.r.t. the particular frame in question (rather
143  * than global parameters).  Some of these may apply to each parsing stage, others
144  * only to some a particular one.  These parameters are effectively zeroed at start
145  * of each frame's processing, i.e. parsing virtual method invocation sequence.
146  *
147  * Since: 0.10.33
148  */
149 typedef struct {
150   GstBuffer * buffer;
151   GstBuffer * out_buffer;
152   guint       flags;
153   guint64     offset;
154   gint        overhead;
155   /*< private >*/
156   gint        size;
157   guint       _gst_reserved_i[2];
158   gpointer    _gst_reserved_p[2];
159   guint       _private_flags;
160 } GstBaseParseFrame;
161
162 typedef struct _GstBaseParse GstBaseParse;
163 typedef struct _GstBaseParseClass GstBaseParseClass;
164 typedef struct _GstBaseParsePrivate GstBaseParsePrivate;
165
166 /**
167  * GstBaseParse:
168  * @element: the parent element.
169  *
170  * The opaque #GstBaseParse data structure.
171  */
172 struct _GstBaseParse {
173   GstElement     element;
174
175   /*< protected >*/
176   /* source and sink pads */
177   GstPad         *sinkpad;
178   GstPad         *srcpad;
179
180   guint           flags;
181
182   /* MT-protected (with STREAM_LOCK) */
183   GstSegment      segment;
184
185   /*< private >*/
186   gpointer       _gst_reserved[GST_PADDING_LARGE];
187   GstBaseParsePrivate *priv;
188 };
189
190 /**
191  * GstBaseParseClass:
192  * @parent_class: the parent class
193  * @start:          Optional.
194  *                  Called when the element starts processing.
195  *                  Allows opening external resources.
196  * @stop:           Optional.
197  *                  Called when the element stops processing.
198  *                  Allows closing external resources.
199  * @set_sink_caps:  allows the subclass to be notified of the actual caps set.
200  * @get_sink_caps:  allows the subclass to do its own sink get caps if needed.
201  * @handle_frame:   Parses the input data into valid frames as defined by subclass
202  *                  which should be passed to gst_base_parse_finish_frame().
203  *                  The frame's input buffer is guaranteed writable,
204  *                  whereas the input frame ownership is held by caller
205  *                  (so subclass should make a copy if it needs to hang on).
206  *                  Input buffer (data) is provided by baseclass with as much
207  *                  metadata set as possible by baseclass according to upstream
208  *                  information and/or subclass settings,
209  *                  though subclass may still set buffer timestamp and duration
210  *                  if desired.
211  * @convert:        Optional.
212  *                  Convert between formats.
213  * @sink_event:     Optional.
214  *                  Event handler on the sink pad. This function should chain
215  *                  up to the parent implementation to let the default handler
216  *                  run.
217  * @src_event:      Optional.
218  *                  Event handler on the source pad. Should chain up to the
219  *                  parent to let the default handler run.
220  * @pre_push_frame: Optional.
221  *                   Called just prior to pushing a frame (after any pending
222  *                   events have been sent) to give subclass a chance to perform
223  *                   additional actions at this time (e.g. tag sending) or to
224  *                   decide whether this buffer should be dropped or not
225  *                   (e.g. custom segment clipping).
226  * @detect:         Optional.
227  *                   Called until it doesn't return GST_FLOW_OK anymore for
228  *                   the first buffers. Can be used by the subclass to detect
229  *                   the stream format. Since: 0.10.36
230  *
231  * Subclasses can override any of the available virtual methods or not, as
232  * needed. At minimum @check_valid_frame and @parse_frame needs to be
233  * overridden.
234  */
235 struct _GstBaseParseClass {
236   GstElementClass parent_class;
237
238   /*< public >*/
239   /* virtual methods for subclasses */
240
241   gboolean      (*start)              (GstBaseParse * parse);
242
243   gboolean      (*stop)               (GstBaseParse * parse);
244
245   gboolean      (*set_sink_caps)      (GstBaseParse * parse,
246                                        GstCaps      * caps);
247
248   GstFlowReturn (*handle_frame)       (GstBaseParse      * parse,
249                                        GstBaseParseFrame * frame,
250                                        gint              * skipsize);
251
252   GstFlowReturn (*pre_push_frame)     (GstBaseParse      * parse,
253                                        GstBaseParseFrame * frame);
254
255   gboolean      (*convert)            (GstBaseParse * parse,
256                                        GstFormat      src_format,
257                                        gint64         src_value,
258                                        GstFormat      dest_format,
259                                        gint64       * dest_value);
260
261   gboolean      (*sink_event)         (GstBaseParse * parse,
262                                        GstEvent     * event);
263
264   gboolean      (*src_event)          (GstBaseParse * parse,
265                                        GstEvent     * event);
266
267   GstCaps *     (*get_sink_caps)      (GstBaseParse * parse,
268                                        GstCaps      * filter);
269
270   GstFlowReturn (*detect)             (GstBaseParse * parse,
271                                        GstBuffer    * buffer);
272
273   /*< private >*/
274   gpointer       _gst_reserved[GST_PADDING_LARGE];
275 };
276
277 GType           gst_base_parse_get_type (void);
278
279 GType           gst_base_parse_frame_get_type (void);
280
281 GstBaseParseFrame * gst_base_parse_frame_new  (GstBuffer              * buffer,
282                                                GstBaseParseFrameFlags   flags,
283                                                gint                     overhead);
284
285 void            gst_base_parse_frame_init      (GstBaseParseFrame * frame);
286
287 void            gst_base_parse_frame_free      (GstBaseParseFrame * frame);
288
289 GstFlowReturn   gst_base_parse_push_frame      (GstBaseParse      * parse,
290                                                 GstBaseParseFrame * frame);
291
292 GstFlowReturn   gst_base_parse_finish_frame    (GstBaseParse * parse,
293                                                 GstBaseParseFrame * frame,
294                                                 gint size);
295
296 void            gst_base_parse_set_duration    (GstBaseParse      * parse,
297                                                 GstFormat           fmt,
298                                                 gint64              duration,
299                                                 gint                interval);
300
301 void            gst_base_parse_set_average_bitrate (GstBaseParse   * parse,
302                                                     guint            bitrate);
303
304 void            gst_base_parse_set_min_frame_size (GstBaseParse    * parse,
305                                                    guint             min_size);
306
307 void            gst_base_parse_set_has_timing_info (GstBaseParse   * parse,
308                                                     gboolean         has_timing);
309
310 void            gst_base_parse_set_syncable    (GstBaseParse * parse,
311                                                 gboolean       syncable);
312
313 void            gst_base_parse_set_passthrough (GstBaseParse * parse,
314                                                 gboolean       passthrough);
315
316 void            gst_base_parse_set_frame_rate  (GstBaseParse * parse,
317                                                 guint          fps_num,
318                                                 guint          fps_den,
319                                                 guint          lead_in,
320                                                 guint          lead_out);
321
322 void            gst_base_parse_set_latency     (GstBaseParse * parse,
323                                                 GstClockTime min_latency,
324                                                 GstClockTime max_latency);
325
326 gboolean        gst_base_parse_convert_default (GstBaseParse * parse,
327                                                 GstFormat      src_format,
328                                                 gint64         src_value,
329                                                 GstFormat      dest_format,
330                                                 gint64       * dest_value);
331
332 gboolean        gst_base_parse_add_index_entry (GstBaseParse * parse,
333                                                 guint64        offset,
334                                                 GstClockTime   ts,
335                                                 gboolean       key,
336                                                 gboolean       force);
337
338 G_END_DECLS
339
340 #endif /* __GST_BASE_PARSE_H__ */